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

212 lines
6.9 KiB
JavaScript
Raw Normal View History

2021-05-19 22:45:34 +02:00
import FlexContainer from "./flex_container.js";
2022-12-01 13:07:23 +01:00
import appContext from "../../components/app_context.js";
2021-05-19 22:45:34 +02:00
export default class SplitNoteContainer extends FlexContainer {
2021-05-19 22:45:34 +02:00
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-06-13 22:55:31 +02:00
this.class('split-note-container-widget');
2021-05-19 22:45:34 +02:00
this.css('flex-grow', '1');
2021-06-13 22:55:31 +02:00
this.collapsible();
2021-05-19 22:45:34 +02:00
}
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-24 21:05:44 +02:00
$renderedWidget.attr("data-ntx-id", noteContext.ntxId);
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
2023-04-03 23:47:24 +02:00
async openNewNoteSplitEvent({ntxId, notePath, hoistedNoteId, viewScope}) {
2022-12-08 23:29:08 +01:00
const mainNtxId = appContext.tabManager.getActiveMainContext().ntxId;
if (!ntxId) {
logError("empty ntxId!");
2022-12-08 23:29:08 +01:00
ntxId = mainNtxId;
}
hoistedNoteId = hoistedNoteId || appContext.tabManager.getActiveContext().hoistedNoteId;
2022-12-08 23:29:08 +01:00
const noteContext = await appContext.tabManager.openEmptyTab(null, hoistedNoteId, mainNtxId);
2021-05-19 22:45:34 +02:00
2021-05-24 21:43:24 +02:00
// remove the original position of newly created note context
2021-05-24 21:05:44 +02:00
const ntxIds = appContext.tabManager.children.map(c => c.ntxId)
.filter(id => id !== noteContext.ntxId);
2021-05-24 21:43:24 +02:00
// insert the note context after the originating note context
2021-05-24 21:05:44 +02:00
ntxIds.splice(ntxIds.indexOf(ntxId) + 1, 0, noteContext.ntxId);
this.triggerCommand("noteContextReorder", {ntxIdsInOrder: ntxIds});
2021-05-24 21:05:44 +02:00
2021-05-24 21:43:24 +02:00
// move the note context rendered widget after the originating widget
2021-05-24 21:05:44 +02:00
this.$widget.find(`[data-ntx-id="${noteContext.ntxId}"]`)
2021-05-24 21:43:24 +02:00
.insertAfter(this.$widget.find(`[data-ntx-id="${ntxId}"]`));
2021-05-24 21:05:44 +02:00
2021-05-22 12:35:41 +02:00
await appContext.tabManager.activateNoteContext(noteContext.ntxId);
2021-05-20 23:13:34 +02:00
if (notePath) {
2023-04-03 23:47:24 +02:00
await noteContext.setNote(notePath, viewScope);
}
else {
await noteContext.setEmpty();
}
2021-05-20 23:13:34 +02:00
}
2021-05-19 22:45:34 +02:00
closeThisNoteSplitCommand({ntxId}) {
2021-05-24 22:29:49 +02:00
appContext.tabManager.removeNoteContext(ntxId);
}
2023-05-30 02:24:56 +08:00
async moveThisNoteSplitCommand({ntxId, isMovingLeft}) {
if (!ntxId) {
logError("empty ntxId!");
return;
}
const contexts = appContext.tabManager.noteContexts;
const currentIndex = contexts.findIndex(c => c.ntxId === ntxId);
2023-05-31 01:53:55 +08:00
const leftIndex = isMovingLeft ? currentIndex - 1 : currentIndex;
2023-05-30 02:24:56 +08:00
2023-05-31 01:53:55 +08:00
if (currentIndex === -1 || leftIndex < 0 || leftIndex + 1 >= contexts.length) {
2023-06-01 00:48:37 +08:00
logError(`invalid context! currentIndex: ${currentIndex}, leftIndex: ${leftIndex}, contexts.length: ${contexts.length}`);
2023-05-30 02:24:56 +08:00
return;
}
2023-06-01 00:48:37 +08:00
if (contexts[leftIndex].isEmpty() && contexts[leftIndex + 1].isEmpty()) {
2023-05-30 02:24:56 +08:00
// no op
return;
2023-06-01 00:48:37 +08:00
}
2023-05-30 02:24:56 +08:00
2023-05-31 01:53:55 +08:00
const ntxIds = contexts.map(c => c.ntxId);
2023-06-01 00:48:37 +08:00
const newNtxIds = [
...ntxIds.slice(0, leftIndex),
ntxIds[leftIndex + 1],
ntxIds[leftIndex],
...ntxIds.slice(leftIndex + 2),
];
const isChangingMainContext = !contexts[leftIndex].mainNtxId;
2023-05-30 02:24:56 +08:00
2023-05-31 01:53:55 +08:00
this.triggerCommand("noteContextReorder", {
2023-06-01 00:48:37 +08:00
ntxIdsInOrder: newNtxIds,
oldMainNtxId: isChangingMainContext ? ntxIds[leftIndex] : null,
newMainNtxId: isChangingMainContext ? ntxIds[leftIndex + 1]: null,
2023-05-31 01:53:55 +08:00
});
2023-06-01 00:48:37 +08:00
// reorder the note context widgets
2023-05-31 01:53:55 +08:00
this.$widget.find(`[data-ntx-id="${ntxIds[leftIndex]}"]`)
.insertAfter(this.$widget.find(`[data-ntx-id="${ntxIds[leftIndex + 1]}"]`));
2023-05-30 02:24:56 +08:00
2023-05-31 01:53:55 +08:00
// activate context that now contains the original note
await appContext.tabManager.activateNoteContext(isMovingLeft ? ntxIds[leftIndex + 1] : ntxIds[leftIndex]);
2023-05-30 02:24:56 +08:00
}
2021-05-22 17:58:46 +02:00
activeContextChangedEvent() {
this.refresh();
}
noteSwitchedAndActivatedEvent() {
this.refresh();
}
2021-05-24 22:29:49 +02:00
noteContextRemovedEvent({ntxIds}) {
this.children = this.children.filter(c => !ntxIds.includes(c.ntxId));
for (const ntxId of ntxIds) {
this.$widget.find(`[data-ntx-id="${ntxId}"]`).remove();
delete this.widgets[ntxId];
}
}
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-24 21:43:24 +02:00
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
2023-06-30 11:18:34 +02:00
* activation, further note switches are always propagated to the tabs.
2021-05-20 23:13:34 +02:00
*/
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
}
}