2019-08-23 23:00:39 +08:00
|
|
|
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";
|
2019-08-21 21:19:44 +08:00
|
|
|
import { emitter } from "../observable";
|
2019-08-23 04:45:01 +08:00
|
|
|
import { Control } from "./Control";
|
2019-08-28 06:50:38 +08:00
|
|
|
import { Emitter } from "../observable/Emitter";
|
|
|
|
import { ViewOptions } from "./Widget";
|
2019-08-20 05:49:40 +08:00
|
|
|
|
2019-08-23 04:45:01 +08:00
|
|
|
export class Button extends Control {
|
2019-08-23 23:00:39 +08:00
|
|
|
readonly element: HTMLButtonElement = create_element("button", { class: "core_Button" });
|
2019-08-21 21:19:44 +08:00
|
|
|
|
2019-08-28 06:50:38 +08:00
|
|
|
readonly click: Observable<MouseEvent>;
|
2019-08-20 04:56:40 +08:00
|
|
|
|
2019-08-28 06:50:38 +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
|
|
|
|
2019-08-23 23:00:39 +08:00
|
|
|
this.element.append(create_element("span", { class: "core_Button_inner", text }));
|
2019-08-22 04:04:08 +08:00
|
|
|
|
2019-08-26 21:42:12 +08:00
|
|
|
this.disposables(this.enabled.observe(({ value }) => (this.element.disabled = !value)));
|
2019-08-20 05:49:40 +08:00
|
|
|
|
2019-08-26 21:42:12 +08:00
|
|
|
this.element.onclick = (e: MouseEvent) => this._click.emit({ value: e });
|
2019-08-20 04:56:40 +08:00
|
|
|
}
|
2019-08-28 06:50:38 +08:00
|
|
|
|
|
|
|
protected set_enabled(enabled: boolean): void {
|
|
|
|
super.set_enabled(enabled);
|
|
|
|
this.element.disabled = !enabled;
|
|
|
|
}
|
2019-08-20 04:56:40 +08:00
|
|
|
}
|