| 
									
										
										
										
											2017-11-04 19:38:50 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 13:02:39 -04:00
										 |  |  | import treeService from '../services/tree_service.js'; | 
					
						
							|  |  |  | import cloning from '../services/cloning.js'; | 
					
						
							|  |  |  | import link from '../services/link.js'; | 
					
						
							|  |  |  | import noteDetail from '../services/note_detail.js'; | 
					
						
							|  |  |  | import treeUtils from '../services/tree_utils.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"); | 
					
						
							|  |  |  | 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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:02:39 -04:00
										 |  |  |     if (noteDetail.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, | 
					
						
							|  |  |  |         width: 700 | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											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-03-25 11:09:17 -04:00
										 |  |  |     function setDefaultLinkTitle(noteId) { | 
					
						
							|  |  |  |         const noteTitle = treeService.getNoteTitle(noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $linkTitle.val(noteTitle); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     $autoComplete.autocomplete({ | 
					
						
							|  |  |  |         source: await treeService.getAutocompleteItems(), | 
					
						
							|  |  |  |         minLength: 0, | 
					
						
							|  |  |  |         change: () => { | 
					
						
							|  |  |  |             const val = $autoComplete.val(); | 
					
						
							|  |  |  |             const notePath = link.getNodePathFromLabel(val); | 
					
						
							|  |  |  |             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) { | 
					
						
							| 
									
										
										
										
											2017-10-11 21:20:05 -04:00
										 |  |  |                 setDefaultLinkTitle(noteId); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  |         focus: (event, ui) => { | 
					
						
							|  |  |  |             const notePath = link.getNodePathFromLabel(ui.item.value); | 
					
						
							|  |  |  |             const noteId = treeUtils.getNoteIdFromNotePath(notePath); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             setDefaultLinkTitle(noteId); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											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-03-25 11:09:17 -04:00
										 |  |  |     const notePath = link.getNodePathFromLabel(value); | 
					
						
							|  |  |  |     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-03-25 11:09:17 -04:00
										 |  |  |             link.addLinkToEditor(linkTitle, '#' + notePath); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (linkType === 'selected-to-current') { | 
					
						
							|  |  |  |             const prefix = $clonePrefix.val(); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 13:02:39 -04:00
										 |  |  |             cloning.cloneNoteTo(noteId, noteDetail.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:02:39 -04:00
										 |  |  |             cloning.cloneNoteTo(noteDetail.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-03-25 11:09:17 -04:00
										 |  |  | function linkTypeChanged() { | 
					
						
							|  |  |  |     const value = $linkTypes.filter(":checked").val(); | 
					
						
							| 
									
										
										
										
											2018-01-23 20:14:10 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     if (value === 'html') { | 
					
						
							|  |  |  |         $linkTitleFormGroup.show(); | 
					
						
							|  |  |  |         $prefixFormGroup.hide(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         $linkTitleFormGroup.hide(); | 
					
						
							|  |  |  |         $prefixFormGroup.show(); | 
					
						
							| 
									
										
										
										
											2018-01-23 20:14:10 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | $(document).bind('keydown', 'ctrl+l', e => { | 
					
						
							|  |  |  |     showDialog(); | 
					
						
							| 
									
										
										
										
											2017-12-18 23:41:13 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     e.preventDefault(); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2017-11-04 14:43:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | export default { | 
					
						
							|  |  |  |     showDialog | 
					
						
							|  |  |  | }; |