| 
									
										
										
										
											2020-01-11 21:19:56 +01:00
										 |  |  | import GlobalButtonsWidget from "../widgets/global_buttons.js"; | 
					
						
							|  |  |  | import SearchBoxWidget from "../widgets/search_box.js"; | 
					
						
							|  |  |  | import SearchResultsWidget from "../widgets/search_results.js"; | 
					
						
							|  |  |  | import NoteTreeWidget from "../widgets/note_tree.js"; | 
					
						
							| 
									
										
										
										
											2020-01-12 12:30:30 +01:00
										 |  |  | import treeService from "./tree.js"; | 
					
						
							|  |  |  | import noteDetailService from "./note_detail.js"; | 
					
						
							|  |  |  | import TabContext from "./tab_context.js"; | 
					
						
							| 
									
										
										
										
											2020-01-12 12:48:17 +01:00
										 |  |  | import server from "./server.js"; | 
					
						
							| 
									
										
										
										
											2020-01-14 21:52:18 +01:00
										 |  |  | import TabRowWidget from "../widgets/tab_row.js"; | 
					
						
							| 
									
										
										
										
											2020-01-12 23:03:55 +01:00
										 |  |  | import NoteTitleWidget from "../widgets/note_title.js"; | 
					
						
							| 
									
										
										
										
											2020-01-13 20:25:56 +01:00
										 |  |  | import PromotedAttributesWidget from "../widgets/promoted_attributes.js"; | 
					
						
							| 
									
										
										
										
											2020-01-13 21:48:44 +01:00
										 |  |  | import NoteDetailWidget from "../widgets/note_detail.js"; | 
					
						
							| 
									
										
										
										
											2020-01-14 20:27:40 +01:00
										 |  |  | import TabCachingWidget from "../widgets/tab_caching_widget.js"; | 
					
						
							| 
									
										
										
										
											2020-01-14 21:23:32 +01:00
										 |  |  | import NoteInfoWidget from "../widgets/note_info.js"; | 
					
						
							|  |  |  | import NoteRevisionsWidget from "../widgets/note_revisions.js"; | 
					
						
							|  |  |  | import LinkMapWidget from "../widgets/link_map.js"; | 
					
						
							|  |  |  | import SimilarNotesWidget from "../widgets/similar_notes.js"; | 
					
						
							|  |  |  | import WhatLinksHereWidget from "../widgets/what_links_here.js"; | 
					
						
							|  |  |  | import AttributesWidget from "../widgets/attributes.js"; | 
					
						
							| 
									
										
										
										
											2020-01-15 19:40:17 +01:00
										 |  |  | import TitleBarButtonsWidget from "../widgets/title_bar_buttons.js"; | 
					
						
							|  |  |  | import GlobalMenuWidget from "../widgets/global_menu.js"; | 
					
						
							| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  | import RowFlexContainer from "../widgets/row_flex_container.js"; | 
					
						
							| 
									
										
										
										
											2020-01-15 20:10:54 +01:00
										 |  |  | import StandardTopWidget from "../widgets/standard_top_widget.js"; | 
					
						
							| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  | import treeCache from "./tree_cache.js"; | 
					
						
							|  |  |  | import treeUtils from "./tree_utils.js"; | 
					
						
							| 
									
										
										
										
											2020-01-19 15:36:42 +01:00
										 |  |  | import NotePathsWidget from "../widgets/note_paths.js"; | 
					
						
							|  |  |  | import RunScriptButtonsWidget from "../widgets/run_script_buttons.js"; | 
					
						
							|  |  |  | import ProtectedNoteSwitchWidget from "../widgets/protected_note_switch.js"; | 
					
						
							|  |  |  | import NoteTypeWidget from "../widgets/note_type.js"; | 
					
						
							|  |  |  | import NoteActionsWidget from "../widgets/note_actions.js"; | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  | import protectedSessionHolder from "./protected_session_holder.js"; | 
					
						
							| 
									
										
										
										
											2020-01-19 21:12:53 +01:00
										 |  |  | import bundleService from "./bundle.js"; | 
					
						
							| 
									
										
										
										
											2020-01-20 22:35:52 +01:00
										 |  |  | import DialogEventComponent from "./dialog_events.js"; | 
					
						
							| 
									
										
										
										
											2020-01-21 22:54:16 +01:00
										 |  |  | import Entrypoints from "./entrypoints.js"; | 
					
						
							| 
									
										
										
										
											2020-01-11 21:19:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 09:57:28 +01:00
										 |  |  | class AppContext { | 
					
						
							| 
									
										
										
										
											2020-01-11 21:19:56 +01:00
										 |  |  |     constructor() { | 
					
						
							| 
									
										
										
										
											2020-01-20 22:35:52 +01:00
										 |  |  |         this.components = []; | 
					
						
							| 
									
										
										
										
											2020-01-12 12:30:30 +01:00
										 |  |  |         /** @type {TabContext[]} */ | 
					
						
							|  |  |  |         this.tabContexts = []; | 
					
						
							| 
									
										
										
										
											2020-01-12 12:48:17 +01:00
										 |  |  |         this.tabsChangedTaskId = null; | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  |         /** @type {TabRowWidget} */ | 
					
						
							|  |  |  |         this.tabRow = null; | 
					
						
							| 
									
										
										
										
											2020-01-19 21:12:53 +01:00
										 |  |  |         this.activeTabId = null; | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     showWidgets() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.tabRow = new TabRowWidget(this); | 
					
						
							| 
									
										
										
										
											2020-01-12 20:15:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-15 20:10:54 +01:00
										 |  |  |         const topPaneWidgets = [ | 
					
						
							| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  |             new RowFlexContainer(this, [ | 
					
						
							| 
									
										
										
										
											2020-01-15 20:10:54 +01:00
										 |  |  |                 new GlobalMenuWidget(this), | 
					
						
							|  |  |  |                 this.tabRow, | 
					
						
							|  |  |  |                 new TitleBarButtonsWidget(this) | 
					
						
							|  |  |  |             ]), | 
					
						
							|  |  |  |             new StandardTopWidget(this) | 
					
						
							| 
									
										
										
										
											2020-01-15 19:40:17 +01:00
										 |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-15 20:10:54 +01:00
										 |  |  |         const $topPane = $("#top-pane"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (const widget of topPaneWidgets) { | 
					
						
							| 
									
										
										
										
											2020-01-19 15:36:42 +01:00
										 |  |  |             $topPane.append(widget.render()); | 
					
						
							| 
									
										
										
										
											2020-01-15 19:40:17 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const $leftPane = $("#left-pane"); | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         this.noteTreeWidget = new NoteTreeWidget(this); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-13 21:48:44 +01:00
										 |  |  |         const leftPaneWidgets = [ | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  |             new GlobalButtonsWidget(this), | 
					
						
							|  |  |  |             new SearchBoxWidget(this), | 
					
						
							|  |  |  |             new SearchResultsWidget(this), | 
					
						
							|  |  |  |             this.noteTreeWidget | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-13 21:48:44 +01:00
										 |  |  |         for (const widget of leftPaneWidgets) { | 
					
						
							| 
									
										
										
										
											2020-01-19 15:36:42 +01:00
										 |  |  |             $leftPane.append(widget.render()); | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-12 23:03:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-13 21:48:44 +01:00
										 |  |  |         const $centerPane = $("#center-pane"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const centerPaneWidgets = [ | 
					
						
							| 
									
										
										
										
											2020-01-19 15:36:42 +01:00
										 |  |  |             new RowFlexContainer(this, [ | 
					
						
							|  |  |  |                 new TabCachingWidget(this, () => new NotePathsWidget(this)), | 
					
						
							|  |  |  |                 new NoteTitleWidget(this), | 
					
						
							|  |  |  |                 new RunScriptButtonsWidget(this), | 
					
						
							|  |  |  |                 new ProtectedNoteSwitchWidget(this), | 
					
						
							|  |  |  |                 new NoteTypeWidget(this), | 
					
						
							|  |  |  |                 new NoteActionsWidget(this) | 
					
						
							|  |  |  |             ]), | 
					
						
							| 
									
										
										
										
											2020-01-14 20:27:40 +01:00
										 |  |  |             new TabCachingWidget(this, () => new PromotedAttributesWidget(this)), | 
					
						
							|  |  |  |             new TabCachingWidget(this, () => new NoteDetailWidget(this)) | 
					
						
							| 
									
										
										
										
											2020-01-13 21:48:44 +01:00
										 |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (const widget of centerPaneWidgets) { | 
					
						
							| 
									
										
										
										
											2020-01-19 15:36:42 +01:00
										 |  |  |             $centerPane.append(widget.render()); | 
					
						
							| 
									
										
										
										
											2020-01-13 21:48:44 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-14 21:23:32 +01:00
										 |  |  |         const $rightPane = $("#right-pane"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const rightPaneWidgets = [ | 
					
						
							|  |  |  |             new NoteInfoWidget(this), | 
					
						
							|  |  |  |             new TabCachingWidget(this, () => new AttributesWidget(this)), | 
					
						
							|  |  |  |             new TabCachingWidget(this, () => new LinkMapWidget(this)), | 
					
						
							|  |  |  |             new TabCachingWidget(this, () => new NoteRevisionsWidget(this)), | 
					
						
							|  |  |  |             new TabCachingWidget(this, () => new SimilarNotesWidget(this)), | 
					
						
							| 
									
										
										
										
											2020-01-24 22:30:17 +01:00
										 |  |  |             new TabCachingWidget(this, () => new WhatLinksHereWidget(this)) | 
					
						
							| 
									
										
										
										
											2020-01-14 21:23:32 +01:00
										 |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (const widget of rightPaneWidgets) { | 
					
						
							| 
									
										
										
										
											2020-01-19 15:36:42 +01:00
										 |  |  |             $rightPane.append(widget.render()); | 
					
						
							| 
									
										
										
										
											2020-01-14 21:23:32 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-20 22:35:52 +01:00
										 |  |  |         this.components = [ | 
					
						
							| 
									
										
										
										
											2020-01-21 22:54:16 +01:00
										 |  |  |             new Entrypoints(), | 
					
						
							| 
									
										
										
										
											2020-01-13 21:48:44 +01:00
										 |  |  |             this.tabRow, | 
					
						
							| 
									
										
										
										
											2020-01-20 22:35:52 +01:00
										 |  |  |             new DialogEventComponent(this), | 
					
						
							| 
									
										
										
										
											2020-01-13 21:48:44 +01:00
										 |  |  |             ...leftPaneWidgets, | 
					
						
							| 
									
										
										
										
											2020-01-14 21:23:32 +01:00
										 |  |  |             ...centerPaneWidgets, | 
					
						
							|  |  |  |             ...rightPaneWidgets | 
					
						
							| 
									
										
										
										
											2020-01-13 21:48:44 +01:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2020-01-11 21:19:56 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  |     trigger(name, data, sync = false) { | 
					
						
							| 
									
										
										
										
											2020-01-12 23:03:55 +01:00
										 |  |  |         this.eventReceived(name, data); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-21 22:54:16 +01:00
										 |  |  |         for (const component of this.components) { | 
					
						
							|  |  |  |             component.eventReceived(name, data, sync); | 
					
						
							| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-11 21:19:56 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  |     async eventReceived(name, data, sync) { | 
					
						
							| 
									
										
										
										
											2020-01-12 23:03:55 +01:00
										 |  |  |         const fun = this[name + 'Listener']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (typeof fun === 'function') { | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  |             await fun.call(this, data, sync); | 
					
						
							| 
									
										
										
										
											2020-01-12 23:03:55 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-24 21:15:40 +01:00
										 |  |  |     tabNoteSwitchedListener({tabId}) { | 
					
						
							|  |  |  |         if (tabId === this.activeTabId) { | 
					
						
							|  |  |  |             this._setTitleBar(); | 
					
						
							|  |  |  |             this._setCurrentNotePathToHash(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     _setCurrentNotePathToHash() { | 
					
						
							|  |  |  |         const activeTabContext = this.getActiveTabContext(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (activeTabContext && activeTabContext.notePath) { | 
					
						
							|  |  |  |             document.location.hash = (activeTabContext.notePath || "") + "-" + activeTabContext.tabId; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 18:05:06 +01:00
										 |  |  |     noteTitleChangedListener() { | 
					
						
							|  |  |  |         this._setTitleBar(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  |     async _setTitleBar() { | 
					
						
							|  |  |  |         document.title = "Trilium Notes"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const activeTabContext = this.getActiveTabContext(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (activeTabContext && activeTabContext.notePath) { | 
					
						
							|  |  |  |             const note = await treeCache.getNote(treeUtils.getNoteIdFromNotePath(activeTabContext.notePath)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // it helps navigating in history if note title is included in the title
 | 
					
						
							|  |  |  |             document.title += " - " + note.title; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 12:30:30 +01:00
										 |  |  |     /** @return {TabContext[]} */ | 
					
						
							|  |  |  |     getTabContexts() { | 
					
						
							|  |  |  |         return this.tabContexts; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 21:24:14 +01:00
										 |  |  |     /** @returns {TabContext} */ | 
					
						
							|  |  |  |     getTabContextById(tabId) { | 
					
						
							|  |  |  |         return this.tabContexts.find(tc => tc.tabId === tabId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 12:30:30 +01:00
										 |  |  |     /** @returns {TabContext} */ | 
					
						
							|  |  |  |     getActiveTabContext() { | 
					
						
							| 
									
										
										
										
											2020-01-19 21:24:14 +01:00
										 |  |  |         return this.getTabContextById(this.activeTabId); | 
					
						
							| 
									
										
										
										
											2020-01-12 12:30:30 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** @returns {string|null} */ | 
					
						
							|  |  |  |     getActiveTabNotePath() { | 
					
						
							|  |  |  |         const activeContext = this.getActiveTabContext(); | 
					
						
							|  |  |  |         return activeContext ? activeContext.notePath : null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** @return {NoteFull} */ | 
					
						
							|  |  |  |     getActiveTabNote() { | 
					
						
							|  |  |  |         const activeContext = this.getActiveTabContext(); | 
					
						
							|  |  |  |         return activeContext ? activeContext.note : null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** @return {string|null} */ | 
					
						
							|  |  |  |     getActiveTabNoteId() { | 
					
						
							|  |  |  |         const activeNote = this.getActiveTabNote(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return activeNote ? activeNote.noteId : null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** @return {string|null} */ | 
					
						
							|  |  |  |     getActiveTabNoteType() { | 
					
						
							|  |  |  |         const activeNote = this.getActiveTabNote(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return activeNote ? activeNote.type : null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async switchToTab(tabId, notePath) { | 
					
						
							| 
									
										
										
										
											2020-01-24 17:54:47 +01:00
										 |  |  |         const tabContext = this.tabContexts.find(tc => tc.tabId === tabId) | 
					
						
							|  |  |  |                          || this.openEmptyTab(); | 
					
						
							| 
									
										
										
										
											2020-01-12 12:30:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-24 17:54:47 +01:00
										 |  |  |         this.activateTab(tabContext.tabId); | 
					
						
							|  |  |  |         await tabContext.setNote(notePath); | 
					
						
							| 
									
										
										
										
											2020-01-12 12:30:30 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 11:15:23 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return {NoteTreeWidget} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     getMainNoteTree() { | 
					
						
							|  |  |  |         return this.noteTreeWidget; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-12 12:30:30 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     getTab(newTab, state) { | 
					
						
							|  |  |  |         if (!this.getActiveTabContext() || newTab) { | 
					
						
							|  |  |  |             // if it's a new tab explicitly by user then it's in background
 | 
					
						
							| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  |             const ctx = new TabContext(this, this.tabRow, state); | 
					
						
							| 
									
										
										
										
											2020-01-12 12:30:30 +01:00
										 |  |  |             this.tabContexts.push(ctx); | 
					
						
							| 
									
										
										
										
											2020-01-20 22:35:52 +01:00
										 |  |  |             this.components.push(ctx); | 
					
						
							| 
									
										
										
										
											2020-01-12 12:30:30 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return ctx; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             return this.getActiveTabContext(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-20 20:51:22 +01:00
										 |  |  |     async openAndActivateEmptyTab() { | 
					
						
							|  |  |  |         const tabContext = this.openEmptyTab(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await this.activateTab(tabContext.tabId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     openEmptyTab() { | 
					
						
							| 
									
										
										
										
											2020-01-19 21:12:53 +01:00
										 |  |  |         const tabContext = new TabContext(this, this.tabRow); | 
					
						
							|  |  |  |         this.tabContexts.push(tabContext); | 
					
						
							| 
									
										
										
										
											2020-01-20 22:35:52 +01:00
										 |  |  |         this.components.push(tabContext); | 
					
						
							| 
									
										
										
										
											2020-01-20 20:51:22 +01:00
										 |  |  |         return tabContext; | 
					
						
							| 
									
										
										
										
											2020-01-12 12:30:30 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-12 12:48:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-24 17:54:47 +01:00
										 |  |  |     async activateOrOpenNote(noteId) { | 
					
						
							|  |  |  |         for (const tabContext of this.getTabContexts()) { | 
					
						
							|  |  |  |             if (tabContext.note && tabContext.note.noteId === noteId) { | 
					
						
							|  |  |  |                 await tabContext.activate(); | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // if no tab with this note has been found we'll create new tab
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const tabContext = this.openEmptyTab(); | 
					
						
							|  |  |  |         await tabContext.setNote(noteId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 12:48:17 +01:00
										 |  |  |     async filterTabs(noteId) { | 
					
						
							|  |  |  |         for (const tc of this.tabContexts) { | 
					
						
							|  |  |  |             if (tc.notePath && !tc.notePath.split("/").includes(noteId)) { | 
					
						
							| 
									
										
										
										
											2020-01-15 22:27:52 +01:00
										 |  |  |                 this.tabRow.removeTab(tc.tabId); | 
					
						
							| 
									
										
										
										
											2020-01-12 12:48:17 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (this.tabContexts.length === 0) { | 
					
						
							| 
									
										
										
										
											2020-01-20 20:51:22 +01:00
										 |  |  |             this.openAndActivateEmptyTab() | 
					
						
							| 
									
										
										
										
											2020-01-12 12:48:17 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await this.saveOpenTabs(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async saveOpenTabs() { | 
					
						
							|  |  |  |         const openTabs = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 21:12:53 +01:00
										 |  |  |         for (const tabId of this.tabRow.getTabIdsInOrder()) { | 
					
						
							| 
									
										
										
										
											2020-01-12 12:48:17 +01:00
										 |  |  |             const tabContext = appContext.getTabContexts().find(tc => tc.tabId === tabId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (tabContext) { | 
					
						
							|  |  |  |                 const tabState = tabContext.getTabState(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (tabState) { | 
					
						
							|  |  |  |                     openTabs.push(tabState); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await server.put('options', { | 
					
						
							|  |  |  |             openTabs: JSON.stringify(openTabs) | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     clearOpenTabsTask() { | 
					
						
							|  |  |  |         if (this.tabsChangedTaskId) { | 
					
						
							|  |  |  |             clearTimeout(this.tabsChangedTaskId); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-24 17:54:47 +01:00
										 |  |  |     openTabsChangedListener() { | 
					
						
							| 
									
										
										
										
											2020-01-12 12:48:17 +01:00
										 |  |  |         // we don't want to send too many requests with tab changes so we always schedule task to do this in 1 seconds,
 | 
					
						
							|  |  |  |         // but if there's any change in between, we cancel the old one and schedule new one
 | 
					
						
							|  |  |  |         // so effectively we kind of wait until user stopped e.g. quickly switching tabs
 | 
					
						
							|  |  |  |         this.clearOpenTabsTask(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.tabsChangedTaskId = setTimeout(() => this.saveOpenTabs(), 1000); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-12 09:57:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-24 17:54:47 +01:00
										 |  |  |     activateTab(tabId) { | 
					
						
							| 
									
										
										
										
											2020-01-24 20:15:53 +01:00
										 |  |  |         const oldActiveTabId = this.activeTabId; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 21:12:53 +01:00
										 |  |  |         this.activeTabId = tabId; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-24 22:30:17 +01:00
										 |  |  |         this.trigger('activeTabChanged', { oldActiveTabId, newActiveTabId: tabId }); | 
					
						
							| 
									
										
										
										
											2020-01-12 20:15:05 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  |     newTabListener() { | 
					
						
							| 
									
										
										
										
											2020-01-20 20:51:22 +01:00
										 |  |  |         this.openAndActivateEmptyTab(); | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-12 09:57:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 21:12:53 +01:00
										 |  |  |     async removeTab(tabId) { | 
					
						
							|  |  |  |         const tabContextToRemove = this.tabContexts.find(tc => tc.tabId === tabId); | 
					
						
							|  |  |  |         const tabIdsInOrder = this.tabRow.getTabIdsInOrder(); | 
					
						
							| 
									
										
										
										
											2020-01-12 12:30:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 21:12:53 +01:00
										 |  |  |         if (!tabContextToRemove) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-12 12:30:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  |         if (this.tabContexts.length === 0) { | 
					
						
							| 
									
										
										
										
											2020-01-20 20:51:22 +01:00
										 |  |  |             this.openAndActivateEmptyTab(); | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-19 21:12:53 +01:00
										 |  |  |         else { | 
					
						
							|  |  |  |             const oldIdx = tabIdsInOrder.findIndex(tid => tid === tabId); | 
					
						
							| 
									
										
										
										
											2020-01-20 20:51:22 +01:00
										 |  |  |             const newActiveTabId = tabIdsInOrder[oldIdx === tabIdsInOrder.length - 1 ? oldIdx - 1 : oldIdx + 1]; | 
					
						
							| 
									
										
										
										
											2020-01-19 21:12:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (newActiveTabId) { | 
					
						
							|  |  |  |                 this.activateTab(newActiveTabId); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 console.log("Failed to find next tabcontext to activate"); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await tabContextToRemove.remove(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.tabContexts = this.tabContexts.filter(tc => tc.tabId === tabId); | 
					
						
							| 
									
										
										
										
											2020-01-12 12:30:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-24 17:54:47 +01:00
										 |  |  |         this.openTabsChangedListener(); | 
					
						
							| 
									
										
										
										
											2020-01-12 12:30:30 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  |     tabReorderListener() { | 
					
						
							| 
									
										
										
										
											2020-01-24 17:54:47 +01:00
										 |  |  |         this.openTabsChangedListener(); | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     noteChangesSavedListener() { | 
					
						
							|  |  |  |         const activeTabContext = this.getActiveTabContext(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!activeTabContext || !activeTabContext.note) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (activeTabContext.note.isProtected && protectedSessionHolder.isProtectedSessionAvailable()) { | 
					
						
							|  |  |  |             protectedSessionHolder.touchProtectedSession(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-19 21:12:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // run async
 | 
					
						
							|  |  |  |         bundleService.executeRelationBundles(activeTabContext.note, 'runOnNoteChange', activeTabContext); | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-20 20:51:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     activateNextTabListener() { | 
					
						
							|  |  |  |         const tabIdsInOrder = this.tabRow.getTabIdsInOrder(); | 
					
						
							|  |  |  |         const oldIdx = tabIdsInOrder.findIndex(tid => tid === this.activeTabId); | 
					
						
							|  |  |  |         const newActiveTabId = tabIdsInOrder[oldIdx === tabIdsInOrder.length - 1 ? 0 : oldIdx + 1]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.activateTab(newActiveTabId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     activatePreviousTabListener() { | 
					
						
							|  |  |  |         const tabIdsInOrder = this.tabRow.getTabIdsInOrder(); | 
					
						
							|  |  |  |         const oldIdx = tabIdsInOrder.findIndex(tid => tid === this.activeTabId); | 
					
						
							|  |  |  |         const newActiveTabId = tabIdsInOrder[oldIdx === 0 ? tabIdsInOrder.length - 1 : oldIdx - 1]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.activateTab(newActiveTabId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     closeActiveTabListener() { | 
					
						
							|  |  |  |         this.removeTab(this.activeTabId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     openNewTabListener() { | 
					
						
							|  |  |  |         this.openAndActivateEmptyTab(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-20 22:35:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     removeAllTabsListener() { | 
					
						
							|  |  |  |         // TODO
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     removeAllTabsExceptForThis() { | 
					
						
							|  |  |  |         // TODO
 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const appContext = new AppContext(); | 
					
						
							| 
									
										
										
										
											2020-01-12 12:48:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 09:57:28 +01:00
										 |  |  | export default appContext; |