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

109 lines
3.5 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 {
max-height: 400px;
background: var(--accented-background-color);
padding: 10px;
margin-top: 10px;
margin-bottom: 10px;
}
.attachment-content img {
margin: 10px;
max-height: 300px;
max-width: 90%;
object-fit: contain;
}
</style>
2023-04-01 13:58:53 +02:00
<div class="attachment-detail-wrapper">
<div class="attachment-title-line">
<h4 class="attachment-title"></h4>
<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);
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-01 13:58:53 +02:00
this.$wrapper.find('.attachment-title').text(this.attachment.title);
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') {
return `<img src="api/notes/${this.attachment.parentId}/images/${this.attachment.attachmentId}/${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
async entitiesReloadedEvent({loadResults}) {
console.log("AttachmentDetailWidget: entitiesReloadedEvent");
const attachmentChange = loadResults.getAttachments().find(att => att.attachmentId === this.attachment.attachmentId);
if (attachmentChange) {
if (attachmentChange.isDeleted) {
this.toggleInt(false);
} else {
this.attachment = await server.get(`notes/${this.attachment.parentId}/attachments/${this.attachment.attachmentId}?includeContent=true`);
this.refresh();
}
}
}
2023-03-30 23:48:26 +02:00
}