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

151 lines
4.3 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-12-23 23:48:34 +08:00
import splitService from "./split.js";
2019-07-21 17:32:38 +08:00
const $sidebar = $("#right-pane");
2019-12-23 23:48:34 +08:00
const $sidebarContainer = $('#sidebar-container');
2019-12-23 23:48:34 +08:00
const $showSideBarButton = $("#show-sidebar-button");
const $hideSidebarButton = $("#hide-sidebar-button");
$showSideBarButton.hide();
$hideSidebarButton.on('click', () => {
$sidebar.hide();
$showSideBarButton.show();
2019-12-23 23:48:34 +08:00
$hideSidebarButton.hide();
splitService.setupSplitWithoutSidebar();
});
// FIXME shoud run note loaded!
$showSideBarButton.on('click', () => {
$sidebar.show();
$showSideBarButton.hide();
2019-12-23 23:48:34 +08:00
$hideSidebarButton.show();
splitService.setupSplitWithSidebar();
});
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.$widgetContainer = $sidebar.find(`.sidebar-widget-container[data-tab-id=${this.ctx.tabId}]`);
2019-08-17 03:52:36 +08:00
if (this.$widgetContainer.length === 0) {
this.$widgetContainer = $(`<div class="sidebar-widget-container">`).attr('data-tab-id', this.ctx.tabId);
$sidebarContainer.append(this.$widgetContainer);
}
2019-07-21 17:32:38 +08:00
}
2019-08-15 16:04:03 +08:00
isVisible() {
return $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-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"),
2019-11-20 06:02:54 +08:00
import("../widgets/similar_notes.js"),
2019-09-07 16:11:59 +08:00
import("../widgets/edited_notes.js"),
2019-09-08 22:06:42 +08:00
import("../widgets/calendar.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);
}
const widgets = [];
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()) {
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.renderWidgets(widgets);
}
show() {
$sidebarContainer.find('.sidebar-widget-container').each((i, el) => {
$(el).toggle($(el).attr('data-tab-id') === this.ctx.tabId);
});
}
// it's important that this method is sync so that the whole render-update is atomic
// otherwise we could get race issues (doubled widgets etc.)
renderWidgets(widgets) {
// cleanup old widgets
for (const widget of this.widgets) {
if (widget.cleanup) {
widget.cleanup();
}
}
this.widgets = widgets;
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 = widget.render();
widgetsToAppend.push($el);
} 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;