2019-12-27 07:55:32 +08:00
|
|
|
import { button, Icon, icon, span } 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-29 03:36:45 +08:00
|
|
|
import { WidgetOptions } from "./Widget";
|
2019-08-30 00:24:03 +08:00
|
|
|
import { Property } from "../observable/property/Property";
|
|
|
|
import { WritableProperty } from "../observable/property/WritableProperty";
|
|
|
|
import { WidgetProperty } from "../observable/property/WidgetProperty";
|
2019-08-20 05:49:40 +08:00
|
|
|
|
2019-08-30 00:24:03 +08:00
|
|
|
export type ButtonOptions = WidgetOptions & {
|
2019-12-28 06:33:34 +08:00
|
|
|
text?: string | Property<string>;
|
2019-08-30 00:24:03 +08:00
|
|
|
icon_left?: Icon;
|
|
|
|
icon_right?: Icon;
|
2019-08-29 03:36:45 +08:00
|
|
|
};
|
2019-08-21 21:19:44 +08:00
|
|
|
|
2019-09-16 01:32:34 +08:00
|
|
|
export class Button extends Control {
|
2020-01-07 04:09:44 +08:00
|
|
|
private readonly _onmouseup = emitter<MouseEvent>();
|
|
|
|
private readonly _onclick = emitter<MouseEvent>();
|
|
|
|
private readonly _onkeydown = emitter<KeyboardEvent>();
|
2019-08-30 00:24:03 +08:00
|
|
|
private readonly _text: WidgetProperty<string>;
|
|
|
|
private readonly center_element: HTMLSpanElement;
|
2019-08-28 06:50:38 +08:00
|
|
|
|
2020-01-01 01:45:12 +08:00
|
|
|
readonly element = button({ className: "core_Button" });
|
2020-01-07 04:09:44 +08:00
|
|
|
readonly onmouseup: Observable<MouseEvent> = this._onmouseup;
|
|
|
|
readonly onclick: Observable<MouseEvent> = this._onclick;
|
|
|
|
readonly onkeydown: Observable<KeyboardEvent> = this._onkeydown;
|
2020-01-01 01:45:12 +08:00
|
|
|
readonly text: WritableProperty<string>;
|
|
|
|
|
2019-12-28 06:33:34 +08:00
|
|
|
constructor(options?: ButtonOptions) {
|
2019-09-16 01:32:34 +08:00
|
|
|
super(options);
|
2019-08-28 06:50:38 +08:00
|
|
|
|
2019-12-27 07:55:32 +08:00
|
|
|
const inner_element = span({ className: "core_Button_inner" });
|
2019-08-30 00:24:03 +08:00
|
|
|
|
2019-12-28 06:33:34 +08:00
|
|
|
if (options?.icon_left != undefined) {
|
2019-12-27 07:55:32 +08:00
|
|
|
inner_element.append(span({ className: "core_Button_left" }, icon(options.icon_left)));
|
2019-08-30 00:24:03 +08:00
|
|
|
}
|
|
|
|
|
2020-01-01 01:45:12 +08:00
|
|
|
this.center_element = span({ className: "core_Button_center" });
|
2019-08-30 00:24:03 +08:00
|
|
|
inner_element.append(this.center_element);
|
|
|
|
|
2019-12-28 06:33:34 +08:00
|
|
|
if (options?.icon_right != undefined) {
|
2019-12-27 07:55:32 +08:00
|
|
|
inner_element.append(
|
|
|
|
span({ className: "core_Button_right" }, icon(options.icon_right)),
|
|
|
|
);
|
2019-08-30 00:24:03 +08:00
|
|
|
}
|
|
|
|
|
2020-01-07 04:09:44 +08:00
|
|
|
this.element.onmouseup = (e: MouseEvent) => this._onmouseup.emit({ value: e });
|
|
|
|
this.element.onclick = (e: MouseEvent) => this._onclick.emit({ value: e });
|
|
|
|
this.element.onkeydown = (e: KeyboardEvent) => this._onkeydown.emit({ value: e });
|
2020-01-01 11:15:03 +08:00
|
|
|
|
2019-08-30 00:24:03 +08:00
|
|
|
this._text = new WidgetProperty<string>(this, "", this.set_text);
|
|
|
|
this.text = this._text;
|
|
|
|
|
2019-12-28 06:33:34 +08:00
|
|
|
if (typeof options?.text === "string") {
|
|
|
|
this.text.val = options.text;
|
|
|
|
} else if (options?.text) {
|
|
|
|
this.text.bind_to(options.text);
|
|
|
|
} else {
|
|
|
|
this.text.val = "";
|
2019-08-30 00:24:03 +08:00
|
|
|
}
|
2019-09-14 21:15:59 +08:00
|
|
|
|
2019-09-16 01:32:34 +08:00
|
|
|
this.element.append(inner_element);
|
|
|
|
|
2019-12-20 01:54:01 +08:00
|
|
|
this.finalize_construction();
|
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-30 00:24:03 +08:00
|
|
|
|
|
|
|
protected set_text(text: string): void {
|
|
|
|
this.center_element.textContent = text;
|
2019-09-03 22:44:13 +08:00
|
|
|
this.center_element.hidden = text === "";
|
2019-08-30 00:24:03 +08:00
|
|
|
}
|
2019-08-20 04:56:40 +08:00
|
|
|
}
|