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

106 lines
3.2 KiB
JavaScript
Raw Normal View History

2021-05-22 12:35:41 +02:00
import NoteContextAwareWidget from "./note_context_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
2021-05-22 12:35:41 +02:00
export default class NoteContextCachingWidget extends NoteContextAwareWidget {
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
}
2021-05-22 12:35:41 +02:00
async newNoteContextCreatedEvent({noteContext}) {
2021-05-22 12:26:45 +02:00
const {ntxId} = noteContext;
2020-03-06 22:17:07 +01:00
2021-05-22 12:26:45 +02:00
if (this.widgets[ntxId]) {
2020-02-29 16:26:46 +01:00
return;
}
2021-05-22 12:26:45 +02:00
this.widgets[ntxId] = this.widgetFactory();
2020-02-29 16:26:46 +01:00
2021-05-22 12:26:45 +02:00
const $renderedWidget = this.widgets[ntxId].render();
this.widgets[ntxId].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);
2021-05-22 12:26:45 +02:00
await this.widgets[ntxId].handleEvent('setNoteContext', {noteContext});
2020-02-29 16:26:46 +01:00
2021-05-22 12:26:45 +02:00
this.child(this.widgets[ntxId]); // add as child only once it is ready (rendered with noteContext)
2020-02-29 16:26:46 +01:00
}
2021-05-22 12:35:41 +02:00
noteContextRemovedEvent({ntxIds}) {
2021-05-22 12:26:45 +02:00
for (const ntxId of ntxIds) {
const widget = this.widgets[ntxId];
2020-01-19 21:12:53 +01:00
2021-05-20 23:13:34 +02:00
if (widget) {
widget.remove();
2021-05-22 12:26:45 +02:00
delete this.widgets[ntxId];
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) {
2021-05-22 12:26:45 +02:00
for (const ntxId in this.widgets) {
this.widgets[ntxId].toggleExt(show && this.isTab(ntxId));
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) {
2021-05-22 12:35:41 +02:00
if (['noteSwitched', 'noteSwitchedAndActivated'].includes(name)) {
2020-03-15 11:08:16 +01:00
// this event is propagated only to the widgets of a particular tab
2021-05-22 12:26:45 +02:00
let widget = this.widgets[data.noteContext.ntxId];
2021-05-19 22:45:34 +02:00
if (!widget) {
2021-05-22 12:26:45 +02:00
widget = this.widgets[data.noteContext.mainNtxId];
2021-05-19 22:45:34 +02:00
}
2020-03-15 11:08:16 +01:00
2021-05-22 12:35:41 +02:00
if (widget && (widget.hasBeenAlreadyShown || name === 'noteSwitchedAndActivated')) {
2020-04-23 23:08:15 +02:00
widget.hasBeenAlreadyShown = true;
2021-05-22 12:35:41 +02:00
return widget.handleEvent('noteSwitched', data);
2020-03-15 11:08:16 +01:00
}
else {
return Promise.resolve();
}
}
if (name === 'activeTabChanged') {
2021-05-22 12:26:45 +02:00
let widget = this.widgets[data.noteContext.ntxId];
2021-05-19 22:45:34 +02:00
if (!widget) {
2021-05-22 12:26:45 +02:00
widget = this.widgets[data.noteContext.mainNtxId];
2021-05-19 22:45:34 +02:00
}
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
}