﻿
function AssetComboBoxTree()
{
    this.AstTree = new AssetTreePanel();    
    this.AstCBTree = new AstComboBoxTree();    
};



function AstComboBoxTree()
{
    this.CBTree = null;
    this.CBTreeName = null; // ComboBoxTree控件名称(name) 必须赋值    
    this.selectNodeModel =  'leaf';  //选择节点的方式('exceptRoot' 父节点可选,'leaf' 父节点不可选)    
    this.Width = 120;
    this.minListWidth = 300;
};


AssetComboBoxTree.prototype.NewComboBoxTreePanel = function()
{    
    this.AstCBTree.CBTree = new Ext.ux.ComboBoxTree({
        fieldLabel      :  this.AstTree.TreeRootText,
        name            :  this.AstCBTree.CBTreeName,
        hiddenName      :  this.AstCBTree.CBTreeName,
        emptyText       :  '--请选择--',
        tree            :  this.AstTree.TreePanel,
        width           :  this.AstCBTree.Width,
        minListWidth    :  this.AstCBTree.minListWidth,        
        selectNodeModel  :  this.AstCBTree.selectNodeModel
        });      
};

//      使用AssetTreePanel实例
//      var OfficeTreePanel = new AssetTreePanel();
//	    OfficeTreePanel.TreeRootID = "0";
//	    OfficeTreePanel.TreeRootText =  "科室";
//	    OfficeTreePanel.NewTreePanel();
//	    var officetree = OfficeTreePanel.TreePanel;	   
//	    OfficeTreePanel.TreePanel.on('contextmenu',ShowRightMenu);  //可以添加不同的事件
function AssetTreePanel()
{
    this.TreePanel = null;
    this.TreeLoader = null;     //树的数据源

    this.TreeRoot = null;       //根节点

    this.TreeRootID = "";       //根节点的ID值   必须赋值

    this.TreeRootText = "";     //根节点的值 必须赋值

    this.cNode = null;          //选中的当前节点
    
    this.rootvisible = true;   //根节点是否显示
  
    this.isLeaf = null;         //选中的当前节点是否为叶子节点
    
    this.AjaxUrl = "";          //以Ajax方式请求服务器的路径 
    
    //this.UrlParams = "";       //Url参数
    
    this.ShowCheckbox = false;  //带Checkbox的树 , 默认为false
    
    this.ckNodeIDs = null;        //已选中的树节点
    
    this.expandTree = false;      //不需要展开所有节点
    
    this.urlClassName = "";      //执行服务器端的类名
    
    this.urlMethod = "";         //执行服务器端的方法名
    
    
};


AssetTreePanel.prototype.NewTreeRoot = function()
{
    var oThis = this;
   
    oThis.TreeRoot = new Ext.tree.AsyncTreeNode({
        id : oThis.TreeRootID,
        text : oThis.TreeRootText,    
        leaf : 'false',   
        children:[{
            text: 'loading',
            iconCls:'loading',
            leaf: true
        }]
    });

};

AssetTreePanel.prototype.NewTreeLoader = function()
{
    var oThis = this;
    oThis.TreeLoader = new Ext.tree.TreeLoader();
};

//分级取得节点
AssetTreePanel.prototype.NewTreePanel = function()
{
    var oThis = this;
    this.NewTreeRoot();
    this.NewTreeLoader();
    oThis.TreePanel = new Ext.tree.TreePanel({        
        id : oThis.TreePanelID,
        border: false,
        animate:true, 
        //rootVisible:oThis.rootvisible,       
        loader:oThis.TreeLoader,
        lines: true,
        autoScroll:true,        
        root: oThis.TreeRoot
    });
    
    oThis.TreeRoot.on('expand',oThis.getChildTreeNode,oThis);    
};

