naive-ui/packages/utils/resizeDelegate.js
07akioni ffad17ff1f fix(popover): some times it can't disappear normally when mouseleave
mouseleave event sometimes can't fire. So I detect mouseout outside the popover
and activator to make it disappear. Implementation has many tricks. Sigh, life
is so hard.
2019-07-18 00:32:01 +08:00

39 lines
1.1 KiB
JavaScript

class ResizeDelegate {
constructor () {
console.debug('[ResizeDelegate]: Ctor called')
this.handlers = []
this.handleResize = this.handleResize.bind(this)
}
handleResize (e) {
const handlers = this.handlers
if (handlers.length) {
for (const handler of handlers) {
handler(e)
}
}
}
unregisterHandler (handler) {
const handlers = this.handlers
if (handlers.length) {
console.debug('[ResizeDelegate]: remove resize handler from window')
window.removeEventListener('resize', this.handleResize, true)
} else {
// console.debug(handler)
const handlerIndex = handlers.findIndex(h => handler === h)
// console.debug(handlerIndex)
if (~handlerIndex) {
handlers.splice(handlerIndex, 1)
}
}
}
registerHandler (handler) {
if (!this.handlers.length) {
console.debug('[ResizeDelegate]: add resize handler to window')
window.addEventListener('resize', this.handleResize, true)
}
this.handlers.push(handler)
}
}
export default new ResizeDelegate()