KISSY.Editor.add("table/support",function(){function r(a){var c=this,b={},d;for(d in s)(function(e){b[e]=function(){a.fire("save");s[e](c);a.fire("save")}})(d);d=i.ContextMenu.register({editor:a,statusChecker:A,rules:B,width:"120px",funcs:b});C.call(c,d);c.editor=a}function t(a){function c(l){if(!(e.length>0))if(l[0].nodeType==D.NODE_ELEMENT&&u.test(l._4e_name())&&!l.data("selected_cell")){l._4e_setMarker(f,"selected_cell",true);e.push(l)}}for(var b=a.createBookmarks(),d=a.getRanges(),e=[],f={},g=
0;g<d.length;g++){var h=d[g];if(h.collapsed){h=h.getCommonAncestor();(h=h._4e_ascendant("td",true)||h._4e_ascendant("th",true))&&e.push(h)}else{h=new Walker(h);var k;for(h.guard=c;k=h.next();)if((k=k.parent())&&u.test(k._4e_name())&&!k.data("selected_cell")){k._4e_setMarker(f,"selected_cell",true);e.push(k)}}}i.Utils.clearAllMarkers(f);a.selectBookmarks(b);return e}function v(a,c){var b=a.getStartElement()._4e_ascendant("tr");if(b){var d=b._4e_clone(true);d.insertBefore(b);b=(c?d[0]:b[0]).cells;for(d=
0;d<b.length;d++){b[d].innerHTML="";w.ie||(new j(b[d]))._4e_appendBogus()}}}function p(a){if(a instanceof i.Selection){var c=t(a),b=c.length;a=[];for(var d,e,f=0;f<b;f++){var g=c[f].parent(),h=g[0].rowIndex;!f&&(d=h-1);a[h]=g;f==b-1&&(e=h+1)}f=g._4e_ascendant("table");d=new j(e<f[0].rows.length&&f[0].rows[e]||d>0&&f[0].rows[d]||f[0].parentNode);for(f=a.length;f>=0;f--)a[f]&&p(a[f]);return d}else if(a instanceof j){f=a._4e_ascendant("table");f[0].rows.length==1?f._4e_remove():a._4e_remove()}return 0}
function x(a,c){var b=a.getStartElement();if(b=b._4e_ascendant("td",true)||b._4e_ascendant("th",true))for(var d=b._4e_ascendant("table"),e=b[0].cellIndex,f=0;f<d[0].rows.length;f++){var g=d[0].rows[f];if(!(g.cells.length<e+1)){b=new j(g.cells[e].cloneNode(false));w.ie||b._4e_appendBogus();g=new j(g.cells[e]);c?b.insertBefore(g):b.insertAfter(g)}}}function y(a){if(a instanceof i.Selection){var c=t(a),b,d=[];a=c[0]&&c[0]._4e_ascendant("table");var e,f,g;e=0;for(f=c.length;e<f;e++)d.push(c[e][0].cellIndex);
d.sort();e=1;for(f=d.length;e<f;e++)if(d[e]-d[e-1]>1){g=d[e-1]+1;break}g||(g=d[0]>0?d[0]-1:d[d.length-1]+1);d=a[0].rows;e=0;for(f=d.length;e<f;e++)if(b=d[e].cells[g])break;b=b?new j(b):a._4e_previous();for(g=c.length-1;g>=0;g--)c[g]&&y(c[g]);return b}else if(a instanceof j){c=a._4e_ascendant("table");if(!c)return null;b=a[0].cellIndex;for(g=c[0].rows.length-1;g>=0;g--){a=new j(c[0].rows[g]);if(!b&&a[0].cells.length==1)p(a);else a[0].cells[b]&&a[0].removeChild(a[0].cells[b])}}return null}function z(a,
c){var b=new i.Range(a[0].ownerDocument);if(!b.moveToElementEditablePosition(a,c?true:undefined)){b.selectNodeContents(a);b.collapse(c?false:true)}b.select(true)}function m(a){var c=(a=a.getSelection())&&a.getStartElement(),b=c&&c._4e_ascendant("table",true);if(b){a=c._4e_ascendant(function(d){var e=d._4e_name();return b.contains(d)&&(e=="td"||e=="th")},true);c=c._4e_ascendant(function(d){var e=d._4e_name();return b.contains(d)&&e=="tr"},true);return{table:b,td:a,tr:c}}}function n(a){return(a=m(a))&&
a.td}function q(a){return(a=m(a))&&a.tr}var o=KISSY,w=o.UA,j=o.Node,i=o.Editor,D=i.NODE,B=["tr","th","td","tbody","table"],C=i.Utils.addRes,E=i.Utils.destroyRes;o.augment(r,{_tableShow:function(a,c,b){this.editor.useDialog("table/dialog",function(d){d.show(c,b)})},destroy:function(){E.call(this);this.editor.destroyDialog("table/dialog")}});var u=/^(?:td|th)$/,A={"\u8868\u683c\u5c5e\u6027":m,"\u5220\u9664\u8868\u683c":n,"\u5220\u9664\u5217":n,"\u5220\u9664\u884c":q,"\u5728\u4e0a\u65b9\u63d2\u5165\u884c":q,"\u5728\u4e0b\u65b9\u63d2\u5165\u884c":q,"\u5728\u5de6\u4fa7\u63d2\u5165\u5217":n,"\u5728\u53f3\u4fa7\u63d2\u5165\u5217":n},s={"\u8868\u683c\u5c5e\u6027":function(a){var c=m(a.editor);c&&a._tableShow(null,
c.table,c.td)},"\u5220\u9664\u8868\u683c":function(a){var c=(a=a.editor.getSelection())&&a.getStartElement();if(c=c&&c._4e_ascendant("table",true)){a.selectElement(c);var b=a.getRanges()[0];b.collapse();a.selectRanges([b]);a=c.parent();a[0].childNodes.length==1&&a._4e_name()!="body"&&a._4e_name()!="td"?a._4e_remove():c._4e_remove()}},"\u5220\u9664\u884c ":function(a){a=a.editor.getSelection();z(p(a),undefined)},"\u5220\u9664\u5217 ":function(a){a=a.editor.getSelection();(a=y(a))&&z(a,true)},"\u5728\u4e0a\u65b9\u63d2\u5165\u884c":function(a){a=a.editor.getSelection();v(a,true)},
"\u5728\u4e0b\u65b9\u63d2\u5165\u884c":function(a){a=a.editor.getSelection();v(a,undefined)},"\u5728\u5de6\u4fa7\u63d2\u5165\u5217":function(a){a=a.editor.getSelection();x(a,true)},"\u5728\u53f3\u4fa7\u63d2\u5165\u5217":function(a){a=a.editor.getSelection();x(a,undefined)}};i.TableUI=r},{attach:false,requires:["contextmenu"]});
