a button to repaint

This commit is contained in:
Blue 2018-11-19 17:09:43 +03:00
parent 6a11ebf440
commit 49cf653dba
6 changed files with 130 additions and 9 deletions

31
ui/button.js Normal file
View file

@ -0,0 +1,31 @@
class Button {
constructor(image) {
this.element = document.createElement("button");
this.element.style.backgroundImage = "url(images/" + image + ".svg)";
this._boundClick = this._onClick.bind(this);
this.element.addEventListener("click", this._boundClick, false);
this._handlers = [];
}
destructor() {
this.element.removeEventListener("click", this._boundClick);
delete this._boundClick;
delete this._handlers;
delete this.element;
}
addHandler(handler) {
this._handlers.push(handler);
}
setPosition(x, y) {
this.element.style.position = "absolute";
this.element.style.top = y + "px";
this.element.style.left = x + "px";
}
_onClick(e) {
for (let i = 0; i < this._handlers.length; ++i) {
this._handlers[i](e);
}
}
}
export default Button;