| 
									
										
										
										
											2019-11-11 22:57:51 +01:00
										 |  |  | import noteAutocompleteService from "../services/note_autocomplete.js"; | 
					
						
							|  |  |  | import utils from "../services/utils.js"; | 
					
						
							|  |  |  | import toastService from "../services/toast.js"; | 
					
						
							| 
									
										
										
										
											2021-04-16 23:01:56 +02:00
										 |  |  | import froca from "../services/froca.js"; | 
					
						
							| 
									
										
										
										
											2020-02-17 19:42:52 +01:00
										 |  |  | import branchService from "../services/branches.js"; | 
					
						
							| 
									
										
										
										
											2020-02-15 10:41:21 +01:00
										 |  |  | import treeService from "../services/tree.js"; | 
					
						
							| 
									
										
										
										
											2019-11-11 22:57:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | const $dialog = $("#move-to-dialog"); | 
					
						
							|  |  |  | const $form = $("#move-to-form"); | 
					
						
							|  |  |  | const $noteAutoComplete = $("#move-to-note-autocomplete"); | 
					
						
							|  |  |  | const $noteList = $("#move-to-note-list"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-15 10:41:21 +01:00
										 |  |  | let movedBranchIds; | 
					
						
							| 
									
										
										
										
											2019-11-11 22:57:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-15 10:41:21 +01:00
										 |  |  | export async function showDialog(branchIds) { | 
					
						
							|  |  |  |     movedBranchIds = branchIds; | 
					
						
							| 
									
										
										
										
											2019-11-11 22:57:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-09 10:00:13 +01:00
										 |  |  |     utils.openDialog($dialog); | 
					
						
							| 
									
										
										
										
											2019-11-11 22:57:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $noteAutoComplete.val('').trigger('focus'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $noteList.empty(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-15 10:41:21 +01:00
										 |  |  |     for (const branchId of movedBranchIds) { | 
					
						
							| 
									
										
										
										
											2021-04-16 22:57:37 +02:00
										 |  |  |         const branch = froca.getBranch(branchId); | 
					
						
							|  |  |  |         const note = await froca.getNote(branch.noteId); | 
					
						
							| 
									
										
										
										
											2019-11-11 22:57:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $noteList.append($("<li>").text(note.title)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     noteAutocompleteService.initNoteAutocomplete($noteAutoComplete); | 
					
						
							|  |  |  |     noteAutocompleteService.showRecentNotes($noteAutoComplete); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-31 22:33:02 +02:00
										 |  |  | async function moveNotesTo(parentBranchId) { | 
					
						
							|  |  |  |     await branchService.moveToParentNote(movedBranchIds, parentBranchId); | 
					
						
							| 
									
										
										
										
											2019-11-11 22:57:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-16 22:57:37 +02:00
										 |  |  |     const parentBranch = froca.getBranch(parentBranchId); | 
					
						
							| 
									
										
										
										
											2020-05-31 22:33:02 +02:00
										 |  |  |     const parentNote = await parentBranch.getNote(); | 
					
						
							| 
									
										
										
										
											2019-11-11 22:57:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-15 10:41:21 +01:00
										 |  |  |     toastService.showMessage(`Selected notes have been moved into ${parentNote.title}`); | 
					
						
							| 
									
										
										
										
											2019-11-11 22:57:51 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $form.on('submit', () => { | 
					
						
							| 
									
										
										
										
											2020-05-16 22:11:09 +02:00
										 |  |  |     const notePath = $noteAutoComplete.getSelectedNotePath(); | 
					
						
							| 
									
										
										
										
											2019-11-11 22:57:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (notePath) { | 
					
						
							|  |  |  |         $dialog.modal('hide'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-31 22:33:02 +02:00
										 |  |  |         const {noteId, parentNoteId} = treeService.getNoteIdAndParentIdFromNotePath(notePath); | 
					
						
							| 
									
										
										
										
											2021-04-16 22:57:37 +02:00
										 |  |  |         froca.getBranchId(parentNoteId, noteId).then(branchId => moveNotesTo(branchId)); | 
					
						
							| 
									
										
										
										
											2019-11-11 22:57:51 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2020-10-12 21:05:34 +02:00
										 |  |  |         logError("No path to move to."); | 
					
						
							| 
									
										
										
										
											2019-11-11 22:57:51 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return false; | 
					
						
							| 
									
										
										
										
											2020-05-16 22:11:09 +02:00
										 |  |  | }); |