-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
48 lines (40 loc) · 1.39 KB
/
index.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
import Scrollbar from 'smooth-scrollbar'
import './default.css'
import { bestListener, bestOptions } from './utils'
export default {
install (Vue, options) {
// helpers
Vue.prototype.$scrollbar = Scrollbar
Vue.scrollbar = Scrollbar
// directive
Vue.directive('smoothscrollbar', {
bind (el) {
el.classList.add('smooth-vuebar')
},
inserted (el, binding) {
const possibilities = [binding.value, options]
const scrollbar = Scrollbar.init(el, bestOptions(possibilities))
const listener = bestListener(possibilities)
if (listener) scrollbar.addListener(listener)
el.dispatchEvent(new CustomEvent('insert', { detail: el }))
},
componentUpdated (el, binding) {
const scrollbar = Scrollbar.get(el)
if (!scrollbar) return
// remove old listener, if defined
const oldListener = bestListener([binding.oldValue, options])
if (oldListener) scrollbar.removeListener(oldListener)
// add the new listener, if defined
const listener = bestListener([binding.value, options])
if (listener) scrollbar.addListener(listener)
// refresh
scrollbar.update()
},
unbind (el) {
const scrollbar = Scrollbar.get(el)
if (scrollbar) scrollbar.destroy()
el.dispatchEvent(new CustomEvent('unbind', { detail: el }))
}
})
}
}