Notes/src/public/javascripts/widgets/tab_caching_widget.js

89 lines
2.5 KiB
JavaScript
Raw Normal View History

2020-01-14 20:27:40 +01:00
import TabAwareWidget from "./tab_aware_widget.js";
2020-02-16 20:09:59 +01:00
import keyboardActionsService from "../services/keyboard_actions.js";
2020-01-14 20:27:40 +01:00
export default class TabCachingWidget extends TabAwareWidget {
2020-02-27 00:58:10 +01:00
constructor(widgetFactory) {
super();
2020-01-14 20:27:40 +01:00
this.widgetFactory = widgetFactory;
this.widgets = {};
}
doRender() {
return this.$widget = $(`<div class="marker" style="display: none;">`);
2020-01-14 20:27:40 +01:00
}
handleEventInChildren(name, data) {
2020-02-08 17:44:34 +01:00
// stop propagation of the event to the children, individual tab widget should not know about tab switching
// since they are per-tab
2020-02-28 11:46:35 +01:00
if (name === 'tabNoteSwitchedAndActivated') {
name = 'tabNoteSwitched';
2020-02-28 11:46:35 +01:00
}
if (name === 'tabNoteSwitched') {
// this event is propagated only to the widgets of a particular tab
const widget = this.widgets[data.tabContext.tabId];
if (widget) {
return widget.handleEvent(name, data);
}
else {
return Promise.resolve();
}
}
if (name !== 'activeTabChanged') {
return super.handleEventInChildren(name, data);
2020-02-12 20:31:31 +01:00
}
return Promise.resolve();
2020-02-08 17:44:34 +01:00
}
2020-03-06 22:17:07 +01:00
async newTabOpenedEvent({tabContext}) {
const {tabId} = tabContext;
2020-02-29 16:26:46 +01:00
if (this.widgets[tabId]) {
return;
}
this.widgets[tabId] = this.widgetFactory();
const $renderedWidget = this.widgets[tabId].render();
2020-03-06 22:17:07 +01:00
this.widgets[tabId].toggleExt(this.widgets[tabId]);
2020-02-29 16:26:46 +01:00
this.$widget.after($renderedWidget);
keyboardActionsService.updateDisplayedShortcuts($renderedWidget);
2020-03-07 13:40:46 +01:00
await this.widgets[tabId].handleEvent('setTabContext', {tabContext});
2020-02-29 16:26:46 +01:00
this.child(this.widgets[tabId]); // add as child only once it is ready (rendered with tabContext)
}
2020-03-07 13:40:46 +01:00
async refresh() {
const activeTabId = this.tabContext && this.tabContext.tabId;
2020-02-27 14:35:12 +01:00
2020-03-07 13:40:46 +01:00
for (const tabId in this.widgets) {
this.widgets[tabId].toggleExt(tabId === activeTabId);
2020-01-14 20:27:40 +01:00
}
}
2020-01-19 11:37:24 +01:00
2020-02-16 19:23:49 +01:00
tabRemovedEvent({tabId}) {
2020-01-19 21:12:53 +01:00
const widget = this.widgets[tabId];
if (widget) {
widget.remove();
2020-02-09 21:13:05 +01:00
delete this.widgets[tabId];
this.children = this.children.filter(ch => ch !== widget);
2020-01-19 21:12:53 +01:00
}
}
2020-03-06 22:17:07 +01:00
toggleInt(show) {} // not needed
toggleByTab(show) {
2020-01-19 11:37:24 +01:00
for (const tabId in this.widgets) {
2020-03-06 22:17:07 +01:00
this.widgets[tabId].toggleExt(show && this.isTab(tabId));
2020-01-19 11:37:24 +01:00
}
}
2020-01-14 20:27:40 +01:00
}