forked from surmon-china/vue-awesome-swiper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssr.js
56 lines (54 loc) · 1.74 KB
/
ssr.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
var Swiper = window.Swiper = require('swiper')
var swiper = {
install: function(Vue) {
var getInstanceName = function(el, binding, vnode) {
var customInstanceName = ''
if (binding.arg) {
customInstanceName = binding.arg
} else if (vnode.data.attrs && vnode.data.attrs.instanceName) {
customInstanceName = vnode.data.attrs.instanceName
} else if (el.id) {
customInstanceName = el.id
}
var instanceName = customInstanceName || 'swiper'
return instanceName
}
Vue.directive('swiper', {
bind: function(el, binding, vnode) {
var _this = vnode.context
if (!el.className.includes('swiper-container')) {
el.className += (!!el.className ? ' ' : '' + 'swiper-container')
}
},
inserted: function(el, binding, vnode) {
var _this = vnode.context
var options = binding.value
var instanceName = getInstanceName(el, binding, vnode)
var swiper = _this[instanceName]
if (!swiper) {
_this[instanceName] = new Swiper(el, options)
}
},
componentUpdated: function(el, binding, vnode) {
var instanceName = getInstanceName(el, binding, vnode)
var swiper = vnode.context[instanceName]
if (swiper) {
swiper.update(true)
swiper.updatePagination(true)
if (binding.value.loop) {
swiper.reLoop()
}
}
},
unbind: function(el, binding, vnode) {
var instanceName = getInstanceName(el, binding, vnode)
var swiper = vnode.context[instanceName]
if (swiper) {
swiper.destroy()
delete vnode.context[instanceName]
}
}
})
}
}
module.exports = swiper