mirror of
https://github.com/zadam/trilium.git
synced 2025-10-20 15:19:01 +02:00
refactor(book_properties): move rendering to book_properties
This commit is contained in:
parent
196bba9cda
commit
51b7955ccd
@ -3,7 +3,8 @@ import attributeService from "../../services/attributes.js";
|
|||||||
import { t } from "../../services/i18n.js";
|
import { t } from "../../services/i18n.js";
|
||||||
import type FNote from "../../entities/fnote.js";
|
import type FNote from "../../entities/fnote.js";
|
||||||
import type { EventData } from "../../components/app_context.js";
|
import type { EventData } from "../../components/app_context.js";
|
||||||
import { bookPropertiesConfig, renderBookProperty } from "./book_properties_config.js";
|
import { bookPropertiesConfig, BookProperty } from "./book_properties_config.js";
|
||||||
|
import attributes from "../../services/attributes.js";
|
||||||
|
|
||||||
const TPL = /*html*/`
|
const TPL = /*html*/`
|
||||||
<div class="book-properties-widget">
|
<div class="book-properties-widget">
|
||||||
@ -98,10 +99,7 @@ export default class BookPropertiesWidget extends NoteContextAwareWidget {
|
|||||||
const bookPropertiesData = bookPropertiesConfig[viewType];
|
const bookPropertiesData = bookPropertiesConfig[viewType];
|
||||||
if (bookPropertiesData) {
|
if (bookPropertiesData) {
|
||||||
for (const property of bookPropertiesData.properties) {
|
for (const property of bookPropertiesData.properties) {
|
||||||
this.$propertiesContainer.append(renderBookProperty(property, {
|
this.$propertiesContainer.append(this.renderBookProperty(property));
|
||||||
note: this.note,
|
|
||||||
triggerCommand: this.triggerCommand.bind(this)
|
|
||||||
}));
|
|
||||||
this.labelsToWatch.push(property.bindToLabel);
|
this.labelsToWatch.push(property.bindToLabel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -126,4 +124,54 @@ export default class BookPropertiesWidget extends NoteContextAwareWidget {
|
|||||||
this.refresh();
|
this.refresh();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
renderBookProperty(property: BookProperty) {
|
||||||
|
const $container = $("<div>");
|
||||||
|
const note = this.note;
|
||||||
|
if (!note) {
|
||||||
|
return $container;
|
||||||
|
}
|
||||||
|
switch (property.type) {
|
||||||
|
case "checkbox":
|
||||||
|
const $label = $("<label>").text(property.label);
|
||||||
|
const $checkbox = $("<input>", {
|
||||||
|
type: "checkbox",
|
||||||
|
class: "form-check-input",
|
||||||
|
});
|
||||||
|
$checkbox.on("change", () => {
|
||||||
|
if ($checkbox.prop("checked")) {
|
||||||
|
attributes.setLabel(note.noteId, property.bindToLabel);
|
||||||
|
} else {
|
||||||
|
attributes.removeOwnedLabelByName(note, property.bindToLabel);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
$checkbox.prop("checked", note.hasOwnedLabel(property.bindToLabel));
|
||||||
|
$label.prepend($checkbox);
|
||||||
|
$container.append($label);
|
||||||
|
break;
|
||||||
|
case "button":
|
||||||
|
const $button = $("<button>", {
|
||||||
|
type: "button",
|
||||||
|
class: "btn btn-sm"
|
||||||
|
}).text(property.label);
|
||||||
|
if (property.title) {
|
||||||
|
$button.attr("title", property.title);
|
||||||
|
}
|
||||||
|
if (property.icon) {
|
||||||
|
$button.prepend($("<span>", { class: property.icon }));
|
||||||
|
}
|
||||||
|
$button.on("click", () => {
|
||||||
|
property.onClick({
|
||||||
|
note,
|
||||||
|
triggerCommand: this.triggerCommand.bind(this)
|
||||||
|
});
|
||||||
|
});
|
||||||
|
$container.append($button);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $container;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import attributes from "../../services/attributes";
|
|||||||
import { ViewTypeOptions } from "../../services/note_list_renderer"
|
import { ViewTypeOptions } from "../../services/note_list_renderer"
|
||||||
import NoteContextAwareWidget from "../note_context_aware_widget";
|
import NoteContextAwareWidget from "../note_context_aware_widget";
|
||||||
|
|
||||||
type BookProperty = CheckBoxProperty | ButtonProperty;
|
export type BookProperty = CheckBoxProperty | ButtonProperty;
|
||||||
|
|
||||||
interface BookConfig {
|
interface BookConfig {
|
||||||
properties: BookProperty[];
|
properties: BookProperty[];
|
||||||
@ -79,45 +79,3 @@ export const bookPropertiesConfig: Record<ViewTypeOptions, BookConfig> = {
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export function renderBookProperty(property: BookProperty, context: BookContext) {
|
|
||||||
const $container = $("<div>");
|
|
||||||
const { note } = context;
|
|
||||||
switch (property.type) {
|
|
||||||
case "checkbox":
|
|
||||||
const $label = $("<label>").text(property.label);
|
|
||||||
const $checkbox = $("<input>", {
|
|
||||||
type: "checkbox",
|
|
||||||
class: "form-check-input",
|
|
||||||
});
|
|
||||||
$checkbox.on("change", () => {
|
|
||||||
if ($checkbox.prop("checked")) {
|
|
||||||
attributes.setLabel(note.noteId, property.bindToLabel);
|
|
||||||
} else {
|
|
||||||
attributes.removeOwnedLabelByName(note, property.bindToLabel);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
$checkbox.prop("checked", note.hasOwnedLabel(property.bindToLabel));
|
|
||||||
$label.prepend($checkbox);
|
|
||||||
$container.append($label);
|
|
||||||
break;
|
|
||||||
case "button":
|
|
||||||
const $button = $("<button>", {
|
|
||||||
type: "button",
|
|
||||||
class: "btn btn-sm"
|
|
||||||
}).text(property.label);
|
|
||||||
if (property.title) {
|
|
||||||
$button.attr("title", property.title);
|
|
||||||
}
|
|
||||||
if (property.icon) {
|
|
||||||
$button.prepend($("<span>", { class: property.icon }));
|
|
||||||
}
|
|
||||||
$button.on("click", () => {
|
|
||||||
property.onClick(context);
|
|
||||||
});
|
|
||||||
$container.append($button);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $container;
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user