251 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">
2024-07-26 14:02:54 +08:00
<h5 class="modal-title">${t('export.export_note_title')} <span class="export-note-title"></span></h5>
2024-09-03 18:15:10 +02:00
<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">
<input class="export-type-subtree form-check-input" type="radio" name="export-type" value="subtree">
${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">
<input class="form-check-input" type="radio" name="export-subtree-format" value="html">
${t('export.format_html_zip')}
2022-06-16 15:04:57 +02:00
</label>
</div>
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" type="radio" name="export-subtree-format" value="markdown">
${t('export.format_markdown')}
2022-06-16 15:04:57 +02:00
</label>
</div>
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" type="radio" name="export-subtree-format" value="opml">
${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">
<input class="form-check-input" type="radio" name="opml-version" value="1.0">
${t('export.opml_version_1')}
2022-06-16 15:04:57 +02:00
</label>
</div>
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" type="radio" name="opml-version" value="2.0">
${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">
<input class="form-check-input" type="radio" name="export-type" value="single">
${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">
<input class="form-check-input" type="radio" name="export-single-format" value="html">
${t('export.format_html')}
2022-06-16 15:04:57 +02:00
</label>
</div>
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" type="radio" name="export-single-format" value="markdown">
${t('export.format_markdown')}
2022-06-16 15:04:57 +02:00
</label>
</div>
</div>
</div>
<div class="modal-footer">
<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();
this.taskId = '';
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");
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) {
toastService.showError(t('export.choose_export_type'));
2022-06-16 15:04:57 +02:00
return;
}
const exportFormat = exportType === 'subtree'
? this.$widget.find("input[name=export-subtree-format]:checked").val()
: this.$widget.find("input[name=export-single-format]:checked").val();
const exportVersion = exportFormat === 'opml'
? this.$widget.find("input[name='opml-version']:checked").val()
: "1.0";
this.exportBranch(this.branchId, exportType, exportFormat, exportVersion);
return false;
});
this.$widget.find('input[name=export-type]').on('change', e => {
if (e.currentTarget.value === 'subtree') {
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();
}
else {
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();
}
});
this.$widget.find('input[name=export-subtree-format]').on('change', e => {
if (e.currentTarget.value === 'opml') {
this.$opmlVersions.slideDown();
}
else {
this.$opmlVersions.slideUp();
}
});
}
2024-09-03 18:15:10 +02:00
async showExportDialogEvent({ notePath, defaultType }) {
2022-06-16 15:04:57 +02:00
this.taskId = '';
this.$exportButton.removeAttr("disabled");
if (defaultType === 'subtree') {
this.$subtreeType.prop("checked", true).trigger('change');
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);
}
}
ws.subscribeToMessages(async message => {
const makeToast = (id, message) => ({
id: id,
title: t('export.export_status'),
2022-06-16 15:04:57 +02:00
message: message,
icon: "arrow-square-up-right"
});
if (message.taskType !== 'export') {
return;
}
if (message.type === 'taskError') {
toastService.closePersistent(message.taskId);
toastService.showError(message.message);
}
else if (message.type === 'taskProgressCount') {
toastService.showPersistent(makeToast(message.taskId, t('export.export_in_progress', { progressCount: message.progressCount })));
2022-06-16 15:04:57 +02:00
}
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);
}
});