signal-desktop/js/views/file_input_view.js
lilia 99a2685f93 Store attachments as binary blobs
Move base64 encoding of attachments to an AttachmentView. This makes
image rendering an asynchronous task so we fire an update event to
indicate to the parent MessageListView that its content has changed
height and it is time to scroll down.
2014-11-25 12:42:44 -08:00

54 lines
1.6 KiB
JavaScript

var Whisper = Whisper || {};
(function () {
'use strict';
Whisper.FileInputView = Backbone.View.extend({
tagName: 'span',
className: 'file-input',
initialize: function() {
this.$input = this.$el.find('input[type=file]');
},
events: {
'change': 'previewImages'
},
addThumb: function(e) {
this.$el.append($('<img>').attr( "src", e.target.result ));
},
previewImages: function() {
this.$el.find('img').remove();
var files = this.$input.prop('files');
for (var i = 0; i < files.length; i++) {
var FR = new FileReader();
FR.onload = this.addThumb.bind(this);
FR.readAsDataURL(files[i]);
}
},
hasFiles: function() {
var files = this.$input.prop('files');
return files && files.length && files.length > 0;
},
getFiles: function() {
var promises = [];
var files = this.$input.prop('files');
for (var i = 0; i < files.length; i++) {
var contentType = files[i].type;
var p = new Promise(function(resolve, reject) {
var FR = new FileReader();
FR.onload = function(e) {
resolve({data: e.target.result, contentType: contentType});
};
FR.readAsArrayBuffer(files[i]);
}.bind(this));
promises.push(p);
}
this.$el.find('img').remove();
return Promise.all(promises);
}
});
})();