| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  | import treeCache from "./tree_cache.js"; | 
					
						
							| 
									
										
										
										
											2020-01-19 21:12:53 +01:00
										 |  |  | import bundleService from "./bundle.js"; | 
					
						
							| 
									
										
										
										
											2020-02-15 10:41:21 +01:00
										 |  |  | import DialogCommandExecutor from "./dialog_command_executor.js"; | 
					
						
							| 
									
										
										
										
											2020-01-21 22:54:16 +01:00
										 |  |  | import Entrypoints from "./entrypoints.js"; | 
					
						
							| 
									
										
										
										
											2020-02-05 22:08:45 +01:00
										 |  |  | import options from "./options.js"; | 
					
						
							| 
									
										
										
										
											2020-02-02 22:32:44 +01:00
										 |  |  | import utils from "./utils.js"; | 
					
						
							| 
									
										
										
										
											2020-02-05 22:08:45 +01:00
										 |  |  | import ZoomService from "./zoom.js"; | 
					
						
							| 
									
										
										
										
											2020-02-07 21:08:55 +01:00
										 |  |  | import TabManager from "./tab_manager.js"; | 
					
						
							| 
									
										
										
										
											2020-02-09 21:13:05 +01:00
										 |  |  | import treeService from "./tree.js"; | 
					
						
							| 
									
										
										
										
											2020-02-16 19:21:17 +01:00
										 |  |  | import Component from "../widgets/component.js"; | 
					
						
							| 
									
										
										
										
											2020-02-16 20:09:59 +01:00
										 |  |  | import keyboardActionsService from "./keyboard_actions.js"; | 
					
						
							| 
									
										
										
										
											2020-03-01 15:19:16 +01:00
										 |  |  | import MobileScreenSwitcherExecutor from "../widgets/mobile_screen_switcher.js"; | 
					
						
							| 
									
										
										
										
											2020-03-17 12:28:02 +01:00
										 |  |  | import MainTreeExecutors from "./main_tree_executors.js"; | 
					
						
							| 
									
										
										
										
											2020-01-11 21:19:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 19:21:17 +01:00
										 |  |  | class AppContext extends Component { | 
					
						
							| 
									
										
										
										
											2020-03-01 10:41:23 +01:00
										 |  |  |     setLayout(layout) { | 
					
						
							| 
									
										
										
										
											2020-02-06 21:47:31 +01:00
										 |  |  |         this.layout = layout; | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-05 22:08:45 +01:00
										 |  |  |     async start() { | 
					
						
							| 
									
										
										
										
											2020-02-02 22:04:28 +01:00
										 |  |  |         this.showWidgets(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-16 21:16:09 +01:00
										 |  |  |         await Promise.all([treeCache.initializedPromise, options.initializedPromise]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-07 21:08:55 +01:00
										 |  |  |         this.tabManager.loadTabs(); | 
					
						
							| 
									
										
										
										
											2020-02-02 22:32:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-25 11:02:59 +01:00
										 |  |  |         setTimeout(() => bundleService.executeStartupBundles(), 2000); | 
					
						
							| 
									
										
										
										
											2020-02-02 22:04:28 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 22:04:28 +01:00
										 |  |  |     showWidgets() { | 
					
						
							| 
									
										
										
										
											2020-02-16 19:21:17 +01:00
										 |  |  |         const rootWidget = this.layout.getRootWidget(this); | 
					
						
							|  |  |  |         const $renderedWidget = rootWidget.render(); | 
					
						
							| 
									
										
										
										
											2020-01-14 21:23:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:09:59 +01:00
										 |  |  |         keyboardActionsService.updateDisplayedShortcuts($renderedWidget); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-09 22:31:52 +01:00
										 |  |  |         $("body").append($renderedWidget); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-29 14:32:26 +01:00
										 |  |  |         $renderedWidget.on('click', "[data-trigger-command]", e => { | 
					
						
							|  |  |  |             const commandName = $(e.target).attr('data-trigger-command'); | 
					
						
							| 
									
										
										
										
											2020-02-09 22:31:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-29 14:32:26 +01:00
										 |  |  |             this.triggerCommand(commandName); | 
					
						
							| 
									
										
										
										
											2020-02-09 22:31:52 +01:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2020-02-05 22:08:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-27 10:03:14 +01:00
										 |  |  |         this.tabManager = new TabManager(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-15 10:41:21 +01:00
										 |  |  |         this.executors = [ | 
					
						
							| 
									
										
										
										
											2020-02-16 19:54:11 +01:00
										 |  |  |             this.tabManager, | 
					
						
							| 
									
										
										
										
											2020-02-27 10:03:14 +01:00
										 |  |  |             new DialogCommandExecutor(), | 
					
						
							| 
									
										
										
										
											2020-03-17 12:28:02 +01:00
										 |  |  |             new Entrypoints(), | 
					
						
							|  |  |  |             new MainTreeExecutors() | 
					
						
							| 
									
										
										
										
											2020-01-13 21:48:44 +01:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2020-02-05 22:08:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-01 15:19:16 +01:00
										 |  |  |         if (utils.isMobile()) { | 
					
						
							|  |  |  |             this.executors.push(new MobileScreenSwitcherExecutor()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-27 10:03:14 +01:00
										 |  |  |         this.child(rootWidget); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (const executor of this.executors) { | 
					
						
							|  |  |  |             this.child(executor); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-02-17 22:38:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-05 22:08:45 +01:00
										 |  |  |         if (utils.isElectron()) { | 
					
						
							| 
									
										
										
										
											2020-02-27 10:03:14 +01:00
										 |  |  |             this.child(new ZoomService()); | 
					
						
							| 
									
										
										
										
											2020-02-05 22:08:45 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-02-06 21:16:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 19:21:17 +01:00
										 |  |  |         this.triggerEvent('initialRenderComplete'); | 
					
						
							| 
									
										
										
										
											2020-01-11 21:19:56 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-29 19:43:19 +01:00
										 |  |  |     /** @return {Promise} */ | 
					
						
							|  |  |  |     triggerEvent(name, data) { | 
					
						
							|  |  |  |         return this.handleEvent(name, data); | 
					
						
							| 
									
										
										
										
											2020-02-01 22:29:32 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-15 10:41:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-29 19:43:19 +01:00
										 |  |  |     /** @return {Promise} */ | 
					
						
							|  |  |  |     triggerCommand(name, data = {}) { | 
					
						
							| 
									
										
										
										
											2020-02-15 10:41:21 +01:00
										 |  |  |         for (const executor of this.executors) { | 
					
						
							| 
									
										
										
										
											2020-02-29 19:43:19 +01:00
										 |  |  |             const fun = executor[name + "Command"]; | 
					
						
							| 
									
										
										
										
											2020-02-15 10:41:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-29 19:43:19 +01:00
										 |  |  |             if (fun) { | 
					
						
							|  |  |  |                 return executor.callMethod(fun, data); | 
					
						
							| 
									
										
										
										
											2020-02-15 10:41:21 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-17 22:38:46 +01:00
										 |  |  |         console.debug(`Unhandled command ${name}, converting to event.`); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-29 19:43:19 +01:00
										 |  |  |         return this.triggerEvent(name, data); | 
					
						
							| 
									
										
										
										
											2020-02-15 10:41:21 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 19:21:17 +01:00
										 |  |  |     getComponentByEl(el) { | 
					
						
							|  |  |  |         return $(el).closest(".component").prop('component'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-15 10:41:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 19:23:49 +01:00
										 |  |  |     async protectedSessionStartedEvent() { | 
					
						
							| 
									
										
										
										
											2020-02-16 19:21:17 +01:00
										 |  |  |         await treeCache.loadInitialTree(); | 
					
						
							| 
									
										
										
										
											2020-02-15 10:41:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 19:21:17 +01:00
										 |  |  |         this.triggerEvent('treeCacheReloaded'); | 
					
						
							| 
									
										
										
										
											2020-02-15 10:41:21 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-01 10:41:23 +01:00
										 |  |  | const appContext = new AppContext(); | 
					
						
							| 
									
										
										
										
											2020-01-12 12:48:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 10:41:43 +01:00
										 |  |  | // we should save all outstanding changes before the page/app is closed
 | 
					
						
							|  |  |  | $(window).on('beforeunload', () => { | 
					
						
							| 
									
										
										
										
											2020-02-16 19:21:17 +01:00
										 |  |  |     appContext.triggerEvent('beforeUnload'); | 
					
						
							| 
									
										
										
										
											2020-02-02 10:41:43 +01:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 20:07:34 +01:00
										 |  |  | function isNotePathInAddress() { | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |     const [notePath, tabId] = treeService.getHashValueFromAddress(); | 
					
						
							| 
									
										
										
										
											2020-02-03 20:07:34 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return notePath.startsWith("root") | 
					
						
							|  |  |  |         // empty string is for empty/uninitialized tab
 | 
					
						
							|  |  |  |         || (notePath === '' && !!tabId); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $(window).on('hashchange', function() { | 
					
						
							|  |  |  |     if (isNotePathInAddress()) { | 
					
						
							| 
									
										
										
										
											2020-02-09 21:13:05 +01:00
										 |  |  |         const [notePath, tabId] = treeService.getHashValueFromAddress(); | 
					
						
							| 
									
										
										
										
											2020-02-03 20:07:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-08 20:53:07 +01:00
										 |  |  |         appContext.tabManager.switchToTab(tabId, notePath); | 
					
						
							| 
									
										
										
										
											2020-02-03 20:07:34 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 09:57:28 +01:00
										 |  |  | export default appContext; |