| 
									
										
										
										
											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"]'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-25 21:09:15 +01:00
										 |  |  | async function showDialogForClone() { | 
					
						
							|  |  |  |     showDialog('selected-to-current'); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function showDialog(linkType) { | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     glob.activeDialog = $dialog; | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-14 20:21:27 +01:00
										 |  |  |     if (noteDetailService.getActiveNoteType() === '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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-25 21:09:15 +01:00
										 |  |  |     if (linkType==='selected-to-current') { | 
					
						
							|  |  |  |         setLinkType(linkType); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-06 17:47:40 +01:00
										 |  |  |     $dialog.modal(); | 
					
						
							| 
									
										
										
										
											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-11-07 17:16:33 +01:00
										 |  |  |     noteAutocompleteService.initNoteAutocomplete($autoComplete); | 
					
						
							| 
									
										
										
										
											2018-07-26 16:24:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-07 17:16:33 +01:00
										 |  |  |     $autoComplete.on('autocomplete:selected', function(event, suggestion, dataset) { | 
					
						
							|  |  |  |         if (!suggestion.path) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-07 17:16:33 +01:00
										 |  |  |         const noteId = treeUtils.getNoteIdFromNotePath(suggestion.path); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-07 17:16:33 +01:00
										 |  |  |         if (noteId) { | 
					
						
							|  |  |  |             setDefaultLinkTitle(noteId); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-07 17:16:33 +01:00
										 |  |  |     $autoComplete.on('autocomplete:cursorchanged', function(event, suggestion, dataset) { | 
					
						
							|  |  |  |         const noteId = treeUtils.getNoteIdFromNotePath(suggestion.path); | 
					
						
							| 
									
										
										
										
											2018-07-26 16:24:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-07 17:16:33 +01:00
										 |  |  |         setDefaultLinkTitle(noteId); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-07-26 16:24:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-07 17:16:33 +01:00
										 |  |  |     noteAutocompleteService.showRecentNotes($autoComplete); | 
					
						
							| 
									
										
										
										
											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(() => { | 
					
						
							| 
									
										
										
										
											2018-11-07 17:16:33 +01:00
										 |  |  |     const notePath = $autoComplete.getSelectedPath(); | 
					
						
							| 
									
										
										
										
											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-11-06 17:47:40 +01:00
										 |  |  |             $dialog.modal('hide'); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-14 20:21:27 +01:00
										 |  |  |             cloningService.cloneNoteTo(noteId, noteDetailService.getActiveNoteId(), prefix); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-06 17:47:40 +01:00
										 |  |  |             $dialog.modal('hide'); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |         else if (linkType === 'current-to-selected') { | 
					
						
							|  |  |  |             const prefix = $clonePrefix.val(); | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-14 20:21:27 +01:00
										 |  |  |             cloningService.cloneNoteTo(noteDetailService.getActiveNoteId(), noteId, prefix); | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-06 17:47:40 +01:00
										 |  |  |             $dialog.modal('hide'); | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-11-07 17:16:33 +01:00
										 |  |  |     else { | 
					
						
							|  |  |  |         console.error("No path to add link."); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											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-03-25 11:09:17 -04:00
										 |  |  | $linkTypes.change(linkTypeChanged); | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-07 20:45:32 +01:00
										 |  |  | // return back focus to note text detail after quitting add link
 | 
					
						
							|  |  |  | // the problem is that cursor position is reset
 | 
					
						
							|  |  |  | $dialog.on("hidden.bs.modal", () => noteDetailText.focus()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | export default { | 
					
						
							| 
									
										
										
										
											2019-03-25 21:09:15 +01:00
										 |  |  |     showDialog, | 
					
						
							|  |  |  |     showDialogForClone | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | }; |