(function(){"use strict";u8.Base.Widgets.TreeNode=function(e,t){$assert(e,"The TreeNode needs a model.");$.extend(this,t);this._model=e;this.parent=null;this.id=this._model._nextNodeId++;this._model._nodeMap[this.id]=this;if(!this.children){this.children=[]}else{this._initChildren()}};u8.Base.Widgets.TreeNode.makeClass({name:"TreeNode"});$.extend(u8.Base.Widgets.TreeNode.prototype,{attr:function(){return u8.Base.ComponentMixIn.attr.apply(this,arguments)},update:function(){this._on("Update",{node:this})},appendChild:function(e){this._model._nodeMap[e.id]=e;e.parent=this;this.children.push(e);this._on("AddNode",{node:e,index:this.children.length-1,insertBeforeNode:null})},insertChild:function(e,t){if(!t&&t!==0){this.appendChild(e);return}this._model._nodeMap[e.id]=e;e.parent=this;var i;if(typeof t=="number"){i=t;t=this.children[i]}else{i=this.children.indexOf(t)}this.children.splice(i,0,e);this._on("AddNode",{node:e,insertBeforeNode:t,index:i})},removeChild:function(e){if(e===undefined||e===null){return}var t;if(typeof e=="number"){t=e;e=this.children[t];if(!e){return}}else{t=this.children.indexOf(e)}this.children.splice(t,1);delete this._model._nodeMap[e.id];e.parent=null;this._on("RemoveNode",{node:e,parent:this})},each:function(e,t){if(t===undefined){t=this.parent?this.parent.children.indexOf(this):0}if(e(this,t)===false){return false}var i=this.children;if(i){for(var n=0;n<i.length;n++){var r=i[n];if(r.each(e,n)===false){return false}}}return true},findNode:function(e){var t;this.each(function(i){if(e(i)){t=i;return false}return true});return t},getDepth:function(){return this.getParents().length},getParents:function(e){var t=[];var i=this.parent;while(i){if(e&&e(i)||!e){t.push(i)}i=i.parent}return t},getRoot:function(){return this.parent?this.parent.getRoot():this},isLastChild:function(){if(!this.parent){return false}return this==this.parent.children.peek()},toString_:function(){return this.text||"#"+this.id},_initChildren:function(){var e=this.children;if(!e){return}for(var t=0;t<e.length;t++){var i=e[t];if(!i._model){i=e[t]=this._model.createNode(i)}i.parent=this}},_on:function(e,t){if(!this._model.contains(this)){return}this._model.raise(e,t)}})})();