Notes/src/public/app/widgets/template_switch.js

46 lines
1.5 KiB
JavaScript
Raw Normal View History

import SwitchWidget from "./switch.js";
import attributeService from "../services/attributes.js";
2024-09-08 22:14:51 +03:00
import { t } from "../services/i18n.js";
/**
* Switch for the basic properties widget which allows the user to select whether the note is a template or not, which toggles the `#template` attribute.
*/
export default class TemplateSwitchWidget extends SwitchWidget {
isEnabled() {
2025-01-09 18:07:02 +02:00
return super.isEnabled() && !this.noteId.startsWith("_options");
}
doRender() {
super.doRender();
this.switchOnName = t("template_switch.template");
this.switchOnTooltip = t("template_switch.toggle-on-hint");
this.switchOffName = t("template_switch.template");
this.switchOffTooltip = t("template_switch.toggle-off-hint");
this.$helpButton.attr("data-help-page", "template.html").show();
}
async switchOn() {
2025-01-09 18:07:02 +02:00
await attributeService.setLabel(this.noteId, "template");
}
async switchOff() {
2025-01-09 18:07:02 +02:00
for (const templateAttr of this.note.getOwnedLabels("template")) {
await attributeService.removeAttributeById(this.noteId, templateAttr.attributeId);
}
}
async refreshWithNote(note) {
const isTemplate = note.hasLabel("template");
this.isToggled = isTemplate;
}
2025-01-09 18:07:02 +02:00
entitiesReloadedEvent({ loadResults }) {
if (loadResults.getAttributeRows().find((attr) => attr.type === "label" && attr.name === "template" && attr.noteId === this.noteId)) {
this.refresh();
}
}
2025-01-09 18:07:02 +02:00
}