Notes/src/public/app/widgets/attachment_detail.js

144 lines
4.8 KiB
JavaScript
Raw Normal View History

2023-04-01 23:55:04 +02:00
import utils from "../services/utils.js";
import AttachmentActionsWidget from "./buttons/attachments_actions.js";
2023-03-30 23:48:26 +02:00
import BasicWidget from "./basic_widget.js";
2023-04-01 23:55:04 +02:00
import server from "../services/server.js";
2023-03-30 23:48:26 +02:00
const TPL = `
<div class="attachment-detail">
<style>
.attachment-detail-wrapper {
margin-bottom: 20px;
}
.attachment-title-line {
display: flex;
align-items: baseline;
}
.attachment-details {
margin-left: 10px;
}
.attachment-content pre {
background: var(--accented-background-color);
padding: 10px;
margin-top: 10px;
margin-bottom: 10px;
}
2023-04-11 17:45:51 +02:00
.attachment-detail-wrapper.list-view .attachment-content pre {
max-height: 400px;
}
2023-03-30 23:48:26 +02:00
.attachment-content img {
margin: 10px;
2023-04-11 17:45:51 +02:00
}
.attachment-detail-wrapper.list-view .attachment-content img {
2023-03-30 23:48:26 +02:00
max-height: 300px;
max-width: 90%;
object-fit: contain;
}
2023-04-11 17:45:51 +02:00
.attachment-detail-wrapper.full-detail .attachment-content img {
max-width: 90%;
object-fit: contain;
}
2023-03-30 23:48:26 +02:00
</style>
2023-04-01 13:58:53 +02:00
<div class="attachment-detail-wrapper">
<div class="attachment-title-line">
2023-04-11 17:45:51 +02:00
<h4 class="attachment-title"></h4>
2023-04-01 13:58:53 +02:00
<div class="attachment-details"></div>
2023-04-01 23:55:04 +02:00
<div style="flex: 1 1;"></div>
2023-04-01 13:58:53 +02:00
<div class="attachment-actions-container"></div>
</div>
<div class="attachment-content"></div>
</div>
2023-03-30 23:48:26 +02:00
</div>`;
export default class AttachmentDetailWidget extends BasicWidget {
constructor(attachment) {
super();
2023-04-01 23:55:04 +02:00
this.contentSized();
2023-03-30 23:48:26 +02:00
this.attachment = attachment;
2023-04-01 13:58:53 +02:00
this.attachmentActionsWidget = new AttachmentActionsWidget(attachment);
2023-04-11 17:45:51 +02:00
this.isFullDetail = true;
2023-04-01 13:58:53 +02:00
this.child(this.attachmentActionsWidget);
2023-03-30 23:48:26 +02:00
}
doRender() {
this.$widget = $(TPL);
2023-04-01 23:55:04 +02:00
this.refresh();
super.doRender();
}
refresh() {
this.$widget.find('.attachment-detail-wrapper')
.empty()
.append(
$(TPL)
.find('.attachment-detail-wrapper')
.html()
);
2023-03-30 23:48:26 +02:00
this.$wrapper = this.$widget.find('.attachment-detail-wrapper');
2023-04-11 17:45:51 +02:00
this.$wrapper.addClass(this.isFullDetail ? "full-detail" : "list-view");
if (!this.isFullDetail) {
this.$wrapper.find('.attachment-title').append(
$('<a href="javascript:">')
.attr("data-note-path", this.attachment.parentId)
.attr("data-view-mode", "attachments")
.attr("data-attachment-id", this.attachment.attachmentId)
.text(this.attachment.title)
);
} else {
this.$wrapper.find('.attachment-title')
.text(this.attachment.title);
}
2023-04-20 00:11:09 +02:00
const {utcDateScheduledForDeletionSince} = this.attachment;
if (utcDateScheduledForDeletionSince) {
const scheduledSinceTimestamp = utils.parseDate(utcDateScheduledForDeletionSince)?.getTime();
const interval = 3600 * 1000;
const deletionTimestamp = scheduledSinceTimestamp + interval;
const willBeDeletedInSeconds = Math.round((deletionTimestamp - Date.now()) / 1000);
this.$wrapper.find('.attachment-title').append(`Will be deleted in ${willBeDeletedInSeconds} seconds.`);
}
2023-04-01 13:58:53 +02:00
this.$wrapper.find('.attachment-details')
.text(`Role: ${this.attachment.role}, Size: ${utils.formatSize(this.attachment.contentLength)}`);
this.$wrapper.find('.attachment-actions-container').append(this.attachmentActionsWidget.render());
this.$wrapper.find('.attachment-content').append(this.renderContent());
2023-03-30 23:48:26 +02:00
}
2023-04-01 13:58:53 +02:00
renderContent() {
if (this.attachment.content) {
return $("<pre>").text(this.attachment.content);
} else if (this.attachment.role === 'image') {
2023-04-17 22:40:53 +02:00
return `<img src="api/attachments/${this.attachment.attachmentId}/image/${encodeURIComponent(this.attachment.title)}?${this.attachment.utcDateModified}">`;
2023-03-30 23:48:26 +02:00
} else {
return '';
}
}
2023-04-01 23:55:04 +02:00
2023-04-03 23:47:24 +02:00
async entitiesReloadedEvent({loadResults}) {
2023-04-01 23:55:04 +02:00
const attachmentChange = loadResults.getAttachments().find(att => att.attachmentId === this.attachment.attachmentId);
if (attachmentChange) {
if (attachmentChange.isDeleted) {
this.toggleInt(false);
} else {
2023-04-17 23:21:28 +02:00
this.attachment = await server.get(`attachments/${this.attachment.attachmentId}?includeContent=true`);
2023-04-01 23:55:04 +02:00
this.refresh();
}
}
}
2023-03-30 23:48:26 +02:00
}