Notes/src/public/app/widgets/buttons/abstract_button.ts

95 lines
2.6 KiB
TypeScript
Raw Normal View History

2021-05-24 22:29:49 +02:00
import NoteContextAwareWidget from "../note_context_aware_widget.js";
2021-05-18 22:14:35 +02:00
2023-01-02 22:32:05 +01:00
const TPL = `<button class="button-widget bx"
2024-09-03 11:28:50 +02:00
data-bs-toggle="tooltip"
2023-01-02 22:32:05 +01:00
title=""></button>`;
2021-05-18 22:14:35 +02:00
2025-01-07 11:11:38 +02:00
type TitlePlacement = "top" | "bottom" | "left" | "right";
type StringOrCallback = string | (() => string);
2025-01-07 11:11:38 +02:00
type ContextMenuHandler = (e: JQuery.ContextMenuEvent<any, any, any, any> | null) => void;
export interface AbstractButtonWidgetSettings {
2025-01-07 11:11:38 +02:00
titlePlacement: TitlePlacement;
title: StringOrCallback | null;
icon: StringOrCallback | null;
2025-01-07 11:11:38 +02:00
onContextMenu: ContextMenuHandler | null;
}
export default class AbstractButtonWidget<SettingsT extends AbstractButtonWidgetSettings> extends NoteContextAwareWidget {
protected settings!: SettingsT;
protected tooltip!: bootstrap.Tooltip;
2025-01-07 11:11:38 +02:00
2021-05-24 22:29:49 +02:00
isEnabled() {
return true;
}
2021-05-18 22:14:35 +02:00
doRender() {
this.$widget = $(TPL);
2025-01-07 11:11:38 +02:00
// Fix once bootstrap is available as non-UMD
//@ts-ignore
2024-09-03 11:28:50 +02:00
this.tooltip = new bootstrap.Tooltip(this.$widget, {
html: true,
title: () => this.getTitle(),
2025-01-09 18:07:02 +02:00
trigger: "hover",
placement: this.settings.titlePlacement,
2025-01-09 18:07:02 +02:00
fallbackPlacements: [this.settings.titlePlacement]
});
2021-05-18 22:14:35 +02:00
2022-11-25 15:29:57 +01:00
if (this.settings.onContextMenu) {
2025-01-09 18:07:02 +02:00
this.$widget.on("contextmenu", (e) => {
2024-09-03 11:28:50 +02:00
this.tooltip.hide();
2022-11-25 15:29:57 +01:00
2025-01-07 11:11:38 +02:00
if (this.settings.onContextMenu) {
this.settings.onContextMenu(e);
}
return false; // blocks default browser right click menu
2022-11-25 15:29:57 +01:00
});
}
2021-05-18 22:14:35 +02:00
super.doRender();
}
2022-12-02 16:46:14 +01:00
getTitle() {
2025-01-09 18:07:02 +02:00
return typeof this.settings.title === "function" ? this.settings.title() : this.settings.title;
2022-12-02 16:46:14 +01:00
}
2021-05-18 22:14:35 +02:00
refreshIcon() {
2023-01-02 21:08:20 +01:00
for (const className of this.$widget[0].classList) {
2021-05-28 22:47:59 +02:00
if (className.startsWith("bx-")) {
2023-01-02 21:08:20 +01:00
this.$widget.removeClass(className);
2021-05-28 22:47:59 +02:00
}
}
2025-01-09 18:07:02 +02:00
const icon = typeof this.settings.icon === "function" ? this.settings.icon() : this.settings.icon;
2022-12-02 16:46:14 +01:00
2025-01-07 11:11:38 +02:00
if (icon) {
this.$widget.addClass(icon);
}
2021-05-22 21:35:25 +02:00
}
initialRenderCompleteEvent() {
this.refreshIcon();
2021-05-18 22:14:35 +02:00
}
2025-01-07 11:11:38 +02:00
icon(icon: StringOrCallback) {
2021-05-22 21:35:25 +02:00
this.settings.icon = icon;
2021-05-18 22:14:35 +02:00
return this;
}
2025-01-07 11:11:38 +02:00
title(title: StringOrCallback) {
2021-05-22 21:35:25 +02:00
this.settings.title = title;
2021-05-18 22:14:35 +02:00
return this;
}
2025-01-07 11:11:38 +02:00
titlePlacement(placement: TitlePlacement) {
2021-05-22 22:55:24 +02:00
this.settings.titlePlacement = placement;
return this;
}
2025-01-07 11:11:38 +02:00
onContextMenu(handler: ContextMenuHandler) {
2022-11-25 15:29:57 +01:00
this.settings.onContextMenu = handler;
2022-12-23 20:17:04 +01:00
return this;
2022-11-25 15:29:57 +01:00
}
2022-12-11 13:20:37 +01:00
}