ordersprinter/webapp/php/3rdparty/hayageek_jquery_uploadfile.js

8 lines
16 KiB
JavaScript

/*!
* jQuery Upload File Plugin
* version: 4.0.10
* @requires jQuery v1.5 or later & form plugin
* Copyright (c) 2013 Ravishanker Kusuma
* http://hayageek.com/
*/
(function(b){if(b.fn.ajaxForm==undefined){b.getScript(("https:"==document.location.protocol?"https://":"http://")+"malsup.github.io/jquery.form.js")}var a={};a.fileapi=b("<input type='file'/>").get(0).files!==undefined;a.formdata=window.FormData!==undefined;b.fn.uploadFile=function(h){var t=b.extend({url:"",method:"POST",enctype:"multipart/form-data",returnType:null,allowDuplicates:true,duplicateStrict:false,allowedTypes:"*",acceptFiles:"*",fileName:"file",formData:false,dynamicFormData:false,maxFileSize:-1,maxFileCount:-1,multiple:true,dragDrop:true,autoSubmit:true,showCancel:true,showAbort:true,showDone:false,showDelete:false,showError:true,showStatusAfterSuccess:true,showStatusAfterError:true,showFileCounter:true,fileCounterStyle:"). ",showFileSize:true,showProgress:false,nestedForms:true,showDownload:false,onLoad:function(s){},onSelect:function(s){return true},onSubmit:function(s,B){},onSuccess:function(C,B,D,s){},onError:function(D,s,C,B){},onCancel:function(B,s){},onAbort:function(B,s){},downloadCallback:false,deleteCallback:false,afterUploadAll:false,serialize:true,sequential:false,sequentialCount:2,customProgressBar:false,abortButtonClass:"ajax-file-upload-abort",cancelButtonClass:"ajax-file-upload-cancel",dragDropContainerClass:"ajax-upload-dragdrop",dragDropHoverClass:"state-hover",errorClass:"ajax-file-upload-error",uploadButtonClass:"ajax-file-upload",dragDropStr:"<span><b>Drag &amp; Drop Files</b></span>",uploadStr:"Upload",abortStr:"Abort",cancelStr:"Cancel",deletelStr:"Delete",doneStr:"Done",multiDragErrorStr:"Multiple File Drag &amp; Drop is not allowed.",extErrorStr:"is not allowed. Allowed extensions: ",duplicateErrorStr:"is not allowed. File already exists.",sizeErrorStr:"is not allowed. Allowed Max size: ",uploadErrorStr:"Upload is not allowed",maxFileCountErrorStr:" is not allowed. Maximum allowed files are:",downloadStr:"Download",customErrorKeyStr:"jquery-upload-file-error",showQueueDiv:false,statusBarWidth:400,dragdropWidth:400,showPreview:false,previewHeight:"auto",previewWidth:"100%",extraHTML:false,uploadQueueOrder:"top",headers:{}},h);this.fileCounter=1;this.selectedFiles=0;var g="ajax-file-upload-"+(new Date().getTime());this.formGroup=g;this.errorLog=b("<div></div>");this.responses=[];this.existingFileNames=[];if(!a.formdata){t.dragDrop=false}if(!a.formdata||t.maxFileCount===1){t.multiple=false}b(this).html("");var p=this;var v=b("<div>"+t.uploadStr+"</div>");b(v).addClass(t.uploadButtonClass);(function o(){if(b.fn.ajaxForm){if(t.dragDrop){var s=b('<div class="'+t.dragDropContainerClass+'" style="vertical-align:top;"></div>').width(t.dragdropWidth);b(p).append(s);b(s).append(v);b(s).append(b(t.dragDropStr));c(p,t,s)}else{b(p).append(v)}b(p).append(p.errorLog);if(t.showQueueDiv){p.container=b("#"+t.showQueueDiv)}else{p.container=b("<div class='ajax-file-upload-container'></div>").insertAfter(b(p))}t.onLoad.call(this,p);y(p,g,t,v)}else{window.setTimeout(o,10)}})();this.startUpload=function(){b("form").each(function(B,s){if(b(this).hasClass(p.formGroup)){q.push(b(this))}});if(q.length>=1){f()}};this.getFileCount=function(){return p.selectedFiles};this.stopUpload=function(){b("."+t.abortButtonClass).each(function(B,s){if(b(this).hasClass(p.formGroup)){b(this).click()}});b("."+t.cancelButtonClass).each(function(B,s){if(b(this).hasClass(p.formGroup)){b(this).click()}})};this.cancelAll=function(){b("."+t.cancelButtonClass).each(function(B,s){if(b(this).hasClass(p.formGroup)){b(this).click()}})};this.update=function(s){t=b.extend(t,s)};this.enqueueFile=function(s){if(!(s instanceof File)){return}var B=[s];w(t,p,B)};this.reset=function(s){p.fileCounter=1;p.selectedFiles=0;p.errorLog.html("");if(s!=false){p.container.html("")}};this.remove=function(){p.container.html("");b(p).remove()};this.createProgress=function(C,E,D){var B=new n(this,t);B.progressDiv.show();B.progressbar.width("100%");var s="";if(t.showFileCounter){s=p.fileCounter+t.fileCounterStyle+C}else{s=C}if(t.showFileSize){s+=" ("+x(D)+")"}B.filename.html(s);p.fileCounter++;p.selectedFiles++;if(t.showPreview){B.preview.attr("src",E);B.preview.show()}if(t.showDownload){B.download.show();B.download.click(function(){if(t.downloadCallback){t.downloadCallback.call(p,[C],B)}})}if(t.showDelete){B.del.show();B.del.click(function(){B.statusbar.hide().remove();var F=[C];if(t.deleteCallback){t.deleteCallback.call(this,F,B)}p.selectedFiles-=1;e(t,p)})}return B};this.getResponses=function(){return this.responses};var q=[];var u=[];var d=false;function f(){if(d){return}d=true;(function s(){if(!t.sequential){t.sequentialCount=99999}if(q.length==0&&u.length==0){if(t.afterUploadAll){t.afterUploadAll(p)}d=false}else{if(u.length<t.sequentialCount){var B=q.shift();if(B!=undefined){u.push(B);B.removeClass(p.formGroup);B.submit()}}window.setTimeout(s,100)}})()}function c(D,B,C){C.on("dragenter",function(s){s.stopPropagation();s.preventDefault();b(this).addClass(B.dragDropHoverClass)});C.on("dragover",function(E){E.stopPropagation();E.preventDefault();var s=b(this);if(s.hasClass(B.dragDropContainerClass)&&!s.hasClass(B.dragDropHoverClass)){s.addClass(B.dragDropHoverClass)}});C.on("drop",function(E){E.preventDefault();b(this).removeClass(B.dragDropHoverClass);D.errorLog.html("");var s=E.originalEvent.dataTransfer.files;if(!B.multiple&&s.length>1){if(B.showError){b("<div class='"+B.errorClass+"'>"+B.multiDragErrorStr+"</div>").appendTo(D.errorLog)}return}if(B.onSelect(s)==false){return}w(B,D,s)});C.on("dragleave",function(s){b(this).removeClass(B.dragDropHoverClass)});b(document).on("dragenter",function(s){s.stopPropagation();s.preventDefault()});b(document).on("dragover",function(E){E.stopPropagation();E.preventDefault();var s=b(this);if(!s.hasClass(B.dragDropContainerClass)){s.removeClass(B.dragDropHoverClass)}});b(document).on("drop",function(s){s.stopPropagation();s.preventDefault();b(this).removeClass(B.dragDropHoverClass)})}function x(s){var C="";var B=s/1024;if(parseInt(B)>1024){var D=B/1024;C=D.toFixed(2)+" MB"}else{C=B.toFixed(2)+" KB"}return C}function m(E){var F=[];if(jQuery.type(E)=="string"){F=E.split("&")}else{F=b.param(E).split("&")}var B=F.length;var s=[];var D,C;for(D=0;D<B;D++){F[D]=F[D].replace(/\+/g," ");C=F[D].split("=");s.push([decodeURIComponent(C[0]),decodeURIComponent(C[1])])}return s}function i(O,J,B){var K=b.extend({},O);var D=new FormData();var E=[];var H=O.fileName.replace("[]","");var N="";for(var I=0;I<B.length;I++){if(!j(J,O,B[I].name)){if(O.showError){b("<div><font color='red'><b>"+B[I].name+"</b> "+O.extErrorStr+O.allowedTypes+"</font></div>").appendTo(J.errorLog)}continue}if(O.maxFileSize!=-1&&B[I].size>O.maxFileSize){if(O.showError){b("<div><font color='red'><b>"+B[I].name+"</b> "+O.sizeErrorStr+x(O.maxFileSize)+"</font></div>").appendTo(J.errorLog)}continue}D.append(H+"[]",B[I]);E.push(B[I].name);N+=J.fileCounter+"). "+B[I].name+"<br>";J.fileCounter++}if(E.length==0){return}var F=O.formData;if(F){var M=m(F);for(var G=0;G<M.length;G++){if(M[G]){D.append(M[G][0],M[G][1])}}}K.fileData=D;var L=new n(J,O);L.filename.html(N);var C=b("<form style='display:block; position:absolute;left: 150px;' class='"+J.formGroup+"' method='"+O.method+"' action='"+O.url+"' enctype='"+O.enctype+"'></form>");C.appendTo("body");A(C,K,L,E,J)}function w(O,I,B){for(var J=0;J<B.length;J++){if(!j(I,O,B[J].name)){if(O.showError){b("<div class='"+O.errorClass+"'><b>"+B[J].name+"</b> "+O.extErrorStr+O.allowedTypes+"</div>").appendTo(I.errorLog)}continue}if(!O.allowDuplicates&&r(I,B[J].name)){if(O.showError){b("<div class='"+O.errorClass+"'><b>"+B[J].name+"</b> "+O.duplicateErrorStr+"</div>").appendTo(I.errorLog)}continue}if(O.maxFileSize!=-1&&B[J].size>O.maxFileSize){if(O.showError){b("<div class='"+O.errorClass+"'><b>"+B[J].name+"</b> "+O.sizeErrorStr+x(O.maxFileSize)+"</div>").appendTo(I.errorLog)}continue}if(O.maxFileCount!=-1&&I.selectedFiles>=O.maxFileCount){if(O.showError){b("<div class='"+O.errorClass+"'><b>"+B[J].name+"</b> "+O.maxFileCountErrorStr+O.maxFileCount+"</div>").appendTo(I.errorLog)}continue}I.selectedFiles++;I.existingFileNames.push(B[J].name);var K=b.extend({},O);var D=new FormData();var H=O.fileName.replace("[]","");D.append(H,B[J]);var F=O.formData;if(F){var M=m(F);for(var G=0;G<M.length;G++){if(M[G]){D.append(M[G][0],M[G][1])}}}K.fileData=D;var L=new n(I,O);var N="";if(O.showFileCounter){N=I.fileCounter+O.fileCounterStyle+B[J].name}else{N=B[J].name}if(O.showFileSize){N+=" ("+x(B[J].size)+")"}L.filename.html(N);var C=b("<form style='display:block; position:absolute;left: 150px;' class='"+I.formGroup+"' method='"+O.method+"' action='"+O.url+"' enctype='"+O.enctype+"'></form>");C.appendTo("body");var E=[];E.push(B[J].name);A(C,K,L,E,I,B[J]);I.fileCounter++}}function j(D,C,F){var E=C.allowedTypes.toLowerCase().split(/[\s,]+/g);var B=F.split(".").pop().toLowerCase();if(C.allowedTypes!="*"&&jQuery.inArray(B,E)<0){return false}return true}function r(D,B){var C=false;if(D.existingFileNames.length){for(var s=0;s<D.existingFileNames.length;s++){if(D.existingFileNames[s]==B||t.duplicateStrict&&D.existingFileNames[s].toLowerCase()==B.toLowerCase()){C=true}}}return C}function l(B,C){if(B.existingFileNames.length){for(var s=0;s<C.length;s++){var D=B.existingFileNames.indexOf(C[s]);if(D!=-1){B.existingFileNames.splice(D,1)}}}}function z(B,C){if(B){C.show();var s=new FileReader();s.onload=function(D){C.attr("src",D.target.result)};s.readAsDataURL(B)}}function e(B,D){if(B.showFileCounter){var C=b(D.container).find(".ajax-file-upload-filename").length;D.fileCounter=C+1;b(D.container).find(".ajax-file-upload-filename").each(function(H,F){var s=b(this).html().split(B.fileCounterStyle);var E=parseInt(s[0])-1;var G=C+B.fileCounterStyle+s[1];b(this).html(G);C--})}}function y(H,G,D,B){var I="ajax-upload-id-"+(new Date().getTime());var F=b("<form method='"+D.method+"' action='"+D.url+"' enctype='"+D.enctype+"'></form>");var C="<input type='file' id='"+I+"' name='"+D.fileName+"' accept='"+D.acceptFiles+"'/>";if(D.multiple){if(D.fileName.indexOf("[]")!=D.fileName.length-2){D.fileName+="[]"}C="<input type='file' id='"+I+"' name='"+D.fileName+"' accept='"+D.acceptFiles+"' multiple/>"}var E=b(C).appendTo(F);E.change(function(){H.errorLog.html("");var P=D.allowedTypes.toLowerCase().split(",");var L=[];if(this.files){for(M=0;M<this.files.length;M++){L.push(this.files[M].name)}if(D.onSelect(this.files)==false){return}}else{var N=b(this).val();var K=[];L.push(N);if(!j(H,D,N)){if(D.showError){b("<div class='"+D.errorClass+"'><b>"+N+"</b> "+D.extErrorStr+D.allowedTypes+"</div>").appendTo(H.errorLog)}return}K.push({name:N,size:"NA"});if(D.onSelect(K)==false){return}}e(D,H);B.unbind("click");F.hide();y(H,G,D,B);F.addClass(G);if(D.serialize&&a.fileapi&&a.formdata){F.removeClass(G);var O=this.files;F.remove();w(D,H,O)}else{var J="";for(var M=0;M<L.length;M++){if(D.showFileCounter){J+=H.fileCounter+D.fileCounterStyle+L[M]+"<br>"}else{J+=L[M]+"<br>"}H.fileCounter++}if(D.maxFileCount!=-1&&(H.selectedFiles+L.length)>D.maxFileCount){if(D.showError){b("<div class='"+D.errorClass+"'><b>"+J+"</b> "+D.maxFileCountErrorStr+D.maxFileCount+"</div>").appendTo(H.errorLog)}return}H.selectedFiles+=L.length;var s=new n(H,D);s.filename.html(J);A(F,D,s,L,H,null)}});if(D.nestedForms){F.css({margin:0,padding:0});B.css({position:"relative",overflow:"hidden",cursor:"default"});E.css({position:"absolute",cursor:"pointer",top:"0px",width:"100%",height:"100%",left:"0px","z-index":"100",opacity:"0.0",filter:"alpha(opacity=0)","-ms-filter":"alpha(opacity=0)","-khtml-opacity":"0.0","-moz-opacity":"0.0"});F.appendTo(B)}else{F.appendTo(b("body"));F.css({margin:0,padding:0,display:"block",position:"absolute",left:"-250px"});if(navigator.appVersion.indexOf("MSIE ")!=-1){B.attr("for",I)}else{B.click(function(){E.click()})}}}function k(C,B){this.statusbar=b("<div class='ajax-file-upload-statusbar'></div>").width(B.statusBarWidth);this.preview=b("<img class='ajax-file-upload-preview' />").width(B.previewWidth).height(B.previewHeight).appendTo(this.statusbar).hide();this.filename=b("<div class='ajax-file-upload-filename'></div>").appendTo(this.statusbar);this.progressDiv=b("<div class='ajax-file-upload-progress'>").appendTo(this.statusbar).hide();this.progressbar=b("<div class='ajax-file-upload-bar'></div>").appendTo(this.progressDiv);this.abort=b("<div>"+B.abortStr+"</div>").appendTo(this.statusbar).hide();this.cancel=b("<div>"+B.cancelStr+"</div>").appendTo(this.statusbar).hide();this.done=b("<div>"+B.doneStr+"</div>").appendTo(this.statusbar).hide();this.download=b("<div>"+B.downloadStr+"</div>").appendTo(this.statusbar).hide();this.del=b("<div>"+B.deletelStr+"</div>").appendTo(this.statusbar).hide();this.abort.addClass("ajax-file-upload-red");this.done.addClass("ajax-file-upload-green");this.download.addClass("ajax-file-upload-green");this.cancel.addClass("ajax-file-upload-red");this.del.addClass("ajax-file-upload-red");return this}function n(D,B){var C=null;if(B.customProgressBar){C=new B.customProgressBar(D,B)}else{C=new k(D,B)}C.abort.addClass(D.formGroup);C.abort.addClass(B.abortButtonClass);C.cancel.addClass(D.formGroup);C.cancel.addClass(B.cancelButtonClass);if(B.extraHTML){C.extraHTML=b("<div class='extrahtml'>"+B.extraHTML()+"</div>").insertAfter(C.filename)}if(B.uploadQueueOrder=="replace"){b(D.container).html(C.statusbar)}else{if(B.uploadQueueOrder=="bottom"){b(D.container).append(C.statusbar)}else{b(D.container).prepend(C.statusbar)}}return C}function A(H,G,B,D,I,F){var E=null;var C={cache:false,contentType:false,processData:false,forceSync:false,type:G.method,data:G.formData,formData:G.fileData,dataType:G.returnType,headers:G.headers,beforeSubmit:function(M,J,L){if(G.onSubmit.call(this,D)!=false){if(G.dynamicFormData){var s=m(G.dynamicFormData());if(s){for(var K=0;K<s.length;K++){if(s[K]){if(G.fileData!=undefined){L.formData.append(s[K][0],s[K][1])}else{L.data[s[K][0]]=s[K][1]}}}}}if(G.extraHTML){b(B.extraHTML).find("input,select,textarea").each(function(O,N){if(G.fileData!=undefined){L.formData.append(b(this).attr("name"),b(this).val())}else{L.data[b(this).attr("name")]=b(this).val()}})}return true}B.statusbar.append("<div class='"+G.errorClass+"'>"+G.uploadErrorStr+"</div>");B.cancel.show();H.remove();B.cancel.click(function(){q.splice(q.indexOf(H),1);l(I,D);B.statusbar.remove();G.onCancel.call(I,D,B);I.selectedFiles-=D.length;e(G,I)});return false},beforeSend:function(K,J){for(var s in J.headers){K.setRequestHeader(s,J.headers[s])}B.progressDiv.show();B.cancel.hide();B.done.hide();if(G.showAbort){B.abort.show();B.abort.click(function(){l(I,D);K.abort();I.selectedFiles-=D.length;G.onAbort.call(I,D,B)})}if(!a.formdata){B.progressbar.width("5%")}else{B.progressbar.width("1%")}},uploadProgress:function(M,s,L,K){if(K>98){K=98}var J=K+"%";if(K>1){B.progressbar.width(J)}if(G.showProgress){B.progressbar.html(J);B.progressbar.css("text-align","center")}},success:function(J,s,L){B.cancel.remove();u.pop();if(G.returnType=="json"&&b.type(J)=="object"&&J.hasOwnProperty(G.customErrorKeyStr)){B.abort.hide();var K=J[G.customErrorKeyStr];G.onError.call(this,D,200,K,B);if(G.showStatusAfterError){B.progressDiv.hide();B.statusbar.append("<span class='"+G.errorClass+"'>ERROR: "+K+"</span>")}else{B.statusbar.hide();B.statusbar.remove()}I.selectedFiles-=D.length;H.remove();return}I.responses.push(J);B.progressbar.width("100%");if(G.showProgress){B.progressbar.html("100%");B.progressbar.css("text-align","center")}B.abort.hide();G.onSuccess.call(this,D,J,L,B);if(G.showStatusAfterSuccess){if(G.showDone){B.done.show();B.done.click(function(){B.statusbar.hide("slow");B.statusbar.remove()})}else{B.done.hide()}if(G.showDelete){B.del.show();B.del.click(function(){l(I,D);B.statusbar.hide().remove();if(G.deleteCallback){G.deleteCallback.call(this,J,B)}I.selectedFiles-=D.length;e(G,I)})}else{B.del.hide()}}else{B.statusbar.hide("slow");B.statusbar.remove()}if(G.showDownload){B.download.show();B.download.click(function(){if(G.downloadCallback){G.downloadCallback(J,B)}})}H.remove()},error:function(K,s,J){B.cancel.remove();u.pop();B.abort.hide();if(K.statusText=="abort"){B.statusbar.hide("slow").remove();e(G,I)}else{G.onError.call(this,D,s,J,B);if(G.showStatusAfterError){B.progressDiv.hide();B.statusbar.append("<span class='"+G.errorClass+"'>ERROR: "+J+"</span>")}else{B.statusbar.hide();B.statusbar.remove()}I.selectedFiles-=D.length}H.remove()}};if(G.showPreview&&F!=null){if(F.type.toLowerCase().split("/").shift()=="image"){z(F,B.preview)}}if(G.autoSubmit){H.ajaxForm(C);q.push(H);f()}else{if(G.showCancel){B.cancel.show();B.cancel.click(function(){q.splice(q.indexOf(H),1);l(I,D);H.remove();B.statusbar.remove();G.onCancel.call(I,D,B);I.selectedFiles-=D.length;e(G,I)})}H.ajaxForm(C)}}return this}}(jQuery));