1 line
15 KiB
JavaScript
1 line
15 KiB
JavaScript
(function(n){var s,r=n.telerik,k=/\.([^\.]+)$/;r.upload=function(v,w){n.extend(this,w);this.element=v;this.name=v.name;var u=n(v);this.wrapper=u.closest(".t-upload");if(this.wrapper.length==0){this.wrapper=this._wrapInput(u)}this._setActiveInput(u);this.toggle(this.enabled);u.closest("form").bind({submit:n.proxy(this._onParentFormSubmit,this),reset:n.proxy(this._onParentFormReset,this)});if(this.async.saveUrl!=s){this._module=this._getSupportsFormData()?new o(this):new f(this)}else{this._module=new g(this)}if(this._getSupportsDrop()){this._setupDropZone()}this.wrapper.delegate(".t-upload-action","click",n.proxy(this._onFileAction,this)).delegate(".t-upload-selected","click",n.proxy(this._onUploadSelected,this)).delegate(".t-file","t:progress",n.proxy(this._onFileProgress,this)).delegate(".t-file","t:upload-success",n.proxy(this._onUploadSuccess,this)).delegate(".t-file","t:upload-error",n.proxy(this._onUploadError,this));r.bind(this.wrapper,{load:this.onLoad,select:this.onSelect,upload:this.onUpload,success:this.onSuccess,error:this.onError,complete:this.onComplete,cancel:this.onCancel,remove:this.onRemove});r.trigger(this.wrapper,"load")};r.upload.prototype={enable:function(){this.toggle(true)},disable:function(){this.toggle(false)},toggle:function(u){this.wrapper.toggleClass("t-state-disabled",!u)},_addInput:function(u){u.insertAfter(this.element).data("tUpload",this);n(this.element).hide().removeAttr("id");this._setActiveInput(u)},_setActiveInput:function(u){var v=this.wrapper;this.element=u;u.attr("multiple",this._getSupportsMultiple()?this.multiple:false).attr("autocomplete","off").click(function(w){if(v.hasClass("t-state-disabled")){w.preventDefault()}}).change(n.proxy(this._onInputChange,this))},_onInputChange:function(w){var u=n(w.target),v=r.trigger(this.wrapper,"select",{files:j(u)});if(!v){u.trigger("t:select")}},_enqueueFile:function(y,v){var w=n(".t-upload-files",this.wrapper);if(w.length==0){w=n("<ul class='t-upload-files t-reset'></ul>").appendTo(this.wrapper);if(!this.showFileList){w.hide()}}var u=n(".t-file",w);var x=n("<li class='t-file'><span class='t-icon'></span><span class='t-filename'>"+y+"</span></li>").appendTo(w).data(v);if(!this.multiple){u.trigger("t:remove")}return x},_removeFileEntry:function(v){var u=v.closest(".t-upload-files");if(n(".t-file",u).length==1){u.remove();this._hideUploadButton()}else{v.remove()}},_setFileAction:function(w,u){var v={remove:"t-delete",cancel:"t-cancel",retry:"t-retry"};if(!v.hasOwnProperty(u)){return}this._clearFileAction(w);w.append(this._renderAction(v[u],this.localization[u]).addClass("t-upload-action"))},_setFileState:function(u,w){var x={uploading:{cssClass:"t-loading",text:this.localization.statusUploading},uploaded:{cssClass:"t-success",text:this.localization.statusUploaded},failed:{cssClass:"t-fail",text:this.localization.statusFailed}};var v=x[w];if(v){var y=u.children(".t-icon").text(v.text);y[0].className="t-icon "+v.cssClass}},_renderAction:function(v,u){if(v!=""){return n("<button type='button' class='t-button t-button-icontext'><span class='t-icon "+v+"'></span>"+u+"</button>")}else{return n("<button type='button' class='t-button'>"+u+"</button>")}},_clearFileAction:function(u){u.find(".t-upload-action").remove()},_onFileAction:function(y){if(!this.wrapper.hasClass("t-state-disabled")){var u=n(y.target).closest(".t-upload-action"),x=u.find(".t-icon"),w=u.closest(".t-file"),v={files:w.data("fileNames")};if(x.hasClass("t-delete")){if(!r.trigger(this.wrapper,"remove",v)){w.trigger("t:remove")}}else{if(x.hasClass("t-cancel")){r.trigger(this.wrapper,"cancel",v);w.trigger("t:cancel")}else{if(x.hasClass("t-retry")){w.trigger("t:retry")}}}}return false},_onUploadSelected:function(){this.wrapper.trigger("t:saveSelected");return false},_onFileProgress:function(w,v){var u=n(".t-progress-status",w.target);if(u.length==0){u=n("<span class='t-progress'><span class='t-progress-status' style='width: 0;'></span></span>").appendTo(n(".t-filename",w.target)).find(".t-progress-status")}u.width(v+"%")},_onUploadSuccess:function(x,w,u){var v=q(x);this._setFileState(v,"uploaded");r.trigger(this.wrapper,"success",{files:v.data("fileNames"),response:w,operation:"upload",XMLHttpRequest:u});if(this._supportsRemove()){this._setFileAction(v,"remove")}else{this._clearFileAction(v)}this._checkAllComplete()},_onUploadError:function(x,u){var w=q(x);this._setFileState(w,"failed");this._setFileAction(w,"retry");var v=r.trigger(this.wrapper,"error",{operation:"upload",files:w.data("fileNames"),XMLHttpRequest:u});t("Server response: "+u.responseText);if(!v){this._alert("Error! Upload failed. Unexpected server response - see console.")}this._checkAllComplete()},_showUploadButton:function(){var u=n(".t-upload-selected",this.wrapper);if(u.length==0){u=this._renderAction("",this.localization.uploadSelectedFiles).addClass("t-upload-selected")}this.wrapper.append(u)},_hideUploadButton:function(){n(".t-upload-selected",this.wrapper).remove()},_onParentFormSubmit:function(){this.element.trigger("t:abort");var v=this;if(!this.element.value){var u=n(this.element).attr("name","");setTimeout(function(){u.attr("name",v.name)},0)}},_onParentFormReset:function(){n(".t-file",this.wrapper).trigger("t:remove")},_getSupportsFormData:function(){return typeof(FormData)!="undefined"},_getSupportsMultiple:function(){return !n.browser.opera},_getSupportsDrop:function(){var x=this._getUserAgent().toLowerCase(),v=/chrome/.test(x),u=!v&&/safari/.test(x),w=u&&/windows/.test(x);return !w&&this._getSupportsFormData()},_getUserAgent:function(){return navigator.userAgent},_setupDropZone:function(){n(".t-upload-button",this.wrapper).wrap("<div class='t-dropzone'></div>");var u=n(".t-dropzone",this.wrapper).append(n("<em>"+this.localization.dropFilesHere+"</em>")).bind({dragenter:d,dragover:function(v){v.preventDefault()},drop:n.proxy(this._onDrop,this)});h(u,function(){u.addClass("t-dropzone-hovered")},function(){u.removeClass("t-dropzone-hovered")});h(n(document),function(){u.addClass("t-dropzone-active")},function(){u.removeClass("t-dropzone-active")})},_onDrop:function(w){var u=w.originalEvent.dataTransfer,v=u.files;d(w);if(v.length>0){n(".t-dropzone",this.wrapper).trigger("t:select",[v])}},_supportsRemove:function(){return this.async.removeUrl!=s},_submitRemove:function(w,u,x){var v={};v.fileNames=w;n.ajax({type:"POST",dataType:"json",url:this.async.removeUrl,traditional:true,data:v,success:u,error:x})},_alert:function(u){alert(u)},_wrapInput:function(u){u.wrap("<div class='t-widget t-upload'><div class='t-button t-upload-button'></div></div>");u.closest(".t-button").append("<span>"+this.localization.select+"</span>");return u.closest(".t-upload")},_checkAllComplete:function(){if(n(".t-file .t-icon.t-loading",this.wrapper).length==0){r.trigger(this.wrapper,"complete")}}};n.fn.tUpload=function(u){return r.create(this,{name:"tUpload",init:function(v,w){return new r.upload(v,w)},options:u})};n.fn.tUpload.defaults={enabled:true,multiple:true,showFileList:true,async:{},localization:{select:"Select...",cancel:"Cancel",retry:"Retry",remove:"Remove",uploadSelectedFiles:"Upload files",dropFilesHere:"drop files here to upload",statusUploading:"uploading",statusUploaded:"uploaded",statusFailed:"failed"}};var g=function(u){this.name="syncUploadModule";this.element=u.wrapper;this.upload=u;this.element.bind("t:select",n.proxy(this.onSelect,this)).bind("t:remove",n.proxy(this.onRemove,this)).closest("form").attr("enctype","multipart/form-data").attr("encoding","multipart/form-data")};g.prototype={onSelect:function(x){var u=this.upload;var v=n(x.target);u._addInput(v.clone().val(""));var w=u._enqueueFile(m(v),{relatedInput:v});u._setFileAction(w,"remove")},onRemove:function(v){var u=q(v);u.data("relatedInput").remove();this.upload._removeFileEntry(u)}};var f=function(u){this.name="iframeUploadModule";this.element=u.wrapper;this.upload=u;this.iframes=[];this.element.bind("t:select",n.proxy(this.onSelect,this)).bind("t:cancel",n.proxy(this.onCancel,this)).bind("t:retry",n.proxy(this.onRetry,this)).bind("t:remove",n.proxy(this.onRemove,this)).bind("t:saveSelected",n.proxy(this.onSaveSelected,this)).bind("t:abort",n.proxy(this.onAbort,this))};f.prototype={onSelect:function(x){var u=this.upload,v=n(x.target);var w=this.prepareUpload(v);if(u.async.autoUpload){this.performUpload(w)}else{if(u._supportsRemove()){this.upload._setFileAction(w,"remove")}u._showUploadButton()}},prepareUpload:function(z){var x=this.upload;var w=n(x.element);x._addInput(z.clone().val(""));var y=this.createFrame(x.name+"_"+this.iframes.length);this.registerFrame(y);var v=this.createForm(x.async.saveUrl,y.attr("name")).append(w);var u=x._enqueueFile(m(z),{frame:y,relatedInput:w,fileNames:j(z)});y.data({form:v,file:u});return u},performUpload:function(u){var y={files:u.data("fileNames")},w=u.data("frame"),z=this.upload;if(!r.trigger(z.wrapper,"upload",y)){z._hideUploadButton();w.appendTo(document.body);var v=w.data("form").appendTo(document.body);var x=z.async.saveUrl;if(y.data){v[0].action=x+(/\?/.test(x)?"&":"?")+n.param(y.data)}z._setFileAction(u,"cancel");z._setFileState(u,"uploading");w.one("load",n.proxy(this.onIframeLoad,this));v[0].submit()}else{z._removeFileEntry(w.data("file"));this.cleanupFrame(w);this.unregisterFrame(w)}},onSaveSelected:function(v){var u=this;n(".t-file",this.element).each(function(){var w=n(this),x=a(w);if(!x){u.performUpload(w)}})},onIframeLoad:function(w){var v=n(w.target);try{var u=v.contents().text()}catch(w){u="Error trying to get server response: "+w}this.processResponse(v,u)},processResponse:function(w,y){var u=w.data("file"),x=this,v={responseText:y};i(y,function(z){n.extend(v,{statusText:"OK",status:"200"});u.trigger("t:upload-success",[z,v]);x.cleanupFrame(w);x.unregisterFrame(w)},function(){n.extend(v,{statusText:"error",status:"500"});u.trigger("t:upload-error",[v])})},onCancel:function(v){var u=n(v.target).data("frame");this.stopFrameSubmit(u);this.cleanupFrame(u);this.unregisterFrame(u);this.upload._removeFileEntry(u.data("file"))},onRetry:function(v){var u=q(v);this.performUpload(u)},onRemove:function(w){var u=q(w);var v=u.data("frame");if(v){this.unregisterFrame(v);this.upload._removeFileEntry(u);this.cleanupFrame(v)}else{e(u,this.upload)}},onAbort:function(){var u=this.element,v=this;n.each(this.iframes,function(){n("input",this.data("form")).appendTo(u);v.stopFrameSubmit(this[0]);this.data("form").remove();this.remove()});this.iframes=[]},createFrame:function(u){return n("<iframe name='"+u+"' id='"+u+"' style='display:none;' />")},createForm:function(u,v){return n("<form enctype='multipart/form-data' method='POST' action='"+u+"' target='"+v+"'/>")},stopFrameSubmit:function(u){if(typeof(u.stop)!="undefined"){u.stop()}else{if(u.document){u.document.execCommand("Stop");u.contentWindow.location.href=u.contentWindow.location.href}}},registerFrame:function(u){this.iframes.push(u)},unregisterFrame:function(u){this.iframes=n.grep(this.iframes,function(v){return v.attr("name")!=u.attr("name")})},cleanupFrame:function(u){var v=u.data("form");u.data("file").data("frame",null);setTimeout(function(){v.remove();u.remove()},1)}};var o=function(u){this.name="formDataUploadModule";this.element=u.wrapper;this.upload=u;this.element.bind("t:select",n.proxy(this.onSelect,this)).bind("t:cancel",n.proxy(this.onCancel,this)).bind("t:remove",n.proxy(this.onRemove,this)).bind("t:retry",n.proxy(this.onRetry,this)).bind("t:saveSelected",n.proxy(this.onSaveSelected,this)).bind("t:abort",n.proxy(this.onAbort,this))};o.prototype={onSelect:function(y,v){var z=this.upload,x=this,A=n(y.target),w=v?c(v):this.getInputFiles(A),u=this.prepareUpload(A,w);n.each(u,function(){if(z.async.autoUpload){x.performUpload(this)}else{if(z._supportsRemove()){z._setFileAction(this,"remove")}z._showUploadButton()}})},prepareUpload:function(u,v){var w=this.enqueueFiles(v);if(u.is("input")){n.each(w,function(){n(this).data("relatedInput",u)});u.data("relatedFileEntries",w);this.upload._addInput(u.clone().val(""))}return w},enqueueFiles:function(x){var z=this.upload;fileEntries=[];for(var u=0;u<x.length;u++){var w=x[u],y=w.name;var v=z._enqueueFile(y,{fileNames:[w]});v.data("formData",this.createFormData(x[u]));fileEntries.push(v)}return fileEntries},getInputFiles:function(u){return j(u)},performUpload:function(y){var u=this.upload,v=y.data("formData"),x={files:y.data("fileNames")};if(!r.trigger(this.element,"upload",x)){u._setFileAction(y,"cancel");u._hideUploadButton();var w=this.upload.async.saveUrl;if(x.data){w+=(/\?/.test(w)?"&":"?")+n.param(x.data)}u._setFileState(y,"uploading");this.postFormData(w,v,y)}else{this.removeFileEntry(y)}},onSaveSelected:function(v){var u=this;n(".t-file",this.element).each(function(){var w=n(this),x=a(w);if(!x){u.performUpload(w)}})},onCancel:function(v){var u=q(v);this.stopUploadRequest(u);this.removeFileEntry(u)},onRetry:function(v){var u=q(v);this.performUpload(u)},onRemove:function(v){var u=q(v);if(u.children(".t-icon").is(".t-success")){e(u,this.upload)}else{this.removeFileEntry(u)}},postFormData:function(u,v,y){var x=new XMLHttpRequest(),w=this;y.data("request",x);x.addEventListener("load",function(z){w.onRequestSuccess.call(w,z,y)},false);x.addEventListener("error",function(z){w.onRequestError.call(w,z,y)},false);x.upload.addEventListener("progress",function(z){w.onRequestProgress.call(w,z,y)},false);x.open("POST",u);x.send(v)},createFormData:function(u){var v=new FormData();v.append(this.upload.name,u.rawFile);return v},onRequestSuccess:function(x,u){var w=x.target,v=this;i(w.responseText,function(y){u.trigger("t:upload-success",[y,w]);v.cleanupFileEntry(u)},function(){u.trigger("t:upload-error",[w])})},onRequestError:function(w,u){var v=w.target;u.trigger("t:upload-error",[v])},cleanupFileEntry:function(u){var w=u.data("relatedInput"),v=true;if(w){n.each(w.data("relatedFileEntries"),function(){if(this.parent().length>0&&this[0]!=u[0]){v=v&&this.children(".t-icon").is(".t-success")}});if(v){w.remove()}}u.data("formData",null)},removeFileEntry:function(u){this.cleanupFileEntry(u);this.upload._removeFileEntry(u)},onRequestProgress:function(w,u){var v=Math.round(w.loaded*100/w.total);u.trigger("t:progress",[v])},stopUploadRequest:function(u){u.data("request").abort()}};function m(u){return n.map(j(u),function(v){return v.name}).join(", ")}function j(v){var u=v[0];if(u.files){return c(u.files)}else{return[{name:l(u.value),extension:p(u.value),size:null}]}}function c(u){return n.map(u,function(v){return b(v)})}function b(u){var v=u.name||u.fileName;return{name:v,extension:p(v),size:u.size||u.fileSize,rawFile:u}}function p(u){return u.match(k)[0]||""}function l(u){var v=u.lastIndexOf("\\");return(v!=-1)?u.substr(v+1):u}function e(v,x){var w=v.data("fileNames");var u=n.map(w,function(A){return A.name});x._submitRemove(u,function z(B,C,A){x._removeFileEntry(v);r.trigger(x.wrapper,"success",{operation:"remove",files:w,response:B,XMLHttpRequest:A})},function y(A,C,C){var B=r.trigger(x.wrapper,"error",{operation:"remove",files:w,XMLHttpRequest:A});t("Server response: "+A.responseText);if(!B){x._alert("Error! Remove operation failed. Unexpected response - see console.")}})}function i(v,u,w){try{var x=n.parseJSON(v);u(x)}catch(y){w()}}function d(u){u.stopPropagation();u.preventDefault()}function h(x,w,y){var v,u;x.bind("dragenter",function(z){w();u=new Date();if(!v){v=setInterval(function(){var A=new Date()-u;if(A>100){y();clearInterval(v);v=null}},100)}}).bind("dragover",function(z){u=new Date()})}function a(u){return u.children(".t-icon").is(".t-loading, .t-success, .t-fail")}function t(u){if(typeof(console)!="undefined"&&console.log){console.log(u)}}function q(u){return n(u.target).closest(".t-file")}})(jQuery); |