2015-01-17 00:06:38 +01:00
|
|
|
/* vim: ts=4:sw=4:expandtab
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2014-10-25 03:44:30 +02:00
|
|
|
var Whisper = Whisper || {};
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
'use strict';
|
2015-01-19 02:43:25 +01:00
|
|
|
|
2015-03-03 01:31:04 +01:00
|
|
|
Whisper.FileSizeToast = Whisper.ToastView.extend({
|
|
|
|
template: $('#file-size-modal').html()
|
|
|
|
});
|
|
|
|
|
2014-10-25 03:44:30 +02:00
|
|
|
Whisper.FileInputView = Backbone.View.extend({
|
|
|
|
tagName: 'span',
|
|
|
|
className: 'file-input',
|
|
|
|
initialize: function() {
|
|
|
|
this.$input = this.$el.find('input[type=file]');
|
2015-01-17 00:39:01 +01:00
|
|
|
this.thumb = new Whisper.AttachmentPreviewView();
|
2015-02-06 07:42:16 +01:00
|
|
|
this.$el.addClass('file-input');
|
2014-10-25 03:44:30 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
events: {
|
2015-01-16 03:17:54 +01:00
|
|
|
'change': 'previewImages',
|
2015-01-24 21:00:56 +01:00
|
|
|
'click .close': 'deleteFiles',
|
|
|
|
'click .paperclip': 'open'
|
|
|
|
},
|
|
|
|
|
|
|
|
open: function() {
|
|
|
|
this.$input.click();
|
2014-10-25 03:44:30 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
addThumb: function(e) {
|
2015-01-17 00:39:01 +01:00
|
|
|
this.thumb.src = e.target.result;
|
2015-01-22 10:39:07 +01:00
|
|
|
this.$el.find('.paperclip').append(this.thumb.render().el);
|
2014-10-25 03:44:30 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
previewImages: function() {
|
2015-01-16 03:17:54 +01:00
|
|
|
this.clearForm();
|
2014-10-25 03:44:30 +02:00
|
|
|
var files = this.$input.prop('files');
|
|
|
|
for (var i = 0; i < files.length; i++) {
|
2015-03-03 01:31:04 +01:00
|
|
|
var limitKb = 1000000;
|
|
|
|
switch (files[i].type.split('/')[0]) {
|
|
|
|
case 'image': limitKb = 420; break;
|
|
|
|
case 'audio': limitKb = 32000; break;
|
|
|
|
case 'video': limitKb = 8000; break;
|
|
|
|
}
|
|
|
|
if ((files[i].size/1024).toFixed(4) >= limitKb) {
|
|
|
|
new Whisper.FileSizeToast({
|
|
|
|
model: {limit: limitKb}
|
|
|
|
}).render();
|
2015-01-16 03:17:54 +01:00
|
|
|
this.deleteFiles();
|
|
|
|
}
|
|
|
|
else {
|
2015-03-03 03:01:59 +01:00
|
|
|
var FR = new FileReader();
|
2015-01-16 03:17:54 +01:00
|
|
|
FR.onload = this.addThumb.bind(this);
|
|
|
|
FR.readAsDataURL(files[i]);
|
|
|
|
}
|
2014-10-25 03:44:30 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
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++) {
|
2015-03-02 22:55:30 +01:00
|
|
|
promises.push(this.readFile(files[i]));
|
2014-10-25 03:44:30 +02:00
|
|
|
}
|
2015-01-16 03:17:54 +01:00
|
|
|
this.clearForm();
|
2014-10-25 03:44:30 +02:00
|
|
|
return Promise.all(promises);
|
2015-01-16 03:17:54 +01:00
|
|
|
},
|
2014-10-25 03:44:30 +02:00
|
|
|
|
2015-02-19 09:20:22 +01:00
|
|
|
readFile: function(file) {
|
|
|
|
var contentType = file.type;
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
var FR = new FileReader();
|
|
|
|
FR.onload = function(e) {
|
|
|
|
resolve({data: e.target.result, contentType: contentType});
|
|
|
|
};
|
|
|
|
FR.readAsArrayBuffer(file);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-01-16 03:17:54 +01:00
|
|
|
clearForm: function() {
|
2015-01-17 00:39:01 +01:00
|
|
|
this.thumb.remove();
|
2015-01-16 03:17:54 +01:00
|
|
|
},
|
|
|
|
|
2015-01-25 10:18:10 +01:00
|
|
|
deleteFiles: function(e) {
|
|
|
|
if (e) { e.stopPropagation(); }
|
2015-01-16 03:17:54 +01:00
|
|
|
this.clearForm();
|
|
|
|
this.$input.wrap('<form>').parent('form').trigger('reset');
|
|
|
|
this.$input.unwrap();
|
|
|
|
}
|
2014-10-25 03:44:30 +02:00
|
|
|
});
|
|
|
|
})();
|