Skip to content

Commit 3c60f18

Browse files
committed
chore(client/ts): port template_switch
1 parent 6aba099 commit 3c60f18

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/public/app/widgets/switch.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ const TPL = `
9696

9797
export default class SwitchWidget extends NoteContextAwareWidget {
9898

99-
private $switchOn!: JQuery<HTMLElement>;
100-
private $switchOnName!: JQuery<HTMLElement>;
101-
private $switchOnButton!: JQuery<HTMLElement>;
102-
private $switchOff!: JQuery<HTMLElement>;
103-
private $switchOffName!: JQuery<HTMLElement>;
104-
private $switchOffButton!: JQuery<HTMLElement>;
105-
private $helpButton!: JQuery<HTMLElement>;
99+
protected $switchOn!: JQuery<HTMLElement>;
100+
protected $switchOnName!: JQuery<HTMLElement>;
101+
protected $switchOnButton!: JQuery<HTMLElement>;
102+
protected $switchOff!: JQuery<HTMLElement>;
103+
protected $switchOffName!: JQuery<HTMLElement>;
104+
protected $switchOffButton!: JQuery<HTMLElement>;
105+
protected $helpButton!: JQuery<HTMLElement>;
106106

107107
doRender() {
108108
this.$widget = $(TPL);

src/public/app/widgets/template_switch.js renamed to src/public/app/widgets/template_switch.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import SwitchWidget from "./switch.js";
22
import attributeService from "../services/attributes.js";
33
import { t } from "../services/i18n.js";
4+
import type FNote from "../entities/fnote.js";
5+
import type { EventData } from "../components/app_context.js";
46

57
/**
68
* 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.
79
*/
810
export default class TemplateSwitchWidget extends SwitchWidget {
911
isEnabled() {
10-
return super.isEnabled() && !this.noteId.startsWith("_options");
12+
return super.isEnabled() && !this.noteId?.startsWith("_options");
1113
}
1214

1315
doRender() {
@@ -23,22 +25,28 @@ export default class TemplateSwitchWidget extends SwitchWidget {
2325
}
2426

2527
async switchOn() {
26-
await attributeService.setLabel(this.noteId, "template");
28+
if (this.noteId) {
29+
await attributeService.setLabel(this.noteId, "template");
30+
}
2731
}
2832

2933
async switchOff() {
34+
if (!this.note || !this.noteId) {
35+
return;
36+
}
37+
3038
for (const templateAttr of this.note.getOwnedLabels("template")) {
3139
await attributeService.removeAttributeById(this.noteId, templateAttr.attributeId);
3240
}
3341
}
3442

35-
async refreshWithNote(note) {
43+
async refreshWithNote(note: FNote) {
3644
const isTemplate = note.hasLabel("template");
3745
this.$switchOn.toggle(!isTemplate);
3846
this.$switchOff.toggle(!!isTemplate);
3947
}
4048

41-
entitiesReloadedEvent({ loadResults }) {
49+
entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
4250
if (loadResults.getAttributeRows().find((attr) => attr.type === "label" && attr.name === "template" && attr.noteId === this.noteId)) {
4351
this.refresh();
4452
}

0 commit comments

Comments
 (0)