📄 treeview.js
字号:
* @param treeId {String} the id of the tree instance
* @param nodeIndex {String} the index of the node to return
* @return {Node} the node instance requested, null if not found
*/
YAHOO.widget.TreeView.getNode = function(treeId, nodeIndex) {
var t = YAHOO.widget.TreeView.getTree(treeId);
return (t) ? t.getNodeByIndex(nodeIndex) : null;
};
/**
* Adds an event. Replace with event manager when available
*
* @param el the elment to bind the handler to
* @param {string} sType the type of event handler
* @param {function} fn the callback to invoke
* @param {boolean} capture if true event is capture phase, bubble otherwise
*/
YAHOO.widget.TreeView.addHandler = function (el, sType, fn, capture) {
capture = (capture) ? true : false;
if (el.addEventListener) {
el.addEventListener(sType, fn, capture);
} else if (el.attachEvent) {
el.attachEvent("on" + sType, fn);
} else {
el["on" + sType] = fn;
}
};
/**
* Attempts to preload the images defined in the styles used to draw the tree by
* rendering off-screen elements that use the styles.
*/
YAHOO.widget.TreeView.preload = function(prefix) {
prefix = prefix || "ygtv";
var styles = ["tn","tm","tmh","tp","tph","ln","lm","lmh","lp","lph","loading"];
var sb = [];
for (var i = 0; i < styles.length; ++i) {
sb[sb.length] = '<span class="' + prefix + styles[i] + '"> </span>';
}
var f = document.createElement("DIV");
var s = f.style;
s.position = "absolute";
s.top = "-1000px";
s.left = "-1000px";
f.innerHTML = sb.join("");
document.body.appendChild(f);
};
YAHOO.widget.TreeView.addHandler(window,
"load", YAHOO.widget.TreeView.preload);
/**
* 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 (oData) { 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,
/**
* This flag is set to true when the html is generated for this node's
* children, and set to false when new children are added.
* @type boolean
*/
childrenRendered: false,
/**
* Dynamically loaded nodes only fetch the data the first time they are
* expanded. This flag is set to true once the data has been fetched.
* @type boolean
*/
dynamicLoadComplete: 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,
/**
* Used to configure what happens when a dynamic load node is expanded
* and we discover that it does not have children. By default, it is
* treated as if it still could have children (plus/minus icon). Set
* iconMode to have it display like a leaf node instead.
* @type int
*/
iconMode: 0,
/**
* The node type
* @private
*/
_type: "Node",
/*
spacerPath: "http://us.i1.yimg.com/us.yimg.com/i/space.gif",
expandedText: "Expanded",
collapsedText: "Collapsed",
loadingText: "Loading",
*/
/**
* 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.expanded = expanded;
// 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.
* @param {Node} parentNode this node's parent node
*/
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();
}
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);
}
return true;
},
/**
* Appends a node to the child collection.
*
* @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
* @param parentNode {Node} the node to append to.
*/
appendTo: function(parentNode) {
return parentNode.appendChild(this);
},
/**
* Inserts this node before this supplied node
*
* @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
*
* @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) {
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
*
* @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.
*
* @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() {
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 the id for this node's spacer image. The spacer is positioned
* over the toggle and provides feedback for screen readers.
* @return {string} the id for the spacer image
*/
/*
getSpacerId: function() {
return "ygtvspacer" + 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 toggle html element
*/
getToggleEl: function() {
return document.getElementById(this.getToggleElId());
},
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -