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";
|
2019-08-07 02:05:05 +08:00
|
|
|
import AttributesWidget from "../widgets/attributes.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;
|
2019-08-17 03:29:44 +08:00
|
|
|
this.state = state;
|
2019-08-07 04:39:27 +08:00
|
|
|
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() {
|
2019-08-07 04:39:27 +08:00
|
|
|
this.widgets = [];
|
2019-08-16 03:08:41 +08:00
|
|
|
this.$widgetContainer.empty();
|
2019-07-21 17:32:38 +08:00
|
|
|
|
2019-08-16 03:18:33 +08:00
|
|
|
const widgetClasses = [AttributesWidget, LinkMapWidget, 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) {
|
2019-08-17 03:29:44 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-08-07 04:39:27 +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;
|