From 3c60f181ffedf94aed31208b86920029fab54d6f Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Wed, 29 Jan 2025 18:39:59 +0200 Subject: [PATCH] chore(client/ts): port template_switch --- src/public/app/widgets/switch.ts | 14 +++++++------- .../{template_switch.js => template_switch.ts} | 16 ++++++++++++---- 2 files changed, 19 insertions(+), 11 deletions(-) rename src/public/app/widgets/{template_switch.js => template_switch.ts} (74%) diff --git a/src/public/app/widgets/switch.ts b/src/public/app/widgets/switch.ts index 514891e54..270e7e75c 100644 --- a/src/public/app/widgets/switch.ts +++ b/src/public/app/widgets/switch.ts @@ -96,13 +96,13 @@ const TPL = ` export default class SwitchWidget extends NoteContextAwareWidget { - private $switchOn!: JQuery; - private $switchOnName!: JQuery; - private $switchOnButton!: JQuery; - private $switchOff!: JQuery; - private $switchOffName!: JQuery; - private $switchOffButton!: JQuery; - private $helpButton!: JQuery; + protected $switchOn!: JQuery; + protected $switchOnName!: JQuery; + protected $switchOnButton!: JQuery; + protected $switchOff!: JQuery; + protected $switchOffName!: JQuery; + protected $switchOffButton!: JQuery; + protected $helpButton!: JQuery; doRender() { this.$widget = $(TPL); diff --git a/src/public/app/widgets/template_switch.js b/src/public/app/widgets/template_switch.ts similarity index 74% rename from src/public/app/widgets/template_switch.js rename to src/public/app/widgets/template_switch.ts index e36494c23..cdf1ba8fe 100644 --- a/src/public/app/widgets/template_switch.js +++ b/src/public/app/widgets/template_switch.ts @@ -1,13 +1,15 @@ import SwitchWidget from "./switch.js"; import attributeService from "../services/attributes.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. */ export default class TemplateSwitchWidget extends SwitchWidget { isEnabled() { - return super.isEnabled() && !this.noteId.startsWith("_options"); + return super.isEnabled() && !this.noteId?.startsWith("_options"); } doRender() { @@ -23,22 +25,28 @@ export default class TemplateSwitchWidget extends SwitchWidget { } async switchOn() { - await attributeService.setLabel(this.noteId, "template"); + if (this.noteId) { + await attributeService.setLabel(this.noteId, "template"); + } } async switchOff() { + if (!this.note || !this.noteId) { + return; + } + for (const templateAttr of this.note.getOwnedLabels("template")) { await attributeService.removeAttributeById(this.noteId, templateAttr.attributeId); } } - async refreshWithNote(note) { + async refreshWithNote(note: FNote) { const isTemplate = note.hasLabel("template"); this.$switchOn.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)) { this.refresh(); }