| 
									
										
										
										
											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 treeUtils from "./tree_utils.js"; | 
					
						
							|  |  |  | import utils from "./utils.js"; | 
					
						
							| 
									
										
										
										
											2019-05-04 22:44:25 +02:00
										 |  |  | import {NoteTypeContext} from "./note_type.js"; | 
					
						
							|  |  |  | import noteDetailService from "./note_detail.js"; | 
					
						
							| 
									
										
										
										
											2019-05-01 23:06:18 +02:00
										 |  |  | import noteDetailCode from "./note_detail_code.js"; | 
					
						
							|  |  |  | import noteDetailText from "./note_detail_text.js"; | 
					
						
							|  |  |  | import noteDetailFile from "./note_detail_file.js"; | 
					
						
							|  |  |  | import noteDetailImage from "./note_detail_image.js"; | 
					
						
							|  |  |  | import noteDetailSearch from "./note_detail_search.js"; | 
					
						
							|  |  |  | import noteDetailRender from "./note_detail_render.js"; | 
					
						
							|  |  |  | import noteDetailRelationMap from "./note_detail_relation_map.js"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 22:24:43 +02:00
										 |  |  | const $noteTabContentsContainer = $("#note-tab-container"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const el = $('.chrome-tabs')[0]; | 
					
						
							|  |  |  | const chromeTabs = new ChromeTabs(); | 
					
						
							|  |  |  | chromeTabs.init(el); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  | const componentClasses = { | 
					
						
							|  |  |  |     'code': noteDetailCode, | 
					
						
							|  |  |  |     'text': noteDetailText, | 
					
						
							|  |  |  |     'file': noteDetailFile, | 
					
						
							|  |  |  |     'image': noteDetailImage, | 
					
						
							|  |  |  |     'search': noteDetailSearch, | 
					
						
							|  |  |  |     'render': noteDetailRender, | 
					
						
							|  |  |  |     'relation-map': noteDetailRelationMap | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class NoteContext { | 
					
						
							| 
									
										
										
										
											2019-05-03 21:50:14 +02:00
										 |  |  |     constructor(note, openOnBackground) { | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  |         /** @type {NoteFull} */ | 
					
						
							| 
									
										
										
										
											2019-05-02 22:24:43 +02:00
										 |  |  |         this.note = note; | 
					
						
							|  |  |  |         this.noteId = note.noteId; | 
					
						
							|  |  |  |         this.$noteTabContent = $noteTabContentsContainer.find(`[data-note-id="${this.noteId}"]`); | 
					
						
							|  |  |  |         this.$noteTitle = this.$noteTabContent.find(".note-title"); | 
					
						
							|  |  |  |         this.$noteDetailComponents = this.$noteTabContent.find(".note-detail-component"); | 
					
						
							|  |  |  |         this.$protectButton = this.$noteTabContent.find(".protect-button"); | 
					
						
							|  |  |  |         this.$unprotectButton = this.$noteTabContent.find(".unprotect-button"); | 
					
						
							|  |  |  |         this.$childrenOverview = this.$noteTabContent.find(".children-overview"); | 
					
						
							|  |  |  |         this.$scriptArea = this.$noteTabContent.find(".note-detail-script-area"); | 
					
						
							| 
									
										
										
										
											2019-05-03 21:50:14 +02:00
										 |  |  |         this.$savedIndicator = this.$noteTabContent.find(".saved-indicator"); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  |         this.noteType = new NoteTypeContext(this); | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  |         this.components = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.$noteTitle.on('input', () => { | 
					
						
							|  |  |  |             this.noteChanged(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const title = this.$noteTitle.val(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             treeService.setNoteTitle(this.noteId, title); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2019-05-02 22:24:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         this.tab = chromeTabs.addTab({ | 
					
						
							|  |  |  |             title: note.title, | 
					
						
							|  |  |  |             favicon: false | 
					
						
							| 
									
										
										
										
											2019-05-03 21:50:14 +02:00
										 |  |  |         }, { | 
					
						
							|  |  |  |             background: openOnBackground | 
					
						
							| 
									
										
										
										
											2019-05-02 22:24:43 +02:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2019-05-03 21:50:14 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         this.tab.setAttribute('data-note-id', this.noteId); | 
					
						
							| 
									
										
										
										
											2019-05-02 22:24:43 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     setNote(note) { | 
					
						
							|  |  |  |         this.noteId = note.noteId; | 
					
						
							|  |  |  |         this.note = note; | 
					
						
							|  |  |  |         this.$noteTabContent.attr('data-note-id', note.noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         chromeTabs.updateTab(this.tab, {title: note.title}); | 
					
						
							| 
									
										
										
										
											2019-05-04 22:44:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         this.attributes.invalidateAttributes(); | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     getComponent(type) { | 
					
						
							|  |  |  |         if (!type) { | 
					
						
							|  |  |  |             type = this.note.type; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!(type in this.components)) { | 
					
						
							|  |  |  |             this.components[type] = new componentClasses[type](this); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return this.components[type]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async saveNote() { | 
					
						
							|  |  |  |         if (this.note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.note.title = this.$noteTitle.val(); | 
					
						
							| 
									
										
										
										
											2019-05-04 22:44:25 +02:00
										 |  |  |         this.note.content = noteDetailService.getActiveNoteContent(); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await server.put('notes/' + this.note.noteId, this.note.dto); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (this.note.isProtected) { | 
					
						
							|  |  |  |             protectedSessionHolder.touchProtectedSession(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-03 21:50:14 +02:00
										 |  |  |         this.$savedIndicator.fadeIn(); | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // run async
 | 
					
						
							| 
									
										
										
										
											2019-05-03 21:50:14 +02:00
										 |  |  |         bundleService.executeRelationBundles(this.note, 'runOnNoteChange'); | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async saveNoteIfChanged() { | 
					
						
							|  |  |  |         if (this.isNoteChanged) { | 
					
						
							|  |  |  |             await this.saveNote(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     noteChanged() { | 
					
						
							| 
									
										
										
										
											2019-05-04 14:34:03 +02:00
										 |  |  |         if (this.noteChangeDisabled) { | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.isNoteChanged = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-03 21:50:14 +02:00
										 |  |  |         this.$savedIndicator.fadeOut(); | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async showChildrenOverview() { | 
					
						
							| 
									
										
										
										
											2019-05-04 22:44:25 +02:00
										 |  |  |         const attributes = await this.attributes.getAttributes(); | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  |         const hideChildrenOverview = attributes.some(attr => attr.type === 'label' && attr.name === 'hideChildrenOverview') | 
					
						
							|  |  |  |             || this.note.type === 'relation-map' | 
					
						
							|  |  |  |             || this.note.type === 'image' | 
					
						
							|  |  |  |             || this.note.type === 'file'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (hideChildrenOverview) { | 
					
						
							|  |  |  |             this.$childrenOverview.hide(); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.$childrenOverview.empty(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const notePath = await treeService.getActiveNotePath(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (const childBranch of await this.note.getChildBranches()) { | 
					
						
							|  |  |  |             const link = $('<a>', { | 
					
						
							|  |  |  |                 href: 'javascript:', | 
					
						
							|  |  |  |                 text: await treeUtils.getNoteTitle(childBranch.noteId, childBranch.parentNoteId) | 
					
						
							|  |  |  |             }).attr('data-action', 'note').attr('data-note-path', notePath + '/' + childBranch.noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const childEl = $('<div class="child-overview-item">').html(link); | 
					
						
							|  |  |  |             this.$childrenOverview.append(childEl); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.$childrenOverview.show(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     updateNoteView() { | 
					
						
							| 
									
										
										
										
											2019-05-02 22:24:43 +02:00
										 |  |  |         this.$noteTabContent.toggleClass("protected", this.note.isProtected); | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  |         this.$protectButton.toggleClass("active", this.note.isProtected); | 
					
						
							|  |  |  |         this.$protectButton.prop("disabled", this.note.isProtected); | 
					
						
							|  |  |  |         this.$unprotectButton.toggleClass("active", !this.note.isProtected); | 
					
						
							|  |  |  |         this.$unprotectButton.prop("disabled", !this.note.isProtected || !protectedSessionHolder.isProtectedSessionAvailable()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 22:24:43 +02:00
										 |  |  |         for (const clazz of Array.from(this.$noteTabContent[0].classList)) { // create copy to safely iterate over while removing classes
 | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  |             if (clazz.startsWith("type-") || clazz.startsWith("mime-")) { | 
					
						
							| 
									
										
										
										
											2019-05-02 22:24:43 +02:00
										 |  |  |                 this.$noteTabContent.removeClass(clazz); | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 22:24:43 +02:00
										 |  |  |         this.$noteTabContent.addClass(utils.getNoteTypeClass(this.note.type)); | 
					
						
							|  |  |  |         this.$noteTabContent.addClass(utils.getMimeTypeClass(this.note.mime)); | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default NoteContext; |