1
0
Fork 0
wmdeit_kaform/addons/upload/kube.upload.min.js

1 line
6.9 KiB
JavaScript

!function(t){t.add("module","upload",{init:function(t,i){this.app=t,this.utils=t.utils,this.animate=t.animate,this.response=t.response,this.progress=t.progress;var e={size:120,url:!1,urlRemove:!1,param:!1,type:!1,multiple:!1,placeholder:"Drop files here or click to upload",progress:!1,target:!1,append:!1};this.context=i,this.params=i.getParams(e),this.$element=i.getElement(),this.$target=i.getTarget(),this.statusMap=["hover","error","success","drop"]},start:function(){this._buildBox(),this._buildInput(),this._buildCount(),this._buildType(),this._buildPlaceholder(),this._buildSize(),this._buildMultiple(),this._buildItems(),this._buildEvents()},stop:function(){this.$box.remove(),this.$element.off(".kube.upload")},_buildBox:function(){"image"===this.params.type?this.$box=this.$element.find(".upload-item"):this.$box=this.$element},_buildInput:function(){this.$input=t.dom("<input>"),this.$input.attr("type","file"),this.$input.attr("name",this._getParamName()),this.$input.hide(),this.$element.before(this.$input)},_buildCount:function(){this.$inputCount=t.dom("<input>"),this.$inputCount.attr("type","hidden"),this.$inputCount.attr("name",this._getParamName()+"-count"),this.$inputCount.val(0),this.$element.before(this.$inputCount)},_buildType:function(){this.isBox=this.$element.hasClass("upload")},_buildPlaceholder:function(){if(this.isBox){var i=t.dom("<span>");i.addClass("upload-placeholder"),i.html(this.params.placeholder),this.$element.append(i)}},_buildSize:function(){this.isBox?this.$box.css({height:this.params.size+"px"}):"image"===this.params.type&&this.$box.css({width:this.params.size+"px",height:this.params.size+"px"})},_buildMultiple:function(){this.isMultiple=this.params.multiple,this.isMultiple&&this.$input.attr("multiple","true")},_buildItems:function(){if(this.params.type){var t="file"===this.params.type,i=t?this.$target:this.$element,e=t?"_removeFile":"_removeImage",s=i.find(".close");s.on("click",this[e].bind(this)),t||s.closest(".upload-item").addClass("is-uploaded"),this.$inputCount.val(s.length)}},_buildEvents:function(){this.$input.on("change.redactor.upload",this._change.bind(this)),this.$box.on("click.redactor.upload",this._click.bind(this)),this.$box.on("drop.redactor.upload",this._drop.bind(this)),this.$box.on("dragover.redactor.upload",this._dragover.bind(this)),this.$box.on("dragleave.redactor.upload",this._dragleave.bind(this))},_click:function(i){i.preventDefault();var e=t.dom(i.target);e.hasClass("close")||this.$input.click()},_change:function(t){this.app.broadcast("upload.start",this),this._send(t,this.$input.get().files)},_drop:function(t){t.preventDefault(),this._clearStatuses(),this._setStatus("drop"),this.app.broadcast("upload.start",this),this._send(t)},_dragover:function(t){return t.preventDefault(),this._setStatus("hover"),!1},_dragleave:function(t){return t.preventDefault(),this._removeStatus("hover"),!1},_upCount:function(){var t=this.$inputCount.val();t++,this.$inputCount.val(t)},_downCount:function(){var t=this.$inputCount.val();t--,t=0>t?0:t,this.$inputCount.val(t)},_clearCount:function(){this.$inputCount.val(0)},_getParamName:function(){return this.params.param?this.params.param:"file"},_getHiddenName:function(){var t=this._getParamName();return this.isMultiple?t+"-uploaded[]":t+"-uploaded"},_clearStatuses:function(){this.$box.removeClass("is-upload-"+this.statusMap.join(" is-upload-"))},_setStatus:function(t){this.$box.addClass("is-upload-"+t)},_removeStatus:function(t){this.$box.removeClass("is-upload-"+t)},_clearTarget:function(){var i=this.$target.find(".upload-item");i.each(function(i){var e=t.dom(i);this._removeFileRequest(e.attr("data-id"))}.bind(this)),this._clearCount(),this.$target.html("")},_clearBox:function(){var i=this.$target.find(".upload-item");i.each(function(i){var e=t.dom(i);this._removeFileRequest(e.attr("data-id"))}.bind(this)),this._clearCount(),this.$target.html("")},_removeFile:function(i){i.preventDefault();var e=t.dom(i.target),s=e.closest(".upload-item"),a=s.attr("data-id");this.animate.run(s,"fadeOut",function(){s.remove(),this._downCount(),this._removeFileRequest(a),0===this.$target.find(".upload-item").length&&this.$target.html("")}.bind(this))},_removeImage:function(i){i.preventDefault();var e=t.dom(i.target),s=e.closest(".upload-item"),a=s.attr("data-id");if(this.isMultiple)this.animate.run(s,"fadeOut",function(){s.remove(),this._downCount(),this._removeFileRequest(a)}.bind(this));else{var o=s.find("img");e.hide(),this.animate.run(o,"fadeOut",function(){this.$box.html(""),this.$box.removeClass("is-uploaded"),this._clearCount(),this._removeFileRequest(a)}.bind(this))}},_removeFileRequest:function(i){this.params.urlRemove&&t.ajax.post({url:this.params.urlRemove,data:{id:i}})},_send:function(t,i){t=t.originalEvent||t,i=i?i:t.dataTransfer.files;var e=new FormData,s=this._getParamName();e=this._buildData(s,i,e),this.params.append&&(e=this.utils.extendData(e,this.params.append)),this._sendData(e,i,t)},_sendData:function(i,e,s){this.params.progress&&this.progress.show(),t.ajax.post({url:this.params.url,data:i,before:function(t){return this.app.broadcast("upload.beforeSend",this,t)}.bind(this),success:function(t){this._complete(t,s)}.bind(this)})},_buildData:function(t,i,e){for(var s=0;s<i.length;s++)e.append(t+"[]",i[s]);return e},_complete:function(t,i){this._clearStatuses(),this.params.progress&&this.progress.hide();var e=Array.isArray(t)?t[0]:t;if("undefined"!=typeof e.type&&"error"===e.type)this._setStatus("error"),this.response.parse(t),this.app.broadcast("upload.error",this,t);else{switch(this._setStatus("success"),this.params.type){case"image":this._completeBoxImage(t);break;case"file":this._completeBoxFile(t);break;default:this._completeBoxUpload(t)}this.app.broadcast("upload.complete",this,t),setTimeout(this._clearStatuses.bind(this),500)}},_completeBoxUpload:function(t){this.response.parse(t)},_completeBoxImage:function(i){for(var e in i){var s=t.dom("<img>");s.attr("src",i[e].url);var a=t.dom("<span>");a.addClass("close"),a.on("click",this._removeImage.bind(this));var o=t.dom("<input>");o.attr("type","hidden"),o.attr("name",this._getHiddenName()),o.val(i[e].id);var n=t.dom("<div>");if(n.addClass("upload-item is-uploaded"),n.attr("data-id",i[e].id),!this.isMultiple){var r=this.$box.find("img");return 0!==r.length&&this._removeFileRequest(this.$box.attr("data-id")),this.$box.html(""),this.$box.attr("data-id",i[e].id),this.$box.append(a),this.$box.append(s),void this.$box.append(o)}n.append(a),n.append(s),n.append(o),this.$box.last().before(n)}},_completeBoxFile:function(i){this.isMultiple||this._clearTarget();for(var e in i){var s=t.dom("<div>");s.addClass("upload-item"),s.attr("data-id",i[e].id);var a=t.dom("<span>");a.html(i[e].name);var o=t.dom("<span>");o.addClass("close"),o.on("click",this._removeFile.bind(this));var n=t.dom("<input>");if(n.attr("type","hidden"),n.attr("name",this._getHiddenName()),n.val(i[e].id),"undefined"!=typeof i[e].size){var r=t.dom("<em>");r.html(i[e].size),a.append(r)}s.append(o),s.append(a),s.append(n),this.$target.append(s),this._upCount()}}})}(Kube);