2014-09-07 00:33:46 +02:00
|
|
|
var App = App || {};
|
|
|
|
App.Controls = App.Controls || {};
|
|
|
|
|
|
|
|
App.Controls.FileDropper = function(
|
|
|
|
$fileInput,
|
|
|
|
allowMultiple,
|
|
|
|
onChange,
|
|
|
|
jQuery) {
|
|
|
|
|
|
|
|
var $dropDiv = jQuery('<div class="file-handler"></div>');
|
|
|
|
$dropDiv.html((allowMultiple ? 'Drop files here!' : 'Drop file here!') + '<br/>Or just click on this box.');
|
|
|
|
$dropDiv.insertBefore($fileInput);
|
|
|
|
$fileInput.attr('multiple', allowMultiple);
|
|
|
|
$fileInput.hide();
|
|
|
|
|
2014-09-07 09:57:01 +02:00
|
|
|
$fileInput.change(function(e) {
|
2014-09-07 00:33:46 +02:00
|
|
|
addFiles(this.files);
|
|
|
|
});
|
|
|
|
|
2014-09-07 09:57:01 +02:00
|
|
|
$dropDiv.on('dragenter', function(e) {
|
2014-09-07 00:33:46 +02:00
|
|
|
$dropDiv.addClass('active');
|
2014-09-07 09:57:01 +02:00
|
|
|
}).on('dragleave', function(e) {
|
2014-09-07 00:33:46 +02:00
|
|
|
$dropDiv.removeClass('active');
|
2014-09-07 09:57:01 +02:00
|
|
|
}).on('dragover', function(e) {
|
2014-09-07 00:33:46 +02:00
|
|
|
e.preventDefault();
|
2014-09-07 09:57:01 +02:00
|
|
|
}).on('drop', function(e) {
|
2014-09-07 00:33:46 +02:00
|
|
|
e.preventDefault();
|
|
|
|
addFiles(e.originalEvent.dataTransfer.files);
|
2014-09-07 09:57:01 +02:00
|
|
|
}).on('click', function(e) {
|
2014-09-07 00:33:46 +02:00
|
|
|
$fileInput.show().focus().trigger('click').hide();
|
|
|
|
$dropDiv.addClass('active');
|
|
|
|
});
|
|
|
|
|
|
|
|
function addFiles(files) {
|
|
|
|
$dropDiv.removeClass('active');
|
|
|
|
if (!allowMultiple && files.length > 1) {
|
2014-09-08 22:02:28 +02:00
|
|
|
window.alert('Cannot select multiple files.');
|
2014-09-07 00:33:46 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
onChange(files);
|
|
|
|
}
|
2014-09-08 22:02:28 +02:00
|
|
|
|
|
|
|
};
|
2014-09-07 00:33:46 +02:00
|
|
|
|
|
|
|
App.DI.register('fileDropper', App.Controls.FileDropper);
|