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 57 58 59 60 61 62 63
|
const onListener = (function() { if (document.addEventListener) return function(element, event, handler) { if (element && event && handler) element.addEventListener(event, handler) } return function(element, event, handler) { if (element && event && handler) element.attachEvent('on' + event, handler) } })()
const nodeList = []
const ctx = 'Xin-FAS:clickoutside'
let mousedownNode
let seed = 0
onListener(document, 'mousedown', e => mousedownNode = e.target)
onListener(document, 'mouseup', e => nodeList.forEach(el => documentHandler(el, e.target)))
const documentHandler = (el, mouseupNode) => { if ( !el[ctx] || el.contains(mouseupNode) || el.contains(mousedownNode) ) return el[ctx].cb() } export default { bind (el, binding) { nodeList.push(el) el[ctx] = { cb: binding.value, id: seed++ } }, update (el, binding) { el[ctx] = { cb: binding.value, id: seed++ } }, unbind (el) { for (let [index, item] of nodeList.entries()) { if (item.id === el[ctx].id) { nodeList.splice(index, 1) break } } delete el[ctx] } }
|