{I" class:ETI"ProcessedAsset; FI"logical_path; TI"upload.js; FI" pathname; TI"H/Users/jching/code/file_upload/app/assets/javascripts/upload.js.erb; FI"content_type; TI"application/javascript; TI" mtime; Tl+PțRI"length; TiI"digest; TI"%972df3918f413bead0f13843fa45f636; FI"source; TI" $(function() { // prevent default browser drapdrop behavior $(document).bind('drop dragover', function (e) { e.preventDefault(); }); function setProgress(item, percent) { item.attr('aria-valuenow', percent) .find(".bar").css("width", percent + "%"); } function setupFileField(inp) { var input = $(inp), form = input.parents("form"), attributeName = input.attr("data-attr-name"), modelName = input.attr("data-model-name"), container = form.find("." + attributeName), list = container.find("ul"), multiple = input.attr("multiple"), preview = form.find(input.attr("data-preview") || ".preview"), add = form.find(".add"), inputPrefix = modelName + "[" + attributeName + "_attributes]", inputName, autosubmit = input.attr("data-autosubmit"), url = input.attr("data-file-upload"); if(multiple) { inputName = inputPrefix + "[REPLACE_ID][key]"; } else { inputName = inputPrefix + "[key]"; } input.fileupload({ url: url, dataType: 'json', maxFileSize: 5000000, formData: function(form) { return []; }, type: 'POST', // this is called after the files have been uploaded done: function(e, data) { // replace the input with a new one replaceInput(this); if(preview.length > 0) { window.loadImage(data.files[0], function(img) { preview.html(img); }); } // add field with file id $.each(data.result.files, function(i, file) { // load preview if(multiple) { // add checkbox field var item = data.files[i].item; item.find(".progress").replaceWith( $('') .attr('name', inputName.replace("REPLACE_ID", Math.floor(Math.random() * 1000000))) .val(file.id) ); } else { // add key hidden field container.find(".inputs .progress").replaceWith(''); } }); if(autosubmit) { form.submit(); } }, fail: function(e, data){ replaceInput(this); if (data.jqXHR.status === 413) { message = 'The size limit is 10MB.'; } else { message = 'Upload failed, please try again.'; } container.find('.error').html(message); // data.files is 1-length array with the file of the ajax event $.each(data.files, function(i, file) { var item = data.files[i].item; item.remove(); }); }, // this is called after selecting a file add: function(e, data) { // clear errors container.find('.error').html(''); $.each(data.files, function(i, file) { if(multiple) { file.item = $("
").append(''); file.item.find('label').text(file.name + " (" + (Math.round(file.size/10.24)/100) + "kB)" ) .prepend($("