2018-11-20 11:42:02 +00:00
|
|
|
import UI from "../ui.js";
|
|
|
|
|
|
|
|
class SwitchBox extends UI {
|
|
|
|
constructor(value, width, height) {
|
|
|
|
super(document.createElement("div"), width, height);
|
|
|
|
|
|
|
|
this._initialized = true;
|
|
|
|
|
|
|
|
this._boundClick = this._onClick.bind(this);
|
|
|
|
this.element.addEventListener("click", this._boundClick, false);
|
|
|
|
this._createInput();
|
|
|
|
this._setSize();
|
|
|
|
this.setValue(value);
|
|
|
|
}
|
|
|
|
destructor() {
|
|
|
|
this.element.removeEventListener("click", this._boundClick);
|
|
|
|
|
|
|
|
delete this._boundClick;
|
|
|
|
delete this._input;
|
|
|
|
delete this._movable;
|
|
|
|
delete this._circle;
|
|
|
|
|
|
|
|
super.destructor();
|
|
|
|
}
|
|
|
|
setSize(width, height) {
|
|
|
|
super.setSize(width, height);
|
|
|
|
|
|
|
|
if (this._initialized) {
|
|
|
|
this._setSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setValue(val) {
|
|
|
|
if (this._value !== val) {
|
2018-11-20 11:43:12 +00:00
|
|
|
this._value = val;
|
2018-11-20 11:42:02 +00:00
|
|
|
if (this._value) {
|
|
|
|
this._input.style.backgroundColor = "#00b6ff";
|
|
|
|
this._movable.style.right = 0 + "px";
|
|
|
|
} else {
|
|
|
|
this._input.style.backgroundColor = "#b8b8b8";
|
|
|
|
this._movable.style.right = (this._iWidth - this._iHeight) + "px";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_createInput() {
|
|
|
|
this._input = document.createElement("div");
|
|
|
|
this._input.classList.add("switchBox");
|
|
|
|
|
|
|
|
this._movable = document.createElement("div");
|
|
|
|
this._movable.classList.add("mover");
|
|
|
|
|
|
|
|
this._circle = document.createElement("div");
|
|
|
|
this._circle.classList.add("circle");
|
|
|
|
|
|
|
|
this._movable.appendChild(this._circle);
|
|
|
|
this._input.appendChild(this._movable);
|
|
|
|
this.element.appendChild(this._input);
|
|
|
|
}
|
|
|
|
_onClick() {
|
|
|
|
this.setValue(!this._value);
|
|
|
|
}
|
|
|
|
_setSize() {
|
2018-11-20 11:49:16 +00:00
|
|
|
let dh = this._width / 2;
|
2018-11-20 11:42:02 +00:00
|
|
|
dh = Math.min(dh, this._height);
|
2018-11-20 11:49:16 +00:00
|
|
|
let dw = dh * 2;
|
2018-11-20 11:42:02 +00:00
|
|
|
|
|
|
|
this._iWidth = dw;
|
|
|
|
this._iHeight = dh;
|
2018-11-20 11:49:16 +00:00
|
|
|
let circleDiameter = Math.round(dh * 0.75);
|
2018-11-20 11:42:02 +00:00
|
|
|
|
|
|
|
this._input.style.width = dw + "px";
|
|
|
|
this._input.style.height = dh + "px";
|
|
|
|
this._input.style.borderRadius = dh + "px";
|
|
|
|
|
|
|
|
let shift = (this._height - dh) / 2;
|
|
|
|
this.element.style.paddingTop = shift + "px";
|
|
|
|
this.element.style.paddingBottom = shift + "px";
|
|
|
|
|
|
|
|
this._movable.style.right = (dw - dh) + "px";
|
|
|
|
|
|
|
|
shift = (dh - circleDiameter) / 2;
|
|
|
|
this._circle.style.width = circleDiameter + "px";
|
|
|
|
this._circle.style.height = circleDiameter + "px";
|
|
|
|
this._circle.style.right = shift + "px";
|
|
|
|
this._circle.style.top = shift + "px";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SwitchBox;
|