trilium/src/public/javascripts/services/sidebar.js

122 lines
3.6 KiB
JavaScript
Raw Normal View History

2019-08-17 17:28:36 +08:00
import bundleService from "./bundle.js";
2019-08-27 02:21:43 +08:00
import ws from "./ws.js";
2019-08-25 23:41:08 +08:00
import optionsService from "./options.js";
2019-07-21 17:32:38 +08:00
2019-07-21 16:17:08 +08:00
class Sidebar {
/**
* @param {TabContext} ctx
2019-08-17 03:52:36 +08:00
* @param {object} state
2019-07-21 16:17:08 +08:00
*/
2019-08-17 03:52:36 +08:00
constructor(ctx, state = {}) {
2019-08-17 17:28:36 +08:00
/** @property {TabContext} */
2019-07-21 16:17:08 +08:00
this.ctx = ctx;
2019-08-29 02:29:10 +08:00
this.state = Object.assign({
widgets: []
}, state);
this.widgets = [];
2019-07-21 16:17:08 +08:00
this.$sidebar = ctx.$tabContent.find(".note-detail-sidebar");
2019-08-16 03:08:41 +08:00
this.$widgetContainer = this.$sidebar.find(".note-detail-widget-container");
2019-07-21 16:17:08 +08:00
this.$showSideBarButton = this.ctx.$tabContent.find(".show-sidebar-button");
this.$hideSidebarButton = this.$sidebar.find(".hide-sidebar-button");
this.$hideSidebarButton.click(() => {
this.$sidebar.hide();
this.$showSideBarButton.show();
2019-08-15 16:04:03 +08:00
this.ctx.stateChanged();
2019-07-21 16:17:08 +08:00
});
this.$showSideBarButton.click(() => {
this.$sidebar.show();
this.$showSideBarButton.hide();
2019-08-15 16:04:03 +08:00
this.ctx.stateChanged();
this.noteLoaded();
2019-07-21 17:32:38 +08:00
});
2019-08-17 03:52:36 +08:00
this.$showSideBarButton.toggle(!state.visible);
this.$sidebar.toggle(state.visible);
2019-07-21 17:32:38 +08:00
}
2019-08-15 16:04:03 +08:00
isVisible() {
2019-08-17 03:52:36 +08:00
return this.$sidebar.css("display") !== "none";
2019-08-15 16:04:03 +08:00
}
getSidebarState() {
return {
visible: this.isVisible(),
widgets: this.widgets.map(w => w.getWidgetState())
}
}
2019-07-21 17:32:38 +08:00
async noteLoaded() {
if (!this.isVisible() || !this.ctx.note) {
return;
}
2019-08-29 03:15:16 +08:00
for (const widget of this.widgets) {
if (widget.cleanup) {
widget.cleanup();
}
2019-08-29 03:15:16 +08:00
}
this.widgets = [];
2019-07-21 17:32:38 +08:00
2019-08-22 02:24:37 +08:00
const widgetClasses = (await Promise.all([
import("../widgets/note_info.js"),
import("../widgets/link_map.js"),
import("../widgets/note_revisions.js"),
import("../widgets/attributes.js"),
import("../widgets/what_links_here.js"),
import("../widgets/similar_notes.js")
2019-08-22 02:24:37 +08:00
])).map(m => m.default);
2019-07-26 04:31:09 +08:00
2019-08-25 23:41:08 +08:00
const options = await optionsService.waitForOptions();
2019-08-17 17:28:36 +08:00
const widgetRelations = await this.ctx.note.getRelations('widget');
for (const widgetRelation of widgetRelations) {
const widgetClass = await bundleService.getAndExecuteBundle(widgetRelation.value, this.ctx.note);
widgetClasses.push(widgetClass);
}
2019-08-07 03:36:54 +08:00
for (const widgetClass of widgetClasses) {
2019-08-17 17:28:36 +08:00
try {
2019-08-29 02:29:10 +08:00
const widget = new widgetClass(this.ctx, options, this.state);
2019-07-26 04:31:09 +08:00
if (await widget.isEnabled()) {
this.widgets.push(widget);
}
2019-08-17 17:28:36 +08:00
}
catch (e) {
2019-08-27 02:21:43 +08:00
ws.logError(`Error while creating widget ${widgetClass.name}: ${e.message}`);
2019-08-27 02:19:14 +08:00
}
}
this.widgets.sort((a, b) => a.getPosition() < b.getPosition() ? -1 : 1);
const widgetsToAppend = [];
2019-08-27 02:19:14 +08:00
for (const widget of this.widgets) {
try {
const $el = await widget.render();
widgetsToAppend.push($el);
2019-08-27 02:19:14 +08:00
}
catch (e) {
ws.logError(`Error while rendering widget ${widget.widgetName}: ${e.message}`);
2019-08-17 17:28:36 +08:00
}
2019-08-07 03:36:54 +08:00
}
2019-09-07 02:50:57 +08:00
// update at once to reduce flickering
this.$widgetContainer.empty().append(...widgetsToAppend);
2019-07-21 17:32:38 +08:00
}
eventReceived(name, data) {
for (const widget of this.widgets) {
if (widget.eventReceived) {
widget.eventReceived(name, data);
}
}
}
2019-07-21 16:17:08 +08:00
}
export default Sidebar;