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

47 lines
1.6 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();
2024-09-08 22:14:51 +03:00
this.$switchOnName.text(t("template_switch.template"));
this.$switchOnButton.attr("title", t("template_switch.toggle-on-hint"));
this.$switchOffName.text("Template");
2024-09-08 22:14:51 +03:00
this.$switchOffButton.attr("title", 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.$switchOn.toggle(!isTemplate);
this.$switchOff.toggle(!!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
}