/*
Copyright 2012, KISSY UI Library v1.30rc
MIT Licensed
build time: Jul 11 21:28
*/
KISSY.add("editor/plugin/multipleUpload/dialog",function(j,m,v,B,C,s,g){function q(a,e){this.editor=a;this.progressBars={};this.config=e;m.Utils.lazyRun(this,"_prepareShow","_realShow")}function x(a,e){var b=a.parentNode,d=e.nextSibling;b.insertBefore(e,a.nextSibling);b.insertBefore(a,d)}var w=j.UA,i=j.DOM,h=j.all,o=j.JSON,D=B.Dialog,E=m.Utils.debugUrl("plugin/uploader/uploader.longzang.swf");j.augment(q,{addRes:m.Utils.addRes,destroy:m.Utils.destroyRes,_prepareShow:function(){var a=this,e=a.editor,
b=a.config;a.addRes(function(){var b=a.progressBars,e;for(e in b)b.hasOwnProperty(e)&&b[e].destroy()});a.dialog=new D({headerContent:"批量上传",mask:!1,constrain:!1,autoRender:!0,focus4e:!1,width:"600px"});var d=a.dialog;d.on("beforeVisibleChange",function(a){if(!a.newVal)return d.set("xy",[-9999,-9999]),!1});a.addRes(d);var f=d.get("body"),c=h("<div class='ks-editor-upload-btn-wrap'><span style='margin:0 15px 0 0px;color:#969696;display:inline-block;vertical-align:middle;width:450px;'></span></div>").appendTo(f,
g),l=h("<div style='display:none'>").appendTo(f,g),n=h("<a class='ks-editor-button ks-inline-block'>浏&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;览</a>").appendTo(c,g),t=h("<div><table class='ks-editor-upload-list'><thead><tr><th style='width:30px;'>序号</th><th>图片</th><th>大小</th><th style='width:30%'>上传进度</th><th>图片操作</th></tr></thead><tbody></tbody></table></div>").appendTo(l,g),p=t.one("tbody"),k=h("<p style='margin:15px 15px 30px 6px;'><a class='ks-editor-bangpaiupload-delall' style='margin-right:20px;cursor:pointer;margin-left:40px;'>清空列表</a><a class='ks-editor-button ks-editor-bangpaiupload-ok ks-inline-block'>确定上传</a><a class='ks-editor-button ks-editor-bangpaiupload-insertall ks-inline-block' style='margin-left:20px;'>全部插入</a></p>").appendTo(l,
g),u=k.one(".ks-editor-bangpaiupload-ok"),f=k.one(".ks-editor-bangpaiupload-insertall"),o=k.one(".ks-editor-bangpaiupload-delall");j.guid("ks-editor-multipleUpload");k=h("<span>").prependTo(k,g);a._sizeLimit=b.sizeLimit||1E3;a._numberLimit=b.numberLimit||15;var q="允许用户同时上传"+a._numberLimit+"张图片，单张图片容量不超过"+a._sizeLimit/1E3+"M";w.fpvGEQ("10.0.0")||(q="您的flash插件版本过低，该功能不可用，请<a href='http://get.adobe.com/cn/flashplayer/' target='_blank'>点此升级</a>");n.addClass("ks-editor-button-disabled",g);a.tipSpan=c.one("span");
a.tipSpan.html(q);if(w.fpvGEQ("10.0.0")){b.extraHtml&&t.append(b.extraHtml);a._list=p;a._listTable=p.parent("table");a._listWrap=l;a._ds=b.serverUrl;a._dsp=b.serverParams||{};a._fileInput=b.fileInput||"Filedata";a.statusText=k;a.btn=n;a.up=u;t=n.offset();u=2*n.width();k=1.5*n.height();c=h("<div style='"+("position:absolute;width:"+u+"px;height:"+k+"px;z-index:"+m.baseZIndex(9999)+";")+"'>").appendTo(c,g);c.offset(t);a.flashPos=c;c=new C({movie:E,ajbridge:!0,methods:"getReady,removeFile,lock,unlock,setAllowMultipleFiles,setFileFilters,uploadAll".split(","),
holder:c,attrs:{width:u,height:k},params:{wmode:"transparent"},flashVars:{allowedDomain:location.hostname,btn:!0,hand:!0}});a.uploader=c;c.on("mouseOver",function(){n.addClass("ks-editor-button-hover",g)});c.on("mouseOut",function(){n.removeClass("ks-editor-button-hover",g)});a.addRes(c);var v=e.get("document")[0];f.on("click",function(b){for(var c=p.all("tr"),f=0;f<c.length;f++){var g=h(c[f]),i=g.attr("url");i&&((new Image).src=i,e.insertElement(h("<p>&nbsp;<img src='"+i+"'/>&nbsp;</p>",v)),a._removeTrFile(g))}i&&
(l.hide(),d.hide());b.halt()});a.addRes(f);o.on("click",function(b){for(var e=p.all("tr"),d=0;d<e.length;d++){var c=h(e[d]);a._removeTrFile(c)}l.hide();b.halt()});a.addRes(o);p.on("click",function(b){var d=h(b.target),c;b.halt();if(d.hasClass("ks-editor-upload-insert",g)){c=d.parent("tr");var f=c.attr("url");(new Image).src=f;e.insertElement(h("<img src='"+f+"'/>",null,e.get("document")[0]))}if(d.hasClass("ks-editor-upload-delete",g)||d.hasClass("ks-editor-upload-insert",g))c=d.parent("tr"),a._removeTrFile(c);
if(d.hasClass("ks-editor-upload-moveup",g)){if(c=d.parent("tr"),c.css("backgroundColor","#eef4f9"),c.animate({backgroundColor:"#FBFBFB"},1,null,function(){c.css("backgroundColor","")}),d=c.prev(g,g))x(c[0],d[0]),a._syncStatus()}else if(d.hasClass("ks-editor-upload-movedown",g)&&(c=d.parent("tr"),c.css("backgroundColor","#eef4f9"),c.animate({backgroundColor:"#FBFBFB"},1,null,function(){c.css("backgroundColor","")}),d=c.next()))x(c[0],d[0]),a._syncStatus();b.halt()});a.addRes(p);c.on("fileSelect",a._onSelect,
a);c.on("uploadStart",a._onUploadStart,a);c.on("uploadProgress",a._onProgress,a);c.on("uploadCompleteData",a._onUploadCompleteData,a);c.on("contentReady",a._ready,a);c.on("uploadError",a._uploadError,a);s.ready?s.ready(function(){a._restore()}):a._restore();var f=b.previewWidth,y=b.previewSuffix;if(f){var r=new (j.require("overlay"))({mask:!1,prefixCls:"ks-editor-",autoRender:!0,width:f,render:l});a.addRes(r);var z=r.get("contentEl");z.css("border","none");var A=0;l.on("mouseover",function(a){if(a=
h(a.target).parent(".ks-editor-upload-filename")){var b=a.parent("tr");if(b.hasClass("ks-editor-upload-complete",g)){var c=b.attr("url"),b=b.attr("fid");c&&(b!=A&&(A=b,y&&(c=c.replace(/(\.\w+$)/,y)),z.html("<img style='display:block;' src='"+c+"' />")),c=i.offset(a),c.left+=a[0].offsetWidth,r.set("xy",[c.left,c.top]),r.show())}}else r.hide()});a.addRes(l)}!w.webkit&&9!=m.Utils.ieEngine&&d.set("handlers",[d.get("el")])}},_removeTrFile:function(a){var e=this.progressBars,b=a.attr("fid"),d=this.uploader;
if(b)try{d.removeFile(b)}catch(f){}e[b]&&(e[b].destroy(),delete e[b]);a.remove();this.denable();this._syncStatus()},_realShow:function(){this.dialog.center();this.dialog.show()},show:function(){this._prepareShow()},_uploadError:function(a){var e=this.progressBars,b=this.uploader,d=a.id||a.file&&a.file.id;if(d){var f=this._getFileTr(d),c=e[d],g=a.status;b.removeFile(d);a._custom||(g="服务器出错或格式不正确");f&&(c&&c.destroy(),delete e[d],f.one(".ks-editor-upload-progress").html("<div style='color:red;'>"+g+
"</div>"))}},_getFileTr:function(a){for(var e=this._list.all("tr"),b=0;b<e.length;b++){var d=h(e[b]);if(d.attr("fid")==a)return d}},_onUploadStart:function(a){this._getFileTr(a.id||a.file&&a.file.id)[0].className="ks-editor-upload-uploading"},_onUploadCompleteData:function(a){var e=this.uploader,b=j.trim(a.data).replace(/\r|\n/g,"");if(a=a.file.id)try{e.removeFile(a)}catch(d){}if(b){try{b=o.parse(b)}catch(f){throw f;}if(b.error)this._uploadError({id:a,_custom:1,status:b.error});else{if(e=this._getFileTr(a))e.one(".ks-editor-upload-insert").show(),
this._tagComplete(e,b.imgUrl);this._syncStatus()}}},_onProgress:function(a){var e=Math.floor(100*a.bytesLoaded/a.bytesTotal);(a=this.progressBars[a.file.id])&&a.set("progress",e)},ddisable:function(){this.uploader.lock();this.btn.addClass("ks-editor-button-disabled",g);this.flashPos.offset({left:-9999,top:-9999})},denable:function(){this.uploader.unlock();this.btn.removeClass("ks-editor-button-disabled",g);this.flashPos.offset(this.btn.offset())},_syncStatus:function(){var a=this._list,e=1,b=a.all("tr");
if(0==b.length)this._listWrap.hide();else{a.all(".ks-editor-upload-seq").each(function(a){a.html(e++)});for(var d=a=0;d<b.length;d++)h(b[d]).attr("url")||a++;this.statusText.html("队列中剩余"+a+"张图片，点击确定上传，开始上传。 ")}this._save()},_restore:function(){var a=s.getItem("Multiple-Upload-Save"),e=this._list[0];if(a){for(var a=o.parse(decodeURIComponent(a)),b=0;b<a.length;b++){var d=a[b];d.complete=1;d.fid="restore_"+b;this._tagComplete(this._createFileTr(e,d),d.url)}d&&(this._listWrap.show(),this._syncStatus())}},
_tagComplete:function(a,e){a.attr("url",e);a[0].className="ks-editor-upload-complete"},_save:function(){for(var a=this._list.all("tr"),e=[],b=0;b<a.length;b++){var d=h(a[b]),f=d.attr("url");if(f){var c=d.one(".ks-editor-upload-filesize").html(),d=d.one(".ks-editor-upload-filename").text();e.push({name:d,size:c,url:f})}}s.setItem("Multiple-Upload-Save",encodeURIComponent(o.stringify(e)))},_getFilesSize:function(a){var e=0,b;for(b in a)a.hasOwnProperty(b)&&e++;return e},_createFileTr:function(a,e){var b=
this.progressBars,d=e.fid,f=a.insertRow(-1);i.attr(f,"fid",d);var c=f.insertCell(-1);i.attr(c,"class","ks-editor-upload-seq");c=f.insertCell(-1);18<e.name.length&&(e.name=e.name.substring(0,18)+"...");i.html(c,"<div style='width:160px;overflow:hidden;'><div style='width:9999px;text-align:left;'>"+e.name+"</div></div>");i.attr(c,"class","ks-editor-upload-filename");c=f.insertCell(-1);i.html(c,e.size);i.attr(c,"class","ks-editor-upload-filesize");c=f.insertCell(-1);i.attr(c,"class","ks-editor-upload-progress");
c=f.insertCell(-1);i.html(c,"<a class='ks-editor-upload-moveup' href='#'>[上移]</a> &nbsp; <a class='ks-editor-upload-movedown' href='#'>[下移]</a> &nbsp; <a href='#' class='ks-editor-upload-insert' style='"+(e.complete?"":"display:none;")+"' >[插入]</a> &nbsp; <a href='#' class='ks-editor-upload-delete'>[删除]</a> &nbsp;");f=h(f);parseInt(e.size)>this._sizeLimit?(this._uploadError({id:d,_custom:1,status:"图片太大，请压缩至 n M以下".replace(/n/,this._sizeLimit/1E3)}),this.uploader.removeFile(d)):(b[d]=new v({container:f.one(".ks-editor-upload-progress"),
width:"100px",height:"15px"}),e.complete&&b[d].set("progress",100));return f},_onSelect:function(a){var e=this.uploader,b=this._list,d=0,a=a.fileList,f=this._numberLimit;if(a){f=b.children("tr");for(b=0;b<f.length;b++){var c=i.attr(f[b],"fid");c&&a[c]&&delete a[c]}f=this._numberLimit-f.length;c=this._getFilesSize(a);c>f&&alert("系统将只保留 n 张".replace(/n/,this._numberLimit));c>=f&&this.ddisable();this._listWrap.show();c=this._list[0];for(b in a)if(a.hasOwnProperty(b)){d++;var g=a[b],h=Math.floor(g.size/
1E3),j=g.id;d>f?e.removeFile(j):this._createFileTr(c,{size:h+"k",fid:j,name:g.name})}this._syncStatus()}},_ready:function(){var a=this,e=a.uploader,b=a.up,d=a.btn,f=a.flashPos,c=m.Utils.normParams;"ready"!=e.getReady()?(a.tipSpan.html("您的浏览器不支持该功能，请升级当前浏览器，并同时 <a href='http://get.adobe.com/cn/flashplayer/' target='_blank'>点此升级</a> flash 插件"),f.offset({left:-9999,top:-9999})):(d.removeClass("ks-editor-button-disabled",g),f.offset(d.offset()),e.setAllowMultipleFiles(!0),e.setFileFilters([{ext:"*.jpeg;*.jpg;*.png;*.gif",
desc:"图片文件( png,jpg,jpeg,gif )"}]),b.detach(),b.on("click",function(b){e.uploadAll(a._ds,"POST",c(a._dsp),a._fileInput);b.halt()}),a.addRes(b))}});return q},{requires:["editor","../progressbar/","../overlay/","../flashBridge/","../localStorage/"]});
