Notes/src/public/app/widgets/containers/pane_container.js

98 lines
2.9 KiB
JavaScript
Raw Normal View History

2021-05-19 22:45:34 +02:00
import FlexContainer from "./flex_container.js";
import appContext from "../../services/app_context.js";
export default class PaneContainer extends FlexContainer {
constructor(widgetFactory) {
super('row');
this.counter = 0;
this.widgetFactory = widgetFactory;
2021-05-20 23:13:34 +02:00
this.widgets = {};
2021-05-19 22:45:34 +02:00
2021-05-20 23:13:34 +02:00
this.class('pane-container-widget');
2021-05-19 22:45:34 +02:00
this.css('flex-grow', '1');
}
2021-05-20 23:13:34 +02:00
setTabContextEvent({tabContext}) {
/** @var {TabContext} */
this.tabContext = tabContext;
}
2021-05-19 23:00:03 +02:00
2021-05-20 23:13:34 +02:00
async newTabOpenedEvent({tabContext}) {
const widget = this.widgetFactory();
2021-05-19 23:00:03 +02:00
2021-05-20 23:13:34 +02:00
const $renderedWidget = widget.render();
2021-05-19 23:00:03 +02:00
2021-05-20 23:13:34 +02:00
$renderedWidget.on('click', () => {
appContext.tabManager.activateTab(tabContext.tabId);
2021-05-19 23:00:03 +02:00
});
2021-05-20 23:13:34 +02:00
let $parent;
2021-05-19 22:45:34 +02:00
2021-05-20 23:13:34 +02:00
if (!tabContext.parentTabId) {
$parent = $("<div>")
.attr("data-main-tab-id", tabContext.tabId)
.css("display", "flex")
.css("flex-grow", "1");
2021-05-19 23:00:03 +02:00
2021-05-20 23:13:34 +02:00
this.$widget.append($parent);
}
else {
$parent = this.$widget.find(`[data-main-tab-id="${tabContext.parentTabId}"]`);
}
2021-05-19 22:45:34 +02:00
2021-05-20 23:13:34 +02:00
$parent.append($renderedWidget);
2021-05-19 22:45:34 +02:00
2021-05-20 23:13:34 +02:00
this.widgets[tabContext.tabId] = widget;
2021-05-19 23:00:03 +02:00
2021-05-20 23:13:34 +02:00
await widget.handleEvent('setTabContext', { tabContext });
2021-05-19 22:45:34 +02:00
2021-05-20 23:13:34 +02:00
this.child(widget);
}
2021-05-19 22:45:34 +02:00
2021-05-20 23:13:34 +02:00
async openNewPaneCommand() {
const tabContext = await appContext.tabManager.openEmptyTab(null, null, appContext.tabManager.getActiveTabContext().tabId);
2021-05-19 22:45:34 +02:00
2021-05-20 23:13:34 +02:00
await appContext.tabManager.activateTab(tabContext.tabId);
await tabContext.setEmpty();
}
2021-05-19 22:45:34 +02:00
2021-05-20 23:13:34 +02: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.
*/
handleEventInChildren(name, data) {
if (['tabNoteSwitched', 'tabNoteSwitchedAndActivated'].includes(name)) {
// this event is propagated only to the widgets of a particular tab
const widget = this.widgets[data.tabContext.tabId];
if (widget && (widget.hasBeenAlreadyShown || name === 'tabNoteSwitchedAndActivated')) {
widget.hasBeenAlreadyShown = true;
return widget.handleEvent('tabNoteSwitched', data);
}
else {
return Promise.resolve();
}
}
if (name === 'activeTabChanged') {
const widget = this.widgets[data.tabContext.tabId];
if (widget.hasBeenAlreadyShown) {
return Promise.resolve();
}
else {
widget.hasBeenAlreadyShown = true;
return widget.handleEvent(name, data);
}
} else {
return super.handleEventInChildren(name, data);
}
2021-05-19 22:45:34 +02:00
}
}