import utils from '../../services/utils.js'; import treeService from "../../services/tree.js"; import importService from "../../services/import.js"; import options from "../../services/options.js"; import BasicWidget from "../basic_widget.js"; const TPL = ` `; export default class ImportDialog extends BasicWidget { constructor() { super(); this.parentNoteId = null; } doRender() { this.$widget = $(TPL); 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"); this.$form.on('submit', () => { // disabling so that import is not triggered again. this.$importButton.attr("disabled", "disabled"); this.importIntoNote(this.parentNoteId); return false; }); this.$fileUploadInput.on('change', () => { if (this.$fileUploadInput.val()) { this.$importButton.removeAttr("disabled"); } else { this.$importButton.attr("disabled", "disabled"); } }); this.$widget.find('[data-toggle="tooltip"]').tooltip({ html: true }); } async showImportDialogEvent({noteId}) { this.parentNoteId = noteId; this.$fileUploadInput.val('').trigger('change'); // to trigger Import button disabling listener below this.$safeImportCheckbox.prop("checked", true); this.$shrinkImagesCheckbox.prop("checked", options.is('compressImages')); 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 const boolToString = $el => $el.is(":checked") ? "true" : "false"; 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) }; this.$widget.modal('hide'); await importService.uploadFiles(parentNoteId, files, options); } }