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

91 lines
2.9 KiB
JavaScript
Raw Normal View History

2019-07-21 17:32:38 +08:00
import NoteInfoWidget from "../widgets/note_info.js";
2019-07-22 03:55:48 +08:00
import LinkMapWidget from "../widgets/link_map.js";
2019-07-26 04:31:09 +08:00
import NoteRevisionsWidget from "../widgets/note_revisions.js";
import AttributesWidget from "../widgets/attributes.js";
2019-08-20 02:59:40 +08:00
import WhatLinksHereWidget from "../widgets/what_links_here.js";
2019-08-17 17:28:36 +08:00
import bundleService from "./bundle.js";
import messagingService from "./messaging.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;
this.state = state;
this.widgets = [];
2019-08-16 03:08:41 +08:00
this.rendered = false;
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();
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() {
this.widgets = [];
2019-08-16 03:08:41 +08:00
this.$widgetContainer.empty();
2019-07-21 17:32:38 +08:00
2019-08-20 02:59:40 +08:00
const widgetClasses = [AttributesWidget, LinkMapWidget, WhatLinksHereWidget, NoteRevisionsWidget, NoteInfoWidget];
2019-07-26 04:31:09 +08:00
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) {
const state = (this.state.widgets || []).find(s => s.name === widgetClass.name);
2019-08-17 17:28:36 +08:00
try {
const widget = new widgetClass(this.ctx, state);
await widget.renderBody();
2019-07-26 04:31:09 +08:00
2019-08-17 17:28:36 +08:00
this.widgets.push(widget);
this.$widgetContainer.append(widget.getWidgetElement());
}
catch (e) {
messagingService.logError(`Error while loading widget ${widgetClass.name}: ${e.message}`);
}
2019-08-07 03:36:54 +08:00
}
2019-07-21 17:32:38 +08:00
}
syncDataReceived(syncData) {
for (const widget of this.widgets) {
if (widget.syncDataReceived) {
widget.syncDataReceived(syncData);
}
}
}
2019-07-21 16:17:08 +08:00
}
export default Sidebar;