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-02-29 22:04:46 +01:00
|
|
|
import appContext from "../services/app_context.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 = {};
|
|
|
|
}
|
|
|
|
|
2020-02-08 21:54:39 +01:00
|
|
|
isEnabled() {
|
|
|
|
return this.tabContext && this.tabContext.isActive();
|
2020-02-08 20:53:07 +01:00
|
|
|
}
|
|
|
|
|
2020-01-19 15:36:42 +01:00
|
|
|
doRender() {
|
2020-02-08 21:54:39 +01:00
|
|
|
return this.$widget = $(`<div class="marker" style="display: none;">`);
|
2020-01-14 20:27:40 +01:00
|
|
|
}
|
|
|
|
|
2020-02-29 19:43:19 +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') {
|
2020-02-29 19:43:19 +01:00
|
|
|
return super.handleEventInChildren('tabNoteSwitched', data);
|
2020-02-28 11:46:35 +01:00
|
|
|
}
|
|
|
|
else if (name !== 'activeTabChanged') {
|
2020-02-29 19:43:19 +01:00
|
|
|
return super.handleEventInChildren(name, data);
|
2020-02-12 20:31:31 +01:00
|
|
|
}
|
2020-02-29 19:43:19 +01:00
|
|
|
|
|
|
|
return Promise.resolve();
|
2020-02-08 17:44:34 +01:00
|
|
|
}
|
|
|
|
|
2020-02-29 16:26:46 +01:00
|
|
|
async newTabOpenedEvent({tabId}) {
|
|
|
|
if (this.widgets[tabId]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.widgets[tabId] = this.widgetFactory();
|
|
|
|
|
|
|
|
const $renderedWidget = this.widgets[tabId].render();
|
|
|
|
this.$widget.after($renderedWidget);
|
|
|
|
|
|
|
|
keyboardActionsService.updateDisplayedShortcuts($renderedWidget);
|
|
|
|
|
2020-02-29 22:04:46 +01:00
|
|
|
await this.widgets[tabId].handleEvent('setTabContext', {
|
|
|
|
tabContext: appContext.tabManager.getTabContextById(tabId)
|
|
|
|
});
|
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-02-28 11:46:35 +01:00
|
|
|
async refreshWithNote() {
|
2020-01-14 20:27:40 +01:00
|
|
|
for (const widget of Object.values(this.widgets)) {
|
|
|
|
widget.toggle(false);
|
|
|
|
}
|
|
|
|
|
2020-01-24 17:54:47 +01:00
|
|
|
if (!this.tabContext) {
|
2020-02-04 22:46:17 +01:00
|
|
|
console.log(`No tabContext in widget ${this.componentId}.`);
|
2020-01-24 20:15:53 +01:00
|
|
|
|
2020-01-24 17:54:47 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-29 16:26:46 +01:00
|
|
|
const widget = this.widgets[this.tabContext.tabId];
|
2020-02-27 14:35:12 +01:00
|
|
|
|
2020-02-29 16:26:46 +01:00
|
|
|
if (widget) {
|
|
|
|
widget.toggle(widget.isEnabled());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.error(`Widget for tab ${this.tabContext.tabId} not found.`);
|
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-02-08 21:54:39 +01:00
|
|
|
toggle(show) {
|
2020-01-19 11:37:24 +01:00
|
|
|
for (const tabId in this.widgets) {
|
2020-02-08 20:53:07 +01:00
|
|
|
this.widgets[tabId].toggle(
|
|
|
|
show
|
2020-02-09 21:53:10 +01:00
|
|
|
&& this.isTab(tabId)
|
2020-02-08 21:54:39 +01:00
|
|
|
&& this.widgets[tabId].isEnabled());
|
2020-01-19 11:37:24 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-14 20:27:40 +01:00
|
|
|
}
|