/*
Copyright 2013, KISSY UI Library v1.31
MIT Licensed
build time: Jul 29 13:11
*/
KISSY.add("editor/plugin/list-utils/cmd",function(s,r,p,d){function v(d){this.type=d}function w(d,e){var f,h,b,j=e.blockLimit,a=e.elements;if(!j)return!1;if(a)for(b=0;b<a.length&&(f=a[b])&&f[0]!==j[0];b++)if(u[h=f.nodeName()]&&h==d)return f.css("list-style-type");return!1}var u={ol:"insertOrderedList",ul:"insertUnorderedList"},x=r.RANGE,z=r.ElementPath,y=r.Walker,A=s.UA,n=s.Node,t=s.DOM,B=/^h[1-6]$/;v.prototype={changeListType:function(i,e,f,h,b){for(var j=p.listToArray(e.root,f,d,d,d),a=[],i=0;i<
e.contents.length;i++){var c=e.contents[i];if((c=c.closest("li",d))&&c[0]&&!c.data("list_item_processed"))a.push(c),c._4e_setMarker(f,"list_item_processed",!0,d)}c=new n(e.root[0].ownerDocument.createElement(this.type));c.css("list-style-type",b);for(i=0;i<a.length;i++)b=a[i].data("listarray_index"),j[b].parent=c;for(var f=p.arrayToList(j,f,null,"p"),k,b=f.listNode.childNodes.length,i=0;i<b&&(k=new n(f.listNode.childNodes[i]));i++)k.nodeName()==this.type&&h.push(k);e.root.before(f.listNode);e.root.remove()},
createList:function(i,e,f,h){var b=e.contents,i=e.root[0].ownerDocument,j=[];if(1==b.length&&b[0][0]===e.root[0]){var a=new n(i.createElement("div"));b[0][0].nodeType!=t.NodeType.TEXT_NODE&&b[0]._4e_moveChildren(a,d,d);b[0][0].appendChild(a[0]);b[0]=a}e=e.contents[0].parent();for(a=0;a<b.length;a++)e=e._4e_commonAncestor(b[a].parent(),d);for(a=0;a<b.length;a++)for(var c=b[a],k;k=c.parent();){if(k[0]===e[0]){j.push(c);break}c=k}if(!(1>j.length)){b=new n(j[j.length-1][0].nextSibling);a=new n(i.createElement(this.type));
a.css("list-style-type",h);for(f.push(a);j.length;)f=j.shift(),h=new n(i.createElement("li")),B.test(f.nodeName())?h[0].appendChild(f[0]):(f._4e_copyAttributes(h,d,d),f._4e_moveChildren(h,d,d),f.remove()),a[0].appendChild(h[0]),A.ie||h._4e_appendBogus(d);b[0]?a.insertBefore(b,d):e.append(a)}},removeList:function(i,e,f){function h(a){if((l=new n(k[a?"firstChild":"lastChild"]))&&!(l[0].nodeType==t.NodeType.ELEMENT_NODE&&l._4e_isBlockBoundary(d,d))&&(g=e.root[a?"prev":"next"](y.whitespaces(!0),1))&&
!(l[0].nodeType==t.NodeType.ELEMENT_NODE&&g._4e_isBlockBoundary({br:1},d)))l[a?"before":"after"](i.get("document")[0].createElement("br"))}for(var b=p.listToArray(e.root,f,d,d,d),j=[],a=0;a<e.contents.length;a++){var c=e.contents[a];if((c=c.closest("li",d))&&!c.data("list_item_processed"))j.push(c),c._4e_setMarker(f,"list_item_processed",!0,d)}c=null;for(a=0;a<j.length;a++)c=j[a].data("listarray_index"),b[c].indent=-1;for(a=c+1;a<b.length;a++)if(b[a].indent>Math.max(b[a-1].indent,0)){j=b[a-1].indent+
1-b[a].indent;for(c=b[a].indent;b[a]&&b[a].indent>=c;)b[a].indent+=j,a++;a--}var k=p.arrayToList(b,f,null,"p").listNode,l,g;h(!0);h(d);e.root.before(k);e.root.remove()},exec:function(i,e){var f=i.getSelection(),h=f&&f.getRanges();if(h&&!(1>h.length)){for(var b=f.getStartElement(),b=new r.ElementPath(b),j=w(this.type,b),b=f.createBookmarks(!0),a=[],c={};0<h.length;){var k=h.shift(),l=k.getBoundaryNodes(),g=l.startNode,m=l.endNode;g[0].nodeType==t.NodeType.ELEMENT_NODE&&"td"==g.nodeName()&&k.setStartAt(l.startNode,
x.POSITION_AFTER_START);m[0].nodeType==t.NodeType.ELEMENT_NODE&&"td"==m.nodeName()&&k.setEndAt(l.endNode,x.POSITION_BEFORE_END);k=k.createIterator();for(k.forceBrBreak=!1;l=k.getNextParagraph();)if(!l.data("list_block")){l._4e_setMarker(c,"list_block",1,d);for(var m=new z(l),n=m.elements,q=null,p=!1,q=m.blockLimit,o,g=n.length-1;0<=g&&(o=n[g]);g--)if(u[o.nodeName()]&&q.contains(o)){q.removeData("list_group_object");(g=o.data("list_group_object"))?g.contents.push(l):(g={root:o,contents:[l]},a.push(g),
o._4e_setMarker(c,"list_group_object",g,d));p=!0;break}p||(m=q||m.block,m.data("list_group_object")?m.data("list_group_object").contents.push(l):(g={root:m,contents:[l]},m._4e_setMarker(c,"list_group_object",g,d),a.push(g)))}}for(h=[];0<a.length;)g=a.shift(),j?u[g.root.nodeName()]&&(g.root.css("list-style-type")==e?this.removeList(i,g,c):g.root.css("list-style-type",e)):u[g.root.nodeName()]?this.changeListType(i,g,c,h,e):(r.Utils.clearAllMarkers(c),this.createList(i,g,h,e));for(var s=this,g=0;g<h.length;g++)q=
h[g],o=function(a,b){var c=b[a?"prev":"next"](y.whitespaces(!0),1);c&&c[0]&&c.nodeName()==s.type&&c.css("list-style-type")==e&&(c.remove(),c._4e_moveChildren(b,a?!0:!1,d))},o(d,q),o(!0,q);r.Utils.clearAllMarkers(c);f.selectBookmarks(b)}}};return{ListCommand:v,queryActive:w}},{requires:["editor","../list-utils/"]});
