| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  | import cloningService from '../services/cloning.js'; | 
					
						
							|  |  |  | import linkService from '../services/link.js'; | 
					
						
							|  |  |  | import noteDetailService from '../services/note_detail.js'; | 
					
						
							| 
									
										
										
										
											2018-03-25 13:02:39 -04:00
										 |  |  | import treeUtils from '../services/tree_utils.js'; | 
					
						
							| 
									
										
										
										
											2018-05-26 10:04:40 -04:00
										 |  |  | import noteDetailText from "../services/note_detail_text.js"; | 
					
						
							| 
									
										
										
										
											2018-08-16 21:02:42 +02:00
										 |  |  | import noteAutocompleteService from "../services/note_autocomplete.js"; | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04: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"); | 
					
						
							| 
									
										
										
										
											2018-05-26 10:04:40 -04:00
										 |  |  | const $linkTypeDiv = $("#add-link-type-div"); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | const $linkTypes = $("input[name='add-link-type']"); | 
					
						
							|  |  |  | const $linkTypeHtml = $linkTypes.filter('input[value="html"]'); | 
					
						
							| 
									
										
										
										
											2018-08-14 17:36:39 +02:00
										 |  |  | const $showRecentNotesButton = $dialog.find(".show-recent-notes-button"); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | function setLinkType(linkType) { | 
					
						
							|  |  |  |     $linkTypes.each(function () { | 
					
						
							|  |  |  |         $(this).prop('checked', $(this).val() === linkType); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     linkTypeChanged(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-11-04 17:03:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | async function showDialog() { | 
					
						
							|  |  |  |     glob.activeDialog = $dialog; | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  |     if (noteDetailService.getCurrentNoteType() === 'text') { | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         $linkTypeHtml.prop('disabled', false); | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         setLinkType('html'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         $linkTypeHtml.prop('disabled', true); | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         setLinkType('selected-to-current'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     $dialog.dialog({ | 
					
						
							|  |  |  |         modal: true, | 
					
						
							| 
									
										
										
										
											2018-11-05 21:12:03 +01:00
										 |  |  |         width: 800 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     $autoComplete.val('').focus(); | 
					
						
							|  |  |  |     $clonePrefix.val(''); | 
					
						
							|  |  |  |     $linkTitle.val(''); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-12 20:03:23 -04:00
										 |  |  |     async function setDefaultLinkTitle(noteId) { | 
					
						
							|  |  |  |         const noteTitle = await treeUtils.getNoteTitle(noteId); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $linkTitle.val(noteTitle); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-26 16:24:08 +02:00
										 |  |  |     await $autoComplete.autocomplete({ | 
					
						
							| 
									
										
										
										
											2018-08-16 21:02:42 +02:00
										 |  |  |         source: noteAutocompleteService.autocompleteSource, | 
					
						
							| 
									
										
										
										
											2018-07-26 16:24:08 +02:00
										 |  |  |         minLength: 0, | 
					
						
							|  |  |  |         change: async (event, ui) => { | 
					
						
							| 
									
										
										
										
											2018-07-27 09:22:25 +02:00
										 |  |  |             if (!ui.item) { | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 17:59:55 +02:00
										 |  |  |             const notePath = linkService.getNotePathFromLabel(ui.item.value); | 
					
						
							| 
									
										
										
										
											2018-07-26 16:24:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |             if (!notePath) { | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const noteId = treeUtils.getNoteIdFromNotePath(notePath); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |             if (noteId) { | 
					
						
							| 
									
										
										
										
											2018-04-12 20:03:23 -04:00
										 |  |  |                 await setDefaultLinkTitle(noteId); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2018-07-26 16:24:08 +02:00
										 |  |  |         select: function (event, ui) { | 
					
						
							|  |  |  |             if (ui.item.value === 'No results') { | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -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
 | 
					
						
							| 
									
										
										
										
											2018-04-12 20:03:23 -04:00
										 |  |  |         focus: async (event, ui) => { | 
					
						
							| 
									
										
										
										
											2018-07-28 17:59:55 +02:00
										 |  |  |             const notePath = linkService.getNotePathFromLabel(ui.item.value); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |             const noteId = treeUtils.getNoteIdFromNotePath(notePath); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-12 20:03:23 -04:00
										 |  |  |             await setDefaultLinkTitle(noteId); | 
					
						
							| 
									
										
										
										
											2018-07-26 16:24:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             event.preventDefault(); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-07-26 16:24:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-27 09:22:25 +02:00
										 |  |  |     showRecentNotes(); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | $form.submit(() => { | 
					
						
							|  |  |  |     const value = $autoComplete.val(); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 17:59:55 +02:00
										 |  |  |     const notePath = linkService.getNotePathFromLabel(value); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     const noteId = treeUtils.getNoteIdFromNotePath(notePath); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     if (notePath) { | 
					
						
							|  |  |  |         const linkType = $("input[name='add-link-type']:checked").val(); | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         if (linkType === 'html') { | 
					
						
							|  |  |  |             const linkTitle = $linkTitle.val(); | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |             $dialog.dialog("close"); | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-26 10:04:40 -04:00
										 |  |  |             const linkHref = '#' + notePath; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (hasSelection()) { | 
					
						
							|  |  |  |                 const editor = noteDetailText.getEditor(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 editor.execute('link', linkHref); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 linkService.addLinkToEditor(linkTitle, linkHref); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |         else if (linkType === 'selected-to-current') { | 
					
						
							|  |  |  |             const prefix = $clonePrefix.val(); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  |             cloningService.cloneNoteTo(noteId, noteDetailService.getCurrentNoteId(), prefix); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |             $dialog.dialog("close"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (linkType === 'current-to-selected') { | 
					
						
							|  |  |  |             const prefix = $clonePrefix.val(); | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  |             cloningService.cloneNoteTo(noteDetailService.getCurrentNoteId(), noteId, prefix); | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |             $dialog.dialog("close"); | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     return false; | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-26 10:04:40 -04:00
										 |  |  | // returns true if user selected some text, false if there's no selection
 | 
					
						
							|  |  |  | function hasSelection() { | 
					
						
							|  |  |  |     const model = noteDetailText.getEditor().model; | 
					
						
							|  |  |  |     const selection = model.document.selection; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return !selection.isCollapsed; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | function linkTypeChanged() { | 
					
						
							|  |  |  |     const value = $linkTypes.filter(":checked").val(); | 
					
						
							| 
									
										
										
										
											2018-01-23 20:14:10 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-26 10:04:40 -04:00
										 |  |  |     $linkTitleFormGroup.toggle(!hasSelection() && value === 'html'); | 
					
						
							|  |  |  |     $prefixFormGroup.toggle(!hasSelection() && value !== 'html'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $linkTypeDiv.toggle(!hasSelection()); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-23 20:14:10 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-27 09:22:25 +02:00
										 |  |  | function showRecentNotes() { | 
					
						
							|  |  |  |     $autoComplete.autocomplete("search", ""); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | $linkTypes.change(linkTypeChanged); | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-27 09:22:25 +02:00
										 |  |  | $showRecentNotesButton.click(showRecentNotes); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | export default { | 
					
						
							|  |  |  |     showDialog | 
					
						
							|  |  |  | }; |