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

This commit is contained in:
Elian Doran 2024-08-16 23:41:59 +03:00
parent ca6b4c3497
commit b0480e1667
No known key found for this signature in database
2 changed files with 51 additions and 1 deletions

View File

@ -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) {

View File

@ -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();
}
}
}