/*
Copyright 2012, KISSY UI Library v1.30rc
MIT Licensed
build time: Jul 11 21:28
*/
KISSY.add("editor/plugin/table/dialog",function(h,j,m,k){function e(a){return 0!=f(a).length}function l(a){this.editor=a;j.Utils.lazyRun(this,"_prepareTableShow","_realTableShow")}var n=h.Node,o=h.DOM,p=h.UA,f=h.trim,q=m.Dialog,r=j.Utils.addRes,s=j.Utils.destroyRes;h.augment(l,{_tableInit:function(){var a=new q({autoRender:!0,width:"500px",mask:!0,headerContent:"表格",bodyContent:"<div style='padding:20px 20px 10px 20px;'><table class='ks-editor-table-config' style='width:100%'><tr><td><label>行数： <input  data-verify='^(?!0$)\\d+$'  data-warning='行数请输入正整数'  value='2'  class='ks-editor-table-rows ks-editor-table-create-only ks-editor-input' style='margin-left:2px;vertical-align:middle;' size='6' /></label></td><td><label>宽&nbsp;&nbsp;&nbsp;度： <input  data-verify='^(?!0$)\\d+$'  data-warning='宽度请输入正整数' value='200' style='margin-left:2px;vertical-align:middle;' class='ks-editor-table-width ks-editor-input' size='6'/></label> <select class='ks-editor-table-width-unit' title='宽度单位'><option value='px'>像素</option><option value='%'>百分比</option></select></td></tr><tr><td><label>列数： <input  data-verify='^(?!0$)\\d+$'  data-warning='列数请输入正整数' class='ks-editor-table-cols ks-editor-table-create-only ks-editor-input' style='margin-left:2px;vertical-align:middle;'value='3' size='6'/></label></td><td><label>高&nbsp;&nbsp;&nbsp;度： <input  data-verify='^((?!0$)\\d+)?$'  data-warning='高度请输入正整数' value='' style='margin-left:2px;vertical-align:middle;'class='ks-editor-table-height ks-editor-input' size='6'/></label> &nbsp;像素</td></tr><tr><td><label>对齐： </label><select class='ks-editor-table-align' title='对齐'><option value=''>无</option><option value='left'>左对齐</option><option value='right'>右对齐</option><option value='center'>中间对齐</option></select></td><td><label>标题格：</label> <select class='ks-editor-table-head ks-editor-table-create-only' title='标题格'><option value=''>无</option><option value='1'>有</option></select></td></tr><tr><td><label>边框： <input  data-verify='^\\d+$'  data-warning='边框请输入非负整数' value='1' style='margin-left:2px;vertical-align:middle;'class='ks-editor-table-border ks-editor-input' size='6'/></label> &nbsp;像素 <label><input type=\"checkbox\" style=\"vertical-align: middle; margin-left: 5px;\" class=\"ks-editor-table-collapse\" /> 合并边框</label></td><td><label class='ks-editor-table-cellpadding-holder'>边&nbsp;&nbsp;&nbsp;距： <input  data-verify='^(\\d+)?$'  data-warning='边框请输入非负整数' value='0' style='margin-left:2px;vertical-align:middle;'class='ks-editor-table-cellpadding ks-editor-input' size='6'/> &nbsp;像素</label></td></tr><tr><td colspan='2'><label>标题： <input class='ks-editor-table-caption ks-editor-input' style='width:380px;margin-left:2px;vertical-align:middle;'></label></td></tr></table></div>",
footerContent:"<div style='padding:5px 20px 20px;'><a class='ks-editor-table-ok ks-editor-button ks-inline-block' style='margin-right:20px;'>确定</a> <a class='ks-editor-table-cancel ks-editor-button ks-inline-block'>取消</a></div>"}),b=a.get("body"),c=a.get("footer");a.twidth=b.one(".ks-editor-table-width");a.theight=b.one(".ks-editor-table-height");a.tborder=b.one(".ks-editor-table-border");a.tcaption=b.one(".ks-editor-table-caption");a.talign=k.Select.decorate(b.one(".ks-editor-table-align"),{prefixCls:"ks-editor-big-",
elAttrs:{hideFocus:"hideFocus"},width:80,menuCfg:{prefixCls:"ks-editor-",render:b}});a.trows=b.one(".ks-editor-table-rows");a.tcols=b.one(".ks-editor-table-cols");a.thead=k.Select.decorate(b.one(".ks-editor-table-head"),{prefixCls:"ks-editor-big-",elAttrs:{hideFocus:"hideFocus"},width:80,menuCfg:{prefixCls:"ks-editor-",render:b}});a.cellpaddingHolder=b.one(".ks-editor-table-cellpadding-holder");a.cellpadding=b.one(".ks-editor-table-cellpadding");a.tcollapse=b.one(".ks-editor-table-collapse");var d=
c.one(".ks-editor-table-ok"),c=c.one(".ks-editor-table-cancel");a.twidthunit=k.Select.decorate(b.one(".ks-editor-table-width-unit"),{prefixCls:"ks-editor-big-",elAttrs:{hideFocus:"hideFocus"},width:80,menuCfg:{prefixCls:"ks-editor-",render:b}});this.dialog=a;d.on("click",this._tableOk,this);c.on("click",function(b){b&&b.halt();a.hide()});r.call(this,a,a.twidthunit,d,c)},_tableOk:function(a){a&&a.halt();var b=this,c=b.dialog,a=c.get("el").all("input");if("%"==c.twidthunit.get("value")&&(c=parseInt(c.twidth.val()),
!c||100<c||0>c)){alert("宽度百分比：请输入1-100之间");return}j.Utils.verifyInputs(a)&&(b.dialog.hide(),setTimeout(function(){b.selectedTable?b._modifyTable():b._genTable()},0))},_modifyTable:function(){var a=this.dialog,b=this.selectedTable,c=b.one("caption"),d=a.talign.get("value"),i=a.tborder.val();e(d)?b.attr("align",f(d)):b.removeAttr("align");e(i)?b.attr("border",f(i)):b.removeAttr("border");!e(i)||"0"==i?b.addClass("ke_show_border",void 0):b.removeClass("ke_show_border",void 0);e(a.twidth.val())?b.css("width",
f(a.twidth.val())+a.twidthunit.get("value")):b.css("width","");e(a.theight.val())?b.css("height",f(a.theight.val())):b.css("height","");a.tcollapse[0].checked?b.addClass("k-e-collapse-table",void 0):b.removeClass("k-e-collapse-table",void 0);a.cellpadding.val(parseInt(a.cellpadding.val())||0);this.selectedTd&&this.selectedTd.css("padding",a.cellpadding.val());e(a.tcaption.val())?(a=j.Utils.htmlEncode(f(a.tcaption.val())),c&&c[0]?c.html(a):(b=b[0].createCaption(),o.html(b,"<span>"+a+"</span>"))):c&&
c.remove()},_genTable:function(){var a=this.dialog,b="<table ",c=parseInt(a.tcols.val())||1,d=parseInt(a.trows.val())||1,i=p.ie?"&nbsp;":"&nbsp;<br/>",h=this.editor;e(a.talign.get("value"))&&(b+="align='"+f(a.talign.get("value"))+"' ");e(a.tborder.val())&&(b+="border='"+f(a.tborder.val())+"' ");var g=[];e(a.twidth.val())&&g.push("width:"+f(a.twidth.val())+a.twidthunit.get("value")+";");e(a.theight.val())&&g.push("height:"+f(a.theight.val())+"px;");g.length&&(b+="style='"+g.join("")+"' ");g=[];(!e(a.tborder.val())||
"0"==""+f(a.tborder.val()))&&g.push("ke_show_border");a.tcollapse[0].checked&&g.push("k-e-collapse-table");g.length&&(b+="class='"+g.join(" ")+"' ");b+=">";e(a.tcaption.val())&&(b+="<caption><span>"+j.Utils.htmlEncode(f(a.tcaption.val()))+"</span></caption>");if(a.thead.get("value")){b+="<thead><tr>";for(a=0;a<c;a++)b+="<th>"+i+"</th>";b+="</tr></thead>";d-=1}b+="<tbody>";for(g=0;g<d;g++){b+="<tr>";for(a=0;a<c;a++)b+="<td>"+i+"</td>";b+="</tr>"}b=new n(b+"</tbody></table>",null,h.get("document")[0]);
h.insertElement(b)},_fillTableDialog:function(){var a=this.dialog,b=this.selectedTable,c=b.one("caption");this.selectedTd&&a.cellpadding.val(parseInt(this.selectedTd.css("padding"))||"0");a.talign.set("value",b.attr("align")||"");a.tborder.val(b.attr("border")||"0");var d=b.style("width")||""+b.width();a.tcollapse[0].checked=b.hasClass("k-e-collapse-table",void 0);a.twidth.val(d.replace(/px|%|(.*pt)/i,""));-1!=d.indexOf("%")?a.twidthunit.set("value","%"):a.twidthunit.set("value","px");a.theight.val((b.style("height")||
"").replace(/px|%/i,""));d="";c&&(d=c.text());a.tcaption.val(d);c=b.first("thead");d=(b.one("tbody")?b.one("tbody").children().length:0)+(c?c.children("tr").length:0);a.trows.val(d);a.tcols.val(b.one("tr")?b.one("tr").children().length:0);a.thead.set("value",c?"1":"")},_realTableShow:function(){var a=this.dialog;this.selectedTable?(this._fillTableDialog(),a.get("el").all(".ks-editor-table-create-only").attr("disabled","disabled"),a.thead.set("disabled",!0)):(a.get("el").all(".ks-editor-table-create-only").removeAttr("disabled"),
a.thead.set("disabled",!1));this.selectedTd?a.cellpaddingHolder.show():a.cellpaddingHolder.hide();this.dialog.show()},_prepareTableShow:function(){this._tableInit()},show:function(a){h.mix(this,a);this._prepareTableShow()},destroy:function(){s.call(this)}});return l},{requires:["editor","../overlay/","../menubutton/"]});
