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

124 lines
3.8 KiB
JavaScript
Raw Normal View History

2018-11-24 21:44:56 +08:00
import treeService from '../services/tree.js';
import treeUtils from "../services/tree_utils.js";
2019-02-10 22:33:56 +08:00
import utils from "../services/utils.js";
import protectedSessionHolder from "../services/protected_session_holder.js";
2019-02-11 05:30:55 +08:00
import messagingService from "../services/messaging.js";
import infoService from "../services/info.js";
2018-11-24 21:44:56 +08:00
const $dialog = $("#export-dialog");
const $form = $("#export-form");
const $noteTitle = $dialog.find(".note-title");
const $subtreeFormats = $("#export-subtree-formats");
const $singleFormats = $("#export-single-formats");
const $subtreeType = $("#export-type-subtree");
const $singleType = $("#export-type-single");
2019-02-11 05:56:14 +08:00
const $exportProgressWrapper = $("#export-progress-count-wrapper");
const $exportProgressCount = $("#export-progress-count");
2019-02-11 05:30:55 +08:00
const $exportButton = $("#export-button");
let exportId = '';
2018-11-24 21:44:56 +08:00
async function showDialog(defaultType) {
2019-02-11 05:30:55 +08:00
// each opening of the dialog resets the exportId so we don't associate it with previous exports anymore
exportId = '';
$exportButton.removeAttr("disabled");
2019-02-11 05:56:14 +08:00
$exportProgressWrapper.hide();
$exportProgressCount.text('0');
2019-02-11 05:30:55 +08:00
2018-11-24 21:44:56 +08:00
if (defaultType === 'subtree') {
$subtreeType.prop("checked", true).change();
}
else if (defaultType === 'single') {
$singleType.prop("checked", true).change();
}
else {
throw new Error("Unrecognized type " + defaultType);
}
glob.activeDialog = $dialog;
$dialog.modal();
const currentNode = treeService.getCurrentNode();
const noteTitle = await treeUtils.getNoteTitle(currentNode.data.noteId);
$noteTitle.html(noteTitle);
}
$form.submit(() => {
// disabling so export can't be triggered again
2019-02-11 05:30:55 +08:00
$exportButton.attr("disabled", "disabled");
2018-11-24 21:44:56 +08:00
const exportType = $dialog.find("input[name='export-type']:checked").val();
2018-11-25 03:58:38 +08:00
if (!exportType) {
// this shouldn't happen as we always choose default export type
alert("Choose export type first please");
return;
}
const exportFormat = exportType === 'subtree'
? $("input[name=export-subtree-format]:checked").val()
: $("input[name=export-single-format]:checked").val();
2018-11-24 21:44:56 +08:00
const currentNode = treeService.getCurrentNode();
2019-02-10 22:33:56 +08:00
exportBranch(currentNode.data.branchId, exportType, exportFormat);
2018-11-24 21:44:56 +08:00
return false;
});
2019-02-10 22:33:56 +08:00
function exportBranch(branchId, type, format) {
2019-02-11 05:30:55 +08:00
exportId = utils.randomString(10);
const url = utils.getHost() + `/api/notes/${branchId}/export/${type}/${format}/${exportId}?protectedSessionId=` + encodeURIComponent(protectedSessionHolder.getProtectedSessionId());
2019-02-10 22:33:56 +08:00
utils.download(url);
}
2018-11-24 21:44:56 +08:00
$('input[name=export-type]').change(function () {
if (this.value === 'subtree') {
if ($("input[name=export-subtree-format]:checked").length === 0) {
$("input[name=export-subtree-format]:first").prop("checked", true);
}
$subtreeFormats.slideDown();
$singleFormats.slideUp();
}
else {
if ($("input[name=export-single-format]:checked").length === 0) {
$("input[name=export-single-format]:first").prop("checked", true);
}
$subtreeFormats.slideUp();
$singleFormats.slideDown();
}
});
2019-02-11 05:30:55 +08:00
messagingService.subscribeToMessages(async message => {
if (message.type === 'export-error') {
infoService.showError(message.message);
$dialog.modal('hide');
return;
}
if (!message.exportId || message.exportId !== exportId) {
// incoming messages must correspond to this export instance
return;
}
if (message.type === 'export-progress-count') {
2019-02-11 05:56:14 +08:00
$exportProgressWrapper.slideDown();
2019-02-11 05:30:55 +08:00
2019-02-11 05:56:14 +08:00
$exportProgressCount.text(message.progressCount);
2019-02-11 05:30:55 +08:00
}
else if (message.type === 'export-finished') {
$dialog.modal('hide');
infoService.showMessage("Export finished successfully.");
}
});
2018-11-24 21:44:56 +08:00
export default {
showDialog
};