| 
									
										
										
										
											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"; | 
					
						
							| 
									
										
										
										
											2020-01-12 12:30:30 +01:00
										 |  |  | import appContext from "./app_context.js"; | 
					
						
							| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  | import treeUtils from "./tree_utils.js"; | 
					
						
							|  |  |  | import noteDetailService from "./note_detail.js"; | 
					
						
							|  |  |  | import Component from "../widgets/component.js"; | 
					
						
							| 
									
										
										
										
											2019-05-01 23:06:18 +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
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  | class TabContext extends Component { | 
					
						
							| 
									
										
										
										
											2019-05-11 19:44:58 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  |      * @param {AppContext} appContext | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  |      * @param {TabRowWidget} tabRow | 
					
						
							| 
									
										
										
										
											2019-08-16 21:29:44 +02:00
										 |  |  |      * @param {object} state | 
					
						
							| 
									
										
										
										
											2019-05-11 19:44:58 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  |     constructor(appContext, tabRow, state = {}) { | 
					
						
							|  |  |  |         super(appContext); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-11 19:44:58 +02:00
										 |  |  |         this.tabRow = tabRow; | 
					
						
							| 
									
										
										
										
											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.state = state; | 
					
						
							| 
									
										
										
										
											2019-09-04 22:13:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  |         this.attributes = new Attributes(this.appContext, this); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.children.push(this.attributes); | 
					
						
							| 
									
										
										
										
											2019-05-02 22:24:43 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  |     async setNote(notePath) { | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  |         await this.trigger('beforeNoteSwitch', {tabId: this.tabId}, true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-04 22:45:12 +02:00
										 |  |  |         this.notePath = notePath; | 
					
						
							| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  |         const noteId = treeUtils.getNoteIdFromNotePath(notePath); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @property {NoteFull} */ | 
					
						
							|  |  |  |         this.note = await noteDetailService.loadNote(noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-04 22:45:12 +02:00
										 |  |  |         this.tabRow.updateTab(this.$tab[0], {title: this.note.title}); | 
					
						
							| 
									
										
										
										
											2019-05-04 22:44:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-13 22:08:06 +02:00
										 |  |  |         this.setupClasses(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-18 18:01:16 +01: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 11:03:34 +01:00
										 |  |  |         if (this.note.isProtected && protectedSessionHolder.isProtectedSessionAvailable()) { | 
					
						
							|  |  |  |             // FIXME: there are probably more places where this should be done
 | 
					
						
							|  |  |  |             protectedSessionHolder.touchProtectedSession(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-18 18:01:16 +01:00
										 |  |  |         this.trigger('tabNoteSwitched', {tabId: this.tabId}); | 
					
						
							| 
									
										
										
										
											2019-05-13 22:08:06 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-04 21:30:11 +02:00
										 |  |  |     async show() { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-14 22:29:47 +02:00
										 |  |  |     hide() { | 
					
						
							| 
									
										
										
										
											2020-01-16 22:44:36 +01:00
										 |  |  |         // FIXME
 | 
					
						
							| 
									
										
										
										
											2019-05-14 22:29:47 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-26 20:42:34 +01:00
										 |  |  |     isActive() { | 
					
						
							| 
									
										
										
										
											2020-01-15 22:27:52 +01:00
										 |  |  |         return this.tabId === this.tabRow.activeTabId; | 
					
						
							| 
									
										
										
										
											2019-11-26 20:42:34 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.$tab.addClass(this.note.cssClass); | 
					
						
							|  |  |  |         this.$tab.addClass(utils.getNoteTypeClass(this.note.type)); | 
					
						
							|  |  |  |         this.$tab.addClass(utils.getMimeTypeClass(this.note.mime)); | 
					
						
							| 
									
										
										
										
											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() { | 
					
						
							| 
									
										
										
										
											2020-01-13 21:48:44 +01:00
										 |  |  |         return; // FIXME
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  |         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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-15 10:04:03 +02:00
										 |  |  |     getTabState() { | 
					
						
							|  |  |  |         if (!this.notePath) { | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             tabId: this.tabId, | 
					
						
							|  |  |  |             notePath: this.notePath, | 
					
						
							| 
									
										
										
										
											2020-01-15 22:27:52 +01:00
										 |  |  |             active: this.tabRow.activeTabId === this.tabId | 
					
						
							| 
									
										
										
										
											2019-08-15 10:04:03 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     stateChanged() { | 
					
						
							| 
									
										
										
										
											2020-01-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; |