fantasy/ui/button.js

54 lines
1.4 KiB
JavaScript
Raw Normal View History

2018-11-19 15:04:16 +00:00
import UI from "./ui.js";
2018-11-19 15:03:05 +00:00
class Button extends UI {
2018-11-20 15:23:43 +00:00
constructor(image, isRef) {
isRef = isRef !== false;
let element;
if (isRef) {
element = document.createElement("a");
element.href = "#";
} else {
element = document.createElement("button");
}
super(element, Button.width, Button.height);
2018-11-19 15:03:05 +00:00
2018-11-19 14:45:42 +00:00
this.element.classList.add("shadow");
2018-11-20 15:23:43 +00:00
this.element.classList.add("button");
2018-11-19 14:09:43 +00:00
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;
2018-11-19 15:03:05 +00:00
super.destructor();
2018-11-19 14:09:43 +00:00
}
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);
}
}
static get width() {
return width;
}
static get height() {
return height;
}
2018-11-19 14:09:43 +00:00
}
const width = 50;
const height = 50;
2018-11-19 14:09:43 +00:00
export default Button;