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

72 lines
2 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-07-21 17:32:38 +08:00
let widgetIdCtr = 1;
2019-07-21 16:17:08 +08:00
class Sidebar {
/**
* @param {TabContext} ctx
*/
constructor(ctx) {
this.ctx = ctx;
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.$showSideBarButton.hide();
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-15 16:04:03 +08:00
isVisible() {
return this.$sidebar.is(":visible");
}
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
const widgetClasses = [AttributesWidget, LinkMapWidget, NoteRevisionsWidget, NoteInfoWidget];
2019-07-26 04:31:09 +08:00
2019-08-07 03:36:54 +08:00
for (const widgetClass of widgetClasses) {
2019-08-16 03:08:41 +08:00
const widget = new widgetClass(this.ctx);
this.widgets.push(widget);
2019-07-26 04:31:09 +08:00
2019-08-16 03:08:41 +08:00
widget.renderBody(); // let it run in parallel
2019-08-16 03:08:41 +08:00
this.$widgetContainer.append(widget.getWidgetElement());
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;