Notes/src/public/app/widgets/buttons/attachments_actions.js

162 lines
6.8 KiB
JavaScript
Raw Normal View History

import { t } from "../../services/i18n.js";
2023-03-24 10:57:32 +01:00
import BasicWidget from "../basic_widget.js";
2023-03-30 23:48:26 +02:00
import server from "../../services/server.js";
2023-04-01 13:58:53 +02:00
import dialogService from "../../services/dialog.js";
2023-04-01 23:55:04 +02:00
import toastService from "../../services/toast.js";
import ws from "../../services/ws.js";
import appContext from "../../components/app_context.js";
2023-05-03 10:23:20 +02:00
import openService from "../../services/open.js";
2023-09-07 06:33:51 +00:00
import utils from "../../services/utils.js";
2023-03-24 10:57:32 +01:00
const TPL = `
<div class="dropdown attachment-actions">
<style>
.attachment-actions {
width: 35px;
height: 35px;
}
.attachment-actions .dropdown-menu {
2023-04-01 23:55:04 +02:00
width: 20em;
2023-03-24 10:57:32 +01:00
}
.attachment-actions .dropdown-item[disabled], .attachment-actions .dropdown-item[disabled]:hover {
color: var(--muted-text-color) !important;
background-color: transparent !important;
pointer-events: none; /* makes it unclickable */
}
</style>
<button type="button" data-bs-toggle="dropdown" aria-haspopup="true"
2023-06-14 00:28:59 +02:00
aria-expanded="false" class="icon-action icon-action-always-border bx bx-dots-vertical-rounded"
2023-06-30 12:14:58 +02:00
style="position: relative; top: 3px;"></button>
2023-03-24 10:57:32 +01:00
<div class="dropdown-menu dropdown-menu-right">
<a data-trigger-command="openAttachment" class="dropdown-item"
title="${t('attachments_actions.open_externally_title')}">${t('attachments_actions.open_externally')}</a>
2023-09-07 06:33:51 +00:00
<a data-trigger-command="openAttachmentCustom" class="dropdown-item"
title="${t('attachments_actions.open_custom_title')}">${t('attachments_actions.open_custom')}</a>
<a data-trigger-command="downloadAttachment" class="dropdown-item">${t('attachments_actions.download')}</a>
<a data-trigger-command="renameAttachment" class="dropdown-item">${t('attachments_actions.rename_attachment')}</a>
<a data-trigger-command="uploadNewAttachmentRevision" class="dropdown-item">${t('attachments_actions.upload_new_revision')}</a>
<a data-trigger-command="copyAttachmentLinkToClipboard" class="dropdown-item">${t('attachments_actions.copy_link_to_clipboard')}</a>
<a data-trigger-command="convertAttachmentIntoNote" class="dropdown-item">${t('attachments_actions.convert_attachment_into_note')}</a>
<a data-trigger-command="deleteAttachment" class="dropdown-item">${t('attachments_actions.delete_attachment')}</a>
2023-03-24 10:57:32 +01:00
</div>
2023-05-03 22:49:24 +02:00
<input type="file" class="attachment-upload-new-revision-input" style="display: none">
2023-03-24 10:57:32 +01:00
</div>`;
export default class AttachmentActionsWidget extends BasicWidget {
constructor(attachment, isFullDetail) {
2023-03-24 10:57:32 +01:00
super();
this.attachment = attachment;
this.isFullDetail = isFullDetail;
2023-03-24 10:57:32 +01:00
}
2023-05-03 10:23:20 +02:00
get attachmentId() {
return this.attachment.attachmentId;
}
2023-03-24 10:57:32 +01:00
doRender() {
this.$widget = $(TPL);
this.dropdown = bootstrap.Dropdown.getOrCreateInstance(this.$widget.find("[data-bs-toggle='dropdown']"));
this.$widget.on('click', '.dropdown-item', () => this.dropdown.toggle());
2023-05-03 22:49:24 +02:00
this.$uploadNewRevisionInput = this.$widget.find(".attachment-upload-new-revision-input");
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(`attachments/${this.attachmentId}/file`, fileToUpload);
2023-05-03 22:49:24 +02:00
if (result.uploaded) {
toastService.showMessage(t('attachments_actions.upload_success'));
2023-05-03 22:49:24 +02:00
} else {
toastService.showError(t('attachments_actions.upload_failed'));
2023-05-03 22:49:24 +02:00
}
});
const isElectron = utils.isElectron();
if (!this.isFullDetail) {
const $openAttachmentButton = this.$widget.find("[data-trigger-command='openAttachment']");
$openAttachmentButton
.addClass("disabled")
.append($('<span class="disabled-tooltip"> (?)</span>')
.attr("title", t('attachments_actions.open_externally_detail_page'))
);
if (isElectron) {
const $openAttachmentCustomButton = this.$widget.find("[data-trigger-command='openAttachmentCustom']");
$openAttachmentCustomButton
.addClass("disabled")
.append($('<span class="disabled-tooltip"> (?)</span>')
.attr("title", t('attachments_actions.open_externally_detail_page'))
);
}
2023-09-07 06:33:51 +00:00
}
if (!isElectron) {
2023-09-07 06:33:51 +00:00
const $openAttachmentCustomButton = this.$widget.find("[data-trigger-command='openAttachmentCustom']");
$openAttachmentCustomButton
.addClass("disabled")
.append($('<span class="disabled-tooltip"> (?)</span>')
.attr("title", t('attachments_actions.open_custom_client_only'))
2023-09-07 06:33:51 +00:00
);
}
2023-05-03 10:23:20 +02:00
}
async openAttachmentCommand() {
await openService.openAttachmentExternally(this.attachmentId, this.attachment.mime);
}
2023-09-07 06:33:51 +00:00
async openAttachmentCustomCommand() {
await openService.openAttachmentCustom(this.attachmentId, this.attachment.mime);
}
2023-05-03 10:23:20 +02:00
async downloadAttachmentCommand() {
await openService.downloadAttachment(this.attachmentId);
}
2023-05-03 22:49:24 +02:00
async uploadNewAttachmentRevisionCommand() {
this.$uploadNewRevisionInput.trigger('click');
}
2023-05-26 10:36:05 +02:00
async copyAttachmentLinkToClipboardCommand() {
this.parent.copyAttachmentLinkToClipboard();
2023-05-03 10:23:20 +02:00
}
2023-03-30 23:48:26 +02:00
async deleteAttachmentCommand() {
if (!await dialogService.confirm(t('attachments_actions.delete_confirm', { title: this.attachment.title }))) {
return;
2023-04-01 23:55:04 +02:00
}
2023-05-03 10:23:20 +02:00
await server.remove(`attachments/${this.attachmentId}`);
toastService.showMessage(t('attachments_actions.delete_success', { title: this.attachment.title }));
2023-04-01 23:55:04 +02:00
}
async convertAttachmentIntoNoteCommand() {
if (!await dialogService.confirm(t('attachments_actions.convert_confirm', { title: this.attachment.title }))) {
return;
2023-04-01 13:58:53 +02:00
}
const { note: newNote } = await server.post(`attachments/${this.attachmentId}/convert-to-note`)
toastService.showMessage(t('attachments_actions.convert_success', { title: this.attachment.title }));
await ws.waitForMaxKnownEntityChangeId();
await appContext.tabManager.getActiveContext().setNote(newNote.noteId);
2023-03-24 10:57:32 +01:00
}
2023-06-14 00:28:59 +02:00
async renameAttachmentCommand() {
const attachmentTitle = await dialogService.prompt({
title: t('attachments_actions.rename_attachment'),
message: t('attachments_actions.enter_new_name'),
2023-06-14 00:28:59 +02:00
defaultValue: this.attachment.title
});
if (!attachmentTitle?.trim()) {
return;
}
await server.put(`attachments/${this.attachmentId}/rename`, { title: attachmentTitle });
2023-06-14 00:28:59 +02:00
}
2023-03-24 10:57:32 +01:00
}