Files
ultimatemember/assets/js/um-fileupload.min.js
T
Nikita Sinelnikov f60c3fda42 - added npm package.json;
- added .editorconfig from wp.org official repo
- added webpack.config.js sample
- added XML codestyle for PHPStorm
2021-05-25 14:56:22 +03:00

1 line
8.8 KiB
JavaScript

!function(S){S.fn.ajaxForm;var C={};C.fileapi=void 0!==S("<input type='file'/>").get(0).files,C.formdata=void 0!==window.FormData,S.fn.uploadFile=function(e){var p=S.extend({url:"",method:"POST",enctype:"multipart/form-data",formData:{},returnType:null,allowedTypes:"*",fileName:"file",dynamicFormData:function(){return{}},maxFileSize:-1,maxFileCount:-1,multiple:!0,dragDrop:!0,autoSubmit:!0,showCancel:!0,showAbort:!0,showDone:!0,showDelete:!1,showError:!0,showStatusAfterSuccess:!0,showStatusAfterError:!0,showFileCounter:!0,fileCounterStyle:"). ",showProgress:!1,onSelect:function(e){return!0},onSubmit:function(e,r){},onSuccess:function(e,r,t){},onError:function(e,r,t){},deleteCallback:!1,afterUploadAll:!1,uploadButtonClass:"upload",dragDropStr:"",abortStr:"Abort",cancelStr:"Cancel",deletelStr:"Delete",doneStr:"Done",multiDragErrorStr:"Multiple File Drag &amp; Drop is not allowed.",extErrorStr:"",sizeErrorStr:"",uploadErrorStr:"Upload is not allowed",maxFileCountErrorStr:""},e);this.fileCounter=1,this.selectedFiles=0,this.fCounter=0,this.sCounter=0,this.tCounter=0;var s="upload-"+(new Date).getTime();this.formGroup=s,this.hide(),this.errorLog=S("<div></div>"),this.after(this.errorLog),this.responses=[],C.formdata||(p.dragDrop=!1),C.formdata||(p.multiple=!1);var c=this,i=S("<div>"+S(this).html()+"</div>");S(i).addClass(p.uploadButtonClass),function e(){var r,t,o,a;S.fn.ajaxForm?(p.dragDrop?(r=S('<div class="ajax-upload-dragdrop" style="vertical-align:top;"></div>'),S(c).before(r),S(r).append(i),S(r).prepend(S(p.dragDropStr)),t=c,o=p,(a=r).on("dragenter",function(e){e.stopPropagation(),e.preventDefault(),S(this).css("border","2px dashed #ddd")}),a.on("dragover",function(e){e.stopPropagation(),e.preventDefault()}),a.on("drop",function(e){S(this).css("border","2px dashed #ddd"),e.preventDefault(),t.errorLog.html("");e=e.originalEvent.dataTransfer.files;!o.multiple&&1<e.length?o.showError&&S("<div class='um-error-block'>"+o.multiDragErrorStr+"</div>").appendTo(t.errorLog):0!=o.onSelect(e)&&m(o,t,e)}),S(document).on("dragenter",function(e){e.stopPropagation(),e.preventDefault()}),S(document).on("dragover",function(e){e.stopPropagation(),e.preventDefault(),a.css("border","2px dashed #ddd")}),S(document).on("drop",function(e){e.stopPropagation(),e.preventDefault(),a.css("border","2px dashed #ddd")})):S(c).before(i),function i(n,l,d,u){var e="ajax-upload-id-"+(new Date).getTime();var p=S("<form method='"+d.method+"' action='"+d.url+"' enctype='"+d.enctype+"'></form>");var r="<input type='file' id='"+e+"' name='"+d.fileName+"'/>";d.multiple&&(d.fileName.indexOf("[]")!=d.fileName.length-2&&(d.fileName+="[]"),r="<input type='file' id='"+e+"' name='"+d.fileName+"' multiple/>");e=S(r).appendTo(p);e.change(function(){n.errorLog.html("");d.allowedTypes.toLowerCase().split(",");var e=[];if(this.files){for(s=0;s<this.files.length;s++)e.push(this.files[s].name);if(0==d.onSelect(this.files))return}else{var r=S(this).val(),t=[];if(e.push(r),!v(0,d,r))return void(d.showError&&S("<div class='um-error-block'>"+d.extErrorStr+"</div>").appendTo(n.errorLog));if(t.push({name:r,size:"NA"}),0==d.onSelect(t))return}if(g(d,n),u.unbind("click"),p.hide(),i(n,l,d,u),p.addClass(l),C.fileapi&&C.formdata){p.removeClass(l);var o=this.files;m(d,n,o)}else{for(var a="",s=0;s<e.length;s++)d.showFileCounter?a+=n.fileCounter+d.fileCounterStyle+e[s]+"<br>":a+=e[s]+"<br>",n.fileCounter++;-1!=d.maxFileCount&&n.selectedFiles+e.length>d.maxFileCount?d.showError&&S("<div class='um-error-block'>"+d.maxFileCountErrorStr+"</div>").appendTo(n.errorLog):(n.selectedFiles+=e.length,(o=new b(n,d)).filename.html(a),w(p,d,o,e,n))}});p.css({margin:0,padding:0});var r=S(u).width()+10;10==r&&(r=120);r=u.height()+10;10==r&&(r=35);u.css({position:"relative",overflow:"hidden",cursor:"default"});e.css({position:"absolute",cursor:"pointer",top:"0px",width:"100%",height:"34px",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"});p.appendTo(u)}(c,s,p,i)):window.setTimeout(e,10)}(),this.startUpload=function(){S("."+this.formGroup).each(function(e,r){S(this).is("form")&&S(this).submit()})},this.stopUpload=function(){S(".upload-red").each(function(e,r){S(this).hasClass(c.formGroup)&&S(this).click()})};var h=!(this.getResponses=function(){return this.responses});function f(e){for(var r,t=[],o=(t=("string"==jQuery.type(e)?e:S.param(e)).split("&")).length,a=[],s=0;s<o;s++)t[s]=t[s].replace(/\+/g," "),r=t[s].split("="),a.push([decodeURIComponent(r[0]),decodeURIComponent(r[1])]);return a}function m(e,r,t){for(var o=0;o<t.length;o++)if(v(0,e,t[o].name))if(-1!=e.maxFileSize&&t[o].size>e.maxFileSize)e.showError&&S("<div class='um-error-block'>"+e.sizeErrorStr+"</div>").appendTo(r.errorLog);else if(-1!=e.maxFileCount&&r.selectedFiles>=e.maxFileCount)e.showError&&S("<div class='um-error-block'>"+e.maxFileCountErrorStr+"</div>").appendTo(r.errorLog);else{r.selectedFiles++;var a=e,s=new FormData,i=e.fileName.replace("[]","");s.append(i,t[o]);var n=e.formData;if(n)for(var l=f(n),d=0;d<l.length;d++)l[d]&&s.append(l[d][0],l[d][1]);a.fileData=s;var u=new b(r,e),i="",i=e.showFileCounter?r.fileCounter+e.fileCounterStyle+t[o].name:t[o].name;u.filename.html(i);n=S("<form style='display:block; position:absolute;left: 150px;' class='"+r.formGroup+"' method='"+e.method+"' action='"+e.url+"' enctype='"+e.enctype+"'></form>");n.appendTo("body");i=[];i.push(t[o].name),w(n,a,u,i,r),r.fileCounter++}else e.showError&&S("<div class='um-error-block'>"+e.extErrorStr+"</div>").appendTo(r.errorLog)}function v(e,r,t){var o=r.allowedTypes.toLowerCase().split(","),t=t.split(".").pop().toLowerCase();return!("*"!=r.allowedTypes&&jQuery.inArray(t,o)<0)}function g(o,e){var a;o.showFileCounter&&(a=S(".upload-filename").length,e.fileCounter=a+1,S(".upload-filename").each(function(e,r){var t=S(this).html().split(o.fileCounterStyle),t=(parseInt(t[0]),a+o.fileCounterStyle+t[1]);S(this).html(t),a--}))}function b(e,r){return this.statusbar=S("<div class='upload-statusbar'></div>"),this.filename=S("<div class='upload-filename'></div>").appendTo(this.statusbar),this.progressDiv=S("<div class='upload-progress'>").appendTo(this.statusbar).hide(),this.progressbar=S("<div class='upload-bar "+e.formGroup+"'></div>").appendTo(this.progressDiv),this.abort=S("<div class='upload-red "+e.formGroup+"'>"+r.abortStr+"</div>").appendTo(this.statusbar).hide(),this.cancel=S("<div class='upload-red'>"+r.cancelStr+"</div>").appendTo(this.statusbar).hide(),this.done=S("<div class='upload-green'>"+r.doneStr+"</div>").appendTo(this.statusbar).hide(),this.del=S("<div class='upload-red'>"+r.deletelStr+"</div>").appendTo(this.statusbar).hide(),e.errorLog.after(this.statusbar),this}function w(i,n,l,d,u){var e={cache:!1,contentType:!1,processData:!1,forceSync:!1,data:n.formData,formData:n.fileData,dataType:n.returnType,beforeSubmit:function(e,r,t){if(0==n.onSubmit.call(this,d))return l.statusbar.append("<div class='um-error-block'>"+n.uploadErrorStr+"</div>"),l.cancel.show(),i.remove(),l.cancel.click(function(){l.statusbar.remove()}),!1;var o=n.dynamicFormData();if(o){var a=f(o);if(a)for(var s=0;s<a.length;s++)a[s]&&(null!=n.fileData?t.formData.append(a[s][0],a[s][1]):t.data[a[s][0]]=a[s][1])}return u.tCounter+=d.length,p.afterUploadAll&&!h&&(h=!0,function e(){0!=c.sCounter&&c.sCounter+c.fCounter==c.tCounter?(p.afterUploadAll(c),h=!1):window.setTimeout(e,100)}()),!0},beforeSend:function(e,r){l.progressDiv.show(),l.cancel.hide(),l.done.hide(),n.showAbort&&(l.abort.show(),l.abort.click(function(){e.abort(),u.selectedFiles-=d.length})),C.formdata?l.progressbar.width("1%"):l.progressbar.width("5%")},uploadProgress:function(e,r,t,o){var a=(o=98<o?98:o)+"%";1<o&&l.progressbar.width(a),n.showProgress&&(l.progressbar.html(a),l.progressbar.css("text-align","center"))},success:function(e,r,t){u.responses.push(e),l.progressbar.width("100%"),n.showProgress&&(l.progressbar.html("100%"),l.progressbar.css("text-align","center")),l.abort.hide(),n.onSuccess.call(this,d,e,t),n.showStatusAfterSuccess?(n.showDone?(l.done.show(),l.done.click(function(){l.statusbar.hide("slow"),l.statusbar.remove()})):l.done.hide(),n.showDelete?(l.del.show(),l.del.click(function(){l.statusbar.hide().remove(),n.deleteCallback&&n.deleteCallback.call(this,e,l),u.selectedFiles-=d.length,g(n,u)})):l.del.hide()):(l.statusbar.hide("slow"),l.statusbar.remove()),i.remove(),u.sCounter+=d.length},error:function(e,r,t){l.abort.hide(),"abort"==e.statusText?(l.statusbar.hide("slow").remove(),g(n,u)):(n.onError.call(this,d,r,t),n.showStatusAfterError?(l.progressDiv.hide(),l.statusbar.append("<span class='um-error-block'>ERROR: "+t+"</span>")):(l.statusbar.hide(),l.statusbar.remove()),u.selectedFiles-=d.length),i.remove(),u.fCounter+=d.length}};n.autoSubmit?i.ajaxSubmit(e):(n.showCancel&&(l.cancel.show(),l.cancel.click(function(){i.remove(),l.statusbar.remove(),u.selectedFiles-=d.length,g(n,u)})),i.ajaxForm(e))}return this}}(jQuery);