146 lines
4.1 KiB
JavaScript
Raw Normal View History

2019-05-01 23:06:18 +02:00
import protectedSessionHolder from "./protected_session_holder.js";
import server from "./server.js";
import bundleService from "./bundle.js";
import utils from "./utils.js";
import optionsService from "./options.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";
import treeCache from "./tree_cache.js";
2019-05-01 23:06:18 +02:00
let showSidebarInNewTab = true;
optionsService.addLoadListener(options => {
showSidebarInNewTab = options.is('showSidebarInNewTab');
});
2020-01-15 21:36:01 +01:00
class TabContext extends Component {
2019-05-11 19:44:58 +02:00
/**
2020-01-15 21:36:01 +01:00
* @param {AppContext} appContext
2020-01-12 19:05:09 +01:00
* @param {TabRowWidget} tabRow
* @param {object} state
2019-05-11 19:44:58 +02:00
*/
2020-01-15 21:36:01 +01:00
constructor(appContext, tabRow, state = {}) {
super(appContext);
2019-05-11 19:44:58 +02:00
this.tabRow = tabRow;
this.tabId = state.tabId || utils.randomString(4);
this.state = state;
2019-09-04 22:13:22 +02:00
2020-01-20 20:51:22 +01:00
this.trigger('tabOpened', {tabId: this.tabId});
2019-05-02 22:24:43 +02:00
}
2020-01-24 17:54:47 +01:00
async setNote(inputNotePath) {
const notePath = await treeService.resolveNotePath(inputNotePath);
if (!notePath) {
console.error(`Cannot resolve note path ${inputNotePath}`);
return;
}
if (notePath === this.notePath) {
return;
}
await this.trigger('beforeNoteSwitch', {tabId: this.tabId}, true);
this.notePath = notePath;
this.noteId = treeService.getNoteIdFromNotePath(notePath);
2020-02-02 11:44:08 +01:00
this.autoBookDisabled = false;
2020-01-18 18:01:16 +01:00
//this.cleanup(); // esp. on windows autocomplete is not getting closed automatically
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
if (notePath && notePath === this.notePath) {
await server.post('recent-notes', {
noteId: this.note.noteId,
notePath: this.notePath
});
2019-05-14 22:29:47 +02:00
}
}, 5000);
2020-01-24 20:15:53 +01:00
// should be done somewhere else ...
2019-09-04 22:13:22 +02:00
bundleService.executeRelationBundles(this.note, 'runOnNoteView', this);
2020-01-19 11:03:34 +01:00
if (this.note.isProtected && protectedSessionHolder.isProtectedSessionAvailable()) {
// FIXME: there are probably more places where this should be done
protectedSessionHolder.touchProtectedSession();
}
2020-02-02 18:46:50 +01:00
this.trigger('tabNoteSwitched', {
tabId: this.tabId,
notePath: this.notePath
});
2020-01-24 17:54:47 +01:00
this.trigger('openTabsChanged');
}
/** @property {NoteShort} */
get note() {
return treeCache.notes[this.noteId];
2020-02-01 11:33:31 +01:00
}
/** @return {NoteComplement} */
async getNoteComplement() {
if (!this.noteId) {
return null;
}
return await treeCache.getNoteComplement(this.noteId);
2020-02-01 11:33:31 +01:00
}
2020-01-19 21:12:53 +01:00
async remove() {
await this.trigger('beforeTabRemove', {tabId: this.tabId}, true);
this.trigger('tabRemoved', {tabId: this.tabId});
}
2020-01-21 21:43:23 +01:00
isActive() {
return this.appContext.activeTabId === this.tabId;
}
2019-08-15 10:04:03 +02:00
getTabState() {
if (!this.notePath) {
return null;
}
return {
tabId: this.tabId,
notePath: this.notePath,
2020-01-15 22:27:52 +01:00
active: this.tabRow.activeTabId === this.tabId
2019-08-15 10:04:03 +02:00
}
}
stateChanged() {
2020-01-24 17:54:47 +01:00
appContext.openTabsChangedListener();
}
noteDeletedListener({noteId}) {
2020-02-02 18:46:50 +01:00
if (this.noteId === noteId) {
this.noteId = null;
2020-01-24 17:54:47 +01:00
this.notePath = null;
2020-02-02 18:46:50 +01:00
this.trigger('tabNoteSwitched', {
tabId: this.tabId,
notePath: this.notePath
});
2020-01-24 17:54:47 +01:00
}
2019-08-15 10:04:03 +02:00
}
2020-01-28 21:54:28 +01:00
// FIXME
async _setTitleBar() {
document.title = "Trilium Notes";
const activeTabContext = this.getActiveTabContext();
if (activeTabContext && activeTabContext.notePath) {
const note = await treeCache.getNote(treeService.getNoteIdFromNotePath(activeTabContext.notePath));
// it helps navigating in history if note title is included in the title
document.title += " - " + note.title;
}
}
2019-05-01 22:19:29 +02:00
}
2019-05-08 19:55:24 +02:00
export default TabContext;