| 
									
										
										
										
											2017-11-04 19:38:50 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  | const addLink = (function() { | 
					
						
							|  |  |  |     const dialogEl = $("#insert-link-dialog"); | 
					
						
							|  |  |  |     const formEl = $("#insert-link-form"); | 
					
						
							|  |  |  |     const autoCompleteEl = $("#note-autocomplete"); | 
					
						
							|  |  |  |     const noteDetailEl = $('#note-detail'); | 
					
						
							|  |  |  |     const linkTitleEl = $("#link-title"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function showDialog() { | 
					
						
							| 
									
										
										
										
											2017-11-04 17:03:15 -04:00
										 |  |  |         glob.activeDialog = dialogEl; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  |         noteDetailEl.summernote('editor.saveRange'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         dialogEl.dialog({ | 
					
						
							|  |  |  |             modal: true, | 
					
						
							|  |  |  |             width: 500 | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  |         autoCompleteEl.val('').focus(); | 
					
						
							|  |  |  |         linkTitleEl.val(''); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  |         function setDefaultLinkTitle(noteId) { | 
					
						
							| 
									
										
										
										
											2017-11-04 22:18:36 -04:00
										 |  |  |             const noteTitle = treeUtils.getNoteTitle(noteId); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  |             linkTitleEl.val(noteTitle); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  |         autoCompleteEl.autocomplete({ | 
					
						
							|  |  |  |             source: getAutocompleteItems(glob.allNoteIds), | 
					
						
							|  |  |  |             minLength: 0, | 
					
						
							|  |  |  |             change: () => { | 
					
						
							|  |  |  |                 const val = autoCompleteEl.val(); | 
					
						
							| 
									
										
										
										
											2017-11-04 17:07:03 -04:00
										 |  |  |                 const noteId = link.getNodeIdFromLabel(val); | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 if (noteId) { | 
					
						
							|  |  |  |                     setDefaultLinkTitle(noteId); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             // 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
 | 
					
						
							|  |  |  |             focus: (event, ui) => { | 
					
						
							| 
									
										
										
										
											2017-11-04 17:07:03 -04:00
										 |  |  |                 const noteId = link.getNodeIdFromLabel(ui.item.value); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-11 21:20:05 -04:00
										 |  |  |                 setDefaultLinkTitle(noteId); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  |     formEl.submit(() => { | 
					
						
							|  |  |  |         let val = autoCompleteEl.val(); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 17:07:03 -04:00
										 |  |  |         const noteId = link.getNodeIdFromLabel(val); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  |         if (noteId) { | 
					
						
							|  |  |  |             const linkTitle = linkTitleEl.val(); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  |             dialogEl.dialog("close"); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  |             noteDetailEl.summernote('editor.restoreRange'); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  |             noteDetailEl.summernote('createLink', { | 
					
						
							|  |  |  |                 text: linkTitle, | 
					
						
							|  |  |  |                 url: 'app#' + noteId, | 
					
						
							|  |  |  |                 isNewWindow: true | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  |         return false; | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  |     $(document).bind('keydown', 'alt+l', showDialog); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         showDialog | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | })(); |