import server from "../../services/server.js";
import NoteContextAwareWidget from "../note_context_aware_widget.js";
import toastService from "../../services/toast.js";
import openService from "../../services/open.js";
import utils from "../../services/utils.js";
import protectedSessionHolder from "../../services/protected_session_holder.js";
import { t } from "../../services/i18n.js";
import type FNote from "../../entities/fnote.js";
const TPL = /*html*/`
`;
export default class FilePropertiesWidget extends NoteContextAwareWidget {
private $fileNoteId!: JQuery;
private $fileName!: JQuery;
private $fileType!: JQuery;
private $fileSize!: JQuery;
private $downloadButton!: JQuery;
private $openButton!: JQuery;
private $uploadNewRevisionButton!: JQuery;
private $uploadNewRevisionInput!: JQuery;
get name() {
return "fileProperties";
}
get toggleCommand() {
return "toggleRibbonTabFileProperties";
}
isEnabled() {
return this.note && this.note.type === "file";
}
getTitle() {
return {
show: this.isEnabled(),
activate: true,
title: t("file_properties.title"),
icon: "bx bx-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", () => this.noteId && openService.downloadFileNote(this.noteId));
this.$openButton.on("click", () => this.noteId && this.note && openService.openNoteExternally(this.noteId, this.note.mime));
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 result = await server.upload(`notes/${this.noteId}/file`, fileToUpload);
if (result.uploaded) {
toastService.showMessage(t("file_properties.upload_success"));
this.refresh();
} else {
toastService.showError(t("file_properties.upload_failed"));
}
});
}
async refreshWithNote(note: FNote) {
this.$widget.show();
if (!this.note) {
return;
}
this.$fileNoteId.text(note.noteId);
this.$fileName.text(note.getLabelValue("originalFileName") || "?");
this.$fileType.text(note.mime);
const blob = await this.note.getBlob();
this.$fileSize.text(utils.formatSize(blob?.contentLength ?? 0));
// open doesn't work for protected notes since it works through a browser which isn't in protected session
this.$openButton.toggle(!note.isProtected);
this.$downloadButton.toggle(!note.isProtected || protectedSessionHolder.isProtectedSessionAvailable());
this.$uploadNewRevisionButton.toggle(!note.isProtected || protectedSessionHolder.isProtectedSessionAvailable());
}
}