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

106 lines
3.2 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
}
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-17 22:49:43 +01:00
this.widgets[tabId].toggleExt(false); // new tab is always not active, can be activated after creation
2020-03-06 22:17:07 +01:00
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)
}
2021-05-20 23:13:34 +02:00
tabRemovedEvent({tabIds}) {
for (const tabId of tabIds) {
const widget = this.widgets[tabId];
2020-01-19 21:12:53 +01:00
2021-05-20 23:13:34 +02:00
if (widget) {
widget.remove();
delete this.widgets[tabId];
2020-02-09 21:13:05 +01:00
2021-05-20 23:13:34 +02:00
this.children = this.children.filter(ch => ch !== widget);
}
2020-01-19 21:12:53 +01:00
}
}
2020-03-15 11:08:16 +01:00
async refresh() {
this.toggleExt(true);
}
2020-03-06 22:17:07 +01:00
toggleInt(show) {} // not needed
2020-03-15 11:08:16 +01:00
toggleExt(show) {
2020-01-19 11:37:24 +01:00
for (const tabId in this.widgets) {
2021-05-21 22:34:40 +02:00
this.widgets[tabId].toggleExt(show && this.isTab(tabId));
2020-01-19 11:37:24 +01:00
}
}
2020-03-15 11:08:16 +01:00
/**
* widget.hasBeenAlreadyShown is intended for lazy loading of cached tabs - initial note switches of new tabs
* are not executed, we're waiting for the first tab activation and then we update the tab. After this initial
* activation further note switches are always propagated to the tabs.
*/
2020-03-15 11:08:16 +01:00
handleEventInChildren(name, data) {
if (['tabNoteSwitched', 'tabNoteSwitchedAndActivated'].includes(name)) {
2020-03-15 11:08:16 +01:00
// this event is propagated only to the widgets of a particular tab
2021-05-19 22:45:34 +02:00
let widget = this.widgets[data.tabContext.tabId];
if (!widget) {
widget = this.widgets[data.tabContext.parentTabId];
}
2020-03-15 11:08:16 +01:00
if (widget && (widget.hasBeenAlreadyShown || name === 'tabNoteSwitchedAndActivated')) {
2020-04-23 23:08:15 +02:00
widget.hasBeenAlreadyShown = true;
return widget.handleEvent('tabNoteSwitched', data);
2020-03-15 11:08:16 +01:00
}
else {
return Promise.resolve();
}
}
if (name === 'activeTabChanged') {
2021-05-19 22:45:34 +02:00
let widget = this.widgets[data.tabContext.tabId];
if (!widget) {
widget = this.widgets[data.tabContext.parentTabId];
}
if (widget.hasBeenAlreadyShown) {
return Promise.resolve();
}
else {
widget.hasBeenAlreadyShown = true;
return widget.handleEvent(name, data);
}
} else {
2020-03-15 11:08:16 +01:00
return super.handleEventInChildren(name, data);
}
}
2021-05-19 22:45:34 +02:00
}