2019-05-01 23:06:18 +02:00
|
|
|
import protectedSessionHolder from "./protected_session_holder.js";
|
|
|
|
import server from "./server.js";
|
|
|
|
import utils from "./utils.js";
|
2020-01-12 12:30:30 +01:00
|
|
|
import appContext from "./app_context.js";
|
2020-01-25 09:56:08 +01:00
|
|
|
import treeService from "./tree.js";
|
2020-01-15 21:36:01 +01:00
|
|
|
import Component from "../widgets/component.js";
|
2020-01-25 14:37:12 +01:00
|
|
|
import treeCache from "./tree_cache.js";
|
2020-02-02 22:04:28 +01:00
|
|
|
import hoistedNoteService from "./hoisted_note.js";
|
2019-05-01 23:06:18 +02:00
|
|
|
|
2020-01-15 21:36:01 +01:00
|
|
|
class TabContext extends Component {
|
2019-05-11 19:44:58 +02:00
|
|
|
/**
|
2020-02-09 21:13:05 +01:00
|
|
|
* @param {string|null} tabId
|
2019-05-11 19:44:58 +02:00
|
|
|
*/
|
2020-11-22 23:05:02 +01:00
|
|
|
constructor(tabId = null, hoistedNoteId = 'root') {
|
2020-02-27 10:03:14 +01:00
|
|
|
super();
|
2020-01-15 21:36:01 +01:00
|
|
|
|
2020-02-09 21:13:05 +01:00
|
|
|
this.tabId = tabId || utils.randomString(4);
|
2020-11-22 23:05:02 +01:00
|
|
|
this.hoistedNoteId = hoistedNoteId;
|
2019-05-02 22:24:43 +02:00
|
|
|
}
|
|
|
|
|
2020-02-28 11:46:35 +01:00
|
|
|
setEmpty() {
|
|
|
|
this.triggerEvent('tabNoteSwitched', {
|
2020-03-07 13:40:46 +01:00
|
|
|
tabContext: this,
|
2020-02-28 11:46:35 +01:00
|
|
|
notePath: this.notePath
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-27 12:26:42 +01:00
|
|
|
async setNote(inputNotePath, triggerSwitchEvent = true) {
|
2020-03-23 16:39:03 +01:00
|
|
|
const noteId = treeService.getNoteIdFromNotePath(inputNotePath);
|
2020-08-24 23:33:27 +02:00
|
|
|
let resolvedNotePath;
|
2020-01-24 17:54:47 +01:00
|
|
|
|
2020-03-23 16:39:03 +01:00
|
|
|
if ((await treeCache.getNote(noteId)).isDeleted) {
|
|
|
|
// no point in trying to resolve canonical notePath
|
2020-08-24 23:33:27 +02:00
|
|
|
resolvedNotePath = inputNotePath;
|
2020-01-24 17:54:47 +01:00
|
|
|
}
|
2020-03-23 16:39:03 +01:00
|
|
|
else {
|
2020-08-24 23:33:27 +02:00
|
|
|
resolvedNotePath = await treeService.resolveNotePath(inputNotePath);
|
2020-01-24 17:54:47 +01:00
|
|
|
|
2020-08-24 23:33:27 +02:00
|
|
|
if (!resolvedNotePath) {
|
2020-10-12 21:05:34 +02:00
|
|
|
logError(`Cannot resolve note path ${inputNotePath}`);
|
2020-03-23 16:39:03 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-01-24 17:54:47 +01:00
|
|
|
|
2020-08-24 23:33:27 +02:00
|
|
|
if (resolvedNotePath === this.notePath) {
|
2020-03-23 16:39:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-24 23:33:27 +02:00
|
|
|
if (await hoistedNoteService.checkNoteAccess(resolvedNotePath) === false) {
|
2020-03-23 16:39:03 +01:00
|
|
|
return; // note is outside of hoisted subtree and user chose not to unhoist
|
|
|
|
}
|
2020-10-19 22:10:25 +02:00
|
|
|
|
|
|
|
// if user choise to unhoist, cache was reloaded, but might not contain this note (since it's on unexpanded path)
|
|
|
|
await treeCache.getNote(noteId);
|
2020-02-02 22:04:28 +01:00
|
|
|
}
|
|
|
|
|
2020-03-18 10:08:16 +01:00
|
|
|
await this.triggerEvent('beforeNoteSwitch', {tabContext: this});
|
2020-01-19 20:18:02 +01:00
|
|
|
|
2020-02-03 21:56:45 +01:00
|
|
|
utils.closeActiveDialog();
|
|
|
|
|
2020-08-24 23:33:27 +02:00
|
|
|
this.notePath = resolvedNotePath;
|
2020-03-23 16:39:03 +01:00
|
|
|
this.noteId = noteId;
|
2020-01-25 14:37:12 +01:00
|
|
|
|
2020-02-02 11:44:08 +01:00
|
|
|
this.autoBookDisabled = false;
|
2020-04-06 22:08:54 +02:00
|
|
|
this.textPreviewDisabled = false;
|
2020-04-26 14:26:57 +02:00
|
|
|
this.codePreviewDisabled = false;
|
2020-02-02 11:44:08 +01:00
|
|
|
|
2019-05-14 22:29:47 +02:00
|
|
|
setTimeout(async () => {
|
|
|
|
// we include the note into recent list only if the user stayed on the note at least 5 seconds
|
2020-08-24 23:33:27 +02:00
|
|
|
if (resolvedNotePath && resolvedNotePath === this.notePath) {
|
2019-05-21 21:47:28 +02:00
|
|
|
await server.post('recent-notes', {
|
2019-09-04 22:45:12 +02:00
|
|
|
noteId: this.note.noteId,
|
2019-05-21 21:47:28 +02:00
|
|
|
notePath: this.notePath
|
|
|
|
});
|
2019-05-14 22:29:47 +02:00
|
|
|
}
|
|
|
|
}, 5000);
|
|
|
|
|
2020-04-25 11:09:07 +02:00
|
|
|
protectedSessionHolder.touchProtectedSessionIfNecessary(this.note);
|
2020-01-19 11:03:34 +01:00
|
|
|
|
2020-02-27 12:26:42 +01:00
|
|
|
if (triggerSwitchEvent) {
|
2020-06-04 21:44:34 +02:00
|
|
|
await this.triggerEvent('tabNoteSwitched', {
|
2020-03-07 13:40:46 +01:00
|
|
|
tabContext: this,
|
2020-02-27 12:26:42 +01:00
|
|
|
notePath: this.notePath
|
|
|
|
});
|
|
|
|
}
|
2020-10-19 22:10:25 +02:00
|
|
|
|
2020-10-19 23:58:13 +02:00
|
|
|
if (utils.isDesktop()) {
|
|
|
|
// close dangling autocompletes after closing the tab
|
|
|
|
$(".aa-input").autocomplete("close");
|
|
|
|
}
|
2019-05-13 22:08:06 +02:00
|
|
|
}
|
|
|
|
|
2020-02-01 22:29:32 +01:00
|
|
|
/** @property {NoteShort} */
|
|
|
|
get note() {
|
2020-10-19 22:10:25 +02:00
|
|
|
if (this.noteId && !(this.noteId in treeCache.notes)) {
|
|
|
|
logError(`Cannot find tabContext's note id='${this.noteId}'`);
|
|
|
|
}
|
|
|
|
|
2020-02-01 22:29:32 +01:00
|
|
|
return treeCache.notes[this.noteId];
|
2020-02-01 11:33:31 +01:00
|
|
|
}
|
|
|
|
|
2020-08-17 20:58:34 +02:00
|
|
|
/** @property {string[]} */
|
|
|
|
get notePathArray() {
|
|
|
|
return this.notePath ? this.notePath.split('/') : [];
|
|
|
|
}
|
|
|
|
|
2020-02-01 11:33:31 +01:00
|
|
|
/** @return {NoteComplement} */
|
|
|
|
async getNoteComplement() {
|
|
|
|
if (!this.noteId) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-02-01 18:29:18 +01:00
|
|
|
return await treeCache.getNoteComplement(this.noteId);
|
2020-02-01 11:33:31 +01:00
|
|
|
}
|
|
|
|
|
2020-01-21 21:43:23 +01:00
|
|
|
isActive() {
|
2020-02-16 18:11:32 +01:00
|
|
|
return appContext.tabManager.activeTabId === this.tabId;
|
2020-01-21 21:43:23 +01:00
|
|
|
}
|
|
|
|
|
2019-08-15 10:04:03 +02:00
|
|
|
getTabState() {
|
|
|
|
if (!this.notePath) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
tabId: this.tabId,
|
|
|
|
notePath: this.notePath,
|
2020-11-22 23:05:02 +01:00
|
|
|
hoistedNoteId: this.hoistedNoteId,
|
2020-02-08 21:23:42 +01:00
|
|
|
active: this.isActive()
|
2019-08-15 10:04:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-22 23:05:02 +01:00
|
|
|
async unhoist() {
|
|
|
|
await this.setHoistedNoteId('root');
|
|
|
|
}
|
|
|
|
|
|
|
|
async setHoistedNoteId(noteIdToHoist) {
|
|
|
|
this.hoistedNoteId = noteIdToHoist;
|
|
|
|
|
|
|
|
await this.triggerEvent('hoistedNoteChanged', {
|
|
|
|
noteId: noteIdToHoist,
|
|
|
|
tabId: this.tabId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-16 19:23:49 +01:00
|
|
|
async entitiesReloadedEvent({loadResults}) {
|
2020-02-09 22:31:52 +01:00
|
|
|
if (loadResults.isNoteReloaded(this.noteId)) {
|
|
|
|
const note = await treeCache.getNote(this.noteId);
|
|
|
|
|
|
|
|
if (note.isDeleted) {
|
|
|
|
this.noteId = null;
|
|
|
|
this.notePath = null;
|
|
|
|
|
2020-02-16 19:21:17 +01:00
|
|
|
this.triggerEvent('tabNoteSwitched', {
|
2020-03-07 13:40:46 +01:00
|
|
|
tabContext: this,
|
2020-02-09 22:31:52 +01:00
|
|
|
notePath: this.notePath
|
|
|
|
});
|
|
|
|
}
|
2020-01-24 17:54:47 +01:00
|
|
|
}
|
2019-08-15 10:04:03 +02:00
|
|
|
}
|
2019-05-01 22:19:29 +02:00
|
|
|
}
|
|
|
|
|
2020-06-04 21:44:34 +02:00
|
|
|
export default TabContext;
|