import server from "../services/server.js"; import TabAwareWidget from "./tab_aware_widget.js"; import toastService from "../services/toast.js"; import openService from "../services/open.js"; import utils from "../services/utils.js"; const TPL = `
\`; `; export default class FilePropertiesWidget extends TabAwareWidget { static getType() { return "file"; } renderTitle(note) { return { show: note.type === 'file', activate: true, $title: 'File' }; } doRender() { this.$widget = $(TPL); this.contentSized(); this.$fileNoteId = this.$widget.find(".file-note-id"); this.$fileName = this.$widget.find(".file-filename"); this.$fileType = this.$widget.find(".file-filetype"); this.$fileSize = this.$widget.find(".file-filesize"); this.$downloadButton = this.$widget.find(".file-download"); this.$openButton = this.$widget.find(".file-open"); this.$uploadNewRevisionButton = this.$widget.find(".file-upload-new-revision"); this.$uploadNewRevisionInput = this.$widget.find(".file-upload-new-revision-input"); this.$downloadButton.on('click', () => openService.downloadFileNote(this.noteId)); this.$openButton.on('click', () => openService.openFileNote(this.noteId)); this.$uploadNewRevisionButton.on("click", () => { this.$uploadNewRevisionInput.trigger("click"); }); this.$uploadNewRevisionInput.on('change', async () => { const fileToUpload = this.$uploadNewRevisionInput[0].files[0]; // copy to allow reset below this.$uploadNewRevisionInput.val(''); const formData = new FormData(); formData.append('upload', fileToUpload); const result = await $.ajax({ url: baseApiUrl + 'notes/' + this.noteId + '/file', headers: await server.getHeaders(), data: formData, type: 'PUT', timeout: 60 * 60 * 1000, contentType: false, // NEEDED, DON'T REMOVE THIS processData: false, // NEEDED, DON'T REMOVE THIS }); if (result.uploaded) { toastService.showMessage("New file revision has been uploaded."); this.refresh(); } else { toastService.showError("Upload of a new file revision failed."); } }); } async refreshWithNote(note) { const attributes = note.getAttributes(); const attributeMap = utils.toObject(attributes, l => [l.name, l.value]); this.$widget.show(); this.$fileNoteId.text(note.noteId); this.$fileName.text(attributeMap.originalFileName || "?"); this.$fileType.text(note.mime); const noteComplement = await this.tabContext.getNoteComplement(); this.$fileSize.text(noteComplement.contentLength + " bytes"); // open doesn't work for protected notes since it works through browser which isn't in protected session this.$openButton.toggle(!note.isProtected); } }