| 
									
										
										
										
											2019-05-01 23:06:18 +02:00
										 |  |  | import treeService from "./tree.js"; | 
					
						
							|  |  |  | import protectedSessionHolder from "./protected_session_holder.js"; | 
					
						
							|  |  |  | import server from "./server.js"; | 
					
						
							|  |  |  | import bundleService from "./bundle.js"; | 
					
						
							| 
									
										
										
										
											2019-05-04 22:44:25 +02:00
										 |  |  | import Attributes from "./attributes.js"; | 
					
						
							| 
									
										
										
										
											2019-05-01 23:06:18 +02:00
										 |  |  | import utils from "./utils.js"; | 
					
						
							| 
									
										
										
										
											2019-08-25 17:36:13 +02:00
										 |  |  | import optionsService from "./options.js"; | 
					
						
							| 
									
										
										
										
											2019-07-21 10:17:08 +02:00
										 |  |  | import Sidebar from "./sidebar.js"; | 
					
						
							| 
									
										
										
										
											2020-01-12 12:30:30 +01:00
										 |  |  | import appContext from "./app_context.js"; | 
					
						
							| 
									
										
										
										
											2019-05-01 23:06:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-08 19:55:24 +02:00
										 |  |  | const $tabContentsContainer = $("#note-tab-container"); | 
					
						
							| 
									
										
										
										
											2019-05-02 22:24:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  | const componentClasses = { | 
					
						
							| 
									
										
										
										
											2019-08-26 19:49:19 +02:00
										 |  |  |     'empty': "./note_detail_empty.js", | 
					
						
							|  |  |  |     'text': "./note_detail_text.js", | 
					
						
							|  |  |  |     'code': "./note_detail_code.js", | 
					
						
							|  |  |  |     'file': "./note_detail_file.js", | 
					
						
							|  |  |  |     'image': "./note_detail_image.js", | 
					
						
							|  |  |  |     'search': "./note_detail_search.js", | 
					
						
							|  |  |  |     'render': "./note_detail_render.js", | 
					
						
							|  |  |  |     'relation-map': "./note_detail_relation_map.js", | 
					
						
							| 
									
										
										
										
											2019-10-01 21:11:11 +02:00
										 |  |  |     'protected-session': "./note_detail_protected_session.js", | 
					
						
							|  |  |  |     'book': "./note_detail_book.js" | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-19 23:29:42 +02:00
										 |  |  | let showSidebarInNewTab = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-25 17:36:13 +02:00
										 |  |  | optionsService.addLoadListener(options => { | 
					
						
							| 
									
										
										
										
											2019-08-22 23:31:02 +02:00
										 |  |  |     showSidebarInNewTab = options.is('showSidebarInNewTab'); | 
					
						
							| 
									
										
										
										
											2019-08-19 23:29:42 +02:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-08 19:55:24 +02:00
										 |  |  | class TabContext { | 
					
						
							| 
									
										
										
										
											2019-05-11 19:44:58 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  |      * @param {TabRowWidget} tabRow | 
					
						
							| 
									
										
										
										
											2019-08-16 21:29:44 +02:00
										 |  |  |      * @param {object} state | 
					
						
							| 
									
										
										
										
											2019-05-11 19:44:58 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-08-16 21:29:44 +02:00
										 |  |  |     constructor(tabRow, state = {}) { | 
					
						
							| 
									
										
										
										
											2019-05-11 19:44:58 +02:00
										 |  |  |         this.tabRow = tabRow; | 
					
						
							| 
									
										
										
										
											2019-08-16 21:29:44 +02:00
										 |  |  |         this.tabId = state.tabId || utils.randomString(4); | 
					
						
							| 
									
										
										
										
											2019-05-14 22:29:47 +02:00
										 |  |  |         this.$tab = $(this.tabRow.addTab(this.tabId)); | 
					
						
							| 
									
										
										
										
											2019-09-04 21:30:11 +02:00
										 |  |  |         this.initialized = false; | 
					
						
							|  |  |  |         this.state = state; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-04 22:13:22 +02:00
										 |  |  |     async initTabContent() { | 
					
						
							| 
									
										
										
										
											2019-09-04 21:30:11 +02:00
										 |  |  |         if (this.initialized) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-05 10:59:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-04 22:13:22 +02:00
										 |  |  |         this.initialized = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-08 19:55:24 +02:00
										 |  |  |         this.$tabContent = $(".note-tab-content-template").clone(); | 
					
						
							|  |  |  |         this.$tabContent.removeClass('note-tab-content-template'); | 
					
						
							|  |  |  |         this.$tabContent.attr('data-tab-id', this.tabId); | 
					
						
							| 
									
										
										
										
											2019-08-17 08:49:39 +02:00
										 |  |  |         this.$tabContent.hide(); | 
					
						
							| 
									
										
										
										
											2019-05-05 10:59:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-08 19:55:24 +02:00
										 |  |  |         $tabContentsContainer.append(this.$tabContent); | 
					
						
							| 
									
										
										
										
											2019-05-05 10:59:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-08 19:55:24 +02:00
										 |  |  |         this.$noteDetailComponents = this.$tabContent.find(".note-detail-component"); | 
					
						
							|  |  |  |         this.$scriptArea = this.$tabContent.find(".note-detail-script-area"); | 
					
						
							| 
									
										
										
										
											2019-05-04 14:34:03 +02:00
										 |  |  |         this.noteChangeDisabled = false; | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  |         this.isNoteChanged = false; | 
					
						
							| 
									
										
										
										
											2019-05-04 22:44:25 +02:00
										 |  |  |         this.attributes = new Attributes(this); | 
					
						
							| 
									
										
										
										
											2019-05-13 23:08:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (utils.isDesktop()) { | 
					
						
							| 
									
										
										
										
											2019-09-04 21:30:11 +02:00
										 |  |  |             const sidebarState = this.state.sidebar || { | 
					
						
							| 
									
										
										
										
											2019-08-19 23:29:42 +02:00
										 |  |  |                 visible: showSidebarInNewTab | 
					
						
							|  |  |  |             }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             this.sidebar = new Sidebar(this, sidebarState); | 
					
						
							| 
									
										
										
										
											2019-05-13 23:08:59 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  |         this.components = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-04 22:45:12 +02:00
										 |  |  |         await this.initComponent(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 20:27:30 +02:00
										 |  |  |     async initComponent(disableAutoBook = false) { | 
					
						
							|  |  |  |         this.type = this.getComponentType(disableAutoBook); | 
					
						
							| 
									
										
										
										
											2019-09-04 22:13:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 20:27:30 +02:00
										 |  |  |         if (!(this.type in this.components)) { | 
					
						
							|  |  |  |             const clazz = await import(componentClasses[this.type]); | 
					
						
							| 
									
										
										
										
											2019-09-04 22:13:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 20:27:30 +02:00
										 |  |  |             this.components[this.type] = new clazz.default(this); | 
					
						
							| 
									
										
										
										
											2019-09-04 22:13:22 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-02 22:24:43 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-26 19:49:19 +02:00
										 |  |  |     async setNote(note, notePath) { | 
					
						
							| 
									
										
										
										
											2019-08-17 11:28:36 +02:00
										 |  |  |         /** @property {NoteFull} */ | 
					
						
							| 
									
										
										
										
											2019-05-02 22:24:43 +02:00
										 |  |  |         this.note = note; | 
					
						
							| 
									
										
										
										
											2019-09-04 22:45:12 +02:00
										 |  |  |         this.notePath = notePath; | 
					
						
							|  |  |  |         this.tabRow.updateTab(this.$tab[0], {title: this.note.title}); | 
					
						
							| 
									
										
										
										
											2019-05-04 22:44:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-04 21:30:11 +02:00
										 |  |  |         if (!this.initialized) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-05 10:59:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-08 11:25:57 +02:00
										 |  |  |         this.$scriptArea.empty(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-07 13:09:32 +02:00
										 |  |  |         if (utils.isDesktop()) { | 
					
						
							|  |  |  |             this.attributes.refreshAttributes(); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             // mobile usually doesn't need attributes so we just invalidate
 | 
					
						
							|  |  |  |             this.attributes.invalidateAttributes(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-13 22:08:06 +02:00
										 |  |  |         this.setupClasses(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-14 22:29:47 +02:00
										 |  |  |         this.setCurrentNotePathToHash(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-23 21:13:56 +01:00
										 |  |  |         if (this.sidebar) { | 
					
						
							|  |  |  |             this.sidebar.noteLoaded(); // load async
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-04 21:30:11 +02:00
										 |  |  |         this.noteChangeDisabled = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             await this.renderComponent(); | 
					
						
							|  |  |  |         } finally { | 
					
						
							|  |  |  |             this.noteChangeDisabled = false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-29 19:55:05 +02:00
										 |  |  |         this.setTitleBar(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-01 21:11:11 +02:00
										 |  |  |         this.cleanup(); // esp. on windows autocomplete is not getting closed automatically
 | 
					
						
							| 
									
										
										
										
											2019-06-26 21:08:54 +02: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
 | 
					
						
							| 
									
										
										
										
											2019-05-21 21:47:28 +02:00
										 |  |  |             if (notePath && notePath === this.notePath) { | 
					
						
							|  |  |  |                 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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-04 22:13:22 +02:00
										 |  |  |         bundleService.executeRelationBundles(this.note, 'runOnNoteView', this); | 
					
						
							| 
									
										
										
										
											2020-01-02 19:03:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // after loading new note make sure editor is scrolled to the top
 | 
					
						
							|  |  |  |         this.getComponent().scrollToTop(); | 
					
						
							| 
									
										
										
										
											2020-01-12 23:03:55 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         appContext.trigger('activeNoteChanged'); | 
					
						
							| 
									
										
										
										
											2019-05-13 22:08:06 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-04 21:30:11 +02:00
										 |  |  |     async show() { | 
					
						
							|  |  |  |         if (!this.initialized) { | 
					
						
							| 
									
										
										
										
											2019-09-04 22:13:22 +02:00
										 |  |  |             await this.initTabContent(); | 
					
						
							| 
									
										
										
										
											2019-09-04 21:30:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-04 22:13:22 +02:00
										 |  |  |             this.$tabContent.show(); // show immediately so that user can see something
 | 
					
						
							| 
									
										
										
										
											2019-09-04 21:30:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (this.note) { | 
					
						
							|  |  |  |                 await this.setNote(this.note, this.notePath); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-09-04 22:45:12 +02:00
										 |  |  |             else { | 
					
						
							|  |  |  |                 await this.renderComponent(); // render empty page
 | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-09-04 21:30:11 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-14 22:29:47 +02:00
										 |  |  |         this.$tabContent.show(); | 
					
						
							| 
									
										
										
										
											2019-09-04 22:13:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-23 15:50:24 +01:00
										 |  |  |         if (this.sidebar) { | 
					
						
							|  |  |  |             this.sidebar.show(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-14 22:29:47 +02:00
										 |  |  |         this.setCurrentNotePathToHash(); | 
					
						
							| 
									
										
										
										
											2019-05-29 19:55:05 +02:00
										 |  |  |         this.setTitleBar(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 20:27:30 +02:00
										 |  |  |     async renderComponent(disableAutoBook = false) { | 
					
						
							|  |  |  |         await this.initComponent(disableAutoBook); | 
					
						
							| 
									
										
										
										
											2019-09-04 22:45:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-04 21:30:11 +02:00
										 |  |  |         for (const componentType in this.components) { | 
					
						
							| 
									
										
										
										
											2019-10-05 20:27:30 +02:00
										 |  |  |             if (componentType !== this.type) { | 
					
						
							| 
									
										
										
										
											2019-09-04 21:30:11 +02:00
										 |  |  |                 this.components[componentType].cleanup(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.$noteDetailComponents.hide(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-04 22:13:22 +02:00
										 |  |  |         this.getComponent().show(); | 
					
						
							| 
									
										
										
										
											2019-09-04 21:30:11 +02:00
										 |  |  |         await this.getComponent().render(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-29 19:55:05 +02:00
										 |  |  |     setTitleBar() { | 
					
						
							|  |  |  |         if (!this.$tabContent.is(":visible")) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-14 22:29:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         document.title = "Trilium Notes"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (this.note) { | 
					
						
							|  |  |  |             // it helps navigating in history if note title is included in the title
 | 
					
						
							|  |  |  |             document.title += " - " + this.note.title; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     hide() { | 
					
						
							| 
									
										
										
										
											2019-09-04 21:30:11 +02:00
										 |  |  |         if (this.initialized) { | 
					
						
							|  |  |  |             this.$tabContent.hide(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-14 22:29:47 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     setCurrentNotePathToHash() { | 
					
						
							| 
									
										
										
										
											2019-11-26 20:42:34 +01:00
										 |  |  |         if (this.isActive()) { | 
					
						
							| 
									
										
										
										
											2019-05-14 22:29:47 +02:00
										 |  |  |             document.location.hash = (this.notePath || "") + "-" + this.tabId; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-26 20:42:34 +01:00
										 |  |  |     isActive() { | 
					
						
							|  |  |  |         return this.$tab[0] === this.tabRow.activeTabEl; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-13 22:08:06 +02:00
										 |  |  |     setupClasses() { | 
					
						
							|  |  |  |         for (const clazz of Array.from(this.$tab[0].classList)) { // create copy to safely iterate over while removing classes
 | 
					
						
							|  |  |  |             if (clazz !== 'note-tab') { | 
					
						
							|  |  |  |                 this.$tab.removeClass(clazz); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-08 19:55:24 +02:00
										 |  |  |         for (const clazz of Array.from(this.$tabContent[0].classList)) { // create copy to safely iterate over while removing classes
 | 
					
						
							| 
									
										
										
										
											2019-05-13 22:08:06 +02:00
										 |  |  |             if (clazz !== 'note-tab-content') { | 
					
						
							| 
									
										
										
										
											2019-05-08 19:55:24 +02:00
										 |  |  |                 this.$tabContent.removeClass(clazz); | 
					
						
							| 
									
										
										
										
											2019-05-05 20:45:07 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-13 22:08:06 +02:00
										 |  |  |         this.$tab.addClass(this.note.cssClass); | 
					
						
							|  |  |  |         this.$tab.addClass(utils.getNoteTypeClass(this.note.type)); | 
					
						
							|  |  |  |         this.$tab.addClass(utils.getMimeTypeClass(this.note.mime)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.$tabContent.addClass(this.note.cssClass); | 
					
						
							| 
									
										
										
										
											2019-05-08 19:55:24 +02:00
										 |  |  |         this.$tabContent.addClass(utils.getNoteTypeClass(this.note.type)); | 
					
						
							|  |  |  |         this.$tabContent.addClass(utils.getMimeTypeClass(this.note.mime)); | 
					
						
							| 
									
										
										
										
											2019-05-22 22:26:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         this.$tabContent.toggleClass("protected", this.note.isProtected); | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-05 19:48:30 +02:00
										 |  |  |     getComponent() { | 
					
						
							| 
									
										
										
										
											2019-12-28 19:18:44 +01:00
										 |  |  |         if (!this.components[this.type]) { | 
					
						
							|  |  |  |             throw new Error("Could not find component for type: " + this.type); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 20:27:30 +02:00
										 |  |  |         return this.components[this.type]; | 
					
						
							| 
									
										
										
										
											2019-08-26 19:49:19 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 20:27:30 +02:00
										 |  |  |     getComponentType(disableAutoBook) { | 
					
						
							| 
									
										
										
										
											2019-10-01 21:11:11 +02:00
										 |  |  |         if (!this.note) { | 
					
						
							|  |  |  |             return "empty"; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-05 19:48:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-01 21:11:11 +02:00
										 |  |  |         let type = this.note.type; | 
					
						
							| 
									
										
										
										
											2019-05-05 19:48:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 20:27:30 +02:00
										 |  |  |         if (type === 'text' && !disableAutoBook && utils.isHtmlEmpty(this.note.content) && this.note.hasChildren()) { | 
					
						
							|  |  |  |             type = 'book'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-01 21:11:11 +02:00
										 |  |  |         if (this.note.isProtected) { | 
					
						
							|  |  |  |             if (protectedSessionHolder.isProtectedSessionAvailable()) { | 
					
						
							|  |  |  |                 protectedSessionHolder.touchProtectedSession(); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 type = 'protected-session'; | 
					
						
							| 
									
										
										
										
											2019-05-12 12:58:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-01 21:11:11 +02:00
										 |  |  |                 // user shouldn't be able to edit note title
 | 
					
						
							|  |  |  |                 this.$noteTitle.prop("readonly", true); | 
					
						
							| 
									
										
										
										
											2019-05-05 19:48:30 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-05-12 12:58:55 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-10-01 21:11:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-26 19:49:19 +02:00
										 |  |  |         return type; | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-21 20:24:40 +02:00
										 |  |  |     async activate() { | 
					
						
							|  |  |  |         await this.tabRow.activateTab(this.$tab[0]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  |     async saveNote() { | 
					
						
							|  |  |  |         if (this.note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.note.title = this.$noteTitle.val(); | 
					
						
							| 
									
										
										
										
											2019-05-21 20:24:40 +02:00
										 |  |  |         this.note.content = this.getComponent().getContent(); | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // it's important to set the flag back to false immediatelly after retrieving title and content
 | 
					
						
							|  |  |  |         // otherwise we might overwrite another change (especially async code)
 | 
					
						
							|  |  |  |         this.isNoteChanged = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         treeService.setNoteTitle(this.note.noteId, this.note.title); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-06 22:39:27 +02:00
										 |  |  |         const resp = await server.put('notes/' + this.note.noteId, this.note.dto); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.note.dateModified = resp.dateModified; | 
					
						
							|  |  |  |         this.note.utcDateModified = resp.utcDateModified; | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (this.note.isProtected) { | 
					
						
							|  |  |  |             protectedSessionHolder.touchProtectedSession(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  |         // FIXME trigger "noteSaved" event so that title indicator is triggered
 | 
					
						
							|  |  |  |         this.eventReceived('noteSaved'); | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // run async
 | 
					
						
							| 
									
										
										
										
											2019-05-29 21:48:48 +02:00
										 |  |  |         bundleService.executeRelationBundles(this.note, 'runOnNoteChange', this); | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async saveNoteIfChanged() { | 
					
						
							|  |  |  |         if (this.isNoteChanged) { | 
					
						
							|  |  |  |             await this.saveNote(); | 
					
						
							| 
									
										
										
										
											2019-05-20 22:25:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 12:30:30 +01:00
										 |  |  |             appContext.refreshTabs(this.tabId, this.note.noteId); | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     noteChanged() { | 
					
						
							| 
									
										
										
										
											2019-05-04 14:34:03 +02:00
										 |  |  |         if (this.noteChangeDisabled) { | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.isNoteChanged = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  |         // FIMXE: trigger noteChanged event
 | 
					
						
							| 
									
										
										
										
											2019-05-03 21:50:14 +02:00
										 |  |  |         this.$savedIndicator.fadeOut(); | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-04 22:45:12 +02:00
										 |  |  |     async remove() { | 
					
						
							| 
									
										
										
										
											2019-10-21 21:22:53 +02:00
										 |  |  |         if (this.$tabContent) { | 
					
						
							|  |  |  |             // sometimes there are orphan autocompletes after closing the tab
 | 
					
						
							|  |  |  |             this.cleanup(); | 
					
						
							| 
									
										
										
										
											2019-09-04 22:45:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-21 21:22:53 +02:00
										 |  |  |             await this.saveNoteIfChanged(); | 
					
						
							|  |  |  |             this.$tabContent.remove(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-12-23 21:46:37 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (this.sidebar) { | 
					
						
							|  |  |  |             this.sidebar.remove(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-09-04 22:45:12 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-01 21:11:11 +02:00
										 |  |  |     cleanup() { | 
					
						
							| 
									
										
										
										
											2019-10-15 19:42:39 +02:00
										 |  |  |         if (this.$tabContent && utils.isDesktop()) { | 
					
						
							| 
									
										
										
										
											2019-06-30 20:14:57 +02:00
										 |  |  |             this.$tabContent.find('.aa-input').autocomplete('close'); | 
					
						
							| 
									
										
										
										
											2019-10-01 21:11:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             $('.note-tooltip').remove(); | 
					
						
							| 
									
										
										
										
											2019-06-30 20:14:57 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-06-26 21:08:54 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-08-06 22:39:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-03 21:31:39 +02:00
										 |  |  |     eventReceived(name, data) { | 
					
						
							| 
									
										
										
										
											2019-09-04 21:30:11 +02:00
										 |  |  |         if (!this.initialized) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-03 21:31:39 +02:00
										 |  |  |         this.attributes.eventReceived(name, data); | 
					
						
							| 
									
										
										
										
											2019-08-06 23:20:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-06 22:39:27 +02:00
										 |  |  |         if (this.sidebar) { | 
					
						
							| 
									
										
										
										
											2019-09-03 21:31:39 +02:00
										 |  |  |             this.sidebar.eventReceived(name, data); | 
					
						
							| 
									
										
										
										
											2019-08-06 22:39:27 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-08-15 10:04:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     getTabState() { | 
					
						
							|  |  |  |         if (!this.notePath) { | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             tabId: this.tabId, | 
					
						
							|  |  |  |             notePath: this.notePath, | 
					
						
							|  |  |  |             active: this.tabRow.activeTabEl === this.$tab[0], | 
					
						
							|  |  |  |             sidebar: this.sidebar && this.sidebar.getSidebarState() | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     stateChanged() { | 
					
						
							| 
									
										
										
										
											2020-01-12 12:48:17 +01:00
										 |  |  |         appContext.openTabsChanged(); | 
					
						
							| 
									
										
										
										
											2019-08-15 10:04:03 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-08 19:55:24 +02:00
										 |  |  | export default TabContext; |