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

53 lines
1.3 KiB
JavaScript
Raw Normal View History

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";
2022-11-25 15:29:57 +01:00
2022-12-02 16:46:14 +01:00
export default class ButtonFromNoteWidget extends CommandButtonWidget {
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
}
2022-11-29 16:16:57 +01:00
buttonNoteIdProvider(provider) {
this.settings.buttonNoteIdProvider = provider;
return this;
}
2022-11-25 15:29:57 +01:00
doRender() {
super.doRender();
this.updateIcon();
}
updateIcon() {
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;
}
froca.getNote(buttonNoteId).then(note => {
this.settings.icon = note.getIcon();
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
}
entitiesReloadedEvent({loadResults}) {
2022-11-29 16:16:57 +01:00
const buttonNote = froca.getNoteFromCache(this.buttonNoteIdProvider());
if (!buttonNote) {
return;
}
if (loadResults.getAttributeRows(this.componentId).find(attr =>
2022-11-25 15:29:57 +01:00
attr.type === 'label'
&& attr.name === 'iconClass'
2022-11-29 16:16:57 +01:00
&& attributeService.isAffecting(attr, buttonNote))) {
2022-11-25 15:29:57 +01:00
this.updateIcon();
}
}
}