exbert / client /src /ts /etc /SimpleEventHandler.ts
bhoov's picture
First commit
63858e7
raw
history blame
806 Bytes
/**
* Created by hen on 5/15/17.
* Modified by hoo on 4/16/19.
*/
export class SimpleEventHandler {
element: Element;
eventListeners: object[];
constructor(element: Element) {
this.element = element;
this.eventListeners = []
}
bind(eventNames: string, eventFunction: Function) {
for (const eventName of eventNames.split(' ')) {
this.eventListeners.push({eventName, eventFunction});
const eventFunctionWrap = e => eventFunction(e.detail, e);
this.element.addEventListener(eventName, eventFunctionWrap, false);
}
}
getListeners() {
return this.eventListeners;
}
trigger(eventName: string, detail: object) {
this.element.dispatchEvent(new CustomEvent(eventName, {detail}));
}
}