| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  | $(document).bind('keydown', 'alt+l', () => { | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  |     $("#note-autocomplete").val(''); | 
					
						
							|  |  |  |     $("#link-title").val(''); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  |     const noteDetail = $('#note-detail'); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |     noteDetail.summernote('editor.saveRange'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  |     $("#insert-link-dialog").dialog({ | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |         modal: true, | 
					
						
							|  |  |  |         width: 500 | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-11 21:20:05 -04:00
										 |  |  |     function setDefaultLinkTitle(noteId) { | 
					
						
							| 
									
										
										
										
											2017-10-01 23:07:32 -04:00
										 |  |  |         const noteTitle = getNoteTitle(noteId); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  |         $("#link-title").val(noteTitle); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  |     $("#note-autocomplete").autocomplete({ | 
					
						
							| 
									
										
										
										
											2017-11-04 00:05:08 -04:00
										 |  |  |         source: getAutocompleteItems(glob.allNoteIds), | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |         minLength: 0, | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  |         change: () => { | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  |             const val = $("#note-autocomplete").val(); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |             const noteId = getNodeIdFromLabel(val); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (noteId) { | 
					
						
							| 
									
										
										
										
											2017-10-11 21:20:05 -04:00
										 |  |  |                 setDefaultLinkTitle(noteId); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |             } | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         // this is called when user goes through autocomplete list with keyboard
 | 
					
						
							|  |  |  |         // at this point the item isn't selected yet so we use supplied ui.item to see where the cursor is
 | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  |         focus: (event, ui) => { | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |             const noteId = getNodeIdFromLabel(ui.item.value); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             setDefaultLinkTitle(noteId); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-11 21:20:05 -04:00
										 |  |  | $("#insert-link-form").submit(() => { | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  |     let val = $("#note-autocomplete").val(); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const noteId = getNodeIdFromLabel(val); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (noteId) { | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  |         const linkTitle = $("#link-title").val(); | 
					
						
							|  |  |  |         const noteDetail = $('#note-detail'); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  |         $("#insert-link-dialog").dialog("close"); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         noteDetail.summernote('editor.restoreRange'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         noteDetail.summernote('createLink', { | 
					
						
							| 
									
										
										
										
											2017-10-11 21:20:05 -04:00
										 |  |  |             text: linkTitle, | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |             url: 'app#' + noteId, | 
					
						
							|  |  |  |             isNewWindow: true | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // when click on link popup, in case of internal link, just go the the referenced note instead of default behavior
 | 
					
						
							|  |  |  | // of opening the link in new window/tab
 | 
					
						
							| 
									
										
										
										
											2017-11-03 23:00:35 -04:00
										 |  |  | $(document).on('click', 'div.popover-content a, div.ui-tooltip-content', goToInternalNote); | 
					
						
							|  |  |  | $(document).on('dblclick', '.note-editable a, div.ui-tooltip-content', goToInternalNote); | 
					
						
							| 
									
										
										
										
											2017-10-05 20:08:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-26 23:23:03 -04:00
										 |  |  | function goToInternalNote(e, callback) { | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |     const targetUrl = $(e.target).attr("href"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-11 20:37:27 -04:00
										 |  |  |     const noteId = getNoteIdFromLink(targetUrl); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-11 20:37:27 -04:00
										 |  |  |     if (noteId !== null) { | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |         getNodeByKey(noteId).setActive(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-11 21:20:05 -04:00
										 |  |  |         // this is quite ugly hack, but it seems like we can't close the tooltip otherwise
 | 
					
						
							|  |  |  |         $("[role='tooltip']").remove(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |         e.preventDefault(); | 
					
						
							| 
									
										
										
										
											2017-09-26 23:23:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (callback) { | 
					
						
							|  |  |  |             callback(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-09-26 23:23:03 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-11 21:20:05 -04:00
										 |  |  | function getNoteIdFromLink(url) { | 
					
						
							| 
									
										
										
										
											2017-10-28 12:12:20 -04:00
										 |  |  |     const noteIdMatch = /app#([A-Za-z0-9]{12})/.exec(url); | 
					
						
							| 
									
										
										
										
											2017-10-11 21:20:05 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (noteIdMatch === null) { | 
					
						
							|  |  |  |         return null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         return noteIdMatch[1]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | function getNodeIdFromLabel(label) { | 
					
						
							| 
									
										
										
										
											2017-10-28 12:12:20 -04:00
										 |  |  |     const noteIdMatch = / \(([A-Za-z0-9]{12})\)/.exec(label); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (noteIdMatch !== null) { | 
					
						
							|  |  |  |         return noteIdMatch[1]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return null; | 
					
						
							|  |  |  | } |