(function(){function p(a){for(;a&&!a.$instanceof(zul.sel.Tree);a=a.parent)if(a.isOpen&&!a.isOpen())return!0}function k(a,b){b.isSelected()&&a.$remove(b);if(b=b.treechildren)for(b=b.firstChild;b&&a.length;b=b.nextSibling)k(a,b)}function g(a,b){b.isSelected()&&a.push(b);if(b=b.treechildren)for(b=b.firstChild;b;b=b.nextSibling)g(a,b)}function m(a,b){var c=a.$n();c&&(c.style.display=b?"":"none");if(a=a.treechildren)for(a=a.firstChild;a;a=a.nextSibling)a._visible&&a._open&&m(a,b)}function l(a,b){a=b;for(b=
[a.getChildIndex()];a;)if(a=a.parent.parent,a.$instanceof(zul.sel.Treeitem))b.unshift(a.getChildIndex());else break;return b}function n(a,b){for(var c=0;;){if(a[c]<b[c])return-1;if(a[c]>b[c])break;else if(a[c]==b[c]){if(void 0==a[c])break;c++}else{if(void 0==a[c])return-1;break}}return 1}zul.sel.Treeitem=zk.$extends(zul.sel.ItemWidget,{_open:!0,$define:{open:function(a,b){var c=this.$n("open");var e=this.$n("icon");if(!c||p(this.parent))e&&(c=e.className,e.className=a?c.replace("-right","-down").replace("-close",
"-open"):c.replace("-down","-right").replace("-open","-close"));else{e&&(c=e.className,e.className=a?c.replace("-right","-down").replace("-close","-open"):c.replace("-down","-right").replace("-open","-close"));var d=this.getTree(),f=d?d.ebodytbl:null;e=f?f.clientWidth:0;a||zWatch.fireDown("onHide",this);this._showKids(a);a&&zUtl.fireShown(this);d&&(d._sizeOnOpen(),b||this.fire("onOpen",{open:a},{toServer:d.inPagingMold()||d.isModel()}),d._syncFocus(this),f&&(d._fixhdwcnt=d._fixhdwcnt||0,d._fixhdwcnt++||
(d._fixhdoldwd=e),setTimeout(function(){!--d._fixhdwcnt&&d.$n()&&d._fixhdoldwd!=f.clientWidth&&d._calcSize()},250)))}}},_showKids:function(a){var b=this.treechildren;if(b){var c=b.firstChild;for(b=b._isRealVisible();c;c=c.nextSibling)c.$n().style.display=b&&c.isVisible()&&a?"":"none",c.isOpen()&&c._showKids(a)}},isStripeable_:function(){return!1},getMeshWidget:_zkf=function(){return this.parent?this.parent.getTree():null},getTree:_zkf,getZclass:function(){return this.treerow?this.treerow.getZclass():
null},$n:function(a){return this.treerow?a?this.treerow.$n(a):this.treerow.$n()||jq(this.treerow.uuid,zk)[0]:null},isContainer:function(){return null!=this.treechildren},isEmpty:function(){return!this.treechildren||!this.treechildren.nChildren},getLevel:function(){for(var a=0,b=this;b.parent&&(b=b.parent.parent,b&&!b.$instanceof(zul.sel.Tree));++a);return a},getLabel:function(){var a=this.getFirstCell();return a?a.getLabel():null},setLabel:function(a){this._autoFirstCell().setLabel(a)},getFirstCell:function(){return this.treerow?
this.treerow.firstChild:null},_autoFirstCell:function(){this.treerow||this.appendChild(new zul.sel.Treerow);var a=this.treerow.firstChild;a||(a=new zul.sel.Treecell,this.treerow.appendChild(a));return a},getImage:function(){var a=this.getFirstCell();return a?a.getImage():null},setImage:function(a){this._autoFirstCell().setImage(a);return this},getParentItem:function(){var a=this.parent&&this.parent.parent?this.parent.parent:null;return a&&a.$instanceof(zul.sel.Treeitem)?a:null},_isRealVisible:function(){var a;
return this.isVisible()&&(a=this.parent)&&a._isRealVisible()},_isVisibleInTree:function(){if(this.isVisible()){var a=this.parent,b;return a&&a.isVisible()&&(b=a.parent)?b.$instanceof(zul.sel.Tree)?!0:b._isVisibleInTree():!1}},setVisible:function(a){this.isVisible()!=a&&(this.$supers("setVisible",arguments),this.treerow&&this.treerow.setVisible(a),m(this,this._isRealVisible()));return this},beforeParentChanged_:function(a){var b=this.getTree();b&&b._onTreeitemRemoved(this);a&&(b=a.getTree())&&b._onTreeitemAdded(this);
this.$supers("beforeParentChanged_",arguments)},isRealElement:function(){return!1},insertBefore:function(a,b,c){if(this.$super("insertBefore",a,b,c||!this.z_rod&&a.$instanceof(zul.sel.Treechildren)))return this._fixOnAdd(a,c),!0},appendChild:function(a,b){if(this.$super("appendChild",a,b||!this.z_rod&&a.$instanceof(zul.sel.Treechildren)))return this.insertingBefore_||this._fixOnAdd(a,b),!0},_fixOnAdd:function(a,b){a.$instanceof(zul.sel.Treerow)?this.treerow=a:a.$instanceof(zul.sel.Treechildren)&&
(this.treechildren=a,!b&&this.treerow&&this.rerender())},onChildRemoved_:function(a){this.$supers("onChildRemoved_",arguments);a==this.treerow?this.treerow=null:a==this.treechildren&&(this.treechildren=null,this.childReplacing_||this._syncIcon(!0))},onChildAdded_:function(a){this.$supers("onChildAdded_",arguments);this.childReplacing_?this._fixOnAdd(a,!0):this.desktop&&this._fixOnAdd(a,!0)},removeHTML_:function(a){for(var b,c=this.firstChild;c;c=c.nextSibling)(b=c.$n())&&c.removeHTML_(b);this.$supers("removeHTML_",
arguments)},replaceWidget:function(a){zul.sel.Treeitem._syncSelItems(this,a);this.treechildren&&this.treechildren.detach();this.$supers("replaceWidget",arguments)},_removeChildHTML:function(a){var b;for(a=this.firstChild;a;a=a.nextSibling)a!=this.treerow&&(b=a.$n())&&a.removeHTML_(b)},_renderChildHTML:function(a){var b=this.getTree(),c=b.ebodyrows;if(c&&c.childNodes.length){for(var e=0,d=c.childNodes,f=d.length-1,h=0,k=l(b,this);e<=f;){h=e+f>>>1;var g=zk.Widget.$(d[h].id).parent;g=l(b,g);1==n(k,g)?
e=h+1:(f=h-1,e>=f&&--h)}0<=h?jq(d[h]).after(a):c.firstChild?jq(c.firstChild).before(a):jq(c).append(a)}else jq(c).append(a)},insertChildHTML_:function(a,b,c){(b=b?b.getFirstNode_():null)?jq(b).before(a.redrawHTML_()):this._renderChildHTML(a.redrawHTML_());a.bind(c)},getOldWidget_:function(a){var b=this.$supers("getOldWidget_",arguments);return b&&b.$instanceof(zul.sel.Treerow)?b.parent:b},replaceHTML:function(a,b,c){this._removeChildHTML(a);this.$supers("replaceHTML",arguments)},_syncIcon:function(a){if(this.desktop&&
this.treerow){var b=this.treerow;(b=b.firstChild)&&b._syncIcon(a);if(b=this.treechildren)for(b=b.firstChild;b;b=b.nextSibling)b._syncIcon(a)}},compareItemPos_:function(a){if(this==a)return 0;var b=this.getTree();return n(l(b,a),l(b,this))}},{_syncSelItems:function(a,b){var c;if((c=a.getTree())&&(c=c._selItems))if(a.$instanceof(zul.sel.Treechildren)){for(a=a.firstChild;a;a=a.nextSibling)k(c,a);for(a=b.firstChild;a;a=a.nextSibling)g(c,a)}else k(c,a),g(c,b)}})})();
