137 lines
3.9 KiB
JavaScript
Raw Normal View History

2018-11-24 14:44:56 +01:00
import treeUtils from "../services/tree_utils.js";
2019-02-10 15:33:56 +01:00
import utils from "../services/utils.js";
2019-08-26 20:21:43 +02:00
import ws from "../services/ws.js";
2019-10-20 10:00:18 +02:00
import toastService from "../services/toast.js";
2018-11-24 14:44:56 +01:00
const $dialog = $("#export-dialog");
const $form = $("#export-form");
const $noteTitle = $dialog.find(".export-note-title");
2018-11-24 14:44:56 +01:00
const $subtreeFormats = $("#export-subtree-formats");
const $singleFormats = $("#export-single-formats");
const $subtreeType = $("#export-type-subtree");
const $singleType = $("#export-type-single");
2019-02-10 22:30:55 +01:00
const $exportButton = $("#export-button");
2019-02-16 23:33:40 +01:00
const $opmlVersions = $("#opml-versions");
2019-02-10 22:30:55 +01:00
2019-10-18 22:27:38 +02:00
let taskId = '';
let branchId = null;
2018-11-24 14:44:56 +01:00
export async function showDialog(node, defaultType) {
utils.closeActiveDialog();
2019-10-18 22:27:38 +02:00
// each opening of the dialog resets the taskId so we don't associate it with previous exports anymore
taskId = '';
$exportButton.removeAttr("disabled");
2019-02-10 22:30:55 +01:00
2018-11-24 14:44:56 +01:00
if (defaultType === 'subtree') {
$subtreeType.prop("checked", true).change();
2019-02-16 23:33:40 +01:00
// to show/hide OPML versions
$("input[name=export-subtree-format]:checked").change();
2018-11-24 14:44:56 +01:00
}
else if (defaultType === 'single') {
$singleType.prop("checked", true).change();
}
else {
throw new Error("Unrecognized type " + defaultType);
}
2019-02-16 23:58:42 +01:00
$("#opml-v2").prop("checked", true); // setting default
2018-11-24 14:44:56 +01:00
glob.activeDialog = $dialog;
$dialog.modal();
branchId = node.data.branchId;
const noteTitle = await treeUtils.getNoteTitle(node.data.noteId);
2018-11-24 14:44:56 +01:00
$noteTitle.html(noteTitle);
}
$form.submit(() => {
$dialog.modal('hide');
2019-02-10 22:30:55 +01:00
2018-11-24 14:44:56 +01:00
const exportType = $dialog.find("input[name='export-type']:checked").val();
2018-11-24 20:58:38 +01: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();
2019-02-16 23:33:40 +01:00
const exportVersion = exportFormat === 'opml' ? $dialog.find("input[name='opml-version']:checked").val() : "1.0";
exportBranch(branchId, exportType, exportFormat, exportVersion);
2018-11-24 14:44:56 +01:00
return false;
});
2019-02-16 23:33:40 +01:00
function exportBranch(branchId, type, format, version) {
2019-10-18 22:27:38 +02:00
taskId = utils.randomString(10);
2019-02-10 22:30:55 +01:00
2019-10-18 22:27:38 +02:00
const url = utils.getHost() + `/api/notes/${branchId}/export/${type}/${format}/${version}/${taskId}`;
2019-02-10 15:33:56 +01:00
utils.download(url);
}
2018-11-24 14:44:56 +01: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-16 23:33:40 +01:00
$('input[name=export-subtree-format]').change(function () {
if (this.value === 'opml') {
$opmlVersions.slideDown();
}
else {
$opmlVersions.slideUp();
}
});
function makeToast(id, message) {
return {
id: id,
title: "Export status",
message: message,
icon: "arrow-square-up-right"
};
}
2019-08-26 20:21:43 +02:00
ws.subscribeToMessages(async message => {
2019-10-18 23:19:16 +02:00
if (message.taskType !== 'export') {
return;
}
if (message.type === 'task-error') {
2019-10-20 10:00:18 +02:00
toastService.closePersistent(message.taskId);
toastService.showError(message.message);
2019-02-10 22:30:55 +01:00
}
2019-10-18 23:19:16 +02:00
else if (message.type === 'task-progress-count') {
2019-10-20 10:00:18 +02:00
toastService.showPersistent(makeToast(message.taskId, "Export in progress: " + message.progressCount));
2019-02-10 22:30:55 +01:00
}
2019-10-18 23:19:16 +02:00
else if (message.type === 'task-succeeded') {
const toast = makeToast(message.taskId, "Import finished successfully.");
toast.closeAfter = 5000;
2019-02-10 22:30:55 +01:00
2019-10-20 10:00:18 +02:00
toastService.showPersistent(toast);
2019-02-10 22:30:55 +01:00
}
});