import NoteContextAwareWidget from "../note_context_aware_widget.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"; const TPL = /*html*/`
${t("book_properties.view_type")}:   
`; export default class BookPropertiesWidget extends NoteContextAwareWidget { private $viewTypeSelect!: JQuery; private $expandChildrenButton!: JQuery; private $collapseAllButton!: JQuery; get name() { return "bookProperties"; } get toggleCommand() { return "toggleRibbonTabBookProperties"; } isEnabled() { return this.note && this.note.type === "book"; } getTitle() { return { show: this.isEnabled(), activate: true, title: t("book_properties.book_properties"), icon: "bx bx-book" }; } doRender() { this.$widget = $(TPL); this.contentSized(); this.$viewTypeSelect = this.$widget.find(".view-type-select"); this.$viewTypeSelect.on("change", () => this.toggleViewType(String(this.$viewTypeSelect.val()))); this.$expandChildrenButton = this.$widget.find(".expand-children-button"); this.$expandChildrenButton.on("click", async () => { if (!this.noteId || !this.note) { return; } if (!this.note?.isLabelTruthy("expanded")) { await attributeService.addLabel(this.noteId, "expanded"); } this.triggerCommand("refreshNoteList", { noteId: this.noteId }); }); this.$collapseAllButton = this.$widget.find(".collapse-all-button"); this.$collapseAllButton.on("click", async () => { if (!this.noteId || !this.note) { return; } // owned is important - we shouldn't remove inherited expanded labels for (const expandedAttr of this.note.getOwnedLabels("expanded")) { await attributeService.removeAttributeById(this.noteId, expandedAttr.attributeId); } this.triggerCommand("refreshNoteList", { noteId: this.noteId }); }); } async refreshWithNote(note: FNote) { if (!this.note) { return; } const viewType = this.note.getLabelValue("viewType") || "grid"; this.$viewTypeSelect.val(viewType); this.$expandChildrenButton.toggle(viewType === "list"); this.$collapseAllButton.toggle(viewType === "list"); } async toggleViewType(type: string) { if (!this.noteId) { return; } if (!["list", "grid", "calendar"].includes(type)) { throw new Error(t("book_properties.invalid_view_type", { type })); } await attributeService.setLabel(this.noteId, "viewType", type); } entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) { if (loadResults.getAttributeRows().find((attr) => attr.noteId === this.noteId && attr.name === "viewType")) { this.refresh(); } } }