Retrospring/app/javascript/retrospring/utilities/registerEvents.ts

17 lines
543 B
TypeScript
Raw Normal View History

2021-08-11 05:10:24 -07:00
export interface EventDef {
2021-08-10 04:16:40 -07:00
type: string;
target: Node | NodeList;
handler: EventListenerOrEventListenerObject;
}
export default function registerEvents(events: EventDef[]): void {
events.forEach(event => {
if (event.target instanceof NodeList) {
event.target.forEach(element => {
element.addEventListener(event.type, event.handler);
});
} else if (event.target instanceof Node) {
event.target.addEventListener(event.type, event.handler);
}
});
}