naive-ui/packages/directives/mousemoveoutside.js
2019-09-01 22:11:30 +08:00

54 lines
1.5 KiB
JavaScript

import moveoutsideDelegate from '../utils/moveoutsideDelegate'
const ctx = '@@mousemoveoutsideContext'
function lazyHandler (handler) {
let called = false
return function (e) {
if (called) {
console.debug('[mousemoveoutside] called')
handler(e)
} else {
console.debug('[mousemoveoutside] lazy called')
called = true
}
}
}
const mousemoveoutside = {
bind (el, bindings) {
console.debug('[mousemoveoutside]: bind', el)
if (typeof bindings.value === 'function') {
el[ctx] = {
handler: bindings.modifiers.lazy ? lazyHandler(bindings.value) : bindings.value
}
moveoutsideDelegate.registerHandler(el, el[ctx].handler)
}
},
inserted () {
console.debug('[mousemoveoutside]: inserted')
},
update (el, bindings) {
console.debug('[mousemoveoutside]: update')
if (typeof bindings.value === 'function') {
moveoutsideDelegate.unregisterHandler(el[ctx].handler)
el[ctx].handler = bindings.value
moveoutsideDelegate.registerHandler(el, el[ctx].handler)
}
},
componentUpdated (el, bindings) {
console.debug('[mousemoveoutside]: componentUpdated')
if (typeof bindings.value === 'function') {
moveoutsideDelegate.unregisterHandler(el[ctx].handler)
el[ctx].handler = bindings.value
moveoutsideDelegate.registerHandler(el, el[ctx].handler)
}
},
unbind (el) {
console.debug('[mousemoveoutside]: unbind')
moveoutsideDelegate.unregisterHandler(el[ctx].handler)
}
}
export default mousemoveoutside