(function(){u8.Base.Widgets.FormDesignerTree=function(e,t){this.Widget(e,$.extend({},u8.Base.Widgets.FormDesignerTree.defaults,t));if(!u8.tools.isInstanceOfClass(this.parent,"FormDesigner")){$error(u8.Base.ErrorUids.arguments,"FormDesignerTree must be hosted in a FormDesigner!")}this.designerModel=this.parent.model;this._create()};u8.Base.Widgets.FormDesignerTree.defaults={autoExpandSelection:true};u8.Base.Widgets.FormDesignerTree.makeClass({name:"FormDesignerTree",deriveFrom:u8.Base.Widgets.Widget});$.extend(u8.Base.Widgets.FormDesignerTree.prototype,{_create:function(){this.children.clear();this.$(this.element).empty();this._tree=new u8.Base.Widgets.Tree(this.element,{parent:this,allowExpandRoot:false,showLines:true,onSelectionChange:this._onTreeSelectionChange.bind(this),onAction:this._onTreeAction.bind(this)});this.parent.bind("SelectionChange",this._onFormDesignerSelectionChange.bind(this),this.id);this.designerModel.bind("ItemCreate",this._onModelItemCreate.bind(this),this.id).bind("ItemMove",this._onModelItemMove.bind(this),this.id).bind("ItemDelete",this._onModelItemDelete.bind(this),this.id).bind("ItemUpdate",this._onModelItemUpdate.bind(this),this.id);this._createModel()},dispose:function(){if(this._isDisposed){return}this.parent.unbind("*",this.id);this.parent.model.unbind("*",this.id);this.Widget_dispose()},_createModel:function(){this._itemNodeMap={};var e=new u8.Base.Widgets.TreeModel;e.nodeTypes={form:{canSelect:true},category:{},widget:{canSelect:true},component:{canSelect:true},widgetPart:{canSelect:true}};if(!this.readonly){$.extend(e.nodeTypes.component,{hasAction:true,actionSprite:"u8-img-delete-s",actionTooltip:"Delete"})}e.rootNode=new u8.Base.Widgets.TreeNode(e,{html:"<b>Form</b>",type:"form",_formDesigner:{itemId:"root"}});this._itemNodeMap["root"]=e.rootNode;this._widgetsNode=new u8.Base.Widgets.TreeNode(e,{text:"Widgets",type:"category"});this._componentsNode=new u8.Base.Widgets.TreeNode(e,{text:"Components",type:"category"});e.rootNode.insertChild(this._widgetsNode);e.rootNode.insertChild(this._componentsNode);this._tree.setModel(e);var t=this.designerModel.getItem("root");for(var i=0;i<t.children.length;i++){this._createNode(this.designerModel.getItem(t.children[i]))}},_createNode:function(e,t){if(typeof t=="undefined"){t=true}if(e.isPlaceHolder||e.type=="FormDesignerPlaceholder"){return}var i=this._getParentNodeForItem(e);var n=undefined;if(e.kind=="widget"||e.kind=="component"){n=u8.Base.Widgets.FormDesigner._getSpriteNameForType(e.kind,e.type)}var r=new u8.Base.Widgets.TreeNode(this._tree.getModel(),{html:e.htmlLabel,type:e.kind,sprite:n,_formDesigner:{itemId:e.id}});this._itemNodeMap[e.id]=r;for(var o=0;o<e.children.length;o++){this._createNode(this.designerModel.getItem(e.children[o]))}var s=e.containingArrayIndex;if(t||typeof s=="undefined"){i.insertChild(r)}else{i.insertChild(r,i.children[s])}this._tree.setExpanded(i,true)},_getParentNodeForItem:function(e){if(e.parent=="root"){return e.kind=="widget"?this._widgetsNode:this._componentsNode}var t=this._itemNodeMap[e.parent];var i=this.designerModel.getItem(e.parent);var n=u8.services.reflection.getType(i.type);if(n&&n.designer&&n.designer.childItems&&n.designer.childItems.length>1){var r=e.containingArrayPath.split(".");var o=r.peek();var s;for(var d=0;d<t.children.length;d++){s=t.children[d];if(s.type=="category"&&s._formDesigner.property==o){return s}}s=new u8.Base.Widgets.TreeNode(this._tree.getModel(),{text:o,type:"category",_formDesigner:{property:o}});t.insertChild(s);return s}return t},_onModelItemCreate:function(e,t){this._createNode(t.item,false)},_onModelItemMove:function(e,t){var i=this._itemNodeMap[t.item.id];if(i){i.parent.removeChild(i);var n=this._getParentNodeForItem(t.item);n.insertChild(i)}},_onModelItemDelete:function(e,t){var i=this._itemNodeMap[t.item.id];if(i){i.parent.removeChild(i);delete this._itemNodeMap[t.item.id]}},_onModelItemUpdate:function(e,t){if(t.reason=="rename"){var i=this._itemNodeMap[t.item.id];i.html=t.item.htmlLabel;i.update()}},_onTreeSelectionChange:function(e){var t=e.getSelection();this.parent.setSelectedItemId(t&&t._formDesigner.itemId)},_onTreeAction:function(e,t){var i=t.node._formDesigner&&t.node._formDesigner.itemId;if(!i){return}var n=this.parent.model.getItem(i);if(n.kind=="component"){this.parent.model.deleteItem(n)}},_onFormDesignerSelectionChange:function(e,t){var i=this._itemNodeMap[t.value];if(i){this._tree.setSelectedNode(i,false);if(this.autoExpandSelection&&i.parent){this._tree.setExpanded(i.parent,true)}}}})})();