; (function ($) { 'use strict'; var defaults = { // GENERAL url: '', trackColor: '#eeeeee', progressbarColor: '#6495ed', btnClass: '', btnLabel: '+ Ajouter', // CALLBACKS onuploaded: function () { } }; $.fn.impUploader = function (options) { var uploader = {}, elm = this, files = [], init = function () { var suffix = Math.round(Math.random() * 100000); uploader.settings = $.extend({}, defaults, options); uploader.tag = $(elm); uploader.tag.html(''); uploader.tag.find('input[type=file]').css({display: "none"}); uploader.tag.delegate('input[type=file]', 'change', addfiles); //uploader.tag.delegate('input[type=button]', 'click', upload); uploader.tag.find('label').addClass(uploader.settings.btnClass); }; var upload = function upload(e) { var files = uploader.tag.find('input[type=file]').prop('files'); upl(0); function upl(k){ k = k || 0; var b=k; var file = files[k]; if(files[k]){ var formData = new FormData(); formData.append('file', file); $.ajax({ type: 'post', url: uploader.settings.url, data: formData, dataType: "json", success: function (data) { if(files[b]){ files[b].newfile=data[0]; } if (files.length == k) { var feedback=[]; $.each(uploader.files, function(k){ feedback.push(this.name+"#"+this.newfile); }); uploader.settings.onuploaded(feedback); }else{ setTimeout(function(){ upl(k); },250); } }, error: function (c, o, n) {//gros console.log(c); console.log(o); console.log(n); }, xhr: function () { var myXhr = $.ajaxSettings.xhr(); if (myXhr.upload) { myXhr.upload.addEventListener('progress', function (e) { if (e.lengthComputable) { var percent = Math.floor(e.loaded * 100 / e.total); $('body').find('#pb' + k + ' .progress').css("width", Math.round(percent) + '%'); } else { console.info('Uploaded ' + e.loaded + ' bytes'); // update UI to reflect bytes uploaded } }, false); myXhr.upload.addEventListener('load', function (ev) { k++; }, false); } return myXhr; }, processData: false, contentType: false, //file.type cache: false, enctype: "multipart/form-data" }); } } } var addfiles = function () { uploader.files = $(this).prop('files'); //console.log(uploader.files); var t0 = $('
'); t0.css({"z-index": 2010, top: '40px', left: '-125px', marginLeft: '50%', width: '250px', position: 'fixed', backgroundColor: '#ffffff', padding: '20px', borderRadius: '3px', 'box-shadow': 'inset 0px 1px 3px rgba(0,0,0,0.2)', '-moz-box-shadow': 'inset 0px 1px 3px rgba(0,0,0,0.2)', '-webkit-box-shadow': 'inset 0px 1px 3px rgba(0,0,0,0.2)'}); $.each(uploader.files, function (k) { var file = this; var t1 = $('