AssetTreePanel.prototype.getChildTreeNode = function(node)
{
    var oThis = this;
    var cnode = null;
    if ( node.firstChild.text ==  "loading" ){
        var par = node.id;
//        if(par == 0)
//            par = -99;
        Ext.Ajax.request({
            url:oThis.AjaxUrl,
            params:{id:par},
            method:'GET',
            success:function(v){                 
                    var treedata = Ext.decode(v.responseText);                                   
                    for ( var i = 0 ; i < treedata.length; i++){                                             
                        if ( treedata[i].leaf == true )
                        {
                             cnode = new Ext.tree.TreeNode({                                
                                id:treedata[i].id,
                                text:treedata[i].text,
                                qtip:treedata[i].qtip,
                                leaf:true                                   
                            });                    
                        }
                        else
                        {
                            cnode = new Ext.tree.AsyncTreeNode({                                
                                id:treedata[i].id,
                                text:treedata[i].text,
                                leaf:treedata[i].leaf, 
                                qtip:treedata[i].qtip,
                                children:[{
                                    text:'loading',
                                    iconCls:'loading',
                                    leaf:true
                                }]
                            });
                            cnode.on('expand',oThis.getChildTreeNode,oThis); 
                        }  
                           
                        if ( oThis.ShowCheckbox )
                        {                                          
                            cnode.attributes.checked = (node.attributes.checked == undefined) ? false : node.attributes.checked;
                            
                            cnode.on("checkchange",oThis.RecursionCheckTreeNode,oThis);
                        }                   
                        node.appendChild(cnode);
                    }
                  if ( node.firstChild != null )
                     node.firstChild.remove();
                },
                failure:function(){alert('连接数据库超时!');}
            });
     }
};

//同时取得所有节点
AssetTreePanel.prototype.NewAllNodeTreePanel = function()
{
    var oThis = this;
    oThis.TreePanel = new Ext.tree.TreePanel({        
        id : oThis.TreePanelID,
        border: false,
        animate:true, 
        lines: true,
        autoScroll:true,
        rootVisible:false,
        root:new Ext.tree.AsyncTreeNode({
            id:oThis.TreeRootID,
            text:oThis.TreeRootText,
            expand:true
        }),
        loader: new Ext.tree.TreeLoader({
            url:oThis.AjaxUrl,
            baseParams:{
                Class:oThis.urlClassName,
                Method:oThis.urlMethod
            }
        })
    });    
};

//重载
AssetTreePanel.prototype.ReloadTree = function()
{
    var oThis = this;
//    oThis.TreePanel.getLoader().url = oThis.AjaxUrl;
    
    try
    {    
    //oThis.TreePanel.getRootNode().reload();
    }
    catch(err){}
};

//递归树
AssetTreePanel.prototype.RecursionCheckTreeNode = function(node)
{
    var oThis = this;
    if ( node.hasChildNodes() )
    {
        node.eachChild(function(child){
            child.getUI().toggleCheck(node.attributes.checked);　
　　　　　　child.attributes.checked = node.attributes.checked;
            child.on("checkchange",oThis.RecursionCheckTreeNode,oThis);
　　　　　　oThis.RecursionCheckTreeNode(child);//处理子节点
        });
    }
};

AssetTreePanel.prototype.SetCheckTreeNode = function(node)
{
    
};

//重新加载parentNode的子节点
AssetTreePanel.prototype.AddChildNode = function(parentNode,id,text,leaf,qtip)
{
    var cNode = null;
    
    cNode = new Ext.tree.TreeNode({  
            id:id,
            text:text,
            leaf:leaf,
            qtip:qtip           
        });
        
    parentNode.appendChild(cNode);
        
    if ( parentNode.isLeaf() )
        parentNode.leaf = false;            
    
    parentNode.expand();   
   
};

//修改当前节点
AssetTreePanel.prototype.EditNode = function(cNode,text,qtip)
{
    cNode.setText(text);
    cNode.attributes.qtip = qtip;    
};

//删除选中的节点
AssetTreePanel.prototype.RemoveNode = function(parentNode,cNode)
{
    parentNode.removeChild(cNode);
};