| 
									
										
										
										
											2017-11-04 19:38:50 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  | const addLink = (function() { | 
					
						
							| 
									
										
										
										
											2018-02-10 08:44:34 -05:00
										 |  |  |     const $dialog = $("#add-link-dialog"); | 
					
						
							|  |  |  |     const $form = $("#add-link-form"); | 
					
						
							|  |  |  |     const $autoComplete = $("#note-autocomplete"); | 
					
						
							|  |  |  |     const $linkTitle = $("#link-title"); | 
					
						
							|  |  |  |     const $clonePrefix = $("#clone-prefix"); | 
					
						
							|  |  |  |     const $linkTitleFormGroup = $("#add-link-title-form-group"); | 
					
						
							|  |  |  |     const $prefixFormGroup = $("#add-link-prefix-form-group"); | 
					
						
							|  |  |  |     const $linkTypes = $("input[name='add-link-type']"); | 
					
						
							|  |  |  |     const $linkTypeHtml = $linkTypes.filter('input[value="html"]'); | 
					
						
							| 
									
										
										
										
											2018-01-23 20:14:10 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     function setLinkType(linkType) { | 
					
						
							| 
									
										
										
										
											2018-02-10 08:44:34 -05:00
										 |  |  |         $linkTypes.each(function () { | 
					
						
							| 
									
										
										
										
											2018-01-23 20:14:10 -05:00
										 |  |  |             $(this).prop('checked', $(this).val() === linkType); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         linkTypeChanged(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     function showDialog() { | 
					
						
							| 
									
										
										
										
											2018-02-10 08:44:34 -05:00
										 |  |  |         glob.activeDialog = $dialog; | 
					
						
							| 
									
										
										
										
											2017-11-04 17:03:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-23 20:14:10 -05:00
										 |  |  |         if (noteEditor.getCurrentNoteType() === 'text') { | 
					
						
							| 
									
										
										
										
											2018-02-10 08:44:34 -05:00
										 |  |  |             $linkTypeHtml.prop('disabled', false); | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-23 20:14:10 -05:00
										 |  |  |             setLinkType('html'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2018-02-10 08:44:34 -05:00
										 |  |  |             $linkTypeHtml.prop('disabled', true); | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-23 20:14:10 -05:00
										 |  |  |             setLinkType('selected-to-current'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-10 08:44:34 -05:00
										 |  |  |         $dialog.dialog({ | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  |             modal: true, | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  |             width: 700 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-10 08:44:34 -05:00
										 |  |  |         $autoComplete.val('').focus(); | 
					
						
							|  |  |  |         $clonePrefix.val(''); | 
					
						
							|  |  |  |         $linkTitle.val(''); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  |         function setDefaultLinkTitle(noteId) { | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  |             const noteTitle = treeService.getNoteTitle(noteId); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-10 08:44:34 -05:00
										 |  |  |             $linkTitle.val(noteTitle); | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-10 08:44:34 -05:00
										 |  |  |         $autoComplete.autocomplete({ | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  |             source: treeService.getAutocompleteItems(), | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  |             minLength: 0, | 
					
						
							|  |  |  |             change: () => { | 
					
						
							| 
									
										
										
										
											2018-02-10 08:44:34 -05:00
										 |  |  |                 const val = $autoComplete.val(); | 
					
						
							| 
									
										
										
										
											2017-12-02 13:54:16 -05:00
										 |  |  |                 const notePath = link.getNodePathFromLabel(val); | 
					
						
							| 
									
										
										
										
											2018-01-23 20:14:10 -05:00
										 |  |  |                 if (!notePath) { | 
					
						
							|  |  |  |                     return; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-02 13:54:16 -05:00
										 |  |  |                 const noteId = treeUtils.getNoteIdFromNotePath(notePath); | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 if (noteId) { | 
					
						
							|  |  |  |                     setDefaultLinkTitle(noteId); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             // this is called when user goes through autocomplete list with keyboard
 | 
					
						
							| 
									
										
										
										
											2017-11-20 23:51:28 -05:00
										 |  |  |             // at this point the item isn't selected yet so we use supplied ui.item to see WHERE the cursor is
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  |             focus: (event, ui) => { | 
					
						
							| 
									
										
										
										
											2017-12-02 13:54:16 -05:00
										 |  |  |                 const notePath = link.getNodePathFromLabel(ui.item.value); | 
					
						
							|  |  |  |                 const noteId = treeUtils.getNoteIdFromNotePath(notePath); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-10 08:44:34 -05:00
										 |  |  |     $form.submit(() => { | 
					
						
							|  |  |  |         const value = $autoComplete.val(); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 22:31:30 -05:00
										 |  |  |         const notePath = link.getNodePathFromLabel(value); | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  |         const noteId = treeUtils.getNoteIdFromNotePath(notePath); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 22:31:30 -05:00
										 |  |  |         if (notePath) { | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  |             const linkType = $("input[name='add-link-type']:checked").val(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (linkType === 'html') { | 
					
						
							| 
									
										
										
										
											2018-02-10 08:44:34 -05:00
										 |  |  |                 const linkTitle = $linkTitle.val(); | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-10 08:44:34 -05:00
										 |  |  |                 $dialog.dialog("close"); | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 link.addLinkToEditor(linkTitle, '#' + notePath); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else if (linkType === 'selected-to-current') { | 
					
						
							| 
									
										
										
										
											2018-02-10 08:44:34 -05:00
										 |  |  |                 const prefix = $clonePrefix.val(); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 18:02:41 -05:00
										 |  |  |                 cloning.cloneNoteTo(noteId, noteEditor.getCurrentNoteId(), prefix); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-10 08:44:34 -05:00
										 |  |  |                 $dialog.dialog("close"); | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  |             } | 
					
						
							|  |  |  |             else if (linkType === 'current-to-selected') { | 
					
						
							| 
									
										
										
										
											2018-02-10 08:44:34 -05:00
										 |  |  |                 const prefix = $clonePrefix.val(); | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 18:02:41 -05:00
										 |  |  |                 cloning.cloneNoteTo(noteEditor.getCurrentNoteId(), noteId, prefix); | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-10 08:44:34 -05:00
										 |  |  |                 $dialog.dialog("close"); | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05: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
										 |  |  |         return false; | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-23 20:14:10 -05:00
										 |  |  |     function linkTypeChanged() { | 
					
						
							| 
									
										
										
										
											2018-02-10 08:44:34 -05:00
										 |  |  |         const value = $linkTypes.filter(":checked").val(); | 
					
						
							| 
									
										
										
										
											2018-01-23 20:14:10 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (value === 'html') { | 
					
						
							| 
									
										
										
										
											2018-02-10 08:44:34 -05:00
										 |  |  |             $linkTitleFormGroup.show(); | 
					
						
							|  |  |  |             $prefixFormGroup.hide(); | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2018-02-10 08:44:34 -05:00
										 |  |  |             $linkTitleFormGroup.hide(); | 
					
						
							|  |  |  |             $prefixFormGroup.show(); | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-01-23 20:14:10 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-10 08:44:34 -05:00
										 |  |  |     $linkTypes.change(linkTypeChanged); | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-18 23:41:13 -05:00
										 |  |  |     $(document).bind('keydown', 'ctrl+l', e => { | 
					
						
							|  |  |  |         showDialog(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         e.preventDefault(); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         showDialog | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | })(); |