1
0
mirror of https://github.com/keeweb/keeweb.git synced 2024-06-25 07:37:46 +02:00
keeweb/app/scripts/views/details/details-attachment-view.js
2019-09-15 14:16:32 +02:00

42 lines
1.5 KiB
JavaScript

import Backbone from 'backbone';
import { Shortcuts } from 'comp/app/shortcuts';
const DetailsAttachmentView = Backbone.View.extend({
template: require('templates/details/details-attachment.hbs'),
events: {},
render(complete) {
this.renderTemplate({}, true);
const shortcut = this.$el.find('.details__attachment-preview-download-text-shortcut');
shortcut.html(Shortcuts.actionShortcutSymbol(false));
const blob = new Blob([this.model.getBinary()], { type: this.model.mimeType });
const dataEl = this.$el.find('.details__attachment-preview-data');
switch ((this.model.mimeType || '').split('/')[0]) {
case 'text': {
const reader = new FileReader();
reader.addEventListener('loadend', () => {
$('<pre/>')
.text(reader.result)
.appendTo(dataEl);
complete();
});
reader.readAsText(blob);
return this;
}
case 'image':
$('<img/>')
.attr('src', URL.createObjectURL(blob))
.appendTo(dataEl);
complete();
return this;
}
this.$el.addClass('details__attachment-preview--empty');
this.$el.find('.details__attachment-preview-icon').addClass('fa-' + this.model.icon);
complete();
return this;
}
});
export { DetailsAttachmentView };