Index: openacs-4/packages/ajaxhelper/www/resources/yui/treeview/treeview-debug.js =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/ajaxhelper/www/resources/yui/treeview/treeview-debug.js,v diff -u -r1.1 -r1.2 --- openacs-4/packages/ajaxhelper/www/resources/yui/treeview/treeview-debug.js 21 Oct 2006 06:15:00 -0000 1.1 +++ openacs-4/packages/ajaxhelper/www/resources/yui/treeview/treeview-debug.js 25 Dec 2006 16:40:03 -0000 1.2 @@ -1,15 +1,23 @@ -/* +/* Copyright (c) 2006, Yahoo! Inc. All rights reserved. Code licensed under the BSD License: http://developer.yahoo.net/yui/license.txt -version: 0.11.3 -*/ +version: 0.12.1 +*/ +/** + * The treeview widget is a generic tree building tool. + * @module treeview + * @title TreeView Widget + * @requires yahoo + * @optional animation + * @namespace YAHOO.widget + */ /** - * Contains the tree view state data and the root node. This is an - * ordered tree; child nodes will be displayed in the order created, and - * there currently is no way to change this. + * Contains the tree view state data and the root node. * + * @class TreeView + * @uses YAHOO.util.EventProvider * @constructor * @param {string|HTMLElement} id The id of the element, or the element * itself that the tree will be inserted into. @@ -18,78 +26,75 @@ if (id) { this.init(id); } }; -/** - * Count of all nodes in all trees - * @type int - */ -YAHOO.widget.TreeView.nodeCount = 0; + YAHOO.widget.TreeView.prototype = { /** * The id of tree container element - * + * @property id * @type String */ id: null, /** * The host element for this tree + * @property _el * @private */ _el: null, /** * Flat collection of all nodes in this tree - * + * @property _nodes * @type Node[] * @private */ _nodes: null, /** * We lock the tree control while waiting for the dynamic loader to return - * + * @property locked * @type boolean */ locked: false, /** * The animation to use for expanding children, if any - * + * @property _expandAnim * @type string * @private */ _expandAnim: null, /** * The animation to use for collapsing children, if any - * + * @property _collapseAnim * @type string * @private */ _collapseAnim: null, /** * The current number of animations that are executing - * + * @property _animCount * @type int * @private */ _animCount: 0, /** * The maximum number of animations to run at one time. - * + * @property maxAnim * @type int */ maxAnim: 2, /** * Sets up the animation for expanding children - * - * @param {string} the type of animation (acceptable values defined in - * YAHOO.widget.TVAnim) + * @method setExpandAnim + * @param {string} type the type of animation (acceptable values defined + * in YAHOO.widget.TVAnim) */ setExpandAnim: function(type) { if (YAHOO.widget.TVAnim.isValid(type)) { @@ -99,8 +104,8 @@ /** * Sets up the animation for collapsing children - * - * @param {string} the type of animation (acceptable values defined in + * @method setCollapseAnim + * @param {string} the type of animation (acceptable values defined in * YAHOO.widget.TVAnim) */ setCollapseAnim: function(type) { @@ -112,20 +117,25 @@ /** * Perform the expand animation if configured, or just show the * element if not configured or too many animations are in progress - * + * @method animateExpand * @param el {HTMLElement} the element to animate + * @param node {YAHOO.util.Node} the node that was expanded * @return {boolean} true if animation could be invoked, false otherwise */ - animateExpand: function(el) { + animateExpand: function(el, node) { this.logger.log("animating expand"); if (this._expandAnim && this._animCount < this.maxAnim) { // this.locked = true; var tree = this; - var a = YAHOO.widget.TVAnim.getAnim(this._expandAnim, el, - function() { tree.expandComplete(); }); - if (a) { + var a = YAHOO.widget.TVAnim.getAnim(this._expandAnim, el, + function() { tree.expandComplete(node); }); + if (a) { ++this._animCount; + this.fireEvent("animStart", { + "node": node, + "type": "expand" + }); a.animate(); } @@ -138,20 +148,25 @@ /** * Perform the collapse animation if configured, or just show the * element if not configured or too many animations are in progress - * + * @method animateCollapse * @param el {HTMLElement} the element to animate + * @param node {YAHOO.util.Node} the node that was expanded * @return {boolean} true if animation could be invoked, false otherwise */ - animateCollapse: function(el) { + animateCollapse: function(el, node) { this.logger.log("animating collapse"); if (this._collapseAnim && this._animCount < this.maxAnim) { // this.locked = true; var tree = this; - var a = YAHOO.widget.TVAnim.getAnim(this._collapseAnim, el, - function() { tree.collapseComplete(); }); - if (a) { + var a = YAHOO.widget.TVAnim.getAnim(this._collapseAnim, el, + function() { tree.collapseComplete(node); }); + if (a) { ++this._animCount; + this.fireEvent("animStart", { + "node": node, + "type": "collapse" + }); a.animate(); } @@ -163,25 +178,35 @@ /** * Function executed when the expand animation completes + * @method expandComplete */ - expandComplete: function() { + expandComplete: function(node) { this.logger.log("expand complete: " + this.id); --this._animCount; + this.fireEvent("animComplete", { + "node": node, + "type": "expand" + }); // this.locked = false; }, /** * Function executed when the collapse animation completes + * @method collapseComplete */ - collapseComplete: function() { + collapseComplete: function(node) { this.logger.log("collapse complete: " + this.id); --this._animCount; + this.fireEvent("animComplete", { + "node": node, + "type": "collapse" + }); // this.locked = false; }, /** * Initializes the tree - * + * @method init * @parm {string|HTMLElement} id the id of the element that will hold the tree * @private */ @@ -194,6 +219,62 @@ this.id = this.generateId(id); } + /** + * When animation is enabled, this event fires when the animation + * starts + * @event animStart + * @type CustomEvent + * @param {YAHOO.widget.Node} node the node that is expanding/collapsing + * @parm {String} type the type of animation ("expand" or "collapse") + */ + this.createEvent("animStart", this); + + /** + * When animation is enabled, this event fires when the animation + * completes + * @event animComplete + * @type CustomEvent + * @param {YAHOO.widget.Node} node the node that is expanding/collapsing + * @parm {String} type the type of animation ("expand" or "collapse") + */ + this.createEvent("animComplete", this); + + /** + * Fires when a node is going to be collapsed. Return false to stop + * the collapse. + * @event collapse + * @type CustomEvent + * @param {YAHOO.widget.Node} node the node that is collapsing + */ + this.createEvent("collapse", this); + + /** + * Fires after a node is successfully collapsed. This event will not fire + * if the "collapse" event was cancelled. + * @event collapseComplete + * @type CustomEvent + * @param {YAHOO.widget.Node} node the node that was collapsed + */ + this.createEvent("collapseComplete", this); + + /** + * Fires when a node is going to be expanded. Return false to stop + * the collapse. + * @event expand + * @type CustomEvent + * @param {YAHOO.widget.Node} node the node that is expanding + */ + this.createEvent("expand", this); + + /** + * Fires after a node is successfully expanded. This event will not fire + * if the "expand" event was cancelled. + * @event expandComplete + * @type CustomEvent + * @param {YAHOO.widget.Node} node the node that was expanded + */ + this.createEvent("expandComplete", this); + this._nodes = []; // store a global reference @@ -205,10 +286,20 @@ this.logger = new YAHOO.widget.LogWriter(this.toString()); this.logger.log("tree init: " + this.id); + + //YAHOO.util.Event.onContentReady(this.id, this.handleAvailable, this, true); + YAHOO.util.Event.on(this.id, "click", this.handleClick, this, true); }, + + //handleAvailable: function() { + //var Event = YAHOO.util.Event; + //Event.on(this.id, + //}, + /** * Renders the tree boilerplate and visible nodes + * @method draw */ draw: function() { var html = this.root.getHtml(); @@ -218,6 +309,7 @@ /** * Returns the tree's host element + * @method getEl * @return {HTMLElement} the host element */ getEl: function() { @@ -229,7 +321,7 @@ /** * Nodes register themselves with the tree instance when they are created. - * + * @method regNode * @param node {Node} the node to register * @private */ @@ -239,7 +331,7 @@ /** * Returns the root node of this tree - * + * @method getRoot * @return {Node} the root node */ getRoot: function() { @@ -248,14 +340,14 @@ /** * Configures this tree to dynamically load all child data - * + * @method setDynamicLoad * @param {function} fnDataLoader the function that will be called to get the data * @param iconMode {int} configures the icon that is displayed when a dynamic - * load node is expanded the first time without children. By default, the + * load node is expanded the first time without children. By default, the * "collapse" icon will be used. If set to 1, the leaf node icon will be * displayed. */ - setDynamicLoad: function(fnDataLoader, iconMode) { + setDynamicLoad: function(fnDataLoader, iconMode) { this.root.setDynamicLoad(fnDataLoader, iconMode); }, @@ -264,27 +356,29 @@ * node property. If expand all is called in a tree with nodes that * do not allow multiple siblings to be displayed, only the last sibling * will be expanded. + * @method expandAll */ - expandAll: function() { + expandAll: function() { if (!this.locked) { - this.root.expandAll(); + this.root.expandAll(); } }, /** * Collapses all expanded child nodes in the entire tree. + * @method collapseAll */ - collapseAll: function() { + collapseAll: function() { if (!this.locked) { - this.root.collapseAll(); + this.root.collapseAll(); } }, /** * Returns a node in the tree that has the specified index (this index * is created internally, so this function probably will only be used * in html generated for a given node.) - * + * @method getNodeByIndex * @param {int} nodeIndex the index of the node wanted * @return {Node} the node with index=nodeIndex, null if no match */ @@ -296,7 +390,7 @@ /** * Returns a node that has a matching property and value in the data * object that was passed into its constructor. - * + * @method getNodeByProperty * @param {object} property the property to search (usually a string) * @param {object} value the value we want to find (usuall an int or string) * @return {Node} the matching node, null if no match @@ -313,9 +407,9 @@ }, /** - * Returns a collection of nodes that have a matching property - * and value in the data object that was passed into its constructor. - * + * Returns a collection of nodes that have a matching property + * and value in the data object that was passed into its constructor. + * @method getNodesByProperty * @param {object} property the property to search (usually a string) * @param {object} value the value we want to find (usuall an int or string) * @return {Array} the matching collection of nodes, null if no match @@ -333,13 +427,14 @@ }, /** - * Removes the node and its children, and optionally refreshes the + * Removes the node and its children, and optionally refreshes the * branch of the tree that was affected. + * @method removeNode * @param {Node} The node to remove * @param {boolean} autoRefresh automatically refreshes branch if true * @return {boolean} False is there was a problem, true otherwise. */ - removeNode: function(node, autoRefresh) { + removeNode: function(node, autoRefresh) { // Don't delete the root node if (node.isRoot()) { @@ -368,26 +463,30 @@ * the node, and resets the dynamic load flag. The primary use for * this method is to purge a node and allow it to fetch its data * dynamically again. + * @method removeChildren * @param {Node} node the node to purge */ - removeChildren: function(node) { + removeChildren: function(node) { this.logger.log("Removing children for " + node); while (node.children.length) { this._deleteNode(node.children[0]); } node.childrenRendered = false; node.dynamicLoadComplete = false; - // node.collapse(); - node.expand(); - node.collapse(); + if (node.expanded) { + node.collapse(); + } else { + node.updateIcon(); + } }, /** * Deletes the node and recurses children + * @method _deleteNode * @private */ - _deleteNode: function(node) { + _deleteNode: function(node) { // Remove all the child nodes first this.removeChildren(node); @@ -396,12 +495,13 @@ }, /** - * Removes the node from the tree, preserving the child collection - * to make it possible to insert the branch into another part of the + * Removes the node from the tree, preserving the child collection + * to make it possible to insert the branch into another part of the * tree, or another tree. + * @method popNode * @param {Node} the node to remove */ - popNode: function(node) { + popNode: function(node) { var p = node.parent; // Update the parent's collection of children @@ -432,66 +532,88 @@ node.nextSibling = null; node.tree = null; - // Update the tree's node collection + // Update the tree's node collection delete this._nodes[node.index]; }, /** - * toString + * TreeView instance toString + * @method toString * @return {string} string representation of the tree */ toString: function() { return "TreeView " + this.id; }, /** - * private + * Generates an unique id for an element if it doesn't yet have one + * @method generateId + * @private */ generateId: function(el) { var id = el.id; if (!id) { id = "yui-tv-auto-id-" + YAHOO.widget.TreeView.counter; - YAHOO.widget.TreeView.counter++; + ++YAHOO.widget.TreeView.counter; } return id; }, /** * Abstract method that is executed when a node is expanded + * @method onExpand * @param node {Node} the node that was expanded + * @deprecated use treeobj.subscribe("expand") instead */ onExpand: function(node) { }, /** - * Abstract method that is executed when a node is collapsed + * Abstract method that is executed when a node is collapsed. + * @method onCollapse * @param node {Node} the node that was collapsed. + * @deprecated use treeobj.subscribe("collapse") instead */ onCollapse: function(node) { } }; +YAHOO.augment(YAHOO.widget.TreeView, YAHOO.util.EventProvider); + /** + * Count of all nodes in all trees + * @property YAHOO.widget.TreeView.nodeCount + * @type int + * @static + */ +YAHOO.widget.TreeView.nodeCount = 0; + +/** * Global cache of tree instances - * + * @property YAHOO.widget.TreeView.trees * @type Array + * @static * @private */ YAHOO.widget.TreeView.trees = []; /** + * Counter for generating a new unique element id + * @property YAHOO.widget.TreeView.counter + * @static * @private */ YAHOO.widget.TreeView.counter = 0; /** * Global method for getting a tree by its id. Used in the generated * tree html. - * + * @method YAHOO.widget.TreeView.getTree * @param treeId {String} the id of the tree instance * @return {TreeView} the tree instance requested, null if not found. + * @static */ YAHOO.widget.TreeView.getTree = function(treeId) { var t = YAHOO.widget.TreeView.trees[treeId]; @@ -502,66 +624,91 @@ /** * Global method for getting a node by its id. Used in the generated * tree html. - * + * @method YAHOO.widget.TreeView.getNode * @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 + * @static */ 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 - * + * Add a DOM event + * @method YAHOO.widget.TreeView.addHandler * @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 + * @static */ -YAHOO.widget.TreeView.addHandler = function (el, sType, fn, capture) { - capture = (capture) ? true : false; +YAHOO.widget.TreeView.addHandler = function (el, sType, fn) { if (el.addEventListener) { - el.addEventListener(sType, fn, capture); + el.addEventListener(sType, fn, false); } else if (el.attachEvent) { el.attachEvent("on" + sType, fn); - } else { - el["on" + sType] = fn; } }; /** + * Remove a DOM event + * @method YAHOO.widget.TreeView.removeHandler + * @param el the elment to bind the handler to + * @param {string} sType the type of event handler + * @param {function} fn the callback to invoke + * @static + */ + +YAHOO.widget.TreeView.removeHandler = function (el, sType, fn) { + if (el.removeEventListener) { + el.removeEventListener(sType, fn, false); + } else if (el.detachEvent) { + el.detachEvent("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. + * @method YAHOO.widget.TreeView.preload + * @param {string} prefix the prefix to use to generate the names of the + * images to preload, default is ygtv + * @static */ 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) { + + for (var i = 0; i < styles.length; ++i) { sb[sb.length] = ' '; } - var f = document.createElement("DIV"); + 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.removeHandler(window, + "load", YAHOO.widget.TreeView.preload); + }; -YAHOO.widget.TreeView.addHandler(window, +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. - * + * @namespace YAHOO.widget + * @class Node + * @uses YAHOO.util.EventProvider * @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 @@ -576,133 +723,135 @@ /** * The index for this instance obtained from global counter in YAHOO.widget.TreeView. - * + * @property index * @type int */ index: 0, /** * This node's child node collection. - * - * @type Node[] + * @property children + * @type Node[] */ children: null, /** * Tree instance this node is part of - * + * @property tree * @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(). - * + * @property data * @type object */ data: null, /** * Parent node - * + * @property parent * @type Node */ parent: null, /** * The depth of this node. We start at -1 for the root node. - * + * @property depth * @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. - * + * @property href * @type string */ href: null, /** * The label href target, defaults to current window - * + * @property target * @type string */ target: "_self", /** * The node's expanded/collapsed state - * + * @property expanded * @type boolean */ expanded: false, /** * Can multiple children be expanded at once? - * + * @property multiExpand * @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 + * implementer will want to render the hidden data... @todo verify that we * need this, and implement it if we do. - * + * @property renderHidden * @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. + * @property childrenRendered * @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. + * @property dynamicLoadComplete * @type boolean */ dynamicLoadComplete: false, /** * This node's previous sibling - * + * @property previousSibling * @type Node */ previousSibling: null, /** * This node's next sibling - * + * @property nextSibling * @type Node */ nextSibling: null, /** * We can set the node up to call an external method to get the child * data dynamically. - * + * @property _dynLoad * @type boolean * @private */ _dynLoad: false, /** * Function to execute when we need to get this node's child data. - * + * @property dataLoader * @type function */ dataLoader: null, /** * This is true for dynamically loading nodes while waiting for the * callback to return. - * + * @property isLoading * @type boolean */ isLoading: false, @@ -711,7 +860,7 @@ * 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. - * + * @property hasIcon * @type boolean */ hasIcon: true, @@ -721,12 +870,23 @@ * 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. + * @property iconMode * @type int */ iconMode: 0, /** + * Specifies whether or not the content area of the node should be allowed + * to wrap. + * @property nowrap + * @type boolean + * @default true + */ + nowrap: false, + + /** * The node type + * @property _type * @private */ _type: "Node", @@ -740,20 +900,32 @@ /** * Initializes this node, gets some of the properties from the parent - * + * @method init * @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; this.logger = new YAHOO.widget.LogWriter(this.toString()); + /** + * 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); @@ -765,6 +937,7 @@ * 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 */ @@ -777,7 +950,7 @@ this.parent = parentNode; this.depth = parentNode.depth + 1; - if (! this.href) { + if (!this.href) { this.href = "javascript:" + this.getToggleLink(); } @@ -793,12 +966,14 @@ 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 @@ -817,6 +992,7 @@ /** * 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 */ @@ -826,7 +1002,7 @@ /** * Inserts this node before this supplied node - * + * @method insertBefore * @param node {Node} the node to insert this node before * @return {Node} the inserted node */ @@ -840,7 +1016,7 @@ } var refIndex = node.isChildOf(p); - this.logger.log(refIndex); + //this.logger.log(refIndex); p.children.splice(refIndex, 0, this); if (node.previousSibling) { node.previousSibling.nextSibling = this; @@ -854,10 +1030,10 @@ return this; }, - + /** * Inserts this node after the supplied node - * + * @method insertAfter * @param node {Node} the node to insert after * @return {Node} the inserted node */ @@ -892,9 +1068,9 @@ /** * 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 + * @return {boolean} The node index if this Node is a child of * supplied Node, else -1. * @private */ @@ -912,7 +1088,7 @@ /** * Returns a node array of this node's siblings, null if none. - * + * @method getSiblings * @return Node[] */ getSiblings: function() { @@ -921,9 +1097,10 @@ /** * Shows this node's children + * @method showChildren */ showChildren: function() { - if (!this.tree.animateExpand(this.getChildrenEl())) { + if (!this.tree.animateExpand(this.getChildrenEl(), this)) { if (this.hasChildren()) { this.getChildrenEl().style.display = ""; } @@ -932,18 +1109,19 @@ /** * Hides this node's children + * @method hideChildren */ hideChildren: function() { this.logger.log("hiding " + this.index); - if (!this.tree.animateCollapse(this.getChildrenEl())) { + 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() { @@ -952,7 +1130,7 @@ /** * Returns the id for this node's children div - * + * @method getChildrenElId * @return {string} the element id for this node's children div */ getChildrenElId: function() { @@ -961,27 +1139,29 @@ /** * 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() { @@ -990,6 +1170,7 @@ /** * Returns the div that was generated for this node's children + * @method getChildrenEl * @return {HTMLElement} this node's children div */ getChildrenEl: function() { @@ -998,14 +1179,16 @@ /** * 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 */ /* @@ -1032,15 +1215,17 @@ /** * 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 + "\'," + + 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() { @@ -1050,31 +1235,39 @@ // fire the collapse event handler var ret = this.tree.onCollapse(this); - if ("undefined" != typeof ret && !ret) { - this.logger.log("Collapse was stopped by the event handler"); + if (false === ret) { + this.logger.log("Collapse was stopped by the abstract onCollapse"); return; } - if (!this.getEl()) { - this.expanded = false; + ret = this.tree.fireEvent("collapse", this); + + if (false === ret) { + this.logger.log("Collapse was stopped by a custom event handler"); return; } - // hide the child div - this.hideChildren(); - this.expanded = false; - if (this.hasIcon) { - this.getToggleEl().className = this.getStyle(); + 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. @@ -1083,11 +1276,18 @@ // fire the expand event handler var ret = this.tree.onExpand(this); - if ("undefined" != typeof ret && !ret) { - this.logger.log("Expand was stopped by the event handler"); + if (false === ret) { + this.logger.log("Expand was stopped by the abstract onExpand"); return; } + + ret = this.tree.fireEvent("expand", this); + if (false === ret) { + this.logger.log("Expand was stopped by the custom event handler"); + return; + } + if (!this.getEl()) { this.expanded = true; return; @@ -1101,10 +1301,9 @@ } this.expanded = true; - if (this.hasIcon) { - this.getToggleEl().className = this.getStyle(); - } + this.updateIcon(); + // this.getSpacer().title = this.getStateText(); // We do an extra check for children here because the lazy @@ -1119,18 +1318,29 @@ if (! this.multiExpand) { var sibs = this.getSiblings(); for (var i=0; i 0 || + hasChildren: function(checkForLazyLoad) { + return ( this.children.length > 0 || (checkForLazyLoad && this.isDynamic() && !this.dynamicLoadComplete) ); }, /** * Expands if node is collapsed, collapses otherwise. + * @method toggle */ toggle: function() { if (!this.tree.locked && ( this.hasChildren(true) || this.isDynamic()) ) { @@ -1275,10 +1495,13 @@ /** * Returns the markup for this node and its children. - * + * @method getHtml * @return {string} the markup for this node and its expanded children. */ getHtml: function() { + + this.childrenRendered = false; + var sb = []; sb[sb.length] = '
'; sb[sb.length] = this.getNodeHtml(); @@ -1291,7 +1514,7 @@ * Called when first rendering the tree. We always build the div that will * contain this nodes children, but we don't render the children themselves * unless this node is expanded. - * + * @method getChildrenHtml * @return {string} the children container div html and any expanded children * @private */ @@ -1319,7 +1542,7 @@ /** * Generates the markup for the child nodes. This is not done until the node * is expanded. - * + * @method renderChildren * @return {string} the html for this node's children * @private */ @@ -1336,22 +1559,22 @@ if (this.dataLoader) { this.logger.log("Using dynamic loader defined for this node"); - setTimeout( + setTimeout( function() { - node.dataLoader(node, - function() { - node.loadComplete(); + node.dataLoader(node, + function() { + node.loadComplete(); }); }, 10); - + } else if (this.tree.root.dataLoader) { this.logger.log("Using the tree-level dynamic loader"); - setTimeout( + setTimeout( function() { - node.tree.root.dataLoader(node, - function() { - node.loadComplete(); + node.tree.root.dataLoader(node, + function() { + node.loadComplete(); }); }, 10); @@ -1369,17 +1592,18 @@ /** * Called when we know we have all the child data. + * @method completeRender * @return {string} children html */ completeRender: function() { this.logger.log("completeRender: " + this.index + ", # of children: " + this.children.length); var sb = []; for (var i=0; i < this.children.length; ++i) { - this.children[i].childrenRendered = false; + // this.children[i].childrenRendered = false; sb[sb.length] = this.children[i].getHtml(); } - + this.childrenRendered = true; return sb.join(""); @@ -1388,6 +1612,7 @@ /** * Load complete is the callback function we pass to the data provider * in dynamic load situations. + * @method loadComplete */ loadComplete: function() { this.logger.log("loadComplete: " + this.index); @@ -1400,7 +1625,7 @@ /** * Returns this node's ancestor at the specified depth. - * + * @method getAncestor * @param {int} depth the depth of the ancestor. * @return {Node} the ancestor */ @@ -1411,7 +1636,7 @@ } var p = this.parent; - + while (p.depth > depth) { p = p.parent; } @@ -1424,29 +1649,30 @@ * this node. If this node's ancestor at the specified depth * has a next sibling the presentation is different than if it * does not have a next sibling - * + * @method getDepthStyle * @param {int} depth the depth of the ancestor. * @return {string} the css class for the spacer */ getDepthStyle: function(depth) { - return (this.getAncestor(depth).nextSibling) ? + return (this.getAncestor(depth).nextSibling) ? "ygtvdepthcell" : "ygtvblankdepthcell"; }, /** * Get the markup for the node. This is designed to be overrided so that we can * support different types of nodes. - * + * @method getNodeHtml * @return {string} The HTML that will render this node. */ - getNodeHtml: function() { + getNodeHtml: function() { this.logger.log("Generating html"); - return ""; + return ""; }, /** * Regenerates the html for this node and its children. To be used when the * node is expanded and new children have been added. + * @method refresh */ refresh: function() { // this.loadComplete(); @@ -1461,7 +1687,8 @@ }, /** - * toString + * Node toString + * @method toString * @return {string} string representation of the node */ toString: function() { @@ -1470,50 +1697,55 @@ }; +YAHOO.augment(YAHOO.widget.Node, YAHOO.util.EventProvider); + /** - * A custom YAHOO.widget.Node that handles the unique nature of + * A custom YAHOO.widget.Node that handles the unique nature of * the virtual, presentationless root node. - * + * @namespace YAHOO.widget + * @class RootNode * @extends YAHOO.widget.Node + * @param oTree {YAHOO.widget.TreeView} The tree instance this node belongs to * @constructor */ YAHOO.widget.RootNode = function(oTree) { // Initialize the node with null params. The root node is a // special case where the node has no presentation. So we have // to alter the standard properties a bit. this.init(null, null, true); - - /** + + /* * For the root node, we get the tree reference from as a param * to the constructor instead of from the parent element. - * - * @type TreeView */ this.tree = oTree; }; -YAHOO.widget.RootNode.prototype = new YAHOO.widget.Node(); +YAHOO.extend(YAHOO.widget.RootNode, YAHOO.widget.Node, { + + // overrides YAHOO.widget.Node + getNodeHtml: function() { + return ""; + }, -// overrides YAHOO.widget.Node -YAHOO.widget.RootNode.prototype.getNodeHtml = function() { - return ""; -}; + toString: function() { + return "RootNode"; + }, -YAHOO.widget.RootNode.prototype.toString = function() { - return "RootNode"; -}; + loadComplete: function() { + this.tree.draw(); + } -YAHOO.widget.RootNode.prototype.loadComplete = function() { - this.tree.draw(); -}; +}); /** * The default node presentation. The first parameter should be * either a string that will be used as the node's label, or an object * that has a string propery called label. By default, the clicking the * 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. - * + * @namespace YAHOO.widget + * @class TextNode * @extends YAHOO.widget.Node * @constructor * @param oData {object} a string or object containing the data that will @@ -1522,203 +1754,236 @@ * @param expanded {boolean} the initial expanded/collapsed state */ YAHOO.widget.TextNode = function(oData, oParent, expanded) { - // this.type = "TextNode"; - if (oData) { + if (oData) { this.init(oData, oParent, expanded); this.setUpLabel(oData); } - /** - * @private - */ this.logger = new YAHOO.widget.LogWriter(this.toString()); }; -YAHOO.widget.TextNode.prototype = new YAHOO.widget.Node(); +YAHOO.extend(YAHOO.widget.TextNode, 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. + * @property labelStyle + * @type string + */ + labelStyle: "ygtvlabel", -/** - * 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 + * @property labelElId + * @type string + */ + labelElId: null, -/** - * 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. + * @property label + * @type string + */ + label: 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; + textNodeParentChange: function() { + + /** + * Custom event that is fired when the text node label is clicked. The + * custom event is defined on the tree instance, so there is a single + * event that handles all nodes in the tree. The node clicked is + * provided as an argument + * + * @event labelClick + * @for YAHOO.widget.TreeView + * @param {YAHOO.widget.Node} node the node clicked + */ + if (this.tree && !this.tree.hasEvent("labelClick")) { + this.tree.createEvent("labelClick", this.tree); + } + + }, -/** - * 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; + /** + * Sets up the node label + * @method setUpLabel + * @param oData string containing the label, or an object with a label property + */ + setUpLabel: function(oData) { + + // set up the custom event on the tree + this.textNodeParentChange(); + this.subscribe("parentChange", this.textNodeParentChange); - // update the link - if (oData.href) { - this.href = oData.href; - } + 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; - } + // set the target + if (oData.target) { + this.target = oData.target; + } - if (oData.style) { - this.labelStyle = oData.style; - } + if (oData.style) { + this.labelStyle = oData.style; + } - this.labelElId = "ygtvlabelel" + this.index; -}; + this.labelElId = "ygtvlabelel" + this.index; + }, -/** - * Returns the label element - * - * @return {object} the element - */ -YAHOO.widget.TextNode.prototype.getLabelEl = function() { - return document.getElementById(this.labelElId); -}; + /** + * Returns the label element + * @for YAHOO.widget.TextNode + * @method getLabelEl + * @return {object} the element + */ + getLabelEl: function() { + return document.getElementById(this.labelElId); + }, -// overrides YAHOO.widget.Node -YAHOO.widget.TextNode.prototype.getNodeHtml = function() { - this.logger.log("Generating html"); - var sb = []; + // overrides YAHOO.widget.Node + getNodeHtml: function() { + this.logger.log("Generating html"); + var sb = []; - sb[sb.length] = ''; - sb[sb.length] = ''; + sb[sb.length] = '
'; + sb[sb.length] = ''; + + for (var i=0;i '; + //sb[sb.length] = ''; + sb[sb.length] = ''; + } - for (i=0;i '; - } + var getNode = 'YAHOO.widget.TreeView.getNode(\'' + + this.tree.id + '\',' + this.index + ')'; - var getNode = 'YAHOO.widget.TreeView.getNode(\'' + - this.tree.id + '\',' + this.index + ')'; + sb[sb.length] = ''; - sb[sb.length] = ''; + sb[sb.length] = '
'; - /* - sb[sb.length] = ' '; - } + // overrides YAHOO.widget.Node + getNodeHtml: function() { + this.logger.log("Generating html"); + var sb = []; - if (this.hasIcon) { - sb[sb.length] = ' '; + sb[sb.length] = '
'; } - sb[sb.length] = '> '; - } - sb[sb.length] = '