Refactored file dropper

This commit is contained in:
Marcin Kurczewski
2014-09-25 18:47:44 +02:00
parent d3015627b3
commit 80b7aaf7d1
3 changed files with 40 additions and 18 deletions

View File

@ -3,9 +3,14 @@ App.Controls = App.Controls || {};
App.Controls.FileDropper = function(
$fileInput,
onChange,
_,
jQuery) {
var options = {
onChange: null,
setNames: false,
};
var $dropDiv = jQuery('<div class="file-handler"></div>');
var allowMultiple = $fileInput.attr('multiple');
$dropDiv.html((allowMultiple ? 'Drop files here!' : 'Drop file here!') + '<br/>Or just click on this box.');
@ -37,9 +42,28 @@ App.Controls.FileDropper = function(
window.alert('Cannot select multiple files.');
return;
}
onChange(files);
if (typeof(options.onChange) !== 'undefined') {
options.onChange(files);
}
if (options.setNames && !allowMultiple) {
$dropDiv.text(files[0].name);
}
}
function readAsDataURL(file, callback) {
var reader = new FileReader();
reader.onloadend = function() {
callback(reader.result);
};
reader.readAsDataURL(file);
}
_.extend(options, {
readAsDataURL: readAsDataURL,
});
return options;
};
App.DI.register('fileDropper', App.Controls.FileDropper);