Вход Регистрация
Файл: touchSpace/js/FilesUploader.js
Строк: 573
<?php
function FilesUploader(e){var t=this;t._in_upload=!1,t._paste_btns=[],t._dragndrop_places=[],t.cur_file_id=null,t.events={},t.params=$.extend(!0,{button:null,autoSubmit:!1,buttonClass:{hover:"",focus:"",active:""},dragPlace:null,postData:{},action:{},name:"file",multiple:!1,maxFiles:1,responseType:"json",allowedExtensions:[],notAllowedExtensions:[]},e),t.button=t.params.button?$(t.params.button):null,t.ajax=FilesUploader.isAjaxSupport(),t.files=[],t._init()}$.extend(FilesUploader,{isAjaxSupport:function(){return navigator.userAgent.indexOf("UCBrowser")<0&&navigator.userAgent.indexOf("IEMobile")<0&&"undefined"!=typeof File&&void 0!==(new XMLHttpRequest).upload},needNtiveControls:function(){return"ucbrowser"==Device.browser||"msie"==Device.browser&&Device.v<9},maybeUnsupported:function(){return navigator.userAgent.indexOf("Windows Phone 8.0")>0}}),$.extend(FilesUploader.prototype,{_init:function(){var e=this;e.button&&(e.input_wrap=$("<div>").css({display:"block",position:"absolute",overflow:"hidden",margin:0,padding:0,opacity:0,cursor:"pointer",visibility:"visible",direction:"ltr",zIndex:2147483583,filter:"alpha(opacity=0)"}).prop("ajax_upload_button",!0),e.button.on("mouseover.SpFilesUploader touchmove.SpFilesUploader",function(){e.updateButton()}),e.input_wrap.on("mouseover",function(){e.button.addClass(e.params.buttonClass.hover)}),e.input_wrap.on("mouseout",function(){e.button.removeClass(e.params.buttonClass.hover),e.button.removeClass(e.params.buttonClass.focus)}),e.input_wrap.on("focus",function(){e.button.addClass(e.params.buttonClass.focus)}),e.input_wrap.on("blur",function(){e.button.removeClass(e.params.buttonClass.focus)}),e.input_wrap.insertAfter(e.button),e.on("uploadStart",function(){e.input_wrap.hide()}),e.on("complete",function(){e.input_wrap.show(),e.updateButton()})),e._dragDropMonitor(),e._createInput()},_createInput:function(){var e,t=this;delete t.input,e=ce("input",{type:"file",name:t.params.name}),e.ajax_upload_button=!0,t.params.accept&&(e.accept=t.params.accept),e.onchange=function(){(""!=this.value||this.files.length)&&(t._createInput(),this.onchange=null,t.addFiles(t.ajax?this.files:[this]))},t.input=e,t.button&&(t.input_wrap.empty(),t.input_wrap.append(e),t.updateButton(),extend(e.style,{position:"absolute",right:0,margin:0,padding:0,fontFamily:"sans-serif",cursor:"pointer"}),e.style.cssText+=";font-size: 480px !important;"),setTimeout(function(){t._trigger("input",[e])},0)},updateButton:function(){var e=this;e.button&&(extend(e.input.style,{width:e.button.outerWidth()+"px",height:e.button.outerHeight()+"px",fontSize:"360px"}),e.input_wrap.css({width:e.button.outerWidth(),height:e.button.outerHeight()}).offset(e.button.offset()))},progressAvail:function(){return this.ajax},inUpload:function(){return this._in_upload},getInput:function(){return this.input},getExt:function(e){var t=e.split(".");return t.length>1?t[t.length-1].toLowerCase():""},setPostData:function(e){return this.params.postData=e,this},addFiles:function(e,t){var a,n,r,i,o,s,l,u=this;if(!u._in_upload){for(a={"image/pjpeg":"jpg","image/jpeg":"jpg","image/jpg":"jpg","image/gif":"gif","image/png":"png"},1==u.params.maxFiles&&(u.files=[]),t=t||"upload_",n=0;n<e.length;++n){if(r=e[n],u.params.maxFiles<=u.files.length){u._trigger("limitError");break}if(u.ajax&&!(instanceof File&&r.name)){if(i=a[r.type],!i)continue;r.name=r.filemame=u._genFileName(t)+"."+i}if(o=u._getFilename(u.ajax?r.name:r.value),s=u.getExt(o),l=u.ajax?r.size:void 0,u.params.maxSize&&void 0!==l&&null!==l&&u.params.maxSize<l)u._trigger("sizeError",[o,s,l]);else{if(o){if(u.params.allowedExtensions.length&&!u._checkExt(s,u.params.allowedExtensions)){u._trigger("extError",[o,s,l]);continue}if(u.params.notAllowedExtensions.length&&u._checkExt(s,u.params.notAllowedExtensions)){u._trigger("extError",[o,s,l]);continue}}u._trigger("file",[o,s,l])&&u.files.push({id:u.files.length,file:r,name:o,ext:s,size:l})}}u.params.autoSubmit&&u.files.length&&u.submit()}},submit:function(){var e=this,t=0,a=function(){e.files.length>t?(e._in_upload=!0,e._current_file=e.files[t++],e._submit(a)):(e._in_upload=!1,e._trigger("complete"),e.files=[])};return e._in_upload?this:(!e.ajax&&!e.files.length>0&&(e.addFiles([e.input]),e._createInput()),e.files.length>0?(e._trigger("uploadStart"),a()):e._trigger("notSelectedError"),this)},reset:function(){var e=this;return e._in_upload||(e._trigger("delete"),e.files=[]),this},_submit:function(e){var t,a,n,r,i,o,s,l,u,p=this,d=p._current_file;if(!p._trigger("submit",[d]))return void setTimeout(e,0);if(p.ajax){t=new FormData,post=p.params.postData;for(a in post)if(post[a]instanceof Array)for(n=0;n<post[a].length;++n)t.append(a,post[a][n]);else t.append(a,post[a]);t.append(p.params.name.replace("%d",d.id),d.file,d.name),r=p._xhr(),r.onreadystatechange=function(){try{if(4==r.readyState){if(!r.__is_abort){d._request=null,r.onreadystatechange=function(){};var t=0,a="";try{t=r.status}catch(n){}try{a=r.statusText}catch(n){}t>=200&&300>t?p._onDone(d,r.responseText):p._trigger("error",[d,t,a,r.responseText])}setTimeout(e,0)}}catch(n){p._trigger("error",[d,-1,n.stack||n.message]),setTimeout(e,0)}},r.open("POST",p.params.action,!0);try{r.withCredentials=!0}catch(c){}"onprogress"in r&&(r.upload.onloadstart=function(){p._trigger("progress",[d,0])},r.upload.onprogress=function(e){if(e.lengthComputable){var t=e.loaded/e.total*100;p._trigger("progress",[d,t,e.loaded,e.total])}}),r.setRequestHeader("X-Requested-With","XMLHttpRequest"),r.send(t),d._request=r}else i=p._getUID(),s=!1,navigator.userAgent.indexOf("MSIE 7")>-1?o=document.createElement('<iframe src="javascript:false;" name="'+i+'">'):(o=document.createElement("iframe"),o.src="javascript:false;",o.name=i),o.style.display="none",o.id=i,document.body.appendChild(o),l=function(){delete d._request,$(o).remove(),o=null,$(window).off("."+i)},o.onload=function(t){return t===!1?void setTimeout(e,0):void(d._request&&(delete d._request,setTimeout(function(){s||(p._trigger("error",[d,-1,"Сервер ответил неожиданным ответом. "]),setTimeout(e,0),l())},800)))},u=p._getForm(i,p.params.action,p.params.postData),u.appendChild(d.file),document.body.appendChild(u),$(window).on("message."+i,function(t){delete d._request;var a=t.originalEvent;try{if(0!=p.params.action.indexOf(a.origin))return void console.log("[uploader] non matched origin: ",p.params.action,t.originalEvent.origin);s=!0,p._onDone(d,a.data)}catch(t){p._trigger("error",[d,-1,t.stack||t.message,a.data])}setTimeout(e,0),l()}),d._request=o,u.submit(),$(u).remove(),u=null},cancel:function(e){var t,a,n,r=this;if("number"==typeof e){if(t=r.files[e],t&&t._request){if(r.ajax)t._request.__is_abort=!0,t._request.abort();else{a=t._request,a.onload(!1),a.onload=null;try{a.stop()}catch(i){}try{a.execCommand("stop")}catch(i){}try{a.contentWindow.document.execCommand("stop")}catch(i){}a.src="javascript:false;"}delete t._request,r._trigger("cancel",[t])}}else for(n=0;n<r.files.length;++n)r.cancel(n)},_getForm:function(e,t,a){var n,r,i,o=document.createElement("form");o.action=t,o.method="POST",o.target=e,o.name=this._getUID(),o.style.display="none",o.encoding="multipart/form-data",o.enctype="multipart/form-data";for(n in a)if(r=document.createElement("input"),r.type="hidden",a[n]instanceof Array)for(i=0;i<a[n].length;++i)r.name=n,r.value=a[n][i],o.appendChild(r);else r.name=n,r.value=a[n],o.appendChild(r);return o},_onDone:function(e,t){var a=this;"json"==a.params.responseType&&(t=$.parseJSON(t)),a._trigger("uploaded",[e,t])},_dragDropMonitor:function(){var e,t=this,a=!1;"ondragover"in document.body&&t.ajax&&$(document.body).on("dragover.files_uploader",function(n){var r,i,o=n.originalEvent;if(t._isFile(o.dataTransfer.types)){for(r=0;r<t._dragndrop_places.length;++r)i=$(t._dragndrop_places[r]),i.addClass(i.data("dragavail-class"));a=!0,e&&(clearTimeout(e),e=null)}}).on("dragleave.files_uploader drop.files_uploader",function(n){var r=n.originalEvent;t._isFile(r.dataTransfer.types)&&(a=!1,e||(e=setTimeout(function(){var a,n;for(a=0;a<t._dragndrop_places.length;++a)n=$(t._dragndrop_places[a]),n.removeClass(n.data("dragavail-class"));e=null},300)))})},addDropPlace:function(e){var t,a,n=this;"ondragover"in document.body&&n.ajax&&e&&(a=$(e),a.on("dragenter.SpFilesUploader",function(e){var t=e.originalEvent;n._isFile(t.dataTransfer.types)&&e.preventDefault()}),a.on("dragover.SpFilesUploader",function(e){var r=e.originalEvent;t&&(clearTimeout(t),t=null),n._isFile(r.dataTransfer.types)&&(e.preventDefault(),a.addClass(a.data("dragover-class")),a.removeClass(a.data("dragleave-class")))}),a.on("dragleave.SpFilesUploader",function(e){e.originalEvent;t||(t=setTimeout(function(){a.removeClass(a.data("dragover-class")),a.addClass(a.data("dragleave-class"))},300))}),a.on("drop.SpFilesUploader",function(e){var t=e.originalEvent;t.dataTransfer.files.length>0&&(n.addFiles(t.dataTransfer.files),e.preventDefault(),a.removeClass(a.data("dragover-class")),a.removeClass(a.data("dragleave-class")))}),n._dragndrop_places.push(a))},addPastePlace:function(e){var t,a=this;"onpaste"in document.body&&a.ajax&&e&&(t=$(e).on("paste.SpFilesUploader",function(e){var t,n,r,i,o,s=e.originalEvent;if(s.clipboardData&&s.clipboardData.items){for(t=[],n=s.clipboardData,r=0;r<s.clipboardData.items.length;++r)i=s.clipboardData.items[r],"file"==i.kind&&i.getAsFile&&(o=i.getAsFile(),e.preventDefault(),t.push(o));t.length>0&&a.addFiles(t)}else window.clipboardData&&window.clipboardData.files&&window.clipboardData.files.length>0&&(a.addFiles(window.clipboardData.files),e.preventDefault())}),a._paste_btns.push(t))},destroy:function(){var e,t=this;for($([window,document.body]).off(".SpFilesUploader"),e=0;e<t._dragndrop_places.length;++e)$(t._dragndrop_places[e]).off(".SpFilesUploader");for(e=0;e<t._paste_btns.length;++e)$(t._paste_btns[e]).off(".SpFilesUploader")},on:function(e,t){return this.events[e]||(this.events[e]=[]),this.events[e].push(t),this},_trigger:function(e,t){var a,n,r=!0;if(this.events[e])for(a=0;a<this.events[e].length;++a)if(n=this.events[e][a],n&&n.apply(this,t||[])===!1){r=!1;break}return r},_xhr:function(){var e;if("undefined"!=typeof XMLHttpRequest)e=new window.XMLHttpRequest;else if(window.ActiveXObject)try{e=new window.ActiveXObject("Microsoft.XMLHTTP")}catch(t){return!1}return e},_checkExt:function(e,t){for(var a=0;a<t.length;++a)if(t[a].toLowerCase()===e.toLowerCase())return!0;return!1},_getUID:function(){return"axxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){var t=16*Math.random()|0,a="x"==e?t:3&t|8;return a.toString(16)})},_genFileName:function(e){var t=new Date;return(e||"upload_")+t.getFullYear()+"-"+("0"+(t.getMonth()+1)).slice(-2)+"-"+("0"+t.getDate()).slice(-2)+"_"+("0"+t.getHours()).slice(-2)+"."+("0"+t.getMinutes()).slice(-2)+"."+("0"+t.getSeconds()).slice(-2)},_getFilename:function(e){return e.replace(/\/g,"/").replace(/^.+//g,"").replace(/^C__Data_Users_DefApps_AppData_INTERNETEXPLORER_([^_]+)_/,"")},_isFile:function(e){return $.inArray("Files",e)>-1||$.inArray("application/x-moz-file",e)>-1}});
?>
Онлайн: 0
Реклама