keeweb/app/scripts/views/details/details-attachment-view.js

41 lines
1.5 KiB
JavaScript
Raw Normal View History

2017-01-31 07:50:28 +01:00
const Backbone = require('backbone');
const FeatureDetector = require('../../util/feature-detector');
2015-10-17 23:49:24 +02:00
2017-01-31 07:50:28 +01:00
const DetailsAttachmentView = Backbone.View.extend({
2015-12-16 22:50:45 +01:00
template: require('templates/details/details-attachment.hbs'),
2015-10-17 23:49:24 +02:00
2019-08-16 23:05:39 +02:00
events: {},
2015-10-17 23:49:24 +02:00
render: function(complete) {
this.renderTemplate({}, true);
2017-01-31 07:50:28 +01:00
const shortcut = this.$el.find('.details__attachment-preview-download-text-shortcut');
2015-10-17 23:49:24 +02:00
shortcut.html(FeatureDetector.actionShortcutSymbol(false));
2019-08-16 23:05:39 +02:00
const blob = new Blob([this.model.getBinary()], { type: this.model.mimeType });
2017-01-31 07:50:28 +01:00
const dataEl = this.$el.find('.details__attachment-preview-data');
2015-10-17 23:49:24 +02:00
switch ((this.model.mimeType || '').split('/')[0]) {
case 'text':
2017-01-31 07:50:28 +01:00
const reader = new FileReader();
2016-07-17 13:30:38 +02:00
reader.addEventListener('loadend', () => {
2019-08-16 23:05:39 +02:00
$('<pre/>')
.text(reader.result)
.appendTo(dataEl);
2015-10-17 23:49:24 +02:00
complete();
2016-07-17 13:30:38 +02:00
});
2015-10-17 23:49:24 +02:00
reader.readAsText(blob);
return this;
case 'image':
2019-08-16 23:05:39 +02:00
$('<img/>')
.attr('src', URL.createObjectURL(blob))
.appendTo(dataEl);
2015-10-17 23:49:24 +02:00
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;
}
});
module.exports = DetailsAttachmentView;