2019-09-13 20:48:11 +08:00
|
|
|
import clickoutsideDelegate from '../utils/delegate/clickoutsideDelegate'
|
2019-07-21 23:13:05 +08:00
|
|
|
|
|
|
|
const ctx = '@@clickoutsideContext'
|
|
|
|
|
2019-07-22 17:41:56 +08:00
|
|
|
function lazyHandler (handler) {
|
|
|
|
let called = false
|
2019-08-25 16:58:04 +08:00
|
|
|
return function (e) {
|
2019-07-22 17:41:56 +08:00
|
|
|
if (called) {
|
|
|
|
console.debug('[clickoutside] called')
|
2019-08-25 16:58:04 +08:00
|
|
|
handler(e)
|
2019-07-22 17:41:56 +08:00
|
|
|
} else {
|
|
|
|
console.debug('[clickoutside] lazy called')
|
|
|
|
called = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-22 15:15:55 +08:00
|
|
|
const clickoutside = {
|
2019-07-22 17:41:56 +08:00
|
|
|
bind (el, bindings) {
|
2019-09-16 12:12:07 +08:00
|
|
|
// console.debug('[clickoutside]: bind', el)
|
|
|
|
console.debug('[clickoutside]: bind $el')
|
2019-07-21 23:13:05 +08:00
|
|
|
if (typeof bindings.value === 'function') {
|
|
|
|
el[ctx] = {
|
2019-07-22 17:41:56 +08:00
|
|
|
handler: bindings.modifiers.lazy ? lazyHandler(bindings.value) : bindings.value
|
2019-07-21 23:13:05 +08:00
|
|
|
}
|
2019-07-22 15:15:55 +08:00
|
|
|
clickoutsideDelegate.registerHandler(el, el[ctx].handler, false)
|
2019-07-21 23:13:05 +08:00
|
|
|
}
|
|
|
|
},
|
2019-08-25 16:58:04 +08:00
|
|
|
inserted (el, bindings) {
|
|
|
|
console.debug('[clickoutside]: inserted')
|
|
|
|
// if (typeof bindings.value === 'function') {
|
|
|
|
// el[ctx] = {
|
|
|
|
// handler: bindings.modifiers.lazy ? lazyHandler(bindings.value) : bindings.value
|
|
|
|
// }
|
|
|
|
// clickoutsideDelegate.registerHandler(el, el[ctx].handler, false)
|
|
|
|
// }
|
|
|
|
},
|
2019-07-21 23:13:05 +08:00
|
|
|
update (el, bindings) {
|
2019-08-25 16:58:04 +08:00
|
|
|
console.debug('[clickoutside]: update')
|
|
|
|
if (typeof bindings.value === 'function') {
|
|
|
|
clickoutsideDelegate.unregisterHandler(el[ctx].handler)
|
|
|
|
el[ctx].handler = bindings.value
|
|
|
|
clickoutsideDelegate.registerHandler(el, el[ctx].handler, false)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
componentUpdated (el, bindings) {
|
|
|
|
console.debug('[clickoutside]: componentUpdated')
|
2019-07-21 23:13:05 +08:00
|
|
|
if (typeof bindings.value === 'function') {
|
2019-07-22 17:41:56 +08:00
|
|
|
clickoutsideDelegate.unregisterHandler(el[ctx].handler)
|
2019-07-21 23:13:05 +08:00
|
|
|
el[ctx].handler = bindings.value
|
2019-07-22 15:15:55 +08:00
|
|
|
clickoutsideDelegate.registerHandler(el, el[ctx].handler, false)
|
2019-07-21 23:13:05 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
unbind (el) {
|
2019-07-22 17:41:56 +08:00
|
|
|
console.debug('[clickoutside]: unbind')
|
2019-07-22 15:15:55 +08:00
|
|
|
clickoutsideDelegate.unregisterHandler(el[ctx].handler)
|
2019-07-21 23:13:05 +08:00
|
|
|
}
|
|
|
|
}
|
2019-07-22 15:15:55 +08:00
|
|
|
|
|
|
|
export default clickoutside
|