2025-01-09 18:36:24 +02:00
|
|
|
import type { CommandNames } from "../../components/app_context.js";
|
|
|
|
|
import keyboardActionsService, { type Action } from "../../services/keyboard_actions.js";
|
|
|
|
|
import AbstractButtonWidget, { type AbstractButtonWidgetSettings } from "./abstract_button.js";
|
2022-12-02 16:46:14 +01:00
|
|
|
|
2025-01-07 11:26:49 +02:00
|
|
|
let actions: Action[];
|
2022-12-02 16:46:14 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
keyboardActionsService.getActions().then((as) => (actions = as));
|
2022-12-02 16:46:14 +01:00
|
|
|
|
2025-01-07 11:26:49 +02:00
|
|
|
// TODO: Is this actually used?
|
|
|
|
|
export type ClickHandler = (widget: CommandButtonWidget, e: JQuery.ClickEvent<any, any, any, any>) => void;
|
2025-01-09 18:07:02 +02:00
|
|
|
type CommandOrCallback = CommandNames | (() => CommandNames);
|
2025-01-07 11:26:49 +02:00
|
|
|
|
|
|
|
|
interface CommandButtonWidgetSettings extends AbstractButtonWidgetSettings {
|
|
|
|
|
command?: CommandOrCallback;
|
|
|
|
|
onClick?: ClickHandler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default class CommandButtonWidget extends AbstractButtonWidget<CommandButtonWidgetSettings> {
|
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
this.settings = {
|
2025-01-09 18:07:02 +02:00
|
|
|
titlePlacement: "right",
|
2025-01-07 11:26:49 +02:00
|
|
|
title: null,
|
|
|
|
|
icon: null,
|
|
|
|
|
onContextMenu: null
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-02 16:46:14 +01:00
|
|
|
doRender() {
|
|
|
|
|
super.doRender();
|
|
|
|
|
|
|
|
|
|
if (this.settings.command) {
|
|
|
|
|
this.$widget.on("click", () => {
|
2024-09-03 11:28:50 +02:00
|
|
|
this.tooltip.hide();
|
2022-12-02 16:46:14 +01:00
|
|
|
|
2025-01-07 11:26:49 +02:00
|
|
|
if (this._command) {
|
|
|
|
|
this.triggerCommand(this._command);
|
|
|
|
|
}
|
2022-12-02 16:46:14 +01:00
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
console.warn(`Button widget '${this.componentId}' has no defined command`, this.settings);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getTitle() {
|
|
|
|
|
const title = super.getTitle();
|
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const action = actions.find((act) => act.actionName === this._command);
|
2022-12-02 16:46:14 +01:00
|
|
|
|
|
|
|
|
if (action && action.effectiveShortcuts.length > 0) {
|
|
|
|
|
return `${title} (${action.effectiveShortcuts.join(", ")})`;
|
|
|
|
|
} else {
|
|
|
|
|
return title;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-07 11:26:49 +02:00
|
|
|
onClick(handler: ClickHandler) {
|
2022-12-02 16:46:14 +01:00
|
|
|
this.settings.onClick = handler;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-07 11:26:49 +02:00
|
|
|
command(command: CommandOrCallback) {
|
2022-12-02 16:46:14 +01:00
|
|
|
this.settings.command = command;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get _command() {
|
2025-01-09 18:07:02 +02:00
|
|
|
return typeof this.settings.command === "function" ? this.settings.command() : this.settings.command;
|
2022-12-02 16:46:14 +01:00
|
|
|
}
|
|
|
|
|
}
|