⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 treeview.js

📁 一个很不错的...ajax树..支持复选框
💻 JS
📖 第 1 页 / 共 5 页
字号:
     * @TODO i changed this method add a param oIndex     */    init: function(oData, oParent, expanded, oIndex) {        this.data       = oData;        this.children   = [];        this.index      = oIndex;        ++YAHOO.widget.TreeView.nodeCount;        this.expanded   = expanded;        /**         * The parentChange event is fired when a parent element is applied         * to the node.  This is useful if you need to apply tree-level         * properties to a tree that need to happen if a node is moved from         * one tree to another.         *         * @event parentChange         * @type CustomEvent         */        this.createEvent("parentChange", this);        // oParent should never be null except when we create the root node.        if (oParent) {            oParent.appendChild(this);        }    },    /**     * Certain properties for the node cannot be set until the parent     * is known. This is called after the node is inserted into a tree.     * the parent is also applied to this node's children in order to     * make it possible to move a branch from one tree to another.     * @method applyParent     * @param {Node} parentNode this node's parent node     * @return {boolean} true if the application was successful     */    applyParent: function(parentNode) {        if (!parentNode) {            return false;        }        this.tree   = parentNode.tree;        this.parent = parentNode;        this.depth  = parentNode.depth + 1;        if (!this.href) {            this.href = "javascript:" + this.getToggleLink();        }        // @todo why was this put here.  This causes new nodes added at the        // root level to lose the menu behavior.        // if (! this.multiExpand) {            // this.multiExpand = parentNode.multiExpand;        // }        this.tree.regNode(this);        parentNode.childrenRendered = false;        // cascade update existing children        for (var i=0, len=this.children.length;i<len;++i) {            this.children[i].applyParent(this);        }        this.fireEvent("parentChange");        return true;    },    /**     * Appends a node to the child collection.     * @method appendChild     * @param childNode {Node} the new node     * @return {Node} the child node     * @private     */    appendChild: function(childNode) {        if (this.hasChildren()) {            var sib = this.children[this.children.length - 1];            sib.nextSibling = childNode;            childNode.previousSibling = sib;        }        this.children[this.children.length] = childNode;        childNode.applyParent(this);        return childNode;    },    /**     * Appends this node to the supplied node's child collection     * @method appendTo     * @param parentNode {Node} the node to append to.     * @return {Node} The appended node     */    appendTo: function(parentNode) {        return parentNode.appendChild(this);    },    /**    * Inserts this node before this supplied node    * @method insertBefore    * @param node {Node} the node to insert this node before    * @return {Node} the inserted node    */    insertBefore: function(node) {        var p = node.parent;        if (p) {            if (this.tree) {                this.tree.popNode(this);            }            var refIndex = node.isChildOf(p);            p.children.splice(refIndex, 0, this);            if (node.previousSibling) {                node.previousSibling.nextSibling = this;            }            this.previousSibling = node.previousSibling;            this.nextSibling = node;            node.previousSibling = this;            this.applyParent(p);        }        return this;    },     /**    * Inserts this node after the supplied node    * @method insertAfter    * @param node {Node} the node to insert after    * @return {Node} the inserted node    */    insertAfter: function(node) {        var p = node.parent;        if (p) {            if (this.tree) {                this.tree.popNode(this);            }            var refIndex = node.isChildOf(p);            if (!node.nextSibling) {                this.nextSibling = null;                return this.appendTo(p);            }            p.children.splice(refIndex + 1, 0, this);            node.nextSibling.previousSibling = this;            this.previousSibling = node;            this.nextSibling = node.nextSibling;            node.nextSibling = this;            this.applyParent(p);        }        return this;    },    /**    * Returns true if the Node is a child of supplied Node    * @method isChildOf    * @param parentNode {Node} the Node to check    * @return {boolean} The node index if this Node is a child of     *                   supplied Node, else -1.    * @private    */    isChildOf: function(parentNode) {        if (parentNode && parentNode.children) {            for (var i=0, len=parentNode.children.length; i<len ; ++i) {                if (parentNode.children[i] === this) {                    return i;                }            }        }        return -1;    },    /**     * Returns a node array of this node's siblings, null if none.     * @method getSiblings     * @return Node[]     */    getSiblings: function() {        return this.parent.children;    },    /**     * Shows this node's children     * @method showChildren     */    showChildren: function() {        if (!this.tree.animateExpand(this.getChildrenEl(), this)) {            if (this.hasChildren()) {                this.getChildrenEl().style.display = "";            }        }    },    /**     * Hides this node's children     * @method hideChildren     */    hideChildren: function() {        if (!this.tree.animateCollapse(this.getChildrenEl(), this)) {            this.getChildrenEl().style.display = "none";        }    },    /**     * Returns the id for this node's container div     * @method getElId     * @return {string} the element id     */    getElId: function() {        return "ygtv" + this.index;    },    /**     * Returns the id for this node's children div     * @method getChildrenElId     * @return {string} the element id for this node's children div     */    getChildrenElId: function() {        return "ygtvc" + this.index;    },    /**     * Returns the id for this node's toggle element     * @method getToggleElId     * @return {string} the toggel element id     */    getToggleElId: function() {        return "ygtvt" + this.index;    },    /*     * Returns the id for this node's spacer image.  The spacer is positioned     * over the toggle and provides feedback for screen readers.     * @method getSpacerId     * @return {string} the id for the spacer image     */    /*    getSpacerId: function() {        return "ygtvspacer" + this.index;    },     */    /**     * Returns this node's container html element     * @method getEl     * @return {HTMLElement} the container html element     */    getEl: function() {        return document.getElementById(this.getElId());    },    /**     * Returns the div that was generated for this node's children     * @method getChildrenEl     * @return {HTMLElement} this node's children div     */    getChildrenEl: function() {        return document.getElementById(this.getChildrenElId());    },    /**     * Returns the element that is being used for this node's toggle.     * @method getToggleEl     * @return {HTMLElement} this node's toggle html element     */    getToggleEl: function() {        return document.getElementById(this.getToggleElId());    },    /*     * Returns the element that is being used for this node's spacer.     * @method getSpacer     * @return {HTMLElement} this node's spacer html element     */    /*    getSpacer: function() {        return document.getElementById( this.getSpacerId() ) || {};    },    */    /*    getStateText: function() {        if (this.isLoading) {            return this.loadingText;        } else if (this.hasChildren(true)) {            if (this.expanded) {                return this.expandedText;            } else {                return this.collapsedText;            }        } else {            return "";        }    },    */    /**     * Generates the link that will invoke this node's toggle method     * @method getToggleLink     * @return {string} the javascript url for toggling this node     */    getToggleLink: function() {        return "YAHOO.widget.TreeView.getNode(\'" + this.tree.id + "\'," +             this.index + ").toggle()";    },    /**     * Hides this nodes children (creating them if necessary), changes the     * @method collapse     * toggle style.     */    collapse: function() {        // Only collapse if currently expanded        if (!this.expanded) { return; }        // fire the collapse event handler        var ret = this.tree.onCollapse(this);        if (false === ret) {            return;        }        ret = this.tree.fireEvent("collapse", this);        if (false === ret) {            return;        }        if (!this.getEl()) {            this.expanded = false;        } else {            // hide the child div            this.hideChildren();            this.expanded = false;            this.updateIcon();        }        // this.getSpacer().title = this.getStateText();        ret = this.tree.fireEvent("collapseComplete", this);    },    /**     * Shows this nodes children (creating them if necessary), changes the     * toggle style, and collapses its siblings if multiExpand is not set.     * @method expand     */    expand: function() {        // Only expand if currently collapsed.        if (this.expanded) { return; }        // fire the expand event handler        var ret = this.tree.onExpand(this);        if (false === ret) {            return;        }                ret = this.tree.fireEvent("expand", this);        if (false === ret) {            return;        }        if (!this.getEl()) {            this.expanded = true;            return;        }        if (! this.childrenRendered) {            this.getChildrenEl().innerHTML = this.renderChildren();        } else {        }        this.expanded = true;        this.updateIcon();        // this.getSpacer().title = this.getStateText();        // We do an extra check for children here because the lazy        // load feature can expose nodes that have no children.        // if (!this.hasChildren()) {        if (this.isLoading) {            this.expanded = false;            return;        }        if (! this.multiExpand) {            var sibs = this.getSiblings();            for (var i=0; i<sibs.length; ++i) {                if (sibs[i] != this && sibs[i].expanded) {                     sibs[i].collapse();                 }            }        }        this.showChildren();        ret = this.tree.fireEvent("expandComplete", this);    },    updateIcon: function() {        if (this.hasIcon) {            var el = this.getToggleEl();            if (el) {                el.className = this.getStyle();            }        }    },    /**     * Returns the css style name for the toggle     * @method getStyle     * @return {string} the css class for this node's toggle     */    getStyle: function() {        if (this.isLoading) {            return "ygtvloading";        } else {            // location top or bottom, middle nodes also get the top style            var loc = (this.nextSibling) ? "t" : "l";            // type p=plus(expand), m=minus(collapase), n=none(no children)            var type = "n";            if (this.hasChildren(true) || (this.isDynamic() && !this.getIconMode())) {            // if (this.hasChildren(true)) {                type = (this.expanded) ? "m" : "p";            }            return "ygtv" + loc + type;        }    },    /**     * Returns the hover style for the icon     * @return {string} the css class hover state

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -