phantasmal-world/src/core/gui/Button.ts

33 lines
1.0 KiB
TypeScript
Raw Normal View History

import { create_element } from "./dom";
2019-08-20 04:56:40 +08:00
import "./Button.css";
2019-08-20 21:02:58 +08:00
import { Observable } from "../observable/Observable";
import { emitter } from "../observable";
import { Control } from "./Control";
import { Emitter } from "../observable/Emitter";
import { ViewOptions } from "./Widget";
export class Button extends Control {
readonly element: HTMLButtonElement = create_element("button", { class: "core_Button" });
readonly click: Observable<MouseEvent>;
2019-08-20 04:56:40 +08:00
private readonly _click: Emitter<MouseEvent> = emitter<MouseEvent>();
constructor(text: string, options?: ViewOptions) {
super(options);
this.click = this._click;
2019-08-20 04:56:40 +08:00
this.element.append(create_element("span", { class: "core_Button_inner", text }));
this.disposables(this.enabled.observe(({ value }) => (this.element.disabled = !value)));
this.element.onclick = (e: MouseEvent) => this._click.emit({ value: e });
2019-08-20 04:56:40 +08:00
}
protected set_enabled(enabled: boolean): void {
super.set_enabled(enabled);
this.element.disabled = !enabled;
}
2019-08-20 04:56:40 +08:00
}