分离事件中心

This commit is contained in:
Harvey Zack committed 2018-11-09 12:17:19 +08:00
1 parent b5694fea89
commit 1d40d1c46b
31 files changed
+728 -403

No files matched your search

+37
View File
@@ -0,0 +1,37 @@
import clickInit from './clickInit';
import hoverInit from './hoverInit';
import mousemoveInit from './mousemoveInit';
import resizeInit from './resizeInit';
export default class Events {
constructor(art) {
this.destroyEvents = [];
this.proxy = this.proxy.bind(this);
this.hover = this.hover.bind(this);
clickInit(art, this);
hoverInit(art, this);
mousemoveInit(art, this);
resizeInit(art, this);
}
proxy(target, name, callback, option = {}) {
if (Array.isArray(name)) {
name.forEach(item => this.proxy(target, item, callback, option));
return;
}
target.addEventListener(name, callback, option);
this.destroyEvents.push(() => {
target.removeEventListener(name, callback, option);
});
}
hover(target, mouseenter, mouseleave) {
this.proxy(target, 'mouseenter', mouseenter);
this.proxy(target, 'mouseleave', mouseleave);
}
destroy() {
this.destroyEvents.forEach(event => event());
}
}