Ext Tree 操作类

var FaceYe = {};
FaceYe.Tree = {
    /**
    *
    *根据URL生成一棵简单的树结构.
    */
    init: function(id, dataUrl, title)
    {
        if (Ext.getCmp(id))
        {
            Ext.getCmp(id).destroy();
        }
        var containerElId = Ext.id();
        Ext.getBody().createChild({
            tag: 'div',
            id: containerElId
        });
        var tree = new Ext.tree.TreePanel({
            id: id,
            el: containerElId,
            autoScroll: false,
            width: 195,
            // title :title,
            animate: false,
            frame: false,
            autoHeight: true,
            border: false,
            header: false,
            enableDD: false,
            containerScroll: false,
            rootVisible: false,
            loader: new Ext.tree.TreeLoader({
                dataUrl: dataUrl
            })
        });

        var root = new Ext.tree.AsyncTreeNode({
            text: '根结点',
            draggable: false,
            id: 'root'
        });
        tree.on('click', function(node, e)
        {
            if (!node.isLeaf())
            {
                node.toggle();
            }
        });
        tree.setRootNode(root);
        return tree;
    },
    /**
    * 取得一个节点的所有子节点 包括本节点
    */
    getAllChildren: function(node)
    {
        var children = [];
        children.push(node);
        if (!node.isLeaf())
        {
            for (var i = 0; i < node.childNodes.length; i++)
            {
                children = children.concat(FaceYe.Tree
						.getAllChildren(node.childNodes[i]));
            }
        }
        return children;
    },
    /**
    * 取得一个节点的所有父节点
    * 
    */
    getAllParents: function(node)
    {
        var parentNodes = [];
        parentNodes.push(node);
        if (node.parentNode)
        {
            parentNodes = parentNodes.concat(FaceYe.Tree
					.getAllParents(node.parentNode));
        }
        return parentNodes;
    },
    /**
    * 取得所有子节点中checked为true的节点(TreeNode) 包括本节点
    */
    getChecked: function(node)
    {
        var checked = [];
        if (node.getUI().isChecked())
        {
            checked.push(node);
        }
        if (!node.isLeaf())
        {
            for (var i = 0; i < node.childNodes.length; i++)
            {
                checked = checked.concat(FaceYe.Tree
						.getChecked(node.childNodes[i]));
            }
        }

        return checked;
    },
    /**
    * 取得所有子节点中checked 为true的节点ID 包括本节点
    */
    getCheckedIds: function(node)
    {
        var checked = [];
        if (node.getUI().isChecked() || node.attributes.checked)
        {
            checked.push(node.id);
        }
        if (!node.isLeaf())
        {
            for (var i = 0; i < node.childNodes.length; i++)
            {
                checked = checked.concat(FaceYe.Tree
						.getCheckedIds(node.childNodes[i]));
            }
        }

        return checked;
    },
    /**
    * 当点击子节点时 将父节点选中
    */
    checkedParentNodesWhenCheckChild: function(node)
    {
        var tree = node.getOwnerTree();
        tree.suspendEvents();
        var allParentNodes = FaceYe.Tree.getAllParents(node);
        if (allParentNodes.length > 1)
        {
            for (var i = 0; i < allParentNodes.length; i++)
            {
                if (allParentNodes[i].id != node.id)
                {
                    if (!allParentNodes[i].getUI().isChecked())
                    {
                        allParentNodes[i].getUI().toggleCheck();
                    }
                }
            }
        }
        tree.resumeEvents();
    },
    /**
    * 当当前子节点的父节点的所有子节点中 不存在checked=true的子节点时,父节点不被选中
    */
    unCheckedParents: function(node)
    {
        if (node.parentNode)
        {
            var parentNode = node.parentNode;
            var allCheckedChildrenNodes = FaceYe.Tree.getChecked(parentNode);
            if (allCheckedChildrenNodes.length === 1)
            {
                if (parentNode.getUI().isChecked())
                {
                    parentNode.getUI().toggleCheck();
                    parentNode.attributes.checked = false;
                }
            }
            if (parentNode.parentNode)
            {
                FaceYe.Tree.unCheckedParents(parentNode);
            }
        }
    },
    /**
    * 当点击父节点时 将其所有子节点选中
    */
    checkChildren: function(node)
    {
        var allChildrenNodes = FaceYe.Tree.getAllChildren(node);
        if (allChildrenNodes.length > 1)
        {
            for (var i = 0; i < allChildrenNodes.length; i++)
            {
                if (!allChildrenNodes[i].getUI().isChecked())
                {
                    allChildrenNodes[i].getUI().toggleCheck();
                }
            }
        }
    },
    fireCheckChange: function(node)
    {
        if (node.getUI().isChecked())
        {
            FaceYe.Tree.checkChildren(node);
            FaceYe.Tree.checkedParentNodesWhenCheckChild(node);
        } else
        {
            // 取得当前节点的所有子节点,包括当前节点
            var allChildrenNodes = FaceYe.Tree.getAllChildren(node);
            // 如果当前节点的所有子节点中,不存在checked=true的节点,那么将当前节点置为checked=false.
            //          
            // 如果当前节点有子节点,同时,当前节点checked=false,那么将其所有子节点置为checked=false
            for (var i = 0; i < allChildrenNodes.length; i++)
            {
                if (allChildrenNodes[i].getUI().isChecked())
                {
                    allChildrenNodes[i].getUI().toggleCheck();
                }
            }
            FaceYe.Tree.unCheckedParents(node);
        }
    }
};

你可能感兴趣的:(tree)