241 lines
9.7 KiB
JavaScript
Raw Normal View History

2022-06-16 15:04:57 +02:00
import treeService from "../../services/tree.js";
import utils from "../../services/utils.js";
import ws from "../../services/ws.js";
import toastService from "../../services/toast.js";
import froca from "../../services/froca.js";
import openService from "../../services/open.js";
import BasicWidget from "../basic_widget.js";
import { t } from "../../services/i18n.js";
2022-06-16 15:04:57 +02:00
const TPL = `
<div class="export-dialog modal fade mx-auto" tabindex="-1" role="dialog">
<style>
2022-06-16 20:20:56 +02:00
.export-dialog .export-form .form-check {
2022-06-16 15:04:57 +02:00
padding-top: 10px;
padding-bottom: 10px;
}
2022-06-16 20:20:56 +02:00
.export-dialog .export-form .format-choice {
2022-06-16 15:04:57 +02:00
padding-left: 40px;
display: none;
}
2022-06-16 20:20:56 +02:00
.export-dialog .export-form .opml-versions {
2022-06-16 15:04:57 +02:00
padding-left: 60px;
display: none;
}
2022-06-16 20:20:56 +02:00
.export-dialog .export-form .form-check-label {
2022-06-16 15:04:57 +02:00
padding: 2px;
}
</style>
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
2025-01-09 18:07:02 +02:00
<h5 class="modal-title">${t("export.export_note_title")} <span class="export-note-title"></span></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="${t("export.close")}"></button>
2022-06-16 15:04:57 +02:00
</div>
<form class="export-form">
<div class="modal-body">
<div class="form-check">
<label class="form-check-label tn-radio">
2022-06-16 15:04:57 +02:00
<input class="export-type-subtree form-check-input" type="radio" name="export-type" value="subtree">
2025-01-09 18:07:02 +02:00
${t("export.export_type_subtree")}
2022-06-16 15:04:57 +02:00
</label>
</div>
<div class="export-subtree-formats format-choice">
<div class="form-check">
<label class="form-check-label tn-radio">
2022-06-16 15:04:57 +02:00
<input class="form-check-input" type="radio" name="export-subtree-format" value="html">
2025-01-09 18:07:02 +02:00
${t("export.format_html_zip")}
2022-06-16 15:04:57 +02:00
</label>
</div>
<div class="form-check">
<label class="form-check-label tn-radio">
2022-06-16 15:04:57 +02:00
<input class="form-check-input" type="radio" name="export-subtree-format" value="markdown">
2025-01-09 18:07:02 +02:00
${t("export.format_markdown")}
2022-06-16 15:04:57 +02:00
</label>
</div>
<div class="form-check">
<label class="form-check-label tn-radio">
2022-06-16 15:04:57 +02:00
<input class="form-check-input" type="radio" name="export-subtree-format" value="opml">
2025-01-09 18:07:02 +02:00
${t("export.format_opml")}
2022-06-16 15:04:57 +02:00
</label>
</div>
<div class="opml-versions">
<div class="form-check">
<label class="form-check-label tn-radio">
2022-06-16 15:04:57 +02:00
<input class="form-check-input" type="radio" name="opml-version" value="1.0">
2025-01-09 18:07:02 +02:00
${t("export.opml_version_1")}
2022-06-16 15:04:57 +02:00
</label>
</div>
<div class="form-check">
<label class="form-check-label tn-radio">
2022-06-16 15:04:57 +02:00
<input class="form-check-input" type="radio" name="opml-version" value="2.0">
2025-01-09 18:07:02 +02:00
${t("export.opml_version_2")}
2022-06-16 15:04:57 +02:00
</label>
</div>
</div>
</div>
<div class="form-check">
<label class="form-check-label tn-radio">
2022-06-16 15:04:57 +02:00
<input class="form-check-input" type="radio" name="export-type" value="single">
2025-01-09 18:07:02 +02:00
${t("export.export_type_single")}
2022-06-16 15:04:57 +02:00
</label>
</div>
<div class="export-single-formats format-choice">
<div class="form-check">
<label class="form-check-label tn-radio">
2022-06-16 15:04:57 +02:00
<input class="form-check-input" type="radio" name="export-single-format" value="html">
2025-01-09 18:07:02 +02:00
${t("export.format_html")}
2022-06-16 15:04:57 +02:00
</label>
</div>
<div class="form-check">
<label class="form-check-label tn-radio">
<input class="form-check-input" type="radio" name="export-single-format" value="markdown">
2025-01-09 18:07:02 +02:00
${t("export.format_markdown")}
2022-06-16 15:04:57 +02:00
</label>
</div>
</div>
</div>
<div class="modal-footer">
2025-01-09 18:07:02 +02:00
<button class="export-button btn btn-primary">${t("export.export")}</button>
2022-06-16 15:04:57 +02:00
</div>
</form>
</div>
</div>
</div>`;
export default class ExportDialog extends BasicWidget {
constructor() {
super();
2025-01-09 18:07:02 +02:00
this.taskId = "";
2022-06-16 15:04:57 +02:00
this.branchId = null;
}
doRender() {
this.$widget = $(TPL);
2024-09-03 18:15:10 +02:00
this.modal = bootstrap.Modal.getOrCreateInstance(this.$widget);
2022-06-16 15:04:57 +02:00
this.$form = this.$widget.find(".export-form");
this.$noteTitle = this.$widget.find(".export-note-title");
this.$subtreeFormats = this.$widget.find(".export-subtree-formats");
this.$singleFormats = this.$widget.find(".export-single-formats");
this.$subtreeType = this.$widget.find(".export-type-subtree");
this.$singleType = this.$widget.find(".export-type-single");
this.$exportButton = this.$widget.find(".export-button");
this.$opmlVersions = this.$widget.find(".opml-versions");
2025-01-09 18:07:02 +02:00
this.$form.on("submit", () => {
2024-09-03 18:15:10 +02:00
this.modal.hide();
2022-06-16 15:04:57 +02:00
const exportType = this.$widget.find("input[name='export-type']:checked").val();
if (!exportType) {
2025-01-09 18:07:02 +02:00
toastService.showError(t("export.choose_export_type"));
2022-06-16 15:04:57 +02:00
return;
}
2025-01-09 18:07:02 +02:00
const exportFormat = exportType === "subtree" ? this.$widget.find("input[name=export-subtree-format]:checked").val() : this.$widget.find("input[name=export-single-format]:checked").val();
2022-06-16 15:04:57 +02:00
2025-01-09 18:07:02 +02:00
const exportVersion = exportFormat === "opml" ? this.$widget.find("input[name='opml-version']:checked").val() : "1.0";
2022-06-16 15:04:57 +02:00
this.exportBranch(this.branchId, exportType, exportFormat, exportVersion);
return false;
});
2025-01-09 18:07:02 +02:00
this.$widget.find("input[name=export-type]").on("change", (e) => {
if (e.currentTarget.value === "subtree") {
2022-06-16 15:04:57 +02:00
if (this.$widget.find("input[name=export-subtree-format]:checked").length === 0) {
this.$widget.find("input[name=export-subtree-format]:first").prop("checked", true);
}
this.$subtreeFormats.slideDown();
this.$singleFormats.slideUp();
2025-01-09 18:07:02 +02:00
} else {
2022-06-16 15:04:57 +02:00
if (this.$widget.find("input[name=export-single-format]:checked").length === 0) {
this.$widget.find("input[name=export-single-format]:first").prop("checked", true);
}
this.$subtreeFormats.slideUp();
this.$singleFormats.slideDown();
}
});
2025-01-09 18:07:02 +02:00
this.$widget.find("input[name=export-subtree-format]").on("change", (e) => {
if (e.currentTarget.value === "opml") {
2022-06-16 15:04:57 +02:00
this.$opmlVersions.slideDown();
2025-01-09 18:07:02 +02:00
} else {
2022-06-16 15:04:57 +02:00
this.$opmlVersions.slideUp();
}
});
}
2024-09-03 18:15:10 +02:00
async showExportDialogEvent({ notePath, defaultType }) {
2025-01-09 18:07:02 +02:00
this.taskId = "";
2022-06-16 15:04:57 +02:00
this.$exportButton.removeAttr("disabled");
2025-01-09 18:07:02 +02:00
if (defaultType === "subtree") {
this.$subtreeType.prop("checked", true).trigger("change");
2022-06-16 15:04:57 +02:00
2025-01-09 18:07:02 +02:00
this.$widget.find("input[name=export-subtree-format]:checked").trigger("change");
} else if (defaultType === "single") {
this.$singleType.prop("checked", true).trigger("change");
} else {
2023-05-04 22:16:18 +02:00
throw new Error(`Unrecognized type '${defaultType}'`);
2022-06-16 15:04:57 +02:00
}
this.$widget.find(".opml-v2").prop("checked", true); // setting default
utils.openDialog(this.$widget);
2024-09-03 18:15:10 +02:00
const { noteId, parentNoteId } = treeService.getNoteIdAndParentIdFromUrl(notePath);
2022-06-16 15:04:57 +02:00
this.branchId = await froca.getBranchId(parentNoteId, noteId);
this.$noteTitle.text(await treeService.getNoteTitle(noteId));
2022-06-16 15:04:57 +02:00
}
exportBranch(branchId, type, format, version) {
this.taskId = utils.randomString(10);
2023-04-14 16:49:06 +02:00
const url = openService.getUrlForDownload(`api/branches/${branchId}/export/${type}/${format}/${version}/${this.taskId}`);
2022-06-16 15:04:57 +02:00
openService.download(url);
}
}
2025-01-09 18:07:02 +02:00
ws.subscribeToMessages(async (message) => {
2022-06-16 15:04:57 +02:00
const makeToast = (id, message) => ({
id: id,
2025-01-09 18:07:02 +02:00
title: t("export.export_status"),
2022-06-16 15:04:57 +02:00
message: message,
icon: "arrow-square-up-right"
});
2025-01-09 18:07:02 +02:00
if (message.taskType !== "export") {
2022-06-16 15:04:57 +02:00
return;
}
2025-01-09 18:07:02 +02:00
if (message.type === "taskError") {
2022-06-16 15:04:57 +02:00
toastService.closePersistent(message.taskId);
toastService.showError(message.message);
2025-01-09 18:07:02 +02:00
} else if (message.type === "taskProgressCount") {
toastService.showPersistent(makeToast(message.taskId, t("export.export_in_progress", { progressCount: message.progressCount })));
} else if (message.type === "taskSucceeded") {
const toast = makeToast(message.taskId, t("export.export_finished_successfully"));
2022-06-16 15:04:57 +02:00
toast.closeAfter = 5000;
toastService.showPersistent(toast);
}
});