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
|
|
|
|
2022-12-02 16:46:14 +01:00
|
|
|
export default class AbstractButtonWidget extends NoteContextAwareWidget {
|
2021-05-24 22:29:49 +02:00
|
|
|
isEnabled() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-18 22:14:35 +02:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
2021-05-22 22:55:24 +02:00
|
|
|
this.settings = {
|
2022-11-25 15:29:57 +01:00
|
|
|
titlePlacement: 'right',
|
|
|
|
title: null,
|
|
|
|
icon: null,
|
|
|
|
onContextMenu: null
|
2021-05-22 22:55:24 +02:00
|
|
|
};
|
2021-05-18 22:14:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
doRender() {
|
|
|
|
this.$widget = $(TPL);
|
2024-09-03 11:28:50 +02:00
|
|
|
this.tooltip = new bootstrap.Tooltip(this.$widget, {
|
2024-11-22 23:58:15 +02:00
|
|
|
html: true,
|
|
|
|
title: () => this.getTitle(),
|
|
|
|
trigger: 'hover',
|
2024-11-23 00:07:40 +02:00
|
|
|
placement: this.settings.titlePlacement,
|
|
|
|
fallbackPlacements: [ this.settings.titlePlacement ]
|
2024-09-03 11:28:50 +02:00
|
|
|
})
|
2021-05-18 22:14:35 +02:00
|
|
|
|
2022-11-25 15:29:57 +01:00
|
|
|
if (this.settings.onContextMenu) {
|
|
|
|
this.$widget.on("contextmenu", e => {
|
2024-09-03 11:28:50 +02:00
|
|
|
this.tooltip.hide();
|
2022-11-25 15:29:57 +01:00
|
|
|
|
|
|
|
this.settings.onContextMenu(e);
|
2022-12-25 10:33:31 +01:00
|
|
|
|
|
|
|
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() {
|
|
|
|
return typeof this.settings.title === "function"
|
|
|
|
? this.settings.title()
|
|
|
|
: this.settings.title;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-02 16:46:14 +01:00
|
|
|
const icon = typeof this.settings.icon === "function"
|
|
|
|
? this.settings.icon()
|
|
|
|
: this.settings.icon;
|
|
|
|
|
2023-01-02 21:08:20 +01:00
|
|
|
this.$widget.addClass(icon);
|
2021-05-22 21:35:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
initialRenderCompleteEvent() {
|
|
|
|
this.refreshIcon();
|
2021-05-18 22:14:35 +02:00
|
|
|
}
|
|
|
|
|
2022-12-02 16:46:14 +01:00
|
|
|
/** @param {string|function} icon */
|
2021-05-18 22:14:35 +02:00
|
|
|
icon(icon) {
|
2021-05-22 21:35:25 +02:00
|
|
|
this.settings.icon = icon;
|
2021-05-18 22:14:35 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2022-12-02 16:46:14 +01:00
|
|
|
/** @param {string|function} title */
|
2021-05-18 22:14:35 +02:00
|
|
|
title(title) {
|
2021-05-22 21:35:25 +02:00
|
|
|
this.settings.title = title;
|
2021-05-18 22:14:35 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2022-12-02 16:46:14 +01:00
|
|
|
/** @param {string} placement - "top", "bottom", "left", "right" */
|
2021-05-22 22:55:24 +02:00
|
|
|
titlePlacement(placement) {
|
|
|
|
this.settings.titlePlacement = placement;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2022-11-25 15:29:57 +01:00
|
|
|
onContextMenu(handler) {
|
|
|
|
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
|
|
|
}
|