2019-02-10 21:33:13 +08:00
|
|
|
import treeService from '../services/tree.js';
|
|
|
|
import treeUtils from "../services/tree_utils.js";
|
2019-02-10 22:33:56 +08:00
|
|
|
import server from "../services/server.js";
|
|
|
|
import infoService from "../services/info.js";
|
2019-02-10 23:36:25 +08:00
|
|
|
import messagingService from "../services/messaging.js";
|
2019-02-10 21:33:13 +08:00
|
|
|
|
|
|
|
const $dialog = $("#import-dialog");
|
|
|
|
const $form = $("#import-form");
|
|
|
|
const $noteTitle = $dialog.find(".note-title");
|
2019-02-10 22:33:56 +08:00
|
|
|
const $fileUploadInput = $("#import-file-upload-input");
|
2019-02-10 23:36:25 +08:00
|
|
|
const $importNoteCountWrapper = $("#import-note-count-wrapper");
|
|
|
|
const $importNoteCount = $("#import-note-count");
|
2019-02-10 21:33:13 +08:00
|
|
|
|
|
|
|
async function showDialog() {
|
2019-02-10 23:36:25 +08:00
|
|
|
$importNoteCountWrapper.hide();
|
|
|
|
$importNoteCount.text('0');
|
|
|
|
$fileUploadInput.val('');
|
|
|
|
|
2019-02-10 21:33:13 +08: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 22:33:56 +08:00
|
|
|
importIntoNote(currentNode.data.noteId);
|
2019-02-10 21:33:13 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2019-02-10 22:33:56 +08:00
|
|
|
function importIntoNote(importNoteId) {
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append('upload', $fileUploadInput[0].files[0]);
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: baseApiUrl + 'notes/' + importNoteId + '/import',
|
|
|
|
headers: server.getHeaders(),
|
|
|
|
data: formData,
|
|
|
|
dataType: 'json',
|
|
|
|
type: 'POST',
|
|
|
|
contentType: false, // NEEDED, DON'T REMOVE THIS
|
|
|
|
processData: false, // NEEDED, DON'T REMOVE THIS
|
|
|
|
})
|
|
|
|
.fail((xhr, status, error) => alert('Import error: ' + xhr.responseText))
|
|
|
|
.done(async note => {
|
|
|
|
$dialog.modal('hide');
|
|
|
|
|
2019-02-10 23:36:25 +08:00
|
|
|
infoService.showMessage("Import finished successfully.");
|
2019-02-10 22:33:56 +08:00
|
|
|
|
|
|
|
await treeService.reload();
|
|
|
|
|
|
|
|
if (note) {
|
|
|
|
const node = await treeService.activateNote(note.noteId);
|
|
|
|
|
|
|
|
node.setExpanded(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-10 23:36:25 +08:00
|
|
|
messagingService.subscribeToMessages(message => {
|
|
|
|
if (message.type === 'importNoteCount') {
|
|
|
|
$importNoteCountWrapper.show();
|
|
|
|
|
|
|
|
$importNoteCount.text(message.count);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-02-10 21:33:13 +08:00
|
|
|
export default {
|
|
|
|
showDialog
|
|
|
|
}
|