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

77 lines
2.6 KiB
TypeScript
Raw Normal View History

import BasicWidget from "../basic_widget.js";
import { Tooltip, Dropdown } from "bootstrap";
2025-02-25 02:02:20 +01:00
type PopoverPlacement = Tooltip.PopoverPlacement;
const TPL = `
2024-09-02 19:37:02 +02:00
<div class="dropdown right-dropdown-widget dropend">
<button type="button" data-bs-toggle="dropdown"
2025-02-25 02:02:20 +01:00
aria-haspopup="true" aria-expanded="false"
2022-12-11 13:20:37 +01:00
class="bx right-dropdown-button launcher-button"></button>
2025-02-25 02:02:20 +01:00
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 {
2025-02-25 02:02:20 +01:00
protected iconClass: string;
protected title: string;
protected dropdownTpl: string;
protected settings: { titlePlacement: PopoverPlacement };
protected $dropdownMenu!: JQuery<HTMLElement>;
protected dropdown!: Dropdown;
protected $tooltip!: JQuery<HTMLElement>;
protected tooltip!: Tooltip;
public $dropdownContent!: JQuery<HTMLElement>;
constructor(title: string, iconClass: string, dropdownTpl: string) {
super();
this.iconClass = iconClass;
this.title = title;
this.dropdownTpl = dropdownTpl;
this.settings = {
2025-01-09 18:07:02 +02:00
titlePlacement: "right"
};
}
2025-02-25 02:02:20 +01:00
doRender(): void {
this.$widget = $(TPL);
2021-10-07 21:57:20 +02:00
this.$dropdownMenu = this.$widget.find(".dropdown-menu");
2025-02-25 02:02:20 +01:00
this.dropdown = Dropdown.getOrCreateInstance(this.$widget.find("[data-bs-toggle='dropdown']")[0]);
2024-09-02 19:37:02 +02:00
this.$tooltip = this.$widget.find(".tooltip-trigger").attr("title", this.title);
2025-02-25 02:02:20 +01:00
this.tooltip = new Tooltip(this.$tooltip[0], {
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();
2025-02-25 02:02:20 +01:00
const windowHeight = $(window).height() || 0;
const pixelsToBottom = windowHeight - rect.bottom;
2021-10-07 21:57:20 +02:00
if (pixelsToBottom < 0) {
this.$dropdownMenu.css("top", pixelsToBottom);
}
});
this.$dropdownContent = $(this.dropdownTpl);
this.$widget.find(".dropdown-menu").append(this.$dropdownContent);
}
// to be overridden
2025-02-25 02:02:20 +01:00
async dropdownShown(): Promise<void> {}
}