szurubooru/public_html/js/Controls/FileDropper.js

46 lines
1.1 KiB
JavaScript
Raw Normal View History

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) {
addFiles(this.files);
});
2014-09-07 09:57:01 +02:00
$dropDiv.on('dragenter', function(e) {
$dropDiv.addClass('active');
2014-09-07 09:57:01 +02:00
}).on('dragleave', function(e) {
$dropDiv.removeClass('active');
2014-09-07 09:57:01 +02:00
}).on('dragover', function(e) {
e.preventDefault();
2014-09-07 09:57:01 +02:00
}).on('drop', function(e) {
e.preventDefault();
addFiles(e.originalEvent.dataTransfer.files);
2014-09-07 09:57:01 +02:00
}).on('click', function(e) {
$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.');
return;
}
onChange(files);
}
2014-09-08 22:02:28 +02:00
};
App.DI.register('fileDropper', App.Controls.FileDropper);