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

65 lines
2.0 KiB
JavaScript
Raw Normal View History

import BasicWidget from "../basic_widget.js";
import { Tooltip, Dropdown } from "bootstrap";
const TPL = `
2024-09-02 19:37:02 +02:00
<div class="dropdown right-dropdown-widget dropend">
<button type="button" data-bs-toggle="dropdown"
aria-haspopup="true" aria-expanded="false"
2022-12-11 13:20:37 +01:00
class="bx right-dropdown-button launcher-button"></button>
2024-09-02 19:37:02 +02:00
<div class="tooltip-trigger"></div>
<div class="dropdown-menu dropdown-menu-right"></div>
</div>
`;
export default class RightDropdownButtonWidget extends BasicWidget {
2022-08-05 16:44:26 +02:00
constructor(title, iconClass, dropdownTpl) {
super();
this.iconClass = iconClass;
this.title = title;
this.dropdownTpl = dropdownTpl;
this.settings = {
2025-01-09 18:07:02 +02:00
titlePlacement: "right"
};
}
doRender() {
this.$widget = $(TPL);
2021-10-07 21:57:20 +02:00
this.$dropdownMenu = this.$widget.find(".dropdown-menu");
this.dropdown = Dropdown.getOrCreateInstance(this.$widget.find("[data-bs-toggle='dropdown']"));
2024-09-02 19:37:02 +02:00
this.$tooltip = this.$widget.find(".tooltip-trigger").attr("title", this.title);
this.tooltip = new Tooltip(this.$tooltip, {
placement: this.settings.titlePlacement,
2025-01-09 18:07:02 +02:00
fallbackPlacements: [this.settings.titlePlacement]
});
2024-09-02 19:37:02 +02:00
2025-01-09 18:07:02 +02:00
this.$widget
.find(".right-dropdown-button")
.addClass(this.iconClass)
2024-09-02 19:37:02 +02:00
.on("click", () => this.tooltip.hide())
2025-01-09 18:07:02 +02:00
.on("mouseenter", () => this.tooltip.show())
.on("mouseleave", () => this.tooltip.hide());
2025-01-09 18:07:02 +02:00
this.$widget.on("show.bs.dropdown", async () => {
2021-10-07 21:57:20 +02:00
await this.dropdownShown();
const rect = this.$dropdownMenu[0].getBoundingClientRect();
const pixelsToBottom = $(window).height() - rect.bottom;
if (pixelsToBottom < 0) {
this.$dropdownMenu.css("top", pixelsToBottom);
}
});
this.$dropdownContent = $(this.dropdownTpl);
this.$widget.find(".dropdown-menu").append(this.$dropdownContent);
}
// to be overridden
2025-01-09 18:07:02 +02:00
async dropdownShow() {}
}