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"; import { bookPropertiesConfig, BookProperty } from "./book_properties_config.js"; import attributes from "../../services/attributes.js"; import type { ViewTypeOptions } from "../../services/note_list_renderer.js"; const VIEW_TYPE_MAPPINGS: Record = { grid: t("book_properties.grid"), list: t("book_properties.list"), calendar: t("book_properties.calendar"), table: t("book_properties.table"), geoMap: t("book_properties.geo-map"), board: t("book_properties.board") }; const TPL = /*html*/`
${t("book_properties.view_type")}:   
`; export default class BookPropertiesWidget extends NoteContextAwareWidget { private $viewTypeSelect!: JQuery; private $propertiesContainer!: JQuery; private labelsToWatch: string[] = []; get name() { return "bookProperties"; } get toggleCommand() { return "toggleRibbonTabBookProperties"; } isEnabled() { return this.note && this.note.type === "book"; } getTitle() { return { show: this.isEnabled(), 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.$propertiesContainer = this.$widget.find(".book-properties-container"); } async refreshWithNote(note: FNote) { if (!this.note) { return; } const viewType = this.note.getLabelValue("viewType") || "grid"; this.$viewTypeSelect.val(viewType); this.$propertiesContainer.empty(); const bookPropertiesData = bookPropertiesConfig[viewType]; if (bookPropertiesData) { for (const property of bookPropertiesData.properties) { this.$propertiesContainer.append(this.renderBookProperty(property)); this.labelsToWatch.push(property.bindToLabel); } } } async toggleViewType(type: string) { if (!this.noteId) { return; } if (!VIEW_TYPE_MAPPINGS.hasOwnProperty(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.labelsToWatch.includes(attr.name ?? "")))) { this.refresh(); } } renderBookProperty(property: BookProperty) { const $container = $("
"); $container.addClass(`type-${property.type}`); const note = this.note; if (!note) { return $container; } switch (property.type) { case "checkbox": const $label = $("