163 lines
7.3 KiB
JavaScript
Raw Normal View History

import utils, { escapeQuotes } from "../../services/utils.js";
2022-06-16 14:21:24 +02:00
import treeService from "../../services/tree.js";
import importService from "../../services/import.js";
import options from "../../services/options.js";
import BasicWidget from "../basic_widget.js";
import { t } from "../../services/i18n.js";
import { Modal, Tooltip } from "bootstrap";
2022-06-16 14:21:24 +02:00
const TPL = `
<div class="import-dialog modal fade mx-auto" tabindex="-1" role="dialog">
<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("import.importIntoNote")}</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="${t("import.close")}"></button>
2022-06-16 14:21:24 +02:00
</div>
<form class="import-form">
<div class="modal-body">
<div class="form-group">
2025-01-09 18:07:02 +02:00
<label for="import-file-upload-input"><strong>${t("import.chooseImportFile")}</strong></label>
2022-06-16 14:21:24 +02:00
2025-02-06 07:17:58 +02:00
<label class="tn-file-input tn-input-field">
<input type="file" class="import-file-upload-input form-control-file" multiple />
</label>
2022-06-16 14:21:24 +02:00
2025-01-09 18:07:02 +02:00
<p>${t("import.importDescription")} <strong class="import-note-title"></strong>.
2022-06-16 14:21:24 +02:00
</div>
<div class="form-group">
2025-01-09 18:07:02 +02:00
<strong>${t("import.options")}:</strong>
2022-06-16 14:21:24 +02:00
<div class="checkbox">
<label class="tn-checkbox" data-bs-toggle="tooltip" title="${escapeQuotes(t("import.safeImportTooltip"))}">
2022-06-16 14:21:24 +02:00
<input class="safe-import-checkbox" value="1" type="checkbox" checked>
2025-01-09 18:07:02 +02:00
<span>${t("import.safeImport")}</span>
2022-06-16 14:21:24 +02:00
</label>
</div>
<div class="checkbox">
<label class="tn-checkbox" data-bs-toggle="tooltip" title="${escapeQuotes(t("import.explodeArchivesTooltip"))}">
2022-06-16 14:21:24 +02:00
<input class="explode-archives-checkbox" value="1" type="checkbox" checked>
2025-01-09 18:07:02 +02:00
<span>${t("import.explodeArchives")}</span>
2022-06-16 14:21:24 +02:00
</label>
</div>
<div class="checkbox">
<label class="tn-checkbox" data-bs-toggle="tooltip" title="${escapeQuotes(t("import.shrinkImagesTooltip"))}">
2025-01-09 18:07:02 +02:00
<input class="shrink-images-checkbox" value="1" type="checkbox" checked> <span>${t("import.shrinkImages")}</span>
2022-06-16 14:21:24 +02:00
</label>
</div>
<div class="checkbox">
<label class="tn-checkbox">
2022-06-16 14:21:24 +02:00
<input class="text-imported-as-text-checkbox" value="1" type="checkbox" checked>
2025-01-09 18:07:02 +02:00
${t("import.textImportedAsText")}
2022-06-16 14:21:24 +02:00
</label>
</div>
<div class="checkbox">
<label class="tn-checkbox">
2025-01-09 18:07:02 +02:00
<input class="code-imported-as-code-checkbox" value="1" type="checkbox" checked> ${t("import.codeImportedAsCode")}
2022-06-16 14:21:24 +02:00
</label>
</div>
<div class="checkbox">
<label class="tn-checkbox">
2022-06-16 14:21:24 +02:00
<input class="replace-underscores-with-spaces-checkbox" value="1" type="checkbox" checked>
2025-01-09 18:07:02 +02:00
${t("import.replaceUnderscoresWithSpaces")}
2022-06-16 14:21:24 +02:00
</label>
</div>
</div>
</div>
<div class="modal-footer">
2025-01-09 18:07:02 +02:00
<button class="import-button btn btn-primary">${t("import.import")}</button>
2022-06-16 14:21:24 +02:00
</div>
</form>
</div>
</div>
</div>`;
export default class ImportDialog extends BasicWidget {
constructor() {
super();
this.parentNoteId = null;
}
doRender() {
this.$widget = $(TPL);
Modal.getOrCreateInstance(this.$widget);
2022-06-16 14:21:24 +02:00
this.$form = this.$widget.find(".import-form");
this.$noteTitle = this.$widget.find(".import-note-title");
this.$fileUploadInput = this.$widget.find(".import-file-upload-input");
this.$importButton = this.$widget.find(".import-button");
this.$safeImportCheckbox = this.$widget.find(".safe-import-checkbox");
this.$shrinkImagesCheckbox = this.$widget.find(".shrink-images-checkbox");
this.$textImportedAsTextCheckbox = this.$widget.find(".text-imported-as-text-checkbox");
this.$codeImportedAsCodeCheckbox = this.$widget.find(".code-imported-as-code-checkbox");
this.$explodeArchivesCheckbox = this.$widget.find(".explode-archives-checkbox");
this.$replaceUnderscoresWithSpacesCheckbox = this.$widget.find(".replace-underscores-with-spaces-checkbox");
2025-01-09 18:07:02 +02:00
this.$form.on("submit", () => {
2022-06-16 14:21:24 +02:00
// disabling so that import is not triggered again.
this.$importButton.attr("disabled", "disabled");
this.importIntoNote(this.parentNoteId);
return false;
});
2025-01-09 18:07:02 +02:00
this.$fileUploadInput.on("change", () => {
2022-06-16 14:21:24 +02:00
if (this.$fileUploadInput.val()) {
this.$importButton.removeAttr("disabled");
2025-01-09 18:07:02 +02:00
} else {
2022-06-16 14:21:24 +02:00
this.$importButton.attr("disabled", "disabled");
}
});
2022-11-22 23:53:08 +01:00
2025-01-09 18:07:02 +02:00
let _ = [...this.$widget.find('[data-bs-toggle="tooltip"]')].forEach((element) => {
Tooltip.getOrCreateInstance(element, {
html: true
});
2022-11-22 23:53:08 +01:00
});
2022-06-16 14:21:24 +02:00
}
async showImportDialogEvent({ noteId }) {
2022-06-16 14:21:24 +02:00
this.parentNoteId = noteId;
2025-01-09 18:07:02 +02:00
this.$fileUploadInput.val("").trigger("change"); // to trigger Import button disabling listener below
2022-06-16 14:21:24 +02:00
this.$safeImportCheckbox.prop("checked", true);
2025-01-09 18:07:02 +02:00
this.$shrinkImagesCheckbox.prop("checked", options.is("compressImages"));
2022-06-16 14:21:24 +02:00
this.$textImportedAsTextCheckbox.prop("checked", true);
this.$codeImportedAsCodeCheckbox.prop("checked", true);
this.$explodeArchivesCheckbox.prop("checked", true);
this.$replaceUnderscoresWithSpacesCheckbox.prop("checked", true);
this.$noteTitle.text(await treeService.getNoteTitle(this.parentNoteId));
utils.openDialog(this.$widget);
}
async importIntoNote(parentNoteId) {
const files = Array.from(this.$fileUploadInput[0].files); // shallow copy since we're resetting the upload button below
2025-01-09 18:07:02 +02:00
const boolToString = ($el) => ($el.is(":checked") ? "true" : "false");
2022-06-16 14:21:24 +02:00
const options = {
safeImport: boolToString(this.$safeImportCheckbox),
shrinkImages: boolToString(this.$shrinkImagesCheckbox),
textImportedAsText: boolToString(this.$textImportedAsTextCheckbox),
codeImportedAsCode: boolToString(this.$codeImportedAsCodeCheckbox),
explodeArchives: boolToString(this.$explodeArchivesCheckbox),
replaceUnderscoresWithSpaces: boolToString(this.$replaceUnderscoresWithSpacesCheckbox)
};
2025-01-09 18:07:02 +02:00
this.$widget.modal("hide");
2022-06-16 14:21:24 +02:00
2025-01-09 18:07:02 +02:00
await importService.uploadFiles("notes", parentNoteId, files, options);
2022-06-16 14:21:24 +02:00
}
}