| 
									
										
										
										
											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 20:18:02 +01:00
										 |  |  | import server from "../services/server.js"; | 
					
						
							| 
									
										
										
										
											2020-01-19 21:40:23 +01:00
										 |  |  | import SpacedUpdate from "../services/spaced_update.js"; | 
					
						
							| 
									
										
										
										
											2021-02-27 23:39:02 +01:00
										 |  |  | import appContext from "../services/app_context.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 { | 
					
						
							| 
									
										
										
										
											2021-02-13 20:07:08 +01:00
										 |  |  |         font-size: 180%; | 
					
						
							| 
									
										
										
										
											2020-01-14 20:27:40 +01:00
										 |  |  |         border: 0; | 
					
						
							| 
									
										
										
										
											2020-01-19 18:05:06 +01:00
										 |  |  |         min-width: 5em; | 
					
						
							|  |  |  |         width: 100%; | 
					
						
							| 
									
										
										
										
											2020-01-14 20:27:40 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-29 00:01:07 +02:00
										 |  |  |      | 
					
						
							|  |  |  |     .note-title-container input.note-title.protected { | 
					
						
							|  |  |  |         text-shadow: 4px 4px 4px var(--muted-text-color); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-14 20:27:40 +01:00
										 |  |  |     </style> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 23:05:19 +01:00
										 |  |  |     <input autocomplete="off" value="" placeholder="type note's title here..." class="note-title" tabindex="100"> | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  | </div>`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default class NoteTitleWidget extends TabAwareWidget { | 
					
						
							| 
									
										
										
										
											2020-02-27 10:03:14 +01:00
										 |  |  |     constructor() { | 
					
						
							|  |  |  |         super(); | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         this.spacedUpdate = new SpacedUpdate(async () => { | 
					
						
							|  |  |  |             const title = this.$noteTitle.val(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-25 11:09:07 +02:00
										 |  |  |             protectedSessionHolder.touchProtectedSessionIfNecessary(this.note); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-23 22:46:18 +01:00
										 |  |  |             await server.put(`notes/${this.noteId}/change-title`, {title}, this.componentId); | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2021-02-27 23:39:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         appContext.addBeforeUnloadListener(this); | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 20:15:05 +01:00
										 |  |  |     doRender() { | 
					
						
							| 
									
										
										
										
											2020-01-14 20:27:40 +01:00
										 |  |  |         this.$widget = $(TPL); | 
					
						
							| 
									
										
										
										
											2021-02-13 20:07:08 +01:00
										 |  |  |         this.contentSized(); | 
					
						
							| 
									
										
										
										
											2020-01-14 20:27:40 +01:00
										 |  |  |         this.$noteTitle = this.$widget.find(".note-title"); | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-08 20:53:07 +01:00
										 |  |  |         this.$noteTitle.on('input', () => this.spacedUpdate.scheduleUpdate()); | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 19:33:35 +01:00
										 |  |  |         utils.bindElShortcut(this.$noteTitle, 'return', () => { | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  |             this.triggerCommand('focusOnAttributes', {ntxId: this.noteContext.ntxId}); | 
					
						
							| 
									
										
										
										
											2020-01-19 19:33:35 +01:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-02-01 22:29:32 +01:00
										 |  |  |         this.$noteTitle.prop("readonly", note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()); | 
					
						
							| 
									
										
										
										
											2020-04-29 17:29:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-03 22:49:20 +02:00
										 |  |  |         this.setProtectedStatus(note); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     setProtectedStatus(note) { | 
					
						
							| 
									
										
										
										
											2020-04-29 00:01:07 +02:00
										 |  |  |         this.$noteTitle.toggleClass("protected", !!note.isProtected); | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  |     async beforeNoteSwitchEvent({noteContext}) { | 
					
						
							|  |  |  |         if (this.isTab(noteContext.ntxId)) { | 
					
						
							| 
									
										
										
										
											2020-01-19 21:12:53 +01:00
										 |  |  |             await this.spacedUpdate.updateNowIfNecessary(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  |     async beforeTabRemoveEvent({ntxIds}) { | 
					
						
							|  |  |  |         if (this.isTab(ntxIds)) { | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  |             await this.spacedUpdate.updateNowIfNecessary(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-24 17:54:47 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 19:23:49 +01:00
										 |  |  |     focusOnTitleEvent() { | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  |         if (this.noteContext && this.noteContext.isActive()) { | 
					
						
							| 
									
										
										
										
											2020-01-24 17:54:47 +01:00
										 |  |  |             this.$noteTitle.trigger('focus'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-17 21:39:26 +01:00
										 |  |  |     focusAndSelectTitleEvent() { | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  |         if (this.noteContext && this.noteContext.isActive()) { | 
					
						
							| 
									
										
										
										
											2020-01-24 17:54:47 +01:00
										 |  |  |             this.$noteTitle | 
					
						
							|  |  |  |                 .trigger('focus') | 
					
						
							|  |  |  |                 .trigger('select'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-02 10:41:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-29 00:01:07 +02:00
										 |  |  |     entitiesReloadedEvent({loadResults}) { | 
					
						
							|  |  |  |         if (loadResults.isNoteReloaded(this.noteId)) { | 
					
						
							| 
									
										
										
										
											2020-05-03 22:49:20 +02:00
										 |  |  |             // not updating the title specifically since the synced title might be older than what the user is currently typing
 | 
					
						
							|  |  |  |             this.setProtectedStatus(this.note); | 
					
						
							| 
									
										
										
										
											2020-04-29 00:01:07 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-12-04 22:57:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (loadResults.isNoteReloaded(this.noteId, this.componentId)) { | 
					
						
							|  |  |  |             this.refresh(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-04-29 00:01:07 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 19:23:49 +01:00
										 |  |  |     beforeUnloadEvent() { | 
					
						
							| 
									
										
										
										
											2021-02-27 23:39:02 +01:00
										 |  |  |         return this.spacedUpdate.isAllSavedAndTriggerUpdate(); | 
					
						
							| 
									
										
										
										
											2020-02-02 10:41:43 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-06-14 14:30:57 +02:00
										 |  |  | } |