chore(client/ts): port template_switch

This commit is contained in:
Elian Doran 2025-01-29 18:39:59 +02:00
parent 6aba099a29
commit 3c60f181ff
No known key found for this signature in database
2 changed files with 19 additions and 11 deletions

View File

@ -96,13 +96,13 @@ const TPL = `
export default class SwitchWidget extends NoteContextAwareWidget { export default class SwitchWidget extends NoteContextAwareWidget {
private $switchOn!: JQuery<HTMLElement>; protected $switchOn!: JQuery<HTMLElement>;
private $switchOnName!: JQuery<HTMLElement>; protected $switchOnName!: JQuery<HTMLElement>;
private $switchOnButton!: JQuery<HTMLElement>; protected $switchOnButton!: JQuery<HTMLElement>;
private $switchOff!: JQuery<HTMLElement>; protected $switchOff!: JQuery<HTMLElement>;
private $switchOffName!: JQuery<HTMLElement>; protected $switchOffName!: JQuery<HTMLElement>;
private $switchOffButton!: JQuery<HTMLElement>; protected $switchOffButton!: JQuery<HTMLElement>;
private $helpButton!: JQuery<HTMLElement>; protected $helpButton!: JQuery<HTMLElement>;
doRender() { doRender() {
this.$widget = $(TPL); this.$widget = $(TPL);

View File

@ -1,13 +1,15 @@
import SwitchWidget from "./switch.js"; import SwitchWidget from "./switch.js";
import attributeService from "../services/attributes.js"; import attributeService from "../services/attributes.js";
import { t } from "../services/i18n.js"; import { t } from "../services/i18n.js";
import type FNote from "../entities/fnote.js";
import type { EventData } from "../components/app_context.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. * 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 { export default class TemplateSwitchWidget extends SwitchWidget {
isEnabled() { isEnabled() {
return super.isEnabled() && !this.noteId.startsWith("_options"); return super.isEnabled() && !this.noteId?.startsWith("_options");
} }
doRender() { doRender() {
@ -23,22 +25,28 @@ export default class TemplateSwitchWidget extends SwitchWidget {
} }
async switchOn() { async switchOn() {
if (this.noteId) {
await attributeService.setLabel(this.noteId, "template"); await attributeService.setLabel(this.noteId, "template");
} }
}
async switchOff() { async switchOff() {
if (!this.note || !this.noteId) {
return;
}
for (const templateAttr of this.note.getOwnedLabels("template")) { for (const templateAttr of this.note.getOwnedLabels("template")) {
await attributeService.removeAttributeById(this.noteId, templateAttr.attributeId); await attributeService.removeAttributeById(this.noteId, templateAttr.attributeId);
} }
} }
async refreshWithNote(note) { async refreshWithNote(note: FNote) {
const isTemplate = note.hasLabel("template"); const isTemplate = note.hasLabel("template");
this.$switchOn.toggle(!isTemplate); this.$switchOn.toggle(!isTemplate);
this.$switchOff.toggle(!!isTemplate); this.$switchOff.toggle(!!isTemplate);
} }
entitiesReloadedEvent({ loadResults }) { entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
if (loadResults.getAttributeRows().find((attr) => attr.type === "label" && attr.name === "template" && attr.noteId === this.noteId)) { if (loadResults.getAttributeRows().find((attr) => attr.type === "label" && attr.name === "template" && attr.noteId === this.noteId)) {
this.refresh(); this.refresh();
} }