| 
									
										
										
										
											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'); | 
					
						
							| 
									
										
										
										
											2017-12-09 14:11:35 -05:00
										 |  |  |     const jumpToButtonEl = $('#recent-notes-jump-to'); | 
					
						
							|  |  |  |     const addLinkButtonEl = $('#recent-notes-add-link'); | 
					
						
							| 
									
										
										
										
											2017-11-19 16:35:35 -05:00
										 |  |  |     const addCurrentAsChildEl = $("#recent-notes-add-current-as-child"); | 
					
						
							|  |  |  |     const addRecentAsChildEl = $("#recent-notes-add-recent-as-child"); | 
					
						
							| 
									
										
										
										
											2017-11-04 17:03:15 -04:00
										 |  |  |     const noteDetailEl = $('#note-detail'); | 
					
						
							| 
									
										
										
										
											2017-12-03 10:06:53 -05:00
										 |  |  |     // list of recent note paths
 | 
					
						
							| 
									
										
										
										
											2017-11-04 13:39:26 -04:00
										 |  |  |     let list = []; | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-03 10:42:23 -05:00
										 |  |  |     async function reload() { | 
					
						
							|  |  |  |         const result = await server.get('recent-notes'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-03 10:06:53 -05:00
										 |  |  |         list = result.map(r => r.note_path); | 
					
						
							| 
									
										
										
										
											2017-12-03 10:42:23 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-04 23:46:50 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-03 10:06:53 -05:00
										 |  |  |     function addRecentNote(noteTreeId, notePath) { | 
					
						
							| 
									
										
										
										
											2017-11-28 20:52:38 -05:00
										 |  |  |         setTimeout(async () => { | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |             // we include the note into recent list only if the user stayed on the note at least 5 seconds
 | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:53 -05:00
										 |  |  |             if (notePath && notePath === noteTree.getCurrentNotePath()) { | 
					
						
							| 
									
										
										
										
											2017-12-03 10:06:53 -05:00
										 |  |  |                 const result = await server.put('recent-notes/' + noteTreeId + '/' + encodeURIComponent(notePath)); | 
					
						
							| 
									
										
										
										
											2017-11-28 20:52:38 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 list = result.map(r => r.note_path); | 
					
						
							| 
									
										
										
										
											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 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
										 |  |  |         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-19 12:06:48 -05:00
										 |  |  |         const recNotes = list.filter(note => note !== noteTree.getCurrentNotePath()); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 12:06:48 -05:00
										 |  |  |         $.each(recNotes, (key, valueNotePath) => { | 
					
						
							| 
									
										
										
										
											2017-11-29 22:03:03 -05:00
										 |  |  |             const noteTitle = noteTree.getNotePathTitle(valueNotePath); | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             const option = $("<option></option>") | 
					
						
							| 
									
										
										
										
											2017-11-19 12:06:48 -05:00
										 |  |  |                 .attr("value", valueNotePath) | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |                 .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-19 16:35:35 -05:00
										 |  |  |     function getSelectedNotePath() { | 
					
						
							| 
									
										
										
										
											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-22 23:16:54 -05:00
										 |  |  |     function getSelectedNoteId() { | 
					
						
							|  |  |  |         const notePath = getSelectedNotePath(); | 
					
						
							|  |  |  |         return treeUtils.getNoteIdFromNotePath(notePath); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |     function setActiveNoteBasedOnRecentNotes() { | 
					
						
							| 
									
										
										
										
											2017-11-19 16:35:35 -05:00
										 |  |  |         const notePath = getSelectedNotePath(); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 12:06:48 -05:00
										 |  |  |         noteTree.activateNode(notePath); | 
					
						
							| 
									
										
										
										
											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() { | 
					
						
							| 
									
										
										
										
											2017-11-19 16:35:35 -05:00
										 |  |  |         const notePath = getSelectedNotePath(); | 
					
						
							| 
									
										
										
										
											2017-11-29 22:03:03 -05:00
										 |  |  |         const noteId = treeUtils.getNoteIdFromNotePath(notePath); | 
					
						
							| 
									
										
										
										
											2017-10-01 23:07:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 22:03:03 -05:00
										 |  |  |         const linkTitle = noteTree.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-12-09 14:11:35 -05:00
										 |  |  |         link.addLinkToEditor(linkTitle, '#' + notePath); | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-01 23:07:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 22:31:30 -05:00
										 |  |  |     async function addCurrentAsChild() { | 
					
						
							| 
									
										
										
										
											2017-11-26 19:56:08 -05:00
										 |  |  |         await treeChanges.cloneNoteTo(noteEditor.getCurrentNoteId(), getSelectedNoteId()); | 
					
						
							| 
									
										
										
										
											2017-11-19 16:35:35 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         dialogEl.dialog("close"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function addRecentAsChild() { | 
					
						
							| 
									
										
										
										
											2017-11-26 19:56:08 -05:00
										 |  |  |         await treeChanges.cloneNoteTo(getSelectedNoteId(), noteEditor.getCurrentNoteId()); | 
					
						
							| 
									
										
										
										
											2017-11-19 22:31:30 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         dialogEl.dialog("close"); | 
					
						
							| 
									
										
										
										
											2017-11-19 16:35:35 -05: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-19 16:35:35 -05:00
										 |  |  |         // to get keycodes use http://keycode.info/
 | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |         if (key === 13)// the enter key code
 | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             setActiveNoteBasedOnRecentNotes(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (key === 76 /* l */) { | 
					
						
							|  |  |  |             addLinkBasedOnRecentNotes(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-19 16:35:35 -05:00
										 |  |  |         else if (key === 67 /* c */) { | 
					
						
							|  |  |  |             addCurrentAsChild(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (key === 82 /* r */) { | 
					
						
							|  |  |  |             addRecentAsChild() | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |         else { | 
					
						
							|  |  |  |             return; // avoid prevent default
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         e.preventDefault(); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-10-01 23:07:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-03 10:42:23 -05:00
										 |  |  |     reload(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-18 23:41:13 -05:00
										 |  |  |     $(document).bind('keydown', 'ctrl+e', e => { | 
					
						
							|  |  |  |         showDialog(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         e.preventDefault(); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-11-04 13:39:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-11-19 16:35:35 -05:00
										 |  |  |     addCurrentAsChildEl.click(addCurrentAsChild); | 
					
						
							|  |  |  |     addRecentAsChildEl.click(addRecentAsChild); | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2017-12-03 10:42:23 -05:00
										 |  |  |         addRecentNote, | 
					
						
							|  |  |  |         reload | 
					
						
							| 
									
										
										
										
											2017-11-04 13:21:41 -04:00
										 |  |  |     }; | 
					
						
							|  |  |  | })(); |