📄 node.js
字号:
/* Copyright (c) 2006 Yahoo! Inc. All rights reserved. */
/**
* The base class for all tree nodes. The node's presentation and behavior in
* response to mouse events is handled in Node subclasses.
*
* @param oData {object} a string or object containing the data that will
* be used to render this node
* @param oParent {Node} this node's parent node
* @param expanded {boolean} the initial expanded/collapsed state
* @constructor
*/
YAHOO.widget.Node = function(oData, oParent, expanded) {
if (oParent) { this.init(oData, oParent, expanded); }
};
YAHOO.widget.Node.prototype = {
/**
* The index for this instance obtained from global counter in YAHOO.widget.TreeView.
*
* @type int
*/
index: 0,
/**
* This node's child node collection.
*
* @type Node[]
*/
children: null,
/**
* Tree instance this node is part of
*
* @type TreeView
*/
tree: null,
/**
* The data linked to this node. This can be any object or primitive
* value, and the data can be used in getNodeHtml().
*
* @type object
*/
data: null,
/**
* Parent node
*
* @type Node
*/
parent: null,
/**
* The depth of this node. We start at -1 for the root node.
*
* @type int
*/
depth: -1,
/**
* The href for the node's label. If one is not specified, the href will
* be set so that it toggles the node.
*
* @type string
*/
href: null,
/**
* The label href target, defaults to current window
*
* @type string
*/
target: "_self",
/**
* The node's expanded/collapsed state
*
* @type boolean
*/
expanded: false,
/**
* Can multiple children be expanded at once?
*
* @type boolean
*/
multiExpand: true,
/**
* Should we render children for a collapsed node? It is possible that the
* implementer will want to render the hidden data... @todo verify that we
* need this, and implement it if we do.
*
* @type boolean
*/
renderHidden: false,
/**
* Flag that is set to true the first time this node's children are rendered.
*
* @type boolean
*/
childrenRendered: false,
/**
* This node's previous sibling
*
* @type Node
*/
previousSibling: null,
/**
* This node's next sibling
*
* @type Node
*/
nextSibling: null,
/**
* We can set the node up to call an external method to get the child
* data dynamically.
*
* @type boolean
* @private
*/
_dynLoad: false,
/**
* Function to execute when we need to get this node's child data.
*
* @type function
*/
dataLoader: null,
/**
* This is true for dynamically loading nodes while waiting for the
* callback to return.
*
* @type boolean
*/
isLoading: false,
/**
* The toggle/branch icon will not show if this is set to false. This
* could be useful if the implementer wants to have the child contain
* extra info about the parent, rather than an actual node.
*
* @type boolean
*/
hasIcon: true,
/**
* Initializes this node, gets some of the properties from the parent
*
* @param oData {object} a string or object containing the data that will
* be used to render this node
* @param oParent {Node} this node's parent node
* @param expanded {boolean} the initial expanded/collapsed state
*/
init: function(oData, oParent, expanded) {
this.data = oData;
this.children = [];
this.index = YAHOO.widget.TreeView.nodeCount;
++YAHOO.widget.TreeView.nodeCount;
this.logger = new ygLogger("Node");
this.expanded = expanded;
// oParent should never be null except when we create the root node.
if (oParent) {
this.tree = oParent.tree;
this.parent = oParent;
this.href = "javascript:" + this.getToggleLink();
this.depth = oParent.depth + 1;
this.multiExpand = oParent.multiExpand;
oParent.appendChild(this);
}
},
/**
* Appends a node to the child collection.
*
* @param node {Node} the new node
* @return {Node} the child node
* @private
*/
appendChild: function(node) {
if (this.hasChildren()) {
var sib = this.children[this.children.length - 1];
sib.nextSibling = node;
node.previousSibling = sib;
}
this.tree.regNode(node);
this.children[this.children.length] = node;
return node;
},
/**
* Returns a node array of this node's siblings, null if none.
*
* @return Node[]
*/
getSiblings: function() {
return this.parent.children;
},
/**
* Shows this node's children
*/
showChildren: function() {
if (!this.tree.animateExpand(this.getChildrenEl())) {
if (this.hasChildren()) {
this.getChildrenEl().style.display = "";
}
}
},
/**
* Hides this node's children
*/
hideChildren: function() {
this.logger.debug("hiding " + this.index);
if (!this.tree.animateCollapse(this.getChildrenEl())) {
this.getChildrenEl().style.display = "none";
}
},
/**
* Returns the id for this node's container div
*
* @return {string} the element id
*/
getElId: function() {
return "ygtv" + this.index;
},
/**
* Returns the id for this node's children div
*
* @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
*
* @return {string} the toggel element id
*/
getToggleElId: function() {
return "ygtvt" + this.index;
},
/**
* Returns this node's container html element
*
* @return {HTMLElement} the container html element
*/
getEl: function() {
return document.getElementById(this.getElId());
},
/**
* Returns the div that was generated for this node's children
*
* @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.
*
* @return {HTMLElement} this node's toggel html element
*/
getToggleEl: function() {
return document.getElementById(this.getToggleElId());
},
/**
* Generates the link that will invoke this node's toggle method
*
* @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
* toggle style.
*/
collapse: function() {
// Only collapse if currently expanded
if (!this.expanded) { return; }
if (!this.getEl()) {
this.expanded = false;
return;
}
// hide the child div
this.hideChildren();
this.expanded = false;
if (this.hasIcon) {
this.getToggleEl().className = this.getStyle();
}
// fire the collapse event handler
this.tree.onCollapse(this);
},
/**
* Shows this nodes children (creating them if necessary), changes the
* toggle style, and collapses its siblings if multiExpand is not set.
*/
expand: function() {
// Only expand if currently collapsed.
if (this.expanded) { return; }
if (!this.getEl()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -