Notes/src/public/app/widgets/ribbon_widgets/book_properties.ts

142 lines
4.3 KiB
TypeScript
Raw Normal View History

2021-05-27 23:17:13 +02:00
import NoteContextAwareWidget from "../note_context_aware_widget.js";
import attributeService from "../../services/attributes.js";
import { t } from "../../services/i18n.js";
2025-02-13 22:45:48 +02:00
import type FNote from "../../entities/fnote.js";
import type { EventData } from "../../components/app_context.js";
2021-05-27 23:17:13 +02:00
const TPL = `
<div class="book-properties-widget">
<style>
.book-properties-widget {
padding: 12px 12px 6px 12px;
display: flex;
}
2025-02-13 22:45:48 +02:00
2021-05-27 23:17:13 +02:00
.book-properties-widget > * {
margin-right: 15px;
}
</style>
<div style="display: flex; align-items: baseline">
<span style="white-space: nowrap">${t("book_properties.view_type")}:&nbsp; &nbsp;</span>
2025-02-13 22:45:48 +02:00
2024-09-12 13:55:07 +02:00
<select class="view-type-select form-select form-select-sm">
<option value="grid">${t("book_properties.grid")}</option>
<option value="list">${t("book_properties.list")}</option>
<option value="calendar">${t("book_properties.calendar")}</option>
2021-05-27 23:17:13 +02:00
</select>
</div>
2025-02-13 22:45:48 +02:00
2021-05-27 23:17:13 +02:00
<button type="button"
class="collapse-all-button btn btn-sm"
title="${t("book_properties.collapse_all_notes")}">
2025-02-13 22:45:48 +02:00
2021-05-27 23:17:13 +02:00
<span class="bx bx-layer-minus"></span>
2025-02-13 22:45:48 +02:00
${t("book_properties.collapse")}
2021-05-27 23:17:13 +02:00
</button>
<button type="button"
class="expand-children-button btn btn-sm"
title="${t("book_properties.expand_all_children")}">
2021-05-27 23:17:13 +02:00
<span class="bx bx-move-vertical"></span>
2025-02-13 22:45:48 +02:00
${t("book_properties.expand")}
2021-05-27 23:17:13 +02:00
</button>
</div>
`;
2021-05-27 23:17:13 +02:00
export default class BookPropertiesWidget extends NoteContextAwareWidget {
2025-02-13 22:45:48 +02:00
private $viewTypeSelect!: JQuery<HTMLElement>;
private $expandChildrenButton!: JQuery<HTMLElement>;
private $collapseAllButton!: JQuery<HTMLElement>;
2021-06-27 12:53:05 +02:00
get name() {
return "bookProperties";
}
get toggleCommand() {
return "toggleRibbonTabBookProperties";
}
2021-05-27 23:17:13 +02:00
isEnabled() {
2025-01-09 18:07:02 +02:00
return this.note && this.note.type === "book";
2021-05-27 23:17:13 +02:00
}
getTitle() {
return {
show: this.isEnabled(),
activate: true,
title: t("book_properties.book_properties"),
2025-01-09 18:07:02 +02:00
icon: "bx bx-book"
2021-05-27 23:17:13 +02:00
};
}
doRender() {
this.$widget = $(TPL);
2021-06-13 22:55:31 +02:00
this.contentSized();
2021-05-27 23:17:13 +02:00
2025-01-09 18:07:02 +02:00
this.$viewTypeSelect = this.$widget.find(".view-type-select");
2025-02-13 22:45:48 +02:00
this.$viewTypeSelect.on("change", () => this.toggleViewType(String(this.$viewTypeSelect.val())));
2021-05-27 23:17:13 +02:00
2025-01-09 18:07:02 +02:00
this.$expandChildrenButton = this.$widget.find(".expand-children-button");
this.$expandChildrenButton.on("click", async () => {
2025-02-13 22:45:48 +02:00
if (!this.noteId || !this.note) {
return;
}
if (!this.note?.isLabelTruthy("expanded")) {
2025-01-09 18:07:02 +02:00
await attributeService.addLabel(this.noteId, "expanded");
2021-05-27 23:17:13 +02:00
}
2021-09-29 13:19:21 +02:00
2025-01-09 18:07:02 +02:00
this.triggerCommand("refreshNoteList", { noteId: this.noteId });
2021-05-27 23:17:13 +02:00
});
2025-01-09 18:07:02 +02:00
this.$collapseAllButton = this.$widget.find(".collapse-all-button");
this.$collapseAllButton.on("click", async () => {
2025-02-13 22:45:48 +02:00
if (!this.noteId || !this.note) {
return;
}
2021-05-27 23:17:13 +02:00
// owned is important - we shouldn't remove inherited expanded labels
2025-01-09 18:07:02 +02:00
for (const expandedAttr of this.note.getOwnedLabels("expanded")) {
2021-05-27 23:17:13 +02:00
await attributeService.removeAttributeById(this.noteId, expandedAttr.attributeId);
}
2021-09-29 13:19:21 +02:00
2025-01-09 18:07:02 +02:00
this.triggerCommand("refreshNoteList", { noteId: this.noteId });
2021-05-27 23:17:13 +02:00
});
}
2025-02-13 22:45:48 +02:00
async refreshWithNote(note: FNote) {
if (!this.note) {
return;
}
2025-01-09 18:07:02 +02:00
const viewType = this.note.getLabelValue("viewType") || "grid";
2021-05-27 23:17:13 +02:00
this.$viewTypeSelect.val(viewType);
2025-01-09 18:07:02 +02:00
this.$expandChildrenButton.toggle(viewType === "list");
this.$collapseAllButton.toggle(viewType === "list");
2021-05-27 23:17:13 +02:00
}
2025-02-13 22:45:48 +02:00
async toggleViewType(type: string) {
if (!this.noteId) {
return;
}
if (![ "list", "grid", "calendar"].includes(type)) {
throw new Error(t("book_properties.invalid_view_type", { type }));
2021-05-27 23:17:13 +02:00
}
2025-01-09 18:07:02 +02:00
await attributeService.setLabel(this.noteId, "viewType", type);
2021-05-27 23:17:13 +02:00
}
2025-02-13 22:45:48 +02:00
entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
2025-01-09 18:07:02 +02:00
if (loadResults.getAttributeRows().find((attr) => attr.noteId === this.noteId && attr.name === "viewType")) {
2021-05-27 23:17:13 +02:00
this.refresh();
}
}
}