| 
									
										
										
										
											2019-02-10 14:33:13 +01:00
										 |  |  | import treeService from '../services/tree.js'; | 
					
						
							| 
									
										
										
										
											2019-02-10 19:36:03 +01:00
										 |  |  | import utils from '../services/utils.js'; | 
					
						
							| 
									
										
										
										
											2019-02-10 14:33:13 +01:00
										 |  |  | import treeUtils from "../services/tree_utils.js"; | 
					
						
							| 
									
										
										
										
											2019-02-10 15:33:56 +01:00
										 |  |  | import server from "../services/server.js"; | 
					
						
							|  |  |  | import infoService from "../services/info.js"; | 
					
						
							| 
									
										
										
										
											2019-02-10 16:36:25 +01:00
										 |  |  | import messagingService from "../services/messaging.js"; | 
					
						
							| 
									
										
										
										
											2019-02-10 14:33:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | const $dialog = $("#import-dialog"); | 
					
						
							|  |  |  | const $form = $("#import-form"); | 
					
						
							|  |  |  | const $noteTitle = $dialog.find(".note-title"); | 
					
						
							| 
									
										
										
										
											2019-02-10 15:33:56 +01:00
										 |  |  | const $fileUploadInput = $("#import-file-upload-input"); | 
					
						
							| 
									
										
										
										
											2019-02-10 22:30:55 +01:00
										 |  |  | const $importNoteCountWrapper = $("#import-progress-count-wrapper"); | 
					
						
							|  |  |  | const $importNoteCount = $("#import-progress-count"); | 
					
						
							| 
									
										
										
										
											2019-02-10 16:59:50 +01:00
										 |  |  | const $importButton = $("#import-button"); | 
					
						
							| 
									
										
										
										
											2019-02-10 14:33:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 19:36:03 +01:00
										 |  |  | let importId; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 14:33:13 +01:00
										 |  |  | async function showDialog() { | 
					
						
							| 
									
										
										
										
											2019-02-10 19:36:03 +01:00
										 |  |  |     // each opening of the dialog resets the importId so we don't associate it with previous imports anymore
 | 
					
						
							|  |  |  |     importId = ''; | 
					
						
							| 
									
										
										
										
											2019-02-10 16:36:25 +01:00
										 |  |  |     $importNoteCountWrapper.hide(); | 
					
						
							|  |  |  |     $importNoteCount.text('0'); | 
					
						
							| 
									
										
										
										
											2019-02-10 16:59:50 +01:00
										 |  |  |     $fileUploadInput.val('').change(); // to trigger Import button disabling listener below
 | 
					
						
							| 
									
										
										
										
											2019-02-10 16:36:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 14:33:13 +01:00
										 |  |  |     glob.activeDialog = $dialog; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const currentNode = treeService.getCurrentNode(); | 
					
						
							|  |  |  |     $noteTitle.text(await treeUtils.getNoteTitle(currentNode.data.noteId)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $dialog.modal(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $form.submit(() => { | 
					
						
							|  |  |  |     const currentNode = treeService.getCurrentNode(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 16:59:50 +01:00
										 |  |  |     // disabling so that import is not triggered again.
 | 
					
						
							|  |  |  |     $importButton.attr("disabled", "disabled"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 15:33:56 +01:00
										 |  |  |     importIntoNote(currentNode.data.noteId); | 
					
						
							| 
									
										
										
										
											2019-02-10 14:33:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 15:33:56 +01:00
										 |  |  | function importIntoNote(importNoteId) { | 
					
						
							|  |  |  |     const formData = new FormData(); | 
					
						
							|  |  |  |     formData.append('upload', $fileUploadInput[0].files[0]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 19:36:03 +01:00
										 |  |  |     // we generate it here (and not on opening) for the case when you try to import multiple times from the same
 | 
					
						
							|  |  |  |     // dialog (which shouldn't happen, but still ...)
 | 
					
						
							|  |  |  |     importId = utils.randomString(10); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 15:33:56 +01:00
										 |  |  |     $.ajax({ | 
					
						
							| 
									
										
										
										
											2019-02-10 19:36:03 +01:00
										 |  |  |         url: baseApiUrl + 'notes/' + importNoteId + '/import/' + importId, | 
					
						
							| 
									
										
										
										
											2019-02-10 15:33:56 +01:00
										 |  |  |         headers: server.getHeaders(), | 
					
						
							|  |  |  |         data: formData, | 
					
						
							|  |  |  |         dataType: 'json', | 
					
						
							|  |  |  |         type: 'POST', | 
					
						
							|  |  |  |         contentType: false, // NEEDED, DON'T REMOVE THIS
 | 
					
						
							|  |  |  |         processData: false, // NEEDED, DON'T REMOVE THIS
 | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2019-02-10 16:59:50 +01:00
										 |  |  |         // we actually ignore the error since it can be caused by HTTP timeout and use WS messages instead.
 | 
					
						
							|  |  |  |         .fail((xhr, status, error) => {}); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-02-10 15:33:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 16:59:50 +01:00
										 |  |  | messagingService.subscribeToMessages(async message => { | 
					
						
							| 
									
										
										
										
											2019-02-10 19:53:57 +01:00
										 |  |  |     if (message.type === 'import-error') { | 
					
						
							|  |  |  |         infoService.showError(message.message); | 
					
						
							|  |  |  |         $dialog.modal('hide'); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 19:36:03 +01:00
										 |  |  |     if (!message.importId || message.importId !== importId) { | 
					
						
							|  |  |  |         // incoming messages must correspond to this import instance
 | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 22:30:55 +01:00
										 |  |  |     if (message.type === 'import-progress-count') { | 
					
						
							| 
									
										
										
										
											2019-02-10 16:59:50 +01:00
										 |  |  |         $importNoteCountWrapper.show(); | 
					
						
							| 
									
										
										
										
											2019-02-10 15:33:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 22:30:55 +01:00
										 |  |  |         $importNoteCount.text(message.progressCount); | 
					
						
							| 
									
										
										
										
											2019-02-10 16:59:50 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     else if (message.type === 'import-finished') { | 
					
						
							|  |  |  |         $dialog.modal('hide'); | 
					
						
							| 
									
										
										
										
											2019-02-10 15:33:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 16:59:50 +01:00
										 |  |  |         infoService.showMessage("Import finished successfully."); | 
					
						
							| 
									
										
										
										
											2019-02-10 15:33:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 16:59:50 +01:00
										 |  |  |         await treeService.reload(); | 
					
						
							| 
									
										
										
										
											2019-02-10 15:33:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 16:59:50 +01:00
										 |  |  |         if (message.noteId) { | 
					
						
							|  |  |  |             const node = await treeService.activateNote(message.noteId); | 
					
						
							| 
									
										
										
										
											2019-02-10 16:36:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 16:59:50 +01:00
										 |  |  |             node.setExpanded(true); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $fileUploadInput.change(() => { | 
					
						
							|  |  |  |     if ($fileUploadInput.val()) { | 
					
						
							|  |  |  |         $importButton.removeAttr("disabled"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         $importButton.attr("disabled", "disabled"); | 
					
						
							| 
									
										
										
										
											2019-02-10 16:36:25 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 14:33:13 +01:00
										 |  |  | export default { | 
					
						
							|  |  |  |     showDialog | 
					
						
							|  |  |  | } |