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

34 lines
978 B
TypeScript
Raw Normal View History

import { el } 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 { WidgetOptions } from "./Widget";
type ButtonOptions = WidgetOptions & {
text?: string;
};
export class Button extends Control<HTMLButtonElement> {
readonly click: Observable<MouseEvent>;
2019-08-20 04:56:40 +08:00
private readonly _click: Emitter<MouseEvent> = emitter<MouseEvent>();
constructor(text: string, options?: ButtonOptions) {
super(
el.button({ class: "core_Button" }, el.span({ class: "core_Button_inner", text })),
options,
);
this.click = this._click;
2019-08-20 04:56:40 +08:00
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
}