| 
									
										
										
										
											2017-11-04 19:38:50 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 13:39:26 -04:00
										 |  |  | const recentNotes = (function() { | 
					
						
							| 
									
										
										
										
											2017-11-04 17:03:15 -04:00
										 |  |  |     const dialogEl = $("#recent-notes-dialog"); | 
					
						
							|  |  |  |     const selectBoxEl = $('#recent-notes-select-box'); | 
					
						
							|  |  |  |     const jumpToButtonEl = $('#recentNotesJumpTo'); | 
					
						
							|  |  |  |     const addLinkButtonEl = $('#recentNotesAddLink'); | 
					
						
							|  |  |  |     const noteDetailEl = $('#note-detail'); | 
					
						
							| 
									
										
										
										
											2017-11-04 13:39:26 -04:00
										 |  |  |     let list = []; | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 23:46:50 -04:00
										 |  |  |     $.ajax({ | 
					
						
							|  |  |  |         url: baseApiUrl + 'recent-notes', | 
					
						
							|  |  |  |         type: 'GET', | 
					
						
							| 
									
										
										
										
											2017-11-08 22:33:08 -05:00
										 |  |  |         error: () => showError("Error getting recent notes.") | 
					
						
							| 
									
										
										
										
											2017-11-04 23:46:50 -04:00
										 |  |  |     }).then(result => { | 
					
						
							|  |  |  |         list = result.map(r => r.note_id); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |     function addRecentNote(noteTreeId, noteContentId) { | 
					
						
							|  |  |  |         setTimeout(() => { | 
					
						
							|  |  |  |             // we include the note into recent list only if the user stayed on the note at least 5 seconds
 | 
					
						
							| 
									
										
										
										
											2017-11-04 17:54:27 -04:00
										 |  |  |             if (noteTreeId === noteEditor.getCurrentNoteId() || noteContentId === noteEditor.getCurrentNoteId()) { | 
					
						
							| 
									
										
										
										
											2017-11-04 23:46:50 -04:00
										 |  |  |                 $.ajax({ | 
					
						
							|  |  |  |                     url: baseApiUrl + 'recent-notes/' + noteTreeId, | 
					
						
							|  |  |  |                     type: 'PUT', | 
					
						
							| 
									
										
										
										
											2017-11-08 22:33:08 -05:00
										 |  |  |                     error: () => showError("Error setting recent notes.") | 
					
						
							| 
									
										
										
										
											2017-11-04 23:46:50 -04:00
										 |  |  |                 }).then(result => { | 
					
						
							|  |  |  |                     list = result.map(r => r.note_id); | 
					
						
							|  |  |  |                 }); | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |             } | 
					
						
							|  |  |  |         }, 1500); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 13:39:26 -04:00
										 |  |  |     function removeRecentNote(noteIdToRemove) { | 
					
						
							| 
									
										
										
										
											2017-11-04 23:46:50 -04:00
										 |  |  |         $.ajax({ | 
					
						
							|  |  |  |             url: baseApiUrl + 'recent-notes/' + noteIdToRemove, | 
					
						
							|  |  |  |             type: 'DELETE', | 
					
						
							| 
									
										
										
										
											2017-11-08 22:33:08 -05:00
										 |  |  |             error: () => showError("Error removing note from recent notes.") | 
					
						
							| 
									
										
										
										
											2017-11-04 23:46:50 -04:00
										 |  |  |         }).then(result => { | 
					
						
							|  |  |  |             list = result.map(r => r.note_id); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-11-04 13:39:26 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function showDialog() { | 
					
						
							| 
									
										
										
										
											2017-11-04 17:03:15 -04:00
										 |  |  |         glob.activeDialog = dialogEl; | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 17:03:15 -04:00
										 |  |  |         noteDetailEl.summernote('editor.saveRange'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         dialogEl.dialog({ | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |             modal: true, | 
					
						
							|  |  |  |             width: 800 | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 17:03:15 -04:00
										 |  |  |         selectBoxEl.find('option').remove(); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |         // remove the current note
 | 
					
						
							| 
									
										
										
										
											2017-11-04 17:54:27 -04:00
										 |  |  |         const recNotes = list.filter(note => note !== noteEditor.getCurrentNoteId()); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |         $.each(recNotes, (key, valueNoteId) => { | 
					
						
							| 
									
										
										
										
											2017-11-04 22:18:36 -04:00
										 |  |  |             const noteTitle = treeUtils.getFullName(valueNoteId); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |             if (!noteTitle) { | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const option = $("<option></option>") | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |                 .attr("value", valueNoteId) | 
					
						
							|  |  |  |                 .text(noteTitle); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |             // select the first one (most recent one) by default
 | 
					
						
							|  |  |  |             if (key === 0) { | 
					
						
							|  |  |  |                 option.attr("selected", "selected"); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 17:03:15 -04:00
										 |  |  |             selectBoxEl.append(option); | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-06 22:46:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |     function getSelectedNoteIdFromRecentNotes() { | 
					
						
							| 
									
										
										
										
											2017-11-04 17:03:15 -04:00
										 |  |  |         return selectBoxEl.find("option:selected").val(); | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-01 23:07:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |     function setActiveNoteBasedOnRecentNotes() { | 
					
						
							|  |  |  |         const noteId = getSelectedNoteIdFromRecentNotes(); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 22:18:36 -04:00
										 |  |  |         treeUtils.activateNode(noteId); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 17:03:15 -04:00
										 |  |  |         dialogEl.dialog('close'); | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |     function addLinkBasedOnRecentNotes() { | 
					
						
							|  |  |  |         const noteId = getSelectedNoteIdFromRecentNotes(); | 
					
						
							| 
									
										
										
										
											2017-10-01 23:07:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 22:18:36 -04:00
										 |  |  |         const linkTitle = treeUtils.getNoteTitle(noteId); | 
					
						
							| 
									
										
										
										
											2017-10-01 23:07:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 17:03:15 -04:00
										 |  |  |         dialogEl.dialog("close"); | 
					
						
							| 
									
										
										
										
											2017-10-01 23:07:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 17:03:15 -04:00
										 |  |  |         noteDetailEl.summernote('editor.restoreRange'); | 
					
						
							| 
									
										
										
										
											2017-10-01 23:07:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 17:03:15 -04:00
										 |  |  |         noteDetailEl.summernote('createLink', { | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |             text: linkTitle, | 
					
						
							|  |  |  |             url: 'app#' + noteId, | 
					
						
							|  |  |  |             isNewWindow: true | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-01 23:07:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 17:03:15 -04:00
										 |  |  |     selectBoxEl.keydown(e => { | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |         const key = e.which; | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |         if (key === 13)// the enter key code
 | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             setActiveNoteBasedOnRecentNotes(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (key === 76 /* l */) { | 
					
						
							|  |  |  |             addLinkBasedOnRecentNotes(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             return; // avoid prevent default
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         e.preventDefault(); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-10-01 23:07:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 13:39:26 -04:00
										 |  |  |     $(document).bind('keydown', 'alt+q', showDialog); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 17:03:15 -04:00
										 |  |  |     selectBoxEl.dblclick(e => { | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |         setActiveNoteBasedOnRecentNotes(); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 17:03:15 -04:00
										 |  |  |     jumpToButtonEl.click(setActiveNoteBasedOnRecentNotes); | 
					
						
							|  |  |  |     addLinkButtonEl.click(addLinkBasedOnRecentNotes); | 
					
						
							| 
									
										
										
										
											2017-10-01 23:07:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |     return { | 
					
						
							| 
									
										
										
										
											2017-11-04 13:39:26 -04:00
										 |  |  |         showDialog, | 
					
						
							|  |  |  |         addRecentNote, | 
					
						
							|  |  |  |         removeRecentNote | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |     }; | 
					
						
							|  |  |  | })(); |