2022-11-25 15:29:57 +01:00
|
|
|
import froca from "../../services/froca.js";
|
|
|
|
|
import attributeService from "../../services/attributes.js";
|
2022-12-02 16:46:14 +01:00
|
|
|
import CommandButtonWidget from "./command_button.js";
|
2025-02-11 19:16:11 +02:00
|
|
|
import type { EventData } from "../../components/app_context.js";
|
|
|
|
|
|
|
|
|
|
export type ButtonNoteIdProvider = () => string;
|
2022-11-25 15:29:57 +01:00
|
|
|
|
2022-12-02 16:46:14 +01:00
|
|
|
export default class ButtonFromNoteWidget extends CommandButtonWidget {
|
2025-02-11 19:16:11 +02:00
|
|
|
|
2022-11-25 15:29:57 +01:00
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
|
2022-11-29 16:16:57 +01:00
|
|
|
this.settings.buttonNoteIdProvider = null;
|
2022-11-25 15:29:57 +01:00
|
|
|
}
|
|
|
|
|
|
2025-02-11 19:16:11 +02:00
|
|
|
buttonNoteIdProvider(provider: ButtonNoteIdProvider) {
|
2022-11-29 16:16:57 +01:00
|
|
|
this.settings.buttonNoteIdProvider = provider;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-25 15:29:57 +01:00
|
|
|
doRender() {
|
|
|
|
|
super.doRender();
|
|
|
|
|
|
|
|
|
|
this.updateIcon();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateIcon() {
|
2025-02-11 19:16:11 +02:00
|
|
|
if (!this.settings.buttonNoteIdProvider) {
|
|
|
|
|
console.error(`buttonNoteId for '${this.componentId}' is not defined.`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-29 16:16:57 +01:00
|
|
|
const buttonNoteId = this.settings.buttonNoteIdProvider();
|
|
|
|
|
|
2022-11-30 16:57:51 +01:00
|
|
|
if (!buttonNoteId) {
|
|
|
|
|
console.error(`buttonNoteId for '${this.componentId}' is not defined.`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
froca.getNote(buttonNoteId).then((note) => {
|
2025-02-11 19:16:11 +02:00
|
|
|
const icon = note?.getIcon();
|
|
|
|
|
if (icon) {
|
|
|
|
|
this.settings.icon = icon;
|
|
|
|
|
}
|
2022-11-25 15:29:57 +01:00
|
|
|
|
|
|
|
|
this.refreshIcon();
|
2022-11-30 16:57:51 +01:00
|
|
|
});
|
2022-11-25 15:29:57 +01:00
|
|
|
}
|
|
|
|
|
|
2025-02-11 19:16:11 +02:00
|
|
|
entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
|
|
|
|
|
// TODO: this seems incorrect
|
|
|
|
|
//@ts-ignore
|
2022-11-29 16:16:57 +01:00
|
|
|
const buttonNote = froca.getNoteFromCache(this.buttonNoteIdProvider());
|
|
|
|
|
|
|
|
|
|
if (!buttonNote) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
if (loadResults.getAttributeRows(this.componentId).find((attr) => attr.type === "label" && attr.name === "iconClass" && attributeService.isAffecting(attr, buttonNote))) {
|
2022-11-25 15:29:57 +01:00
|
|
|
this.updateIcon();
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-09 18:07:02 +02:00
|
|
|
}
|