2021-01-30 15:50:46 +01:00
|
|
|
import server from "../../services/server.js";
|
2021-05-22 12:35:41 +02:00
|
|
|
import NoteContextAwareWidget from "../note_context_aware_widget.js";
|
2021-01-30 15:50:46 +01:00
|
|
|
import toastService from "../../services/toast.js";
|
|
|
|
import openService from "../../services/open.js";
|
|
|
|
import utils from "../../services/utils.js";
|
2022-06-13 23:03:56 +02:00
|
|
|
import protectedSessionHolder from "../../services/protected_session_holder.js";
|
2024-08-05 10:32:48 +08:00
|
|
|
import { t } from "../../services/i18n.js";
|
2021-01-30 15:47:24 +01:00
|
|
|
|
|
|
|
const TPL = `
|
|
|
|
<div class="file-properties-widget">
|
|
|
|
<style>
|
|
|
|
.file-table {
|
|
|
|
width: 100%;
|
|
|
|
margin-top: 10px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.file-table th, .file-table td {
|
|
|
|
padding: 5px;
|
|
|
|
overflow-wrap: anywhere;
|
|
|
|
}
|
|
|
|
|
|
|
|
.file-buttons {
|
|
|
|
padding: 10px;
|
|
|
|
display: flex;
|
|
|
|
justify-content: space-evenly;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
|
|
|
|
<table class="file-table">
|
|
|
|
<tr>
|
2024-08-05 10:34:00 +08:00
|
|
|
<th class="text-nowrap">${t("file_properties.note_id")}:</th>
|
2021-01-30 15:47:24 +01:00
|
|
|
<td class="file-note-id"></td>
|
2024-08-05 10:34:00 +08:00
|
|
|
<th class="text-nowrap">${t("file_properties.original_file_name")}:</th>
|
2021-01-30 15:47:24 +01:00
|
|
|
<td class="file-filename"></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
2024-08-05 10:34:00 +08:00
|
|
|
<th class="text-nowrap">${t("file_properties.file_type")}:</th>
|
2021-01-30 15:47:24 +01:00
|
|
|
<td class="file-filetype"></td>
|
2024-08-05 10:34:00 +08:00
|
|
|
<th class="text-nowrap">${t("file_properties.file_size")}:</th>
|
2021-01-30 15:47:24 +01:00
|
|
|
<td class="file-filesize"></td>
|
|
|
|
</tr>
|
|
|
|
|
|
|
|
<tr>
|
|
|
|
<td colspan="4">
|
|
|
|
<div class="file-buttons">
|
2024-08-05 10:32:48 +08:00
|
|
|
<button class="file-download btn btn-sm btn-primary" type="button">${t("file_properties.download")}</button>
|
2021-01-30 15:47:24 +01:00
|
|
|
|
2024-08-05 10:32:48 +08:00
|
|
|
<button class="file-open btn btn-sm btn-primary" type="button">${t("file_properties.open")}</button>
|
2021-01-30 15:47:24 +01:00
|
|
|
|
2024-08-05 10:32:48 +08:00
|
|
|
<button class="file-upload-new-revision btn btn-sm btn-primary">${t("file_properties.upload_new_revision")}</button>
|
2021-01-30 15:47:24 +01:00
|
|
|
|
|
|
|
<input type="file" class="file-upload-new-revision-input" style="display: none">
|
|
|
|
</div>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
2021-01-31 12:15:36 +01:00
|
|
|
</div>`;
|
2021-01-30 15:47:24 +01:00
|
|
|
|
2021-05-22 12:35:41 +02:00
|
|
|
export default class FilePropertiesWidget extends NoteContextAwareWidget {
|
2021-06-27 12:53:05 +02:00
|
|
|
get name() {
|
|
|
|
return "fileProperties";
|
|
|
|
}
|
|
|
|
|
2021-07-05 09:44:41 +02:00
|
|
|
get toggleCommand() {
|
|
|
|
return "toggleRibbonTabFileProperties";
|
|
|
|
}
|
|
|
|
|
2021-01-31 20:07:56 +01:00
|
|
|
isEnabled() {
|
|
|
|
return this.note && this.note.type === 'file';
|
|
|
|
}
|
|
|
|
|
2021-05-23 23:41:01 +02:00
|
|
|
getTitle() {
|
2021-01-30 15:47:24 +01:00
|
|
|
return {
|
2021-01-31 20:07:56 +01:00
|
|
|
show: this.isEnabled(),
|
2021-01-30 15:47:24 +01:00
|
|
|
activate: true,
|
2024-08-05 10:32:48 +08:00
|
|
|
title: t("file_properties.title"),
|
2021-05-23 23:41:01 +02:00
|
|
|
icon: 'bx bx-file'
|
2021-01-30 15:47:24 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
doRender() {
|
|
|
|
this.$widget = $(TPL);
|
2021-06-13 22:55:31 +02:00
|
|
|
this.contentSized();
|
2021-01-30 15:47:24 +01:00
|
|
|
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));
|
2022-09-21 21:41:51 -04:00
|
|
|
this.$openButton.on('click', () => openService.openNoteExternally(this.noteId, this.note.mime));
|
2021-01-30 15:47:24 +01:00
|
|
|
|
|
|
|
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('');
|
|
|
|
|
2023-06-30 15:25:45 +02:00
|
|
|
const result = await server.upload(`notes/${this.noteId}/file`, fileToUpload);
|
2021-01-30 15:47:24 +01:00
|
|
|
|
|
|
|
if (result.uploaded) {
|
2024-08-05 10:32:48 +08:00
|
|
|
toastService.showMessage(t("file_properties.upload_success"));
|
2021-01-30 15:47:24 +01:00
|
|
|
|
|
|
|
this.refresh();
|
2024-08-05 10:32:48 +08:00
|
|
|
} else {
|
|
|
|
toastService.showError(t("file_properties.upload_failed"));
|
2021-01-30 15:47:24 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async refreshWithNote(note) {
|
|
|
|
this.$widget.show();
|
|
|
|
|
|
|
|
this.$fileNoteId.text(note.noteId);
|
2023-06-29 23:32:19 +02:00
|
|
|
this.$fileName.text(note.getLabelValue('originalFileName') || "?");
|
2021-01-30 15:47:24 +01:00
|
|
|
this.$fileType.text(note.mime);
|
|
|
|
|
2023-05-05 22:21:51 +02:00
|
|
|
const blob = await this.note.getBlob();
|
2021-01-30 15:47:24 +01:00
|
|
|
|
2023-06-29 23:32:19 +02:00
|
|
|
this.$fileSize.text(utils.formatSize(blob.contentLength));
|
2021-01-30 15:47:24 +01:00
|
|
|
|
2023-05-05 23:41:11 +02:00
|
|
|
// open doesn't work for protected notes since it works through a browser which isn't in protected session
|
2021-01-30 15:47:24 +01:00
|
|
|
this.$openButton.toggle(!note.isProtected);
|
2024-08-05 10:32:48 +08:00
|
|
|
this.$downloadButton.toggle(!note.isProtected || protectedSessionHolder.isProtectedSessionAvailable());
|
|
|
|
this.$uploadNewRevisionButton.toggle(!note.isProtected || protectedSessionHolder.isProtectedSessionAvailable());
|
2021-01-30 15:47:24 +01:00
|
|
|
}
|
|
|
|
}
|