@yg012n0132n0n3w0n/simplepage
Version:
70 lines (63 loc) • 1.85 kB
JavaScript
var popups = [];
function getSlotNode (el) {
if (!el) return null
if (el.nodeType === 1 && el.getAttribute('slot') === 'trigger') {
return el
}
return getSlotNode(el.nextSibling)
}
document.addEventListener('click', function(event) {
popups.forEach(popup=>{
popup._onDocumentClick(event);
})
});
export default {
ready () {
this._bindTriggerEvent()
},
methods: {
_getTriggerTarget () {
const el = getSlotNode(this.$el)
let els
if (el.getAttribute('slot') === 'trigger') {
els = [el]
} else {
els = getSlotNode(this.$el).querySelectorAll('[slot="trigger"]')
}
const len = els.length
if (len) {
const currentWrap = els[len - 1]
const children = currentWrap.children
if (children && children.length === 1) {
return children[children.length - 1]
} else {
return currentWrap
}
}
return null
},
_bindTriggerEvent () {
const el = this._getTriggerTarget() // 这里获取的是触发源
if (el) {
el.addEventListener('blur', this._onBlur.bind(this), false)
el.addEventListener('click', this._onClick.bind(this), false)
el.addEventListener('focus', this._onFocus.bind(this), false)
el.addEventListener('mousedown', this._onMouseDown.bind(this), false)
el.addEventListener('mouseenter', this._onMouseEnter.bind(this), false)
el.addEventListener('mouseleave', this._onMouseLeave.bind(this), false)
el.addEventListener('touchstart', this._onTouchStart.bind(this), false)
}
}
},
open(instance) {
if (instance) {
popups.push(instance);
}
},
close(instance) {
var index = popups.indexOf(instance);
if (index !== -1) {
popups.splice(instance, 1);
}
}
}