2015-01-16 23:06:38 +00: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 01:44:30 +00:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
2015-03-05 23:44:53 +00:00
|
|
|
window.Whisper = window.Whisper || {};
|
2015-01-19 01:43:25 +00:00
|
|
|
|
2015-03-03 00:31:04 +00:00
|
|
|
Whisper.FileSizeToast = Whisper.ToastView.extend({
|
|
|
|
template: $('#file-size-modal').html()
|
|
|
|
});
|
|
|
|
|
2014-10-25 01:44:30 +00:00
|
|
|
Whisper.FileInputView = Backbone.View.extend({
|
|
|
|
tagName: 'span',
|
|
|
|
className: 'file-input',
|
|
|
|
initialize: function() {
|
|
|
|
this.$input = this.$el.find('input[type=file]');
|
2015-01-16 23:39:01 +00:00
|
|
|
this.thumb = new Whisper.AttachmentPreviewView();
|
2015-02-06 06:42:16 +00:00
|
|
|
this.$el.addClass('file-input');
|
2014-10-25 01:44:30 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
events: {
|
2015-01-16 02:17:54 +00:00
|
|
|
'change': 'previewImages',
|
2015-01-24 20:00:56 +00:00
|
|
|
'click .close': 'deleteFiles',
|
|
|
|
'click .paperclip': 'open'
|
|
|
|
},
|
|
|
|
|
|
|
|
open: function() {
|
|
|
|
this.$input.click();
|
2014-10-25 01:44:30 +00:00
|
|
|
},
|
|
|
|
|
2015-03-03 02:27:14 +00:00
|
|
|
addThumb: function(src) {
|
|
|
|
this.thumb.src = src;
|
2015-01-22 09:39:07 +00:00
|
|
|
this.$el.find('.paperclip').append(this.thumb.render().el);
|
2014-10-25 01:44:30 +00:00
|
|
|
},
|
|
|
|
|
2015-03-04 01:50:24 +00:00
|
|
|
autoScale: function(file) {
|
2015-03-05 00:07:02 +00:00
|
|
|
if (file.type.split('/')[0] !== 'image') {
|
2015-03-04 01:50:24 +00:00
|
|
|
// nothing to do
|
|
|
|
return Promise.resolve(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise(function(resolve, reject) {
|
2015-03-05 02:25:16 +00:00
|
|
|
var url = URL.createObjectURL(file);
|
|
|
|
var img = document.createElement('img');
|
|
|
|
img.onerror = reject;
|
|
|
|
img.onload = function () {
|
|
|
|
URL.revokeObjectURL(url);
|
2015-03-04 01:50:24 +00:00
|
|
|
|
2015-03-05 02:25:16 +00:00
|
|
|
var maxSize = 420 * 1024;
|
|
|
|
var maxHeight = 1920;
|
|
|
|
var maxWidth = 1920;
|
|
|
|
if (img.width <= maxWidth && img.height <= maxHeight &&
|
|
|
|
file.size <= maxSize) {
|
|
|
|
resolve(file);
|
|
|
|
return;
|
|
|
|
}
|
2015-03-04 01:50:24 +00:00
|
|
|
|
2015-03-05 02:25:16 +00:00
|
|
|
// loadImage.scale -> components/blueimp-load-image
|
|
|
|
var canvas = loadImage.scale(img, {
|
|
|
|
canvas: true, maxWidth: 1920, maxHeight: 1920
|
|
|
|
});
|
2015-03-04 01:50:24 +00:00
|
|
|
|
2015-03-05 02:25:16 +00:00
|
|
|
var quality = 0.95;
|
|
|
|
var i = 4;
|
|
|
|
var blob;
|
|
|
|
do {
|
|
|
|
i = i - 1;
|
|
|
|
// dataURLtoBlob -> components/blueimp-canvas-to-blob
|
|
|
|
blob = dataURLtoBlob(
|
|
|
|
canvas.toDataURL('image/jpeg', quality)
|
|
|
|
);
|
|
|
|
quality = quality * maxSize / blob.size;
|
2015-03-04 01:50:24 +00:00
|
|
|
if (quality < 50) {
|
|
|
|
quality = 50;
|
2015-03-05 02:25:16 +00:00
|
|
|
i = 1;
|
2015-03-04 01:50:24 +00:00
|
|
|
}
|
2015-03-05 02:25:16 +00:00
|
|
|
} while (i > 0 && blob.size > maxSize);
|
|
|
|
|
|
|
|
resolve(blob);
|
|
|
|
};
|
|
|
|
img.src = url;
|
2015-03-04 01:50:24 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-10-25 01:44:30 +00:00
|
|
|
previewImages: function() {
|
2015-01-16 02:17:54 +00:00
|
|
|
this.clearForm();
|
2015-03-04 01:50:24 +00:00
|
|
|
var file = this.$input.prop('files')[0];
|
|
|
|
if (!file) { return; }
|
|
|
|
|
|
|
|
var type = file.type.split('/')[0];
|
|
|
|
switch (type) {
|
|
|
|
case 'audio': this.addThumb('/images/audio.png'); break;
|
|
|
|
case 'video': this.addThumb('/images/video.png'); break;
|
|
|
|
case 'image':
|
|
|
|
this.oUrl = URL.createObjectURL(file);
|
|
|
|
this.addThumb(this.oUrl);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.autoScale(file).then(function(blob) {
|
2015-03-03 00:31:04 +00:00
|
|
|
var limitKb = 1000000;
|
2015-03-03 02:27:14 +00:00
|
|
|
switch (type) {
|
2015-03-03 00:31:04 +00:00
|
|
|
case 'image': limitKb = 420; break;
|
|
|
|
case 'audio': limitKb = 32000; break;
|
|
|
|
case 'video': limitKb = 8000; break;
|
|
|
|
}
|
2015-03-04 01:50:24 +00:00
|
|
|
if ((blob.size/1024).toFixed(4) >= limitKb) {
|
2015-03-03 00:31:04 +00:00
|
|
|
new Whisper.FileSizeToast({
|
|
|
|
model: {limit: limitKb}
|
|
|
|
}).render();
|
2015-01-16 02:17:54 +00:00
|
|
|
this.deleteFiles();
|
|
|
|
}
|
2015-03-04 01:50:24 +00:00
|
|
|
}.bind(this));
|
2014-10-25 01:44:30 +00: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-04 01:50:24 +00:00
|
|
|
promises.push(this.getFile(files[i]));
|
2014-10-25 01:44:30 +00:00
|
|
|
}
|
2015-01-16 02:17:54 +00:00
|
|
|
this.clearForm();
|
2014-10-25 01:44:30 +00:00
|
|
|
return Promise.all(promises);
|
2015-01-16 02:17:54 +00:00
|
|
|
},
|
2014-10-25 01:44:30 +00:00
|
|
|
|
2015-03-04 01:50:24 +00:00
|
|
|
getFile: function(file) {
|
|
|
|
file = file || this.$input.prop('files')[0];
|
|
|
|
if (file === undefined) { throw 'No file'; }
|
|
|
|
return this.autoScale(file).then(this.readFile);
|
|
|
|
},
|
|
|
|
|
2015-02-19 08:20:22 +00: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 02:17:54 +00:00
|
|
|
clearForm: function() {
|
2015-03-04 01:50:24 +00:00
|
|
|
if (this.oUrl) {
|
|
|
|
URL.revokeObjectURL(this.oUrl);
|
|
|
|
this.oUrl = null;
|
|
|
|
}
|
2015-01-16 23:39:01 +00:00
|
|
|
this.thumb.remove();
|
2015-01-16 02:17:54 +00:00
|
|
|
},
|
|
|
|
|
2015-01-25 09:18:10 +00:00
|
|
|
deleteFiles: function(e) {
|
|
|
|
if (e) { e.stopPropagation(); }
|
2015-01-16 02:17:54 +00:00
|
|
|
this.clearForm();
|
|
|
|
this.$input.wrap('<form>').parent('form').trigger('reset');
|
|
|
|
this.$input.unwrap();
|
|
|
|
}
|
2014-10-25 01:44:30 +00:00
|
|
|
});
|
|
|
|
})();
|