downthemall/windows/broadcaster.ts
2019-08-20 16:41:37 +02:00

51 lines
1.0 KiB
TypeScript

"use strict";
// License: MIT
import {Keys} from "./keys";
const $ = document.querySelector.bind(document);
export class Broadcaster {
private readonly els: HTMLElement[];
public onaction: Function;
constructor(...els: string[]) {
this.els = els.map(e => $(`#${e}`));
this.onkey = this.onkey.bind(this);
const keys = new Set(this.els.map(el => el.dataset.key));
if (keys.size) {
keys.forEach(key => {
Keys.on(key, this.onkey);
});
}
}
get disabled() {
return this.els[0].classList.contains("disabled");
}
set disabled(val) {
if (val) {
for (const el of this.els) {
el.classList.add("disabled");
}
return;
}
for (const el of this.els) {
el.classList.remove("disabled");
}
}
onkey(evt: KeyboardEvent) {
const {localName} = <HTMLElement> evt.target;
if (localName === "input" || localName === "textarea") {
return undefined;
}
if (this.onaction) {
this.onaction();
}
return true;
}
}