Skip to content

Build your personal knowledge base with Trilium Notes

client: Add a toggle to set template label (closes #348)

Elian Doranb0480e1667ca
diff --git a/src/public/app/widgets/ribbon_widgets/basic_properties.js b/src/public/app/widgets/ribbon_widgets/basic_properties.js
index 89bf727668..a206c66433 100644
--- a/src/public/app/widgets/ribbon_widgets/basic_properties.js
+++ b/src/public/app/widgets/ribbon_widgets/basic_properties.js
@@ -5,6 +5,7 @@ import EditabilitySelectWidget from "../editability_select.js";
import BookmarkSwitchWidget from "../bookmark_switch.js";
import SharedSwitchWidget from "../shared_switch.js";
import { t } from "../../services/i18n.js";
+import TemplateSwitchWidget from "../template_switch.js";
const TPL = `
<div class="basic-properties-widget">
@@ -41,6 +42,8 @@ const TPL = `
<div class="bookmark-switch-container"></div>
<div class="shared-switch-container"></div>
+
+ <div class="template-switch-container"></div>
</div>`;
export default class BasicPropertiesWidget extends NoteContextAwareWidget {
@@ -52,13 +55,15 @@ export default class BasicPropertiesWidget extends NoteContextAwareWidget {
this.editabilitySelectWidget = new EditabilitySelectWidget().contentSized();
this.bookmarkSwitchWidget = new BookmarkSwitchWidget().contentSized();
this.sharedSwitchWidget = new SharedSwitchWidget().contentSized();
+ this.templateSwitchWidget = new TemplateSwitchWidget().contentSized();
this.child(
this.noteTypeWidget,
this.protectedNoteSwitchWidget,
this.editabilitySelectWidget,
this.bookmarkSwitchWidget,
- this.sharedSwitchWidget
+ this.sharedSwitchWidget,
+ this.templateSwitchWidget
);
}
@@ -87,6 +92,7 @@ export default class BasicPropertiesWidget extends NoteContextAwareWidget {
this.$widget.find(".editability-select-container").append(this.editabilitySelectWidget.render());
this.$widget.find(".bookmark-switch-container").append(this.bookmarkSwitchWidget.render());
this.$widget.find(".shared-switch-container").append(this.sharedSwitchWidget.render());
+ this.$widget.find(".template-switch-container").append(this.templateSwitchWidget.render());
}
async refreshWithNote(note) {
diff --git a/src/public/app/widgets/template_switch.js b/src/public/app/widgets/template_switch.js
new file mode 100644
index 0000000000..7d7e363465
--- /dev/null
+++ b/src/public/app/widgets/template_switch.js
@@ -0,0 +1,44 @@
+import SwitchWidget from "./switch.js";
+import attributeService from "../services/attributes.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 {
+
+ doRender() {
+ super.doRender();
+
+ this.$switchOnName.text("Template");
+ this.$switchOnButton.attr("title", "Make the note a template");
+
+ this.$switchOffName.text("Template");
+ this.$switchOffButton.attr("title", "Remove the note as a template");
+
+ this.$helpButton.attr("data-help-page", "template.html").show();
+ this.$helpButton.on('click', e => utils.openHelp($(e.target)));
+ }
+
+ async switchOn() {
+ await attributeService.setLabel(this.noteId, 'template');
+ }
+
+ async switchOff() {
+ 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);
+ }
+
+ entitiesReloadedEvent({loadResults}) {
+ if (loadResults.getAttributeRows().find(attr => attr.type === 'label' && attr.name === "template" && attr.noteId === this.noteId)) {
+ this.refresh();
+ }
+ }
+
+}
\ No newline at end of file