Notes/src/public/app/widgets/buttons/button_widget.js

83 lines
1.8 KiB
JavaScript
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
2021-05-28 22:47:59 +02:00
const TPL = `<span class="button-widget icon-action bx"
2021-05-22 21:51:40 +02:00
data-toggle="tooltip"
2021-05-28 22:47:59 +02:00
title=""></span>`;
2021-05-18 22:14:35 +02:00
2021-05-24 22:29:49 +02:00
export default class ButtonWidget extends NoteContextAwareWidget {
isEnabled() {
return true;
}
2021-05-18 22:14:35 +02:00
constructor() {
super();
2021-05-22 22:55:24 +02:00
this.settings = {
titlePlacement: 'right'
};
2021-05-18 22:14:35 +02:00
}
doRender() {
this.$widget = $(TPL);
this.overflowing();
2021-05-24 21:05:44 +02:00
if (this.settings.command) {
this.$widget.on("click", () => this.triggerCommand(this.settings.command));
}
if (this.settings.onClick) {
this.$widget.on("click", () => this.settings.onClick(this));
}
2021-05-23 20:26:54 +02:00
this.$widget.attr("data-placement", this.settings.titlePlacement);
2021-05-18 22:14:35 +02:00
2021-05-22 21:51:40 +02:00
this.$widget.tooltip({
html: true,
title: () => this.settings.title
});
2021-05-18 22:14:35 +02:00
super.doRender();
}
refreshIcon() {
2021-05-28 22:47:59 +02:00
for (const className of this.$widget[0].classList) {
if (className.startsWith("bx-")) {
this.$widget.removeClass(className);
}
}
2021-05-22 22:55:24 +02:00
this.$widget
.attr("title", this.settings.title)
2021-05-22 21:35:25 +02:00
.addClass(this.settings.icon);
}
initialRenderCompleteEvent() {
this.refreshIcon();
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;
}
title(title) {
2021-05-22 21:35:25 +02:00
this.settings.title = title;
2021-05-18 22:14:35 +02:00
return this;
}
2021-05-22 22:55:24 +02:00
titlePlacement(placement) {
this.settings.titlePlacement = placement;
return this;
}
2021-05-18 22:14:35 +02:00
command(command) {
2021-05-22 21:35:25 +02:00
this.settings.command = command;
2021-05-18 22:14:35 +02:00
return this;
}
2021-05-24 21:05:44 +02:00
onClick(handler) {
this.settings.onClick = handler;
return this;
}
2021-05-18 22:14:35 +02:00
}