📄 treeview.js
字号:
* label will toggle the expanded/collapsed state of the node. By
* changing the href property of the instance, this behavior can be
* changed so that the label will go to the specified href.
*
* @extends YAHOO.widget.Node
* @constructor
* @param oData {object} a string or object containing the data that will
* be used to render this node
* @param oParent {YAHOO.widget.Node} this node's parent node
* @param expanded {boolean} the initial expanded/collapsed state
*/
YAHOO.widget.TextNode = function(oData, oParent, expanded) {
// this.type = "TextNode";
if (oData) {
this.init(oData, oParent, expanded);
this.setUpLabel(oData);
}
/**
* @private
*/
};
YAHOO.widget.TextNode.prototype = new YAHOO.widget.Node();
/**
* The CSS class for the label href. Defaults to ygtvlabel, but can be
* overridden to provide a custom presentation for a specific node.
*
* @type string
*/
YAHOO.widget.TextNode.prototype.labelStyle = "ygtvlabel";
/**
* The derived element id of the label for this node
*
* @type string
*/
YAHOO.widget.TextNode.prototype.labelElId = null;
/**
* The text for the label. It is assumed that the oData parameter will
* either be a string that will be used as the label, or an object that
* has a property called "label" that we will use.
*
* @type string
*/
YAHOO.widget.TextNode.prototype.label = null;
/**
* Sets up the node label
*
* @param oData string containing the label, or an object with a label property
*/
YAHOO.widget.TextNode.prototype.setUpLabel = function(oData) {
if (typeof oData == "string") {
oData = { label: oData };
}
this.label = oData.label;
// update the link
if (oData.href) {
this.href = oData.href;
}
// set the target
if (oData.target) {
this.target = oData.target;
}
if (oData.style) {
this.labelStyle = oData.style;
}
this.labelElId = "ygtvlabelel" + this.index;
};
/**
* Returns the label element
*
* @return {object} the element
*/
YAHOO.widget.TextNode.prototype.getLabelEl = function() {
return document.getElementById(this.labelElId);
};
// overrides YAHOO.widget.Node
YAHOO.widget.TextNode.prototype.getNodeHtml = function() {
var sb = [];
sb[sb.length] = '<table border="0" cellpadding="0" cellspacing="0">';
sb[sb.length] = '<tr>';
for (i=0;i<this.depth;++i) {
// sb[sb.length] = '<td class="ygtvdepthcell"> </td>';
sb[sb.length] = '<td class="' + this.getDepthStyle(i) + '"> </td>';
}
var getNode = 'YAHOO.widget.TreeView.getNode(\'' +
this.tree.id + '\',' + this.index + ')';
sb[sb.length] = '<td';
// sb[sb.length] = ' onselectstart="return false"';
sb[sb.length] = ' id="' + this.getToggleElId() + '"';
sb[sb.length] = ' class="' + this.getStyle() + '"';
if (this.hasChildren(true)) {
sb[sb.length] = ' onmouseover="this.className=';
sb[sb.length] = getNode + '.getHoverStyle()"';
sb[sb.length] = ' onmouseout="this.className=';
sb[sb.length] = getNode + '.getStyle()"';
}
sb[sb.length] = ' onclick="javascript:' + this.getToggleLink() + '">';
/*
sb[sb.length] = '<img id="' + this.getSpacerId() + '"';
sb[sb.length] = ' alt=""';
sb[sb.length] = ' tabindex=0';
sb[sb.length] = ' src="' + this.spacerPath + '"';
sb[sb.length] = ' title="' + this.getStateText() + '"';
sb[sb.length] = ' class="ygtvspacer"';
// sb[sb.length] = ' onkeypress="return ' + getNode + '".onKeyPress()"';
sb[sb.length] = ' />';
*/
sb[sb.length] = ' ';
sb[sb.length] = '</td>';
sb[sb.length] = '<td>';
sb[sb.length] = '<a';
sb[sb.length] = ' id="' + this.labelElId + '"';
sb[sb.length] = ' class="' + this.labelStyle + '"';
sb[sb.length] = ' href="' + this.href + '"';
sb[sb.length] = ' target="' + this.target + '"';
sb[sb.length] = ' onclick="return ' + getNode + '.onLabelClick(' + getNode +')"';
if (this.hasChildren(true)) {
sb[sb.length] = ' onmouseover="document.getElementById(\'';
sb[sb.length] = this.getToggleElId() + '\').className=';
sb[sb.length] = getNode + '.getHoverStyle()"';
sb[sb.length] = ' onmouseout="document.getElementById(\'';
sb[sb.length] = this.getToggleElId() + '\').className=';
sb[sb.length] = getNode + '.getStyle()"';
}
sb[sb.length] = ' >';
sb[sb.length] = this.label;
sb[sb.length] = '</a>';
sb[sb.length] = '</td>';
sb[sb.length] = '</tr>';
sb[sb.length] = '</table>';
return sb.join("");
};
/**
* Executed when the label is clicked
* @param me {Node} this node
* @scope the anchor tag clicked
* @return false to cancel the anchor click
*/
YAHOO.widget.TextNode.prototype.onLabelClick = function(me) {
//return true;
};
YAHOO.widget.TextNode.prototype.toString = function() {
return "TextNode (" + this.index + ") " + this.label;
};
/**
* A menu-specific implementation that differs from TextNode in that only
* one sibling can be expanded at a time.
* @extends YAHOO.widget.TextNode
* @constructor
*/
YAHOO.widget.MenuNode = function(oData, oParent, expanded) {
if (oData) {
this.init(oData, oParent, expanded);
this.setUpLabel(oData);
}
/**
* Menus usually allow only one branch to be open at a time.
* @type boolean
*/
this.multiExpand = false;
/**
* @private
*/
};
YAHOO.widget.MenuNode.prototype = new YAHOO.widget.TextNode();
YAHOO.widget.MenuNode.prototype.toString = function() {
return "MenuNode (" + this.index + ") " + this.label;
};
/**
* This implementation takes either a string or object for the
* oData argument. If is it a string, we will use it for the display
* of this node (and it can contain any html code). If the parameter
* is an object, we look for a parameter called "html" that will be
* used for this node's display.
*
* @extends YAHOO.widget.Node
* @constructor
* @param oData {object} a string or object containing the data that will
* be used to render this node
* @param oParent {YAHOO.widget.Node} this node's parent node
* @param expanded {boolean} the initial expanded/collapsed state
* @param hasIcon {boolean} specifies whether or not leaf nodes should
* have an icon
*/
YAHOO.widget.HTMLNode = function(oData, oParent, expanded, hasIcon) {
if (oData) {
this.init(oData, oParent, expanded);
this.initContent(oData, hasIcon);
}
};
YAHOO.widget.HTMLNode.prototype = new YAHOO.widget.Node();
/**
* The CSS class for the html content container. Defaults to ygtvhtml, but
* can be overridden to provide a custom presentation for a specific node.
*
* @type string
*/
YAHOO.widget.HTMLNode.prototype.contentStyle = "ygtvhtml";
/**
* The generated id that will contain the data passed in by the implementer.
*
* @type string
*/
YAHOO.widget.HTMLNode.prototype.contentElId = null;
/**
* The HTML content to use for this node's display
*
* @type string
*/
YAHOO.widget.HTMLNode.prototype.content = null;
/**
* Sets up the node label
*
* @param {object} An html string or object containing an html property
* @param {boolean} hasIcon determines if the node will be rendered with an
* icon or not
*/
YAHOO.widget.HTMLNode.prototype.initContent = function(oData, hasIcon) {
if (typeof oData == "string") {
oData = { html: oData };
}
this.html = oData.html;
this.contentElId = "ygtvcontentel" + this.index;
this.hasIcon = hasIcon;
/**
* @private
*/
};
/**
* Returns the outer html element for this node's content
*
* @return {HTMLElement} the element
*/
YAHOO.widget.HTMLNode.prototype.getContentEl = function() {
return document.getElementById(this.contentElId);
};
// overrides YAHOO.widget.Node
YAHOO.widget.HTMLNode.prototype.getNodeHtml = function() {
var sb = [];
sb[sb.length] = '<table border="0" cellpadding="0" cellspacing="0">';
sb[sb.length] = '<tr>';
for (i=0;i<this.depth;++i) {
sb[sb.length] = '<td class="' + this.getDepthStyle(i) + '"> </td>';
}
if (this.hasIcon) {
sb[sb.length] = '<td';
sb[sb.length] = ' id="' + this.getToggleElId() + '"';
sb[sb.length] = ' class="' + this.getStyle() + '"';
sb[sb.length] = ' onclick="javascript:' + this.getToggleLink() + '"';
if (this.hasChildren(true)) {
sb[sb.length] = ' onmouseover="this.className=';
sb[sb.length] = 'YAHOO.widget.TreeView.getNode(\'';
sb[sb.length] = this.tree.id + '\',' + this.index + ').getHoverStyle()"';
sb[sb.length] = ' onmouseout="this.className=';
sb[sb.length] = 'YAHOO.widget.TreeView.getNode(\'';
sb[sb.length] = this.tree.id + '\',' + this.index + ').getStyle()"';
}
sb[sb.length] = '> </td>';
}
sb[sb.length] = '<td';
sb[sb.length] = ' id="' + this.contentElId + '"';
sb[sb.length] = ' class="' + this.contentStyle + '"';
sb[sb.length] = ' >';
sb[sb.length] = this.html;
sb[sb.length] = '</td>';
sb[sb.length] = '</tr>';
sb[sb.length] = '</table>';
return sb.join("");
};
YAHOO.widget.HTMLNode.prototype.toString = function() {
return "HTMLNode (" + this.index + ")";
};
/**
* A static factory class for tree view expand/collapse animations
*
* @constructor
*/
YAHOO.widget.TVAnim = function() {
return {
/**
* Constant for the fade in animation
*
* @type string
*/
FADE_IN: "TVFadeIn",
/**
* Constant for the fade out animation
*
* @type string
*/
FADE_OUT: "TVFadeOut",
/**
* Returns a ygAnim instance of the given type
*
* @param type {string} the type of animation
* @param el {HTMLElement} the element to element (probably the children div)
* @param callback {function} function to invoke when the animation is done.
* @return {YAHOO.util.Animation} the animation instance
*/
getAnim: function(type, el, callback) {
if (YAHOO.widget[type]) {
return new YAHOO.widget[type](el, callback);
} else {
return null;
}
},
/**
* Returns true if the specified animation class is available
*
* @param type {string} the type of animation
* @return {boolean} true if valid, false if not
*/
isValid: function(type) {
return (YAHOO.widget[type]);
}
};
} ();
/**
* A 1/2 second fade-in animation.
*
* @constructor
* @param el {HTMLElement} the element to animate
* @param callback {function} function to invoke when the animation is finished
*/
YAHOO.widget.TVFadeIn = function(el, callback) {
/**
* The element to animate
* @type HTMLElement
*/
this.el = el;
/**
* the callback to invoke when the animation is complete
*
* @type function
*/
this.callback = callback;
/**
* @private
*/
};
/**
* Performs the animation
*/
YAHOO.widget.TVFadeIn.prototype = {
animate: function() {
var tvanim = this;
var s = this.el.style;
s.opacity = 0.1;
s.filter = "alpha(opacity=10)";
s.display = "";
// var dur = ( navigator.userAgent.match(/msie/gi) ) ? 0.05 : 0.4;
var dur = 0.4;
// var a = new ygAnim_Fade(this.el, dur, 1);
// a.setStart(0.1);
// a.onComplete = function() { tvanim.onComplete(); };
// var a = new YAHOO.util.Anim(this.el, 'opacity', 0.1, 1);
var a = new YAHOO.util.Anim(this.el, {opacity: {from: 0.1, to: 1, unit:""}}, dur);
a.onComplete.subscribe( function() { tvanim.onComplete(); } );
a.animate();
},
/**
* Clean up and invoke callback
*/
onComplete: function() {
this.callback();
},
toString: function() {
return "TVFadeIn";
}
};
/**
* A 1/2 second fade out animation.
*
* @constructor
* @param el {HTMLElement} the element to animate
* @param callback {Function} function to invoke when the animation is finished
*/
YAHOO.widget.TVFadeOut = function(el, callback) {
/**
* The element to animate
* @type HTMLElement
*/
this.el = el;
/**
* the callback to invoke when the animation is complete
*
* @type function
*/
this.callback = callback;
/**
* @private
*/
};
/**
* Performs the animation
*/
YAHOO.widget.TVFadeOut.prototype = {
animate: function() {
var tvanim = this;
// var dur = ( navigator.userAgent.match(/msie/gi) ) ? 0.05 : 0.4;
var dur = 0.4;
// var a = new ygAnim_Fade(this.el, dur, 0.1);
// a.onComplete = function() { tvanim.onComplete(); };
// var a = new YAHOO.util.Anim(this.el, 'opacity', 1, 0.1);
var a = new YAHOO.util.Anim(this.el, {opacity: {from: 1, to: 0.1, unit:""}}, dur);
a.onComplete.subscribe( function() { tvanim.onComplete(); } );
a.animate();
},
/**
* Clean up and invoke callback
*/
onComplete: function() {
var s = this.el.style;
s.display = "none";
// s.opacity = 1;
s.filter = "alpha(opacity=100)";
this.callback();
},
toString: function() {
return "TVFadeOut";
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -