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

126 lines
3.8 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.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-22 12:35:41 +02:00
async newNoteContextCreatedEvent({noteContext}) {
2021-05-20 23:13:34 +02:00
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-22 12:26:45 +02:00
$renderedWidget.attr("data-tab-id", noteContext.ntxId);
2021-05-19 22:45:34 +02:00
2021-05-22 12:35:41 +02:00
$renderedWidget.on('click', () => appContext.tabManager.activateNoteContext(noteContext.ntxId));
2021-05-19 23:00:03 +02:00
2021-05-21 22:34:40 +02:00
this.$widget.append($renderedWidget);
2021-05-19 22:45:34 +02:00
2021-05-22 22:55:24 +02:00
widget.handleEvent('initialRenderComplete');
2021-05-22 17:58:46 +02:00
widget.toggleExt(false);
2021-05-22 12:26:45 +02:00
this.widgets[noteContext.ntxId] = widget;
2021-05-19 23:00:03 +02:00
2021-05-22 12:26:45 +02:00
await widget.handleEvent('setNoteContext', { noteContext });
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() {
2021-05-22 12:35:41 +02:00
const noteContext = await appContext.tabManager.openEmptyTab(null, 'root', appContext.tabManager.getActiveContext().ntxId);
2021-05-19 22:45:34 +02:00
2021-05-22 12:35:41 +02:00
await appContext.tabManager.activateNoteContext(noteContext.ntxId);
2021-05-20 23:13:34 +02:00
2021-05-22 12:26:45 +02:00
await noteContext.setEmpty();
2021-05-20 23:13:34 +02:00
}
2021-05-19 22:45:34 +02:00
2021-05-22 17:58:46 +02:00
activeContextChangedEvent() {
this.refresh();
}
noteSwitchedAndActivatedEvent() {
this.refresh();
}
2021-05-21 22:34:40 +02:00
async refresh() {
this.toggleExt(true);
}
toggleInt(show) {} // not needed
toggleExt(show) {
2021-05-22 13:04:08 +02:00
const activeMainContext = appContext.tabManager.getActiveMainContext();
const activeNtxId = activeMainContext ? activeMainContext.ntxId : null;
2021-05-21 22:34:40 +02:00
2021-05-22 12:26:45 +02:00
for (const ntxId in this.widgets) {
const noteContext = appContext.tabManager.getNoteContextById(ntxId);
2021-05-22 22:55:24 +02:00
console.log(noteContext, activeNtxId);
2021-05-22 12:26:45 +02:00
const widget = this.widgets[ntxId];
2021-05-22 13:04:08 +02:00
widget.toggleExt(show && activeNtxId && [noteContext.ntxId, noteContext.mainNtxId].includes(activeNtxId));
2021-05-21 22:34:40 +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) {
2021-05-22 12:35:41 +02:00
if (['noteSwitched', 'noteSwitchedAndActivated'].includes(name)) {
2021-05-20 23:13:34 +02:00
// this event is propagated only to the widgets of a particular tab
2021-05-22 12:26:45 +02:00
const widget = this.widgets[data.noteContext.ntxId];
2021-05-20 23:13:34 +02:00
2021-05-21 22:34:40 +02:00
if (!widget) {
return Promise.resolve();
}
2021-05-22 17:58:46 +02:00
if (widget.hasBeenAlreadyShown
|| name === 'noteSwitchedAndActivated'
|| appContext.tabManager.getActiveMainContext() === data.noteContext.getMainContext()
) {
widget.hasBeenAlreadyShown = true;
2021-05-21 22:34:40 +02:00
2021-05-22 17:58:46 +02:00
return [
widget.handleEvent('noteSwitched', data),
this.refreshNotShown(data)
];
2021-05-20 23:13:34 +02:00
}
2021-05-22 17:58:46 +02:00
else {
return Promise.resolve();
2021-05-20 23:13:34 +02:00
}
}
2021-05-22 17:58:46 +02:00
if (name === 'activeContextChanged') {
return this.refreshNotShown(data);
} else {
return super.handleEventInChildren(name, data);
}
}
2021-05-20 23:13:34 +02:00
2021-05-22 17:58:46 +02:00
refreshNotShown(data) {
const promises = [];
2021-05-20 23:13:34 +02:00
2021-05-22 17:58:46 +02:00
for (const subContext of data.noteContext.getMainContext().getSubContexts()) {
const widget = this.widgets[subContext.ntxId];
2021-05-21 22:34:40 +02:00
2021-05-22 17:58:46 +02:00
if (!widget.hasBeenAlreadyShown) {
widget.hasBeenAlreadyShown = true;
2021-05-21 22:34:40 +02:00
2021-05-22 17:58:46 +02:00
promises.push(widget.handleEvent('activeContextChanged', {noteContext: subContext}));
2021-05-20 23:13:34 +02:00
}
}
2021-05-22 17:58:46 +02:00
2021-05-22 22:55:24 +02:00
this.refresh();
2021-05-22 17:58:46 +02:00
return Promise.all(promises);
2021-05-19 22:45:34 +02:00
}
}