| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  | import TabAwareWidget from "./tab_aware_widget.js"; | 
					
						
							|  |  |  | import utils from "../services/utils.js"; | 
					
						
							|  |  |  | import protectedSessionHolder from "../services/protected_session_holder.js"; | 
					
						
							| 
									
										
										
										
											2020-01-19 18:05:06 +01:00
										 |  |  | import treeCache from "../services/tree_cache.js"; | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  | import server from "../services/server.js"; | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | const TPL = `
 | 
					
						
							| 
									
										
										
										
											2020-01-19 15:44:18 +01:00
										 |  |  | <div class="note-title-container"> | 
					
						
							| 
									
										
										
										
											2020-01-14 20:27:40 +01:00
										 |  |  |     <style> | 
					
						
							| 
									
										
										
										
											2020-01-19 15:44:18 +01:00
										 |  |  |     .note-title-container { | 
					
						
							|  |  |  |         flex-grow: 100; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     .note-title-container input.note-title { | 
					
						
							| 
									
										
										
										
											2020-01-14 20:27:40 +01:00
										 |  |  |         margin-left: 15px; | 
					
						
							|  |  |  |         margin-right: 10px; | 
					
						
							|  |  |  |         font-size: 150%; | 
					
						
							|  |  |  |         border: 0; | 
					
						
							| 
									
										
										
										
											2020-01-19 18:05:06 +01:00
										 |  |  |         min-width: 5em; | 
					
						
							|  |  |  |         width: 100%; | 
					
						
							| 
									
										
										
										
											2020-01-14 20:27:40 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     </style> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-18 19:46:30 +01:00
										 |  |  |     <input autocomplete="off" value="" class="note-title" tabindex="1"> | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  | </div>`; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  | class SpacedUpdate { | 
					
						
							|  |  |  |     constructor(updater, updateInterval = 1000) { | 
					
						
							|  |  |  |         this.updater = updater; | 
					
						
							|  |  |  |         this.lastUpdated = Date.now(); | 
					
						
							|  |  |  |         this.changed = false; | 
					
						
							|  |  |  |         this.updateInterval = updateInterval; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     scheduleUpdate() { | 
					
						
							|  |  |  |         this.changed = true; | 
					
						
							|  |  |  |         setTimeout(() => this.triggerUpdate()) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async updateNowIfNecessary() { | 
					
						
							|  |  |  |         if (this.changed) { | 
					
						
							|  |  |  |             this.changed = false; | 
					
						
							|  |  |  |             await this.updater(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     triggerUpdate() { | 
					
						
							|  |  |  |         if (!this.changed) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (Date.now() - this.lastUpdated > this.updateInterval) { | 
					
						
							|  |  |  |             this.updater(); | 
					
						
							|  |  |  |             this.lastUpdated = Date.now(); | 
					
						
							|  |  |  |             this.changed = false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             // update not triggered but changes are still pending so we need to schedule another check
 | 
					
						
							|  |  |  |             this.scheduleUpdate(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  | export default class NoteTitleWidget extends TabAwareWidget { | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  |     constructor(appContext) { | 
					
						
							|  |  |  |         super(appContext); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.spacedUpdate = new SpacedUpdate(async () => { | 
					
						
							|  |  |  |             const noteId = this.tabContext.note.noteId; | 
					
						
							|  |  |  |             const title = this.$noteTitle.val(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const resp = await server.put(`notes/${noteId}/change-title`, {title}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // FIXME: minor - does not propagate to other tab contexts with this note though
 | 
					
						
							|  |  |  |             this.tabContext.note.dateModified = resp.dateModified; | 
					
						
							|  |  |  |             this.tabContext.note.utcDateModified = resp.utcDateModified; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             this.trigger('noteChangesSaved', {noteId}) | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 20:15:05 +01:00
										 |  |  |     doRender() { | 
					
						
							| 
									
										
										
										
											2020-01-14 20:27:40 +01:00
										 |  |  |         this.$widget = $(TPL); | 
					
						
							|  |  |  |         this.$noteTitle = this.$widget.find(".note-title"); | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 18:05:06 +01:00
										 |  |  |         this.$noteTitle.on('input', () => this.titleChanged()); | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 19:33:35 +01:00
										 |  |  |         utils.bindElShortcut(this.$noteTitle, 'return', () => { | 
					
						
							|  |  |  |             this.trigger('focusOnDetail', {tabId: this.tabContext.tabId}); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2020-01-12 20:15:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-14 20:27:40 +01:00
										 |  |  |         return this.$widget; | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 18:05:06 +01:00
										 |  |  |     async titleChanged() { | 
					
						
							|  |  |  |         const {note} = this.tabContext; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!note) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         note.title = this.$noteTitle.val(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  |         this.spacedUpdate.scheduleUpdate(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 18:05:06 +01:00
										 |  |  |         const noteFromCache = await treeCache.getNote(note.noteId); | 
					
						
							|  |  |  |         noteFromCache.title = note.title; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.trigger(`noteTitleChanged`, { | 
					
						
							|  |  |  |             tabId: this.tabContext.tabId, // used to identify that the event comes from this tab so we should not update this tab's input
 | 
					
						
							|  |  |  |             title: note.title, | 
					
						
							|  |  |  |             noteId: note.noteId | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     noteTitleChangedListener({tabId, title, noteId}) { | 
					
						
							|  |  |  |         if (tabId === this.tabContext.tabId | 
					
						
							|  |  |  |             || !this.tabContext.note | 
					
						
							|  |  |  |             || this.tabContext.note.noteId !== noteId) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.$noteTitle.val(title); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 15:36:42 +01:00
										 |  |  |     async refreshWithNote(note) { | 
					
						
							| 
									
										
										
										
											2020-01-12 23:03:55 +01:00
										 |  |  |         this.$noteTitle.val(note.title); | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 11:03:34 +01:00
										 |  |  |         if (note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) { | 
					
						
							|  |  |  |             this.$noteTitle.prop("readonly", true); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 21:12:53 +01:00
										 |  |  |     async beforeNoteSwitchListener({tabId}) { | 
					
						
							|  |  |  |         if (this.isTab(tabId)) { | 
					
						
							|  |  |  |             await this.spacedUpdate.updateNowIfNecessary(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async beforeTabRemoveListener({tabId}) { | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  |         if (this.isTab(tabId)) { | 
					
						
							|  |  |  |             await this.spacedUpdate.updateNowIfNecessary(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  | } |