trilium/src/public/javascripts/dialogs/import.js

102 lines
3.1 KiB
JavaScript
Raw Normal View History

2019-02-10 21:33:13 +08:00
import treeService from '../services/tree.js';
2019-02-11 02:36:03 +08:00
import utils from '../services/utils.js';
2019-02-10 21:33:13 +08:00
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");
const $importButton = $("#import-button");
2019-02-10 21:33:13 +08:00
2019-02-11 02:36:03 +08:00
let importId;
2019-02-10 21:33:13 +08:00
async function showDialog() {
2019-02-11 02:36:03 +08:00
// each opening of the dialog resets the importId so we don't associate it with previous imports anymore
importId = '';
2019-02-10 23:36:25 +08:00
$importNoteCountWrapper.hide();
$importNoteCount.text('0');
$fileUploadInput.val('').change(); // to trigger Import button disabling listener below
2019-02-10 23:36:25 +08:00
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();
// disabling so that import is not triggered again.
$importButton.attr("disabled", "disabled");
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]);
2019-02-11 02:36:03 +08: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 22:33:56 +08:00
$.ajax({
2019-02-11 02:36:03 +08:00
url: baseApiUrl + 'notes/' + importNoteId + '/import/' + importId,
2019-02-10 22:33:56 +08:00
headers: server.getHeaders(),
data: formData,
dataType: 'json',
type: 'POST',
contentType: false, // NEEDED, DON'T REMOVE THIS
processData: false, // NEEDED, DON'T REMOVE THIS
})
// 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 22:33:56 +08:00
messagingService.subscribeToMessages(async message => {
2019-02-11 02:36:03 +08:00
if (!message.importId || message.importId !== importId) {
// incoming messages must correspond to this import instance
return;
}
if (message.type === 'import-note-count') {
$importNoteCountWrapper.show();
2019-02-10 22:33:56 +08:00
$importNoteCount.text(message.count);
}
else if (message.type === 'import-finished') {
$dialog.modal('hide');
2019-02-10 22:33:56 +08:00
infoService.showMessage("Import finished successfully.");
2019-02-10 22:33:56 +08:00
await treeService.reload();
2019-02-10 22:33:56 +08:00
if (message.noteId) {
const node = await treeService.activateNote(message.noteId);
2019-02-10 23:36:25 +08:00
node.setExpanded(true);
}
}
});
$fileUploadInput.change(() => {
if ($fileUploadInput.val()) {
$importButton.removeAttr("disabled");
}
else {
$importButton.attr("disabled", "disabled");
2019-02-10 23:36:25 +08:00
}
});
2019-02-10 21:33:13 +08:00
export default {
showDialog
}