2 Copyright (c) 2011, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 var Dom = YAHOO.util.Dom,
9 Event = YAHOO.util.Event,
11 Widget = YAHOO.widget;
16 * The treeview widget is a generic tree building tool.
18 * @title TreeView Widget
19 * @requires yahoo, dom, event
20 * @optional animation, json, calendar
21 * @namespace YAHOO.widget
25 * Contains the tree view state data and the root node.
28 * @uses YAHOO.util.EventProvider
30 * @param {string|HTMLElement} id The id of the element, or the element itself that the tree will be inserted into.
31 * Existing markup in this element, if valid, will be used to build the tree
32 * @param {Array|Object|String} oConfig (optional) If present, it will be used to build the tree via method <a href="#method_buildTreeFromObject">buildTreeFromObject</a>
35 YAHOO.widget.TreeView = function(id, oConfig) {
36 if (id) { this.init(id); }
38 this.buildTreeFromObject(oConfig);
39 } else if (Lang.trim(this._el.innerHTML)) {
40 this.buildTreeFromMarkup(id);
44 var TV = Widget.TreeView;
49 * The id of tree container element
56 * The host element for this tree
64 * Flat collection of all nodes in this tree. This is a sparse
65 * array, so the length property can't be relied upon for a
66 * node count for the tree.
74 * We lock the tree control while waiting for the dynamic loader to return
81 * The animation to use for expanding children, if any
82 * @property _expandAnim
89 * The animation to use for collapsing children, if any
90 * @property _collapseAnim
97 * The current number of animations that are executing
98 * @property _animCount
105 * The maximum number of animations to run at one time.
112 * Whether there is any subscriber to dblClickEvent
113 * @property _hasDblClickSubscriber
117 _hasDblClickSubscriber: false,
120 * Stores the timer used to check for double clicks
121 * @property _dblClickTimer
122 * @type window.timer object
125 _dblClickTimer: null,
128 * A reference to the Node currently having the focus or null if none.
129 * @property currentFocus
130 * @type YAHOO.widget.Node
135 * If true, only one Node can be highlighted at a time
136 * @property singleNodeHighlight
141 singleNodeHighlight: false,
144 * A reference to the Node that is currently highlighted.
145 * It is only meaningful if singleNodeHighlight is enabled
146 * @property _currentlyHighlighted
147 * @type YAHOO.widget.Node
152 _currentlyHighlighted: null,
155 * Sets up the animation for expanding children
156 * @method setExpandAnim
157 * @param {string} type the type of animation (acceptable values defined
158 * in YAHOO.widget.TVAnim)
160 setExpandAnim: function(type) {
161 this._expandAnim = (Widget.TVAnim.isValid(type)) ? type : null;
165 * Sets up the animation for collapsing children
166 * @method setCollapseAnim
167 * @param {string} type of animation (acceptable values defined in
168 * YAHOO.widget.TVAnim)
170 setCollapseAnim: function(type) {
171 this._collapseAnim = (Widget.TVAnim.isValid(type)) ? type : null;
175 * Perform the expand animation if configured, or just show the
176 * element if not configured or too many animations are in progress
177 * @method animateExpand
178 * @param el {HTMLElement} the element to animate
179 * @param node {YAHOO.util.Node} the node that was expanded
180 * @return {boolean} true if animation could be invoked, false otherwise
182 animateExpand: function(el, node) {
184 if (this._expandAnim && this._animCount < this.maxAnim) {
185 // this.locked = true;
187 var a = Widget.TVAnim.getAnim(this._expandAnim, el,
188 function() { tree.expandComplete(node); });
191 this.fireEvent("animStart", {
205 * Perform the collapse animation if configured, or just show the
206 * element if not configured or too many animations are in progress
207 * @method animateCollapse
208 * @param el {HTMLElement} the element to animate
209 * @param node {YAHOO.util.Node} the node that was expanded
210 * @return {boolean} true if animation could be invoked, false otherwise
212 animateCollapse: function(el, node) {
214 if (this._collapseAnim && this._animCount < this.maxAnim) {
215 // this.locked = true;
217 var a = Widget.TVAnim.getAnim(this._collapseAnim, el,
218 function() { tree.collapseComplete(node); });
221 this.fireEvent("animStart", {
235 * Function executed when the expand animation completes
236 * @method expandComplete
238 expandComplete: function(node) {
240 this.fireEvent("animComplete", {
244 // this.locked = false;
248 * Function executed when the collapse animation completes
249 * @method collapseComplete
251 collapseComplete: function(node) {
253 this.fireEvent("animComplete", {
257 // this.locked = false;
261 * Initializes the tree
263 * @parm {string|HTMLElement} id the id of the element that will hold the tree
267 this._el = Dom.get(id);
268 this.id = Dom.generateId(this._el,"yui-tv-auto-id-");
271 * When animation is enabled, this event fires when the animation
275 * @param {YAHOO.widget.Node} oArgs.node the node that is expanding/collapsing
276 * @param {String} oArgs.type the type of animation ("expand" or "collapse")
278 this.createEvent("animStart", this);
281 * When animation is enabled, this event fires when the animation
283 * @event animComplete
285 * @param {YAHOO.widget.Node} oArgs.node the node that is expanding/collapsing
286 * @param {String} oArgs.type the type of animation ("expand" or "collapse")
288 this.createEvent("animComplete", this);
291 * Fires when a node is going to be collapsed. Return false to stop
295 * @param {YAHOO.widget.Node} node the node that is collapsing
297 this.createEvent("collapse", this);
300 * Fires after a node is successfully collapsed. This event will not fire
301 * if the "collapse" event was cancelled.
302 * @event collapseComplete
304 * @param {YAHOO.widget.Node} node the node that was collapsed
306 this.createEvent("collapseComplete", this);
309 * Fires when a node is going to be expanded. Return false to stop
313 * @param {YAHOO.widget.Node} node the node that is expanding
315 this.createEvent("expand", this);
318 * Fires after a node is successfully expanded. This event will not fire
319 * if the "expand" event was cancelled.
320 * @event expandComplete
322 * @param {YAHOO.widget.Node} node the node that was expanded
324 this.createEvent("expandComplete", this);
327 * Fires when the Enter key is pressed on a node that has the focus
328 * @event enterKeyPressed
330 * @param {YAHOO.widget.Node} node the node that has the focus
332 this.createEvent("enterKeyPressed", this);
335 * Fires when the label in a TextNode or MenuNode or content in an HTMLNode receives a Click.
336 * The listener may return false to cancel toggling and focusing on the node.
339 * @param oArgs.event {HTMLEvent} The event object
340 * @param oArgs.node {YAHOO.widget.Node} node the node that was clicked
342 this.createEvent("clickEvent", this);
345 * Fires when the focus receives the focus, when it changes from a Node
346 * to another Node or when it is completely lost (blurred)
347 * @event focusChanged
349 * @param oArgs.oldNode {YAHOO.widget.Node} Node that had the focus or null if none
350 * @param oArgs.newNode {YAHOO.widget.Node} Node that receives the focus or null if none
353 this.createEvent('focusChanged',this);
356 * Fires when the label in a TextNode or MenuNode or content in an HTMLNode receives a double Click
357 * @event dblClickEvent
359 * @param oArgs.event {HTMLEvent} The event object
360 * @param oArgs.node {YAHOO.widget.Node} node the node that was clicked
363 this.createEvent("dblClickEvent", {
365 onSubscribeCallback: function() {
366 self._hasDblClickSubscriber = true;
371 * Custom event that is fired when the text node label is clicked.
372 * The node clicked is provided as an argument
376 * @param {YAHOO.widget.Node} node the node clicked
377 * @deprecated use clickEvent or dblClickEvent
379 this.createEvent("labelClick", this);
382 * Custom event fired when the highlight of a node changes.
383 * The node that triggered the change is provided as an argument:
384 * The status of the highlight can be checked in
385 * <a href="YAHOO.widget.Node.html#property_highlightState">nodeRef.highlightState</a>.
386 * Depending on <a href="YAHOO.widget.Node.html#property_propagateHighlight">nodeRef.propagateHighlight</a>, other nodes might have changed
387 * @event highlightEvent
389 * @param node {YAHOO.widget.Node} the node that started the change in highlighting state
391 this.createEvent("highlightEvent",this);
396 // store a global reference
397 TV.trees[this.id] = this;
399 // Set up the root node
400 this.root = new Widget.RootNode(this);
402 var LW = Widget.LogWriter;
406 if (this._initEditor) {
410 // YAHOO.util.Event.onContentReady(this.id, this.handleAvailable, this, true);
411 // YAHOO.util.Event.on(this.id, "click", this.handleClick, this, true);
414 //handleAvailable: function() {
415 //var Event = YAHOO.util.Event;
419 * Builds the TreeView from an object.
420 * This is the method called by the constructor to build the tree when it has a second argument.
421 * A tree can be described by an array of objects, each object corresponding to a node.
422 * Node descriptions may contain values for any property of a node plus the following extra properties: <ul>
423 * <li>type: can be one of the following:<ul>
424 * <li> A shortname for a node type (<code>'text','menu','html'</code>) </li>
425 * <li>The name of a Node class under YAHOO.widget (<code>'TextNode', 'MenuNode', 'DateNode'</code>, etc) </li>
426 * <li>a reference to an actual class: <code>YAHOO.widget.DateNode</code></li>
428 * <li>children: an array containing further node definitions</li></ul>
429 * A string instead of an object will produce a node of type 'text' with the given string as its label.
430 * @method buildTreeFromObject
431 * @param oConfig {Array|Object|String} array containing a full description of the tree.
432 * An object or a string will be turned into an array with the given object or string as its only element.
435 buildTreeFromObject: function (oConfig) {
436 var build = function (parent, oConfig) {
437 var i, item, node, children, type, NodeType, ThisType;
438 for (i = 0; i < oConfig.length; i++) {
440 if (Lang.isString(item)) {
441 node = new Widget.TextNode(item, parent);
442 } else if (Lang.isObject(item)) {
443 children = item.children;
444 delete item.children;
445 type = item.type || 'text';
447 switch (Lang.isString(type) && type.toLowerCase()) {
449 node = new Widget.TextNode(item, parent);
452 node = new Widget.MenuNode(item, parent);
455 node = new Widget.HTMLNode(item, parent);
458 if (Lang.isString(type)) {
459 NodeType = Widget[type];
463 if (Lang.isObject(NodeType)) {
464 for (ThisType = NodeType; ThisType && ThisType !== Widget.Node; ThisType = ThisType.superclass.constructor) {}
466 node = new NodeType(item, parent);
473 build(node,children);
479 if (!Lang.isArray(oConfig)) {
484 build(this.root,oConfig);
487 * Builds the TreeView from existing markup. Markup should consist of <UL> or <OL> elements containing <LI> elements.
488 * Each <LI> can have one element used as label and a second optional element which is to be a <UL> or <OL>
489 * containing nested nodes.
490 * Depending on what the first element of the <LI> element is, the following Nodes will be created: <ul>
491 * <li>plain text: a regular TextNode</li>
492 * <li>anchor <A>: a TextNode with its <code>href</code> and <code>target</code> taken from the anchor</li>
493 * <li>anything else: an HTMLNode</li></ul>
494 * Only the first outermost (un-)ordered list in the markup and its children will be parsed.
495 * Nodes will be collapsed unless an <LI> tag has a className called 'expanded'.
496 * All other className attributes will be copied over to the Node className property.
497 * If the <LI> element contains an attribute called <code>yuiConfig</code>, its contents should be a JSON-encoded object
498 * as the one used in method <a href="#method_buildTreeFromObject">buildTreeFromObject</a>.
499 * @method buildTreeFromMarkup
500 * @param id {string|HTMLElement} The id of the element that contains the markup or a reference to it.
502 buildTreeFromMarkup: function (id) {
503 var build = function (markup) {
504 var el, child, branch = [], config = {}, label, yuiConfig;
505 // Dom's getFirstChild and getNextSibling skip over text elements
506 for (el = Dom.getFirstChild(markup); el; el = Dom.getNextSibling(el)) {
507 switch (el.tagName.toUpperCase()) {
511 expanded: Dom.hasClass(el,'expanded'),
512 title: el.title || el.alt || null,
513 className: Lang.trim(el.className.replace(/\bexpanded\b/,'')) || null
515 // I cannot skip over text elements here because I want them for labels
516 child = el.firstChild;
517 if (child.nodeType == 3) {
518 // nodes with only whitespace, tabs and new lines don't count, they are probably just formatting.
519 label = Lang.trim(child.nodeValue.replace(/[\n\t\r]*/g,''));
521 config.type = 'text';
522 config.label = label;
524 child = Dom.getNextSibling(child);
528 if (child.tagName.toUpperCase() == 'A') {
529 config.type = 'text';
530 config.label = child.innerHTML;
531 config.href = child.href;
532 config.target = child.target;
533 config.title = child.title || child.alt || config.title;
535 config.type = 'html';
536 var d = document.createElement('div');
537 d.appendChild(child.cloneNode(true));
538 config.html = d.innerHTML;
539 config.hasIcon = true;
542 // see if after the label it has a further list which will become children of this node.
543 child = Dom.getNextSibling(child);
544 switch (child && child.tagName.toUpperCase()) {
547 config.children = build(child);
550 // if there are further elements or text, it will be ignored.
552 if (YAHOO.lang.JSON) {
553 yuiConfig = el.getAttribute('yuiConfig');
555 yuiConfig = YAHOO.lang.JSON.parse(yuiConfig);
556 config = YAHOO.lang.merge(config,yuiConfig);
567 children: build(child)
576 var markup = Dom.getChildrenBy(Dom.get(id),function (el) {
577 var tag = el.tagName.toUpperCase();
578 return tag == 'UL' || tag == 'OL';
581 this.buildTreeFromObject(build(markup[0]));
586 * Returns the TD element where the event has occurred
587 * @method _getEventTargetTdEl
590 _getEventTargetTdEl: function (ev) {
591 var target = Event.getTarget(ev);
592 // go up looking for a TD with a className with a ygtv prefix
593 while (target && !(target.tagName.toUpperCase() == 'TD' && Dom.hasClass(target.parentNode,'ygtvrow'))) {
594 target = Dom.getAncestorByTagName(target,'td');
596 if (Lang.isNull(target)) { return null; }
597 // If it is a spacer cell, do nothing
598 if (/\bygtv(blank)?depthcell/.test(target.className)) { return null;}
599 // If it has an id, search for the node number and see if it belongs to a node in this tree.
601 var m = target.id.match(/\bygtv([^\d]*)(.*)/);
602 if (m && m[2] && this._nodes[m[2]]) {
609 * Event listener for click events
610 * @method _onClickEvent
613 _onClickEvent: function (ev) {
615 td = this._getEventTargetTdEl(ev),
618 toggle = function (force) {
620 if (force || !node.href) {
623 Event.preventDefault(ev);
626 // For some reason IE8 is providing an event object with
627 // most of the fields missing, but only when clicking on
628 // the node's label, and only when working with inline
629 // editing. This generates a "Member not found" error
630 // in that browser. Determine if this is a browser
631 // bug, or a problem with this code. Already checked to
632 // see if the problem has to do with access the event
633 // in the outer scope, and that isn't the problem.
634 // Maybe the markup for inline editing is broken.
643 node = this.getNodeByElement(td);
648 // exception to handle deprecated event labelClick
649 // @TODO take another look at this deprecation. It is common for people to
650 // only be interested in the label click, so why make them have to test
651 // the node type to figure out whether the click was on the label?
652 target = Event.getTarget(ev);
653 if (Dom.hasClass(target, node.labelStyle) || Dom.getAncestorByClassName(target,node.labelStyle)) {
654 this.fireEvent('labelClick',node);
656 // http://yuilibrary.com/projects/yui2/ticket/2528946
657 // Ensures that any open editor is closed.
658 // Since the editor is in a separate source which might not be included,
659 // we first need to ensure we have the _closeEditor method available
660 if (this._closeEditor) { this._closeEditor(false); }
662 // If it is a toggle cell, toggle
663 if (/\bygtv[tl][mp]h?h?/.test(td.className)) {
666 if (this._dblClickTimer) {
667 window.clearTimeout(this._dblClickTimer);
668 this._dblClickTimer = null;
670 if (this._hasDblClickSubscriber) {
671 this._dblClickTimer = window.setTimeout(function () {
672 self._dblClickTimer = null;
673 if (self.fireEvent('clickEvent', {event:ev,node:node}) !== false) {
678 if (self.fireEvent('clickEvent', {event:ev,node:node}) !== false) {
687 * Event listener for double-click events
688 * @method _onDblClickEvent
691 _onDblClickEvent: function (ev) {
692 if (!this._hasDblClickSubscriber) { return; }
693 var td = this._getEventTargetTdEl(ev);
696 if (!(/\bygtv[tl][mp]h?h?/.test(td.className))) {
697 this.fireEvent('dblClickEvent', {event:ev, node:this.getNodeByElement(td)});
698 if (this._dblClickTimer) {
699 window.clearTimeout(this._dblClickTimer);
700 this._dblClickTimer = null;
705 * Event listener for mouse over events
706 * @method _onMouseOverEvent
709 _onMouseOverEvent:function (ev) {
711 if ((target = this._getEventTargetTdEl(ev)) && (target = this.getNodeByElement(target)) && (target = target.getToggleEl())) {
712 target.className = target.className.replace(/\bygtv([lt])([mp])\b/gi,'ygtv$1$2h');
716 * Event listener for mouse out events
717 * @method _onMouseOutEvent
720 _onMouseOutEvent: function (ev) {
722 if ((target = this._getEventTargetTdEl(ev)) && (target = this.getNodeByElement(target)) && (target = target.getToggleEl())) {
723 target.className = target.className.replace(/\bygtv([lt])([mp])h\b/gi,'ygtv$1$2');
727 * Event listener for key down events
728 * @method _onKeyDownEvent
731 _onKeyDownEvent: function (ev) {
732 var target = Event.getTarget(ev),
733 node = this.getNodeByElement(target),
735 KEY = YAHOO.util.KeyListener.KEY;
740 if (newNode.previousSibling) {
741 newNode = newNode.previousSibling;
743 newNode = newNode.parent;
745 } while (newNode && !newNode._canHaveFocus());
746 if (newNode) { newNode.focus(); }
747 Event.preventDefault(ev);
751 if (newNode.nextSibling) {
752 newNode = newNode.nextSibling;
755 newNode = (newNode.children.length || null) && newNode.children[0];
757 } while (newNode && !newNode._canHaveFocus);
758 if (newNode) { newNode.focus();}
759 Event.preventDefault(ev);
763 if (newNode.parent) {
764 newNode = newNode.parent;
766 newNode = newNode.previousSibling;
768 } while (newNode && !newNode._canHaveFocus());
769 if (newNode) { newNode.focus();}
770 Event.preventDefault(ev);
775 focusOnExpand = function (newNode) {
776 self.unsubscribe('expandComplete',focusOnExpand);
777 moveFocusRight(newNode);
779 moveFocusRight = function (newNode) {
781 if (newNode.isDynamic() && !newNode.childrenRendered) {
782 self.subscribe('expandComplete',focusOnExpand);
788 if (newNode.children.length) {
789 newNode = newNode.children[0];
791 newNode = newNode.nextSibling;
794 } while (newNode && !newNode._canHaveFocus());
795 if (newNode) { newNode.focus();}
798 moveFocusRight(newNode);
799 Event.preventDefault(ev);
804 window.open(node.href,node.target);
806 window.location(node.href);
811 this.fireEvent('enterKeyPressed',node);
812 Event.preventDefault(ev);
815 newNode = this.getRoot();
816 if (newNode.children.length) {newNode = newNode.children[0];}
817 if (newNode._canHaveFocus()) { newNode.focus(); }
818 Event.preventDefault(ev);
821 newNode = newNode.parent.children;
822 newNode = newNode[newNode.length -1];
823 if (newNode._canHaveFocus()) { newNode.focus(); }
824 Event.preventDefault(ev);
828 // case KEY.PAGE_DOWN:
830 case 107: // plus key
831 case 187: // plus key
833 node.parent.expandAll();
838 case 109: // minus key
839 case 189: // minus key
841 node.parent.collapseAll();
851 * Renders the tree boilerplate and visible nodes
855 var html = this.root.getHtml(),
858 if (!this._hasEvents) {
859 Event.on(el, 'click', this._onClickEvent, this, true);
860 Event.on(el, 'dblclick', this._onDblClickEvent, this, true);
861 Event.on(el, 'mouseover', this._onMouseOverEvent, this, true);
862 Event.on(el, 'mouseout', this._onMouseOutEvent, this, true);
863 Event.on(el, 'keydown', this._onKeyDownEvent, this, true);
865 this._hasEvents = true;
869 * Returns the tree's host element
871 * @return {HTMLElement} the host element
875 this._el = Dom.get(this.id);
881 * Nodes register themselves with the tree instance when they are created.
883 * @param node {Node} the node to register
886 regNode: function(node) {
887 this._nodes[node.index] = node;
891 * Returns the root node of this tree
893 * @return {Node} the root node
895 getRoot: function() {
900 * Configures this tree to dynamically load all child data
901 * @method setDynamicLoad
902 * @param {function} fnDataLoader the function that will be called to get the data
903 * @param iconMode {int} configures the icon that is displayed when a dynamic
904 * load node is expanded the first time without children. By default, the
905 * "collapse" icon will be used. If set to 1, the leaf node icon will be
908 setDynamicLoad: function(fnDataLoader, iconMode) {
909 this.root.setDynamicLoad(fnDataLoader, iconMode);
913 * Expands all child nodes. Note: this conflicts with the "multiExpand"
914 * node property. If expand all is called in a tree with nodes that
915 * do not allow multiple siblings to be displayed, only the last sibling
919 expandAll: function() {
921 this.root.expandAll();
926 * Collapses all expanded child nodes in the entire tree.
927 * @method collapseAll
929 collapseAll: function() {
931 this.root.collapseAll();
936 * Returns a node in the tree that has the specified index (this index
937 * is created internally, so this function probably will only be used
938 * in html generated for a given node.)
939 * @method getNodeByIndex
940 * @param {int} nodeIndex the index of the node wanted
941 * @return {Node} the node with index=nodeIndex, null if no match
943 getNodeByIndex: function(nodeIndex) {
944 var n = this._nodes[nodeIndex];
945 return (n) ? n : null;
949 * Returns a node that has a matching property and value in the data
950 * object that was passed into its constructor.
951 * @method getNodeByProperty
952 * @param {object} property the property to search (usually a string)
953 * @param {object} value the value we want to find (usuall an int or string)
954 * @return {Node} the matching node, null if no match
956 getNodeByProperty: function(property, value) {
957 for (var i in this._nodes) {
958 if (this._nodes.hasOwnProperty(i)) {
959 var n = this._nodes[i];
960 if ((property in n && n[property] == value) || (n.data && value == n.data[property])) {
970 * Returns a collection of nodes that have a matching property
971 * and value in the data object that was passed into its constructor.
972 * @method getNodesByProperty
973 * @param {object} property the property to search (usually a string)
974 * @param {object} value the value we want to find (usuall an int or string)
975 * @return {Array} the matching collection of nodes, null if no match
977 getNodesByProperty: function(property, value) {
979 for (var i in this._nodes) {
980 if (this._nodes.hasOwnProperty(i)) {
981 var n = this._nodes[i];
982 if ((property in n && n[property] == value) || (n.data && value == n.data[property])) {
988 return (values.length) ? values : null;
993 * Returns a collection of nodes that have passed the test function
994 * passed as its only argument.
995 * The function will receive a reference to each node to be tested.
997 * @param {function} a boolean function that receives a Node instance and returns true to add the node to the results list
998 * @return {Array} the matching collection of nodes, null if no match
1000 getNodesBy: function(fn) {
1002 for (var i in this._nodes) {
1003 if (this._nodes.hasOwnProperty(i)) {
1004 var n = this._nodes[i];
1010 return (values.length) ? values : null;
1013 * Returns the treeview node reference for an ancestor element
1014 * of the node, or null if it is not contained within any node
1016 * @method getNodeByElement
1017 * @param el {HTMLElement} the element to test
1018 * @return {YAHOO.widget.Node} a node reference or null
1020 getNodeByElement: function(el) {
1022 var p=el, m, re=/ygtv([^\d]*)(.*)/;
1029 return this.getNodeByIndex(m[2]);
1035 if (!p || !p.tagName) {
1040 while (p.id !== this.id && p.tagName.toLowerCase() !== "body");
1046 * When in singleNodeHighlight it returns the node highlighted
1047 * or null if none. Returns null if singleNodeHighlight is false.
1048 * @method getHighlightedNode
1049 * @return {YAHOO.widget.Node} a node reference or null
1051 getHighlightedNode: function() {
1052 return this._currentlyHighlighted;
1057 * Removes the node and its children, and optionally refreshes the
1058 * branch of the tree that was affected.
1059 * @method removeNode
1060 * @param {Node} node to remove
1061 * @param {boolean} autoRefresh automatically refreshes branch if true
1062 * @return {boolean} False is there was a problem, true otherwise.
1064 removeNode: function(node, autoRefresh) {
1066 // Don't delete the root node
1067 if (node.isRoot()) {
1071 // Get the branch that we may need to refresh
1072 var p = node.parent;
1077 // Delete the node and its children
1078 this._deleteNode(node);
1080 // Refresh the parent of the parent
1081 if (autoRefresh && p && p.childrenRendered) {
1089 * wait until the animation is complete before deleting
1090 * to avoid javascript errors
1091 * @method _removeChildren_animComplete
1092 * @param o the custom event payload
1095 _removeChildren_animComplete: function(o) {
1096 this.unsubscribe(this._removeChildren_animComplete);
1097 this.removeChildren(o.node);
1101 * Deletes this nodes child collection, recursively. Also collapses
1102 * the node, and resets the dynamic load flag. The primary use for
1103 * this method is to purge a node and allow it to fetch its data
1104 * dynamically again.
1105 * @method removeChildren
1106 * @param {Node} node the node to purge
1108 removeChildren: function(node) {
1110 if (node.expanded) {
1111 // wait until the animation is complete before deleting to
1112 // avoid javascript errors
1113 if (this._collapseAnim) {
1114 this.subscribe("animComplete",
1115 this._removeChildren_animComplete, this, true);
1116 Widget.Node.prototype.collapse.call(node);
1123 while (node.children.length) {
1124 this._deleteNode(node.children[0]);
1127 if (node.isRoot()) {
1128 Widget.Node.prototype.expand.call(node);
1131 node.childrenRendered = false;
1132 node.dynamicLoadComplete = false;
1138 * Deletes the node and recurses children
1139 * @method _deleteNode
1142 _deleteNode: function(node) {
1143 // Remove all the child nodes first
1144 this.removeChildren(node);
1146 // Remove the node from the tree
1151 * Removes the node from the tree, preserving the child collection
1152 * to make it possible to insert the branch into another part of the
1153 * tree, or another tree.
1155 * @param {Node} node to remove
1157 popNode: function(node) {
1158 var p = node.parent;
1160 // Update the parent's collection of children
1163 for (var i=0, len=p.children.length;i<len;++i) {
1164 if (p.children[i] != node) {
1165 a[a.length] = p.children[i];
1171 // reset the childrenRendered flag for the parent
1172 p.childrenRendered = false;
1174 // Update the sibling relationship
1175 if (node.previousSibling) {
1176 node.previousSibling.nextSibling = node.nextSibling;
1179 if (node.nextSibling) {
1180 node.nextSibling.previousSibling = node.previousSibling;
1183 if (this.currentFocus == node) {
1184 this.currentFocus = null;
1186 if (this._currentlyHighlighted == node) {
1187 this._currentlyHighlighted = null;
1191 node.previousSibling = null;
1192 node.nextSibling = null;
1195 // Update the tree's node collection
1196 delete this._nodes[node.index];
1200 * Nulls out the entire TreeView instance and related objects, removes attached
1201 * event listeners, and clears out DOM elements inside the container. After
1202 * calling this method, the instance reference should be expliclitly nulled by
1203 * implementer, as in myDataTable = null. Use with caution!
1207 destroy : function() {
1208 // Since the label editor can be separated from the main TreeView control
1209 // the destroy method for it might not be there.
1210 if (this._destroyEditor) { this._destroyEditor(); }
1211 var el = this.getEl();
1212 Event.removeListener(el,'click');
1213 Event.removeListener(el,'dblclick');
1214 Event.removeListener(el,'mouseover');
1215 Event.removeListener(el,'mouseout');
1216 Event.removeListener(el,'keydown');
1217 for (var i = 0 ; i < this._nodes.length; i++) {
1218 var node = this._nodes[i];
1219 if (node && node.destroy) {node.destroy(); }
1222 this._hasEvents = false;
1229 * TreeView instance toString
1231 * @return {string} string representation of the tree
1233 toString: function() {
1234 return "TreeView " + this.id;
1238 * Count of nodes in tree
1239 * @method getNodeCount
1240 * @return {int} number of nodes in the tree
1242 getNodeCount: function() {
1243 return this.getRoot().getNodeCount();
1247 * Returns an object which could be used to rebuild the tree.
1248 * It can be passed to the tree constructor to reproduce the same tree.
1249 * It will return false if any node loads dynamically, regardless of whether it is loaded or not.
1250 * @method getTreeDefinition
1251 * @return {Object | false} definition of the tree or false if any node is defined as dynamic
1253 getTreeDefinition: function() {
1254 return this.getRoot().getNodeDefinition();
1258 * Abstract method that is executed when a node is expanded
1260 * @param node {Node} the node that was expanded
1261 * @deprecated use treeobj.subscribe("expand") instead
1263 onExpand: function(node) { },
1266 * Abstract method that is executed when a node is collapsed.
1267 * @method onCollapse
1268 * @param node {Node} the node that was collapsed.
1269 * @deprecated use treeobj.subscribe("collapse") instead
1271 onCollapse: function(node) { },
1274 * Sets the value of a property for all loaded nodes in the tree.
1275 * @method setNodesProperty
1276 * @param name {string} Name of the property to be set
1277 * @param value {any} value to be set
1278 * @param refresh {boolean} if present and true, it does a refresh
1280 setNodesProperty: function(name, value, refresh) {
1281 this.root.setNodesProperty(name,value);
1283 this.root.refresh();
1287 * Event listener to toggle node highlight.
1288 * Can be assigned as listener to clickEvent, dblClickEvent and enterKeyPressed.
1289 * It returns false to prevent the default action.
1290 * @method onEventToggleHighlight
1291 * @param oArgs {any} it takes the arguments of any of the events mentioned above
1292 * @return {false} Always cancels the default action for the event
1294 onEventToggleHighlight: function (oArgs) {
1296 if ('node' in oArgs && oArgs.node instanceof Widget.Node) {
1298 } else if (oArgs instanceof Widget.Node) {
1303 node.toggleHighlight();
1310 /* Backwards compatibility aliases */
1311 var PROT = TV.prototype;
1313 * Renders the tree boilerplate and visible nodes.
1316 * @deprecated Use render instead
1318 PROT.draw = PROT.render;
1320 /* end backwards compatibility aliases */
1322 YAHOO.augment(TV, YAHOO.util.EventProvider);
1325 * Running count of all nodes created in all trees. This is
1326 * used to provide unique identifies for all nodes. Deleting
1327 * nodes does not change the nodeCount.
1328 * @property YAHOO.widget.TreeView.nodeCount
1335 * Global cache of tree instances
1336 * @property YAHOO.widget.TreeView.trees
1344 * Global method for getting a tree by its id. Used in the generated
1346 * @method YAHOO.widget.TreeView.getTree
1347 * @param treeId {String} the id of the tree instance
1348 * @return {TreeView} the tree instance requested, null if not found.
1351 TV.getTree = function(treeId) {
1352 var t = TV.trees[treeId];
1353 return (t) ? t : null;
1358 * Global method for getting a node by its id. Used in the generated
1360 * @method YAHOO.widget.TreeView.getNode
1361 * @param treeId {String} the id of the tree instance
1362 * @param nodeIndex {String} the index of the node to return
1363 * @return {Node} the node instance requested, null if not found
1366 TV.getNode = function(treeId, nodeIndex) {
1367 var t = TV.getTree(treeId);
1368 return (t) ? t.getNodeByIndex(nodeIndex) : null;
1373 * Class name assigned to elements that have the focus
1375 * @property TreeView.FOCUS_CLASS_NAME
1379 * @default "ygtvfocus"
1382 TV.FOCUS_CLASS_NAME = 'ygtvfocus';
1388 var Dom = YAHOO.util.Dom,
1390 Event = YAHOO.util.Event;
1392 * The base class for all tree nodes. The node's presentation and behavior in
1393 * response to mouse events is handled in Node subclasses.
1394 * @namespace YAHOO.widget
1396 * @uses YAHOO.util.EventProvider
1397 * @param oData {object} a string or object containing the data that will
1398 * be used to render this node, and any custom attributes that should be
1399 * stored with the node (which is available in noderef.data).
1400 * All values in oData will be used to set equally named properties in the node
1401 * as long as the node does have such properties, they are not undefined, private or functions,
1402 * the rest of the values will be stored in noderef.data
1403 * @param oParent {Node} this node's parent node
1404 * @param expanded {boolean} the initial expanded/collapsed state (deprecated, use oData.expanded)
1407 YAHOO.widget.Node = function(oData, oParent, expanded) {
1408 if (oData) { this.init(oData, oParent, expanded); }
1411 YAHOO.widget.Node.prototype = {
1414 * The index for this instance obtained from global counter in YAHOO.widget.TreeView.
1421 * This node's child node collection.
1422 * @property children
1428 * Tree instance this node is part of
1435 * The data linked to this node. This can be any object or primitive
1436 * value, and the data can be used in getNodeHtml().
1450 * The depth of this node. We start at -1 for the root node.
1457 * The node's expanded/collapsed state
1458 * @property expanded
1464 * Can multiple children be expanded at once?
1465 * @property multiExpand
1471 * Should we render children for a collapsed node? It is possible that the
1472 * implementer will want to render the hidden data... @todo verify that we
1473 * need this, and implement it if we do.
1474 * @property renderHidden
1477 renderHidden: false,
1480 * This flag is set to true when the html is generated for this node's
1481 * children, and set to false when new children are added.
1482 * @property childrenRendered
1485 childrenRendered: false,
1488 * Dynamically loaded nodes only fetch the data the first time they are
1489 * expanded. This flag is set to true once the data has been fetched.
1490 * @property dynamicLoadComplete
1493 dynamicLoadComplete: false,
1496 * This node's previous sibling
1497 * @property previousSibling
1500 previousSibling: null,
1503 * This node's next sibling
1504 * @property nextSibling
1510 * We can set the node up to call an external method to get the child
1512 * @property _dynLoad
1519 * Function to execute when we need to get this node's child data.
1520 * @property dataLoader
1526 * This is true for dynamically loading nodes while waiting for the
1527 * callback to return.
1528 * @property isLoading
1534 * The toggle/branch icon will not show if this is set to false. This
1535 * could be useful if the implementer wants to have the child contain
1536 * extra info about the parent, rather than an actual node.
1543 * Used to configure what happens when a dynamic load node is expanded
1544 * and we discover that it does not have children. By default, it is
1545 * treated as if it still could have children (plus/minus icon). Set
1546 * iconMode to have it display like a leaf node instead.
1547 * @property iconMode
1553 * Specifies whether or not the content area of the node should be allowed
1562 * If true, the node will alway be rendered as a leaf node. This can be
1563 * used to override the presentation when dynamically loading the entire
1564 * tree. Setting this to true also disables the dynamic load call for the
1573 * The CSS class for the html content container. Defaults to ygtvhtml, but
1574 * can be overridden to provide a custom presentation for a specific node.
1575 * @property contentStyle
1582 * The generated id that will contain the data passed in by the implementer.
1583 * @property contentElId
1589 * Enables node highlighting. If true, the node can be highlighted and/or propagate highlighting
1590 * @property enableHighlight
1594 enableHighlight: true,
1597 * Stores the highlight state. Can be any of:
1599 * <li>0 - not highlighted</li>
1600 * <li>1 - highlighted</li>
1601 * <li>2 - some children highlighted</li>
1603 * @property highlightState
1611 * Tells whether highlighting will be propagated up to the parents of the clicked node
1612 * @property propagateHighlightUp
1617 propagateHighlightUp: false,
1620 * Tells whether highlighting will be propagated down to the children of the clicked node
1621 * @property propagateHighlightDown
1626 propagateHighlightDown: false,
1629 * User-defined className to be added to the Node
1630 * @property className
1647 spacerPath: "http://l.yimg.com/a/i/space.gif",
1648 expandedText: "Expanded",
1649 collapsedText: "Collapsed",
1650 loadingText: "Loading",
1654 * Initializes this node, gets some of the properties from the parent
1656 * @param oData {object} a string or object containing the data that will
1657 * be used to render this node
1658 * @param oParent {Node} this node's parent node
1659 * @param expanded {boolean} the initial expanded/collapsed state
1661 init: function(oData, oParent, expanded) {
1665 this.index = YAHOO.widget.TreeView.nodeCount;
1666 ++YAHOO.widget.TreeView.nodeCount;
1667 this.contentElId = "ygtvcontentel" + this.index;
1669 if (Lang.isObject(oData)) {
1670 for (var property in oData) {
1671 if (oData.hasOwnProperty(property)) {
1672 if (property.charAt(0) != '_' && !Lang.isUndefined(this[property]) && !Lang.isFunction(this[property]) ) {
1673 this[property] = oData[property];
1675 this.data[property] = oData[property];
1680 if (!Lang.isUndefined(expanded) ) { this.expanded = expanded; }
1684 * The parentChange event is fired when a parent element is applied
1685 * to the node. This is useful if you need to apply tree-level
1686 * properties to a tree that need to happen if a node is moved from
1687 * one tree to another.
1689 * @event parentChange
1692 this.createEvent("parentChange", this);
1694 // oParent should never be null except when we create the root node.
1696 oParent.appendChild(this);
1701 * Certain properties for the node cannot be set until the parent
1702 * is known. This is called after the node is inserted into a tree.
1703 * the parent is also applied to this node's children in order to
1704 * make it possible to move a branch from one tree to another.
1705 * @method applyParent
1706 * @param {Node} parentNode this node's parent node
1707 * @return {boolean} true if the application was successful
1709 applyParent: function(parentNode) {
1714 this.tree = parentNode.tree;
1715 this.parent = parentNode;
1716 this.depth = parentNode.depth + 1;
1718 // @todo why was this put here. This causes new nodes added at the
1719 // root level to lose the menu behavior.
1720 // if (! this.multiExpand) {
1721 // this.multiExpand = parentNode.multiExpand;
1724 this.tree.regNode(this);
1725 parentNode.childrenRendered = false;
1727 // cascade update existing children
1728 for (var i=0, len=this.children.length;i<len;++i) {
1729 this.children[i].applyParent(this);
1732 this.fireEvent("parentChange");
1738 * Appends a node to the child collection.
1739 * @method appendChild
1740 * @param childNode {Node} the new node
1741 * @return {Node} the child node
1744 appendChild: function(childNode) {
1745 if (this.hasChildren()) {
1746 var sib = this.children[this.children.length - 1];
1747 sib.nextSibling = childNode;
1748 childNode.previousSibling = sib;
1750 this.children[this.children.length] = childNode;
1751 childNode.applyParent(this);
1753 // part of the IE display issue workaround. If child nodes
1754 // are added after the initial render, and the node was
1755 // instantiated with expanded = true, we need to show the
1756 // children div now that the node has a child.
1757 if (this.childrenRendered && this.expanded) {
1758 this.getChildrenEl().style.display = "";
1765 * Appends this node to the supplied node's child collection
1767 * @param parentNode {Node} the node to append to.
1768 * @return {Node} The appended node
1770 appendTo: function(parentNode) {
1771 return parentNode.appendChild(this);
1775 * Inserts this node before this supplied node
1776 * @method insertBefore
1777 * @param node {Node} the node to insert this node before
1778 * @return {Node} the inserted node
1780 insertBefore: function(node) {
1781 var p = node.parent;
1785 this.tree.popNode(this);
1788 var refIndex = node.isChildOf(p);
1789 p.children.splice(refIndex, 0, this);
1790 if (node.previousSibling) {
1791 node.previousSibling.nextSibling = this;
1793 this.previousSibling = node.previousSibling;
1794 this.nextSibling = node;
1795 node.previousSibling = this;
1797 this.applyParent(p);
1804 * Inserts this node after the supplied node
1805 * @method insertAfter
1806 * @param node {Node} the node to insert after
1807 * @return {Node} the inserted node
1809 insertAfter: function(node) {
1810 var p = node.parent;
1814 this.tree.popNode(this);
1817 var refIndex = node.isChildOf(p);
1819 if (!node.nextSibling) {
1820 this.nextSibling = null;
1821 return this.appendTo(p);
1824 p.children.splice(refIndex + 1, 0, this);
1826 node.nextSibling.previousSibling = this;
1827 this.previousSibling = node;
1828 this.nextSibling = node.nextSibling;
1829 node.nextSibling = this;
1831 this.applyParent(p);
1838 * Returns true if the Node is a child of supplied Node
1840 * @param parentNode {Node} the Node to check
1841 * @return {boolean} The node index if this Node is a child of
1842 * supplied Node, else -1.
1845 isChildOf: function(parentNode) {
1846 if (parentNode && parentNode.children) {
1847 for (var i=0, len=parentNode.children.length; i<len ; ++i) {
1848 if (parentNode.children[i] === this) {
1858 * Returns a node array of this node's siblings, null if none.
1859 * @method getSiblings
1862 getSiblings: function() {
1863 var sib = this.parent.children.slice(0);
1864 for (var i=0;i < sib.length && sib[i] != this;i++) {}
1866 if (sib.length) { return sib; }
1871 * Shows this node's children
1872 * @method showChildren
1874 showChildren: function() {
1875 if (!this.tree.animateExpand(this.getChildrenEl(), this)) {
1876 if (this.hasChildren()) {
1877 this.getChildrenEl().style.display = "";
1883 * Hides this node's children
1884 * @method hideChildren
1886 hideChildren: function() {
1888 if (!this.tree.animateCollapse(this.getChildrenEl(), this)) {
1889 this.getChildrenEl().style.display = "none";
1894 * Returns the id for this node's container div
1896 * @return {string} the element id
1898 getElId: function() {
1899 return "ygtv" + this.index;
1903 * Returns the id for this node's children div
1904 * @method getChildrenElId
1905 * @return {string} the element id for this node's children div
1907 getChildrenElId: function() {
1908 return "ygtvc" + this.index;
1912 * Returns the id for this node's toggle element
1913 * @method getToggleElId
1914 * @return {string} the toggel element id
1916 getToggleElId: function() {
1917 return "ygtvt" + this.index;
1922 * Returns the id for this node's spacer image. The spacer is positioned
1923 * over the toggle and provides feedback for screen readers.
1924 * @method getSpacerId
1925 * @return {string} the id for the spacer image
1928 getSpacerId: function() {
1929 return "ygtvspacer" + this.index;
1934 * Returns this node's container html element
1936 * @return {HTMLElement} the container html element
1939 return Dom.get(this.getElId());
1943 * Returns the div that was generated for this node's children
1944 * @method getChildrenEl
1945 * @return {HTMLElement} this node's children div
1947 getChildrenEl: function() {
1948 return Dom.get(this.getChildrenElId());
1952 * Returns the element that is being used for this node's toggle.
1953 * @method getToggleEl
1954 * @return {HTMLElement} this node's toggle html element
1956 getToggleEl: function() {
1957 return Dom.get(this.getToggleElId());
1960 * Returns the outer html element for this node's content
1961 * @method getContentEl
1962 * @return {HTMLElement} the element
1964 getContentEl: function() {
1965 return Dom.get(this.contentElId);
1970 * Returns the element that is being used for this node's spacer.
1972 * @return {HTMLElement} this node's spacer html element
1975 getSpacer: function() {
1976 return document.getElementById( this.getSpacerId() ) || {};
1981 getStateText: function() {
1982 if (this.isLoading) {
1983 return this.loadingText;
1984 } else if (this.hasChildren(true)) {
1985 if (this.expanded) {
1986 return this.expandedText;
1988 return this.collapsedText;
1997 * Hides this nodes children (creating them if necessary), changes the toggle style.
2000 collapse: function() {
2001 // Only collapse if currently expanded
2002 if (!this.expanded) { return; }
2004 // fire the collapse event handler
2005 var ret = this.tree.onCollapse(this);
2007 if (false === ret) {
2011 ret = this.tree.fireEvent("collapse", this);
2013 if (false === ret) {
2018 if (!this.getEl()) {
2019 this.expanded = false;
2021 // hide the child div
2022 this.hideChildren();
2023 this.expanded = false;
2028 // this.getSpacer().title = this.getStateText();
2030 ret = this.tree.fireEvent("collapseComplete", this);
2035 * Shows this nodes children (creating them if necessary), changes the
2036 * toggle style, and collapses its siblings if multiExpand is not set.
2039 expand: function(lazySource) {
2040 // Only expand if currently collapsed.
2041 if (this.isLoading || (this.expanded && !lazySource)) {
2047 // When returning from the lazy load handler, expand is called again
2048 // in order to render the new children. The "expand" event already
2049 // fired before fething the new data, so we need to skip it now.
2051 // fire the expand event handler
2052 ret = this.tree.onExpand(this);
2054 if (false === ret) {
2058 ret = this.tree.fireEvent("expand", this);
2061 if (false === ret) {
2065 if (!this.getEl()) {
2066 this.expanded = true;
2070 if (!this.childrenRendered) {
2071 this.getChildrenEl().innerHTML = this.renderChildren();
2075 this.expanded = true;
2079 // this.getSpacer().title = this.getStateText();
2081 // We do an extra check for children here because the lazy
2082 // load feature can expose nodes that have no children.
2084 // if (!this.hasChildren()) {
2085 if (this.isLoading) {
2086 this.expanded = false;
2090 if (! this.multiExpand) {
2091 var sibs = this.getSiblings();
2092 for (var i=0; sibs && i<sibs.length; ++i) {
2093 if (sibs[i] != this && sibs[i].expanded) {
2099 this.showChildren();
2101 ret = this.tree.fireEvent("expandComplete", this);
2104 updateIcon: function() {
2106 var el = this.getToggleEl();
2108 el.className = el.className.replace(/\bygtv(([tl][pmn]h?)|(loading))\b/gi,this.getStyle());
2111 el = Dom.get('ygtvtableel' + this.index);
2113 if (this.expanded) {
2114 Dom.replaceClass(el,'ygtv-collapsed','ygtv-expanded');
2116 Dom.replaceClass(el,'ygtv-expanded','ygtv-collapsed');
2122 * Returns the css style name for the toggle
2124 * @return {string} the css class for this node's toggle
2126 getStyle: function() {
2127 if (this.isLoading) {
2128 return "ygtvloading";
2130 // location top or bottom, middle nodes also get the top style
2131 var loc = (this.nextSibling) ? "t" : "l";
2133 // type p=plus(expand), m=minus(collapase), n=none(no children)
2135 if (this.hasChildren(true) || (this.isDynamic() && !this.getIconMode())) {
2136 // if (this.hasChildren(true)) {
2137 type = (this.expanded) ? "m" : "p";
2140 return "ygtv" + loc + type;
2145 * Returns the hover style for the icon
2146 * @return {string} the css class hover state
2147 * @method getHoverStyle
2149 getHoverStyle: function() {
2150 var s = this.getStyle();
2151 if (this.hasChildren(true) && !this.isLoading) {
2158 * Recursively expands all of this node's children.
2161 expandAll: function() {
2162 var l = this.children.length;
2163 for (var i=0;i<l;++i) {
2164 var c = this.children[i];
2165 if (c.isDynamic()) {
2167 } else if (! c.multiExpand) {
2177 * Recursively collapses all of this node's children.
2178 * @method collapseAll
2180 collapseAll: function() {
2181 for (var i=0;i<this.children.length;++i) {
2182 this.children[i].collapse();
2183 this.children[i].collapseAll();
2188 * Configures this node for dynamically obtaining the child data
2189 * when the node is first expanded. Calling it without the callback
2190 * will turn off dynamic load for the node.
2191 * @method setDynamicLoad
2192 * @param fmDataLoader {function} the function that will be used to get the data.
2193 * @param iconMode {int} configures the icon that is displayed when a dynamic
2194 * load node is expanded the first time without children. By default, the
2195 * "collapse" icon will be used. If set to 1, the leaf node icon will be
2198 setDynamicLoad: function(fnDataLoader, iconMode) {
2200 this.dataLoader = fnDataLoader;
2201 this._dynLoad = true;
2203 this.dataLoader = null;
2204 this._dynLoad = false;
2208 this.iconMode = iconMode;
2213 * Evaluates if this node is the root node of the tree
2215 * @return {boolean} true if this is the root node
2217 isRoot: function() {
2218 return (this == this.tree.root);
2222 * Evaluates if this node's children should be loaded dynamically. Looks for
2223 * the property both in this instance and the root node. If the tree is
2224 * defined to load all children dynamically, the data callback function is
2225 * defined in the root node
2227 * @return {boolean} true if this node's children are to be loaded dynamically
2229 isDynamic: function() {
2233 return (!this.isRoot() && (this._dynLoad || this.tree.root._dynLoad));
2239 * Returns the current icon mode. This refers to the way childless dynamic
2240 * load nodes appear (this comes into play only after the initial dynamic
2241 * load request produced no children).
2242 * @method getIconMode
2243 * @return {int} 0 for collapse style, 1 for leaf node style
2245 getIconMode: function() {
2246 return (this.iconMode || this.tree.root.iconMode);
2250 * Checks if this node has children. If this node is lazy-loading and the
2251 * children have not been rendered, we do not know whether or not there
2252 * are actual children. In most cases, we need to assume that there are
2253 * children (for instance, the toggle needs to show the expandable
2254 * presentation state). In other times we want to know if there are rendered
2255 * children. For the latter, "checkForLazyLoad" should be false.
2256 * @method hasChildren
2257 * @param checkForLazyLoad {boolean} should we check for unloaded children?
2258 * @return {boolean} true if this has children or if it might and we are
2259 * checking for this condition.
2261 hasChildren: function(checkForLazyLoad) {
2265 return ( this.children.length > 0 ||
2266 (checkForLazyLoad && this.isDynamic() && !this.dynamicLoadComplete)
2272 * Expands if node is collapsed, collapses otherwise.
2275 toggle: function() {
2276 if (!this.tree.locked && ( this.hasChildren(true) || this.isDynamic()) ) {
2277 if (this.expanded) { this.collapse(); } else { this.expand(); }
2282 * Returns the markup for this node and its children.
2284 * @return {string} the markup for this node and its expanded children.
2286 getHtml: function() {
2288 this.childrenRendered = false;
2290 return ['<div class="ygtvitem" id="' , this.getElId() , '">' ,this.getNodeHtml() , this.getChildrenHtml() ,'</div>'].join("");
2294 * Called when first rendering the tree. We always build the div that will
2295 * contain this nodes children, but we don't render the children themselves
2296 * unless this node is expanded.
2297 * @method getChildrenHtml
2298 * @return {string} the children container div html and any expanded children
2301 getChildrenHtml: function() {
2305 sb[sb.length] = '<div class="ygtvchildren" id="' + this.getChildrenElId() + '"';
2307 // This is a workaround for an IE rendering issue, the child div has layout
2308 // in IE, creating extra space if a leaf node is created with the expanded
2309 // property set to true.
2310 if (!this.expanded || !this.hasChildren()) {
2311 sb[sb.length] = ' style="display:none;"';
2313 sb[sb.length] = '>';
2316 // Don't render the actual child node HTML unless this node is expanded.
2317 if ( (this.hasChildren(true) && this.expanded) ||
2318 (this.renderHidden && !this.isDynamic()) ) {
2319 sb[sb.length] = this.renderChildren();
2322 sb[sb.length] = '</div>';
2328 * Generates the markup for the child nodes. This is not done until the node
2330 * @method renderChildren
2331 * @return {string} the html for this node's children
2334 renderChildren: function() {
2339 if (this.isDynamic() && !this.dynamicLoadComplete) {
2340 this.isLoading = true;
2341 this.tree.locked = true;
2343 if (this.dataLoader) {
2347 node.dataLoader(node,
2349 node.loadComplete();
2353 } else if (this.tree.root.dataLoader) {
2357 node.tree.root.dataLoader(node,
2359 node.loadComplete();
2364 return "Error: data loader not found or not specified.";
2370 return this.completeRender();
2375 * Called when we know we have all the child data.
2376 * @method completeRender
2377 * @return {string} children html
2379 completeRender: function() {
2382 for (var i=0; i < this.children.length; ++i) {
2383 // this.children[i].childrenRendered = false;
2384 sb[sb.length] = this.children[i].getHtml();
2387 this.childrenRendered = true;
2393 * Load complete is the callback function we pass to the data provider
2394 * in dynamic load situations.
2395 * @method loadComplete
2397 loadComplete: function() {
2398 this.getChildrenEl().innerHTML = this.completeRender();
2399 if (this.propagateHighlightDown) {
2400 if (this.highlightState === 1 && !this.tree.singleNodeHighlight) {
2401 for (var i = 0; i < this.children.length; i++) {
2402 this.children[i].highlight(true);
2404 } else if (this.highlightState === 0 || this.tree.singleNodeHighlight) {
2405 for (i = 0; i < this.children.length; i++) {
2406 this.children[i].unhighlight(true);
2408 } // if (highlighState == 2) leave child nodes with whichever highlight state they are set
2411 this.dynamicLoadComplete = true;
2412 this.isLoading = false;
2414 this.tree.locked = false;
2418 * Returns this node's ancestor at the specified depth.
2419 * @method getAncestor
2420 * @param {int} depth the depth of the ancestor.
2421 * @return {Node} the ancestor
2423 getAncestor: function(depth) {
2424 if (depth >= this.depth || depth < 0) {
2428 var p = this.parent;
2430 while (p.depth > depth) {
2438 * Returns the css class for the spacer at the specified depth for
2439 * this node. If this node's ancestor at the specified depth
2440 * has a next sibling the presentation is different than if it
2441 * does not have a next sibling
2442 * @method getDepthStyle
2443 * @param {int} depth the depth of the ancestor.
2444 * @return {string} the css class for the spacer
2446 getDepthStyle: function(depth) {
2447 return (this.getAncestor(depth).nextSibling) ?
2448 "ygtvdepthcell" : "ygtvblankdepthcell";
2452 * Get the markup for the node. This may be overrided so that we can
2453 * support different types of nodes.
2454 * @method getNodeHtml
2455 * @return {string} The HTML that will render this node.
2457 getNodeHtml: function() {
2460 sb[sb.length] = '<table id="ygtvtableel' + this.index + '" border="0" cellpadding="0" cellspacing="0" class="ygtvtable ygtvdepth' + this.depth;
2461 sb[sb.length] = ' ygtv-' + (this.expanded?'expanded':'collapsed');
2462 if (this.enableHighlight) {
2463 sb[sb.length] = ' ygtv-highlight' + this.highlightState;
2465 if (this.className) {
2466 sb[sb.length] = ' ' + this.className;
2468 sb[sb.length] = '"><tr class="ygtvrow">';
2470 for (var i=0;i<this.depth;++i) {
2471 sb[sb.length] = '<td class="ygtvcell ' + this.getDepthStyle(i) + '"><div class="ygtvspacer"></div></td>';
2475 sb[sb.length] = '<td id="' + this.getToggleElId();
2476 sb[sb.length] = '" class="ygtvcell ';
2477 sb[sb.length] = this.getStyle() ;
2478 sb[sb.length] = '"><a href="#" class="ygtvspacer"> </a></td>';
2481 sb[sb.length] = '<td id="' + this.contentElId;
2482 sb[sb.length] = '" class="ygtvcell ';
2483 sb[sb.length] = this.contentStyle + ' ygtvcontent" ';
2484 sb[sb.length] = (this.nowrap) ? ' nowrap="nowrap" ' : '';
2485 sb[sb.length] = ' >';
2486 sb[sb.length] = this.getContentHtml();
2487 sb[sb.length] = '</td></tr></table>';
2493 * Get the markup for the contents of the node. This is designed to be overrided so that we can
2494 * support different types of nodes.
2495 * @method getContentHtml
2496 * @return {string} The HTML that will render the content of this node.
2498 getContentHtml: function () {
2503 * Regenerates the html for this node and its children. To be used when the
2504 * node is expanded and new children have been added.
2507 refresh: function() {
2508 // this.loadComplete();
2509 this.getChildrenEl().innerHTML = this.completeRender();
2512 var el = this.getToggleEl();
2514 el.className = el.className.replace(/\bygtv[lt][nmp]h*\b/gi,this.getStyle());
2522 * @return {string} string representation of the node
2524 toString: function() {
2525 return this._type + " (" + this.index + ")";
2528 * array of items that had the focus set on them
2529 * so that they can be cleaned when focus is lost
2530 * @property _focusHighlightedItems
2531 * @type Array of DOM elements
2534 _focusHighlightedItems: [],
2536 * DOM element that actually got the browser focus
2537 * @property _focusedItem
2544 * Returns true if there are any elements in the node that can
2545 * accept the real actual browser focus
2546 * @method _canHaveFocus
2547 * @return {boolean} success
2550 _canHaveFocus: function() {
2551 return this.getEl().getElementsByTagName('a').length > 0;
2554 * Removes the focus of previously selected Node
2555 * @method _removeFocus
2558 _removeFocus:function () {
2559 if (this._focusedItem) {
2560 Event.removeListener(this._focusedItem,'blur');
2561 this._focusedItem = null;
2564 while ((el = this._focusHighlightedItems.shift())) { // yes, it is meant as an assignment, really
2565 Dom.removeClass(el,YAHOO.widget.TreeView.FOCUS_CLASS_NAME );
2569 * Sets the focus on the node element.
2570 * It will only be able to set the focus on nodes that have anchor elements in it.
2571 * Toggle or branch icons have anchors and can be focused on.
2572 * If will fail in nodes that have no anchor
2574 * @return {boolean} success
2576 focus: function () {
2577 var focused = false, self = this;
2579 if (this.tree.currentFocus) {
2580 this.tree.currentFocus._removeFocus();
2583 var expandParent = function (node) {
2585 expandParent(node.parent);
2586 node.parent.expand();
2593 return (/ygtv(([tl][pmn]h?)|(content))/).test(el.className);
2596 self.getEl().firstChild ,
2598 Dom.addClass(el, YAHOO.widget.TreeView.FOCUS_CLASS_NAME );
2600 var aEl = el.getElementsByTagName('a');
2604 self._focusedItem = aEl;
2605 Event.on(aEl,'blur',function () {
2606 self.tree.fireEvent('focusChanged',{oldNode:self.tree.currentFocus,newNode:null});
2607 self.tree.currentFocus = null;
2608 self._removeFocus();
2613 self._focusHighlightedItems.push(el);
2617 this.tree.fireEvent('focusChanged',{oldNode:this.tree.currentFocus,newNode:this});
2618 this.tree.currentFocus = this;
2620 this.tree.fireEvent('focusChanged',{oldNode:self.tree.currentFocus,newNode:null});
2621 this.tree.currentFocus = null;
2622 this._removeFocus();
2628 * Count of nodes in a branch
2629 * @method getNodeCount
2630 * @return {int} number of nodes in the branch
2632 getNodeCount: function() {
2633 for (var i = 0, count = 0;i< this.children.length;i++) {
2634 count += this.children[i].getNodeCount();
2640 * Returns an object which could be used to build a tree out of this node and its children.
2641 * It can be passed to the tree constructor to reproduce this node as a tree.
2642 * It will return false if the node or any children loads dynamically, regardless of whether it is loaded or not.
2643 * @method getNodeDefinition
2644 * @return {Object | false} definition of the tree or false if the node or any children is defined as dynamic
2646 getNodeDefinition: function() {
2648 if (this.isDynamic()) { return false; }
2650 var def, defs = Lang.merge(this.data), children = [];
2654 if (this.expanded) {defs.expanded = this.expanded; }
2655 if (!this.multiExpand) { defs.multiExpand = this.multiExpand; }
2656 if (this.renderHidden) { defs.renderHidden = this.renderHidden; }
2657 if (!this.hasIcon) { defs.hasIcon = this.hasIcon; }
2658 if (this.nowrap) { defs.nowrap = this.nowrap; }
2659 if (this.className) { defs.className = this.className; }
2660 if (this.editable) { defs.editable = this.editable; }
2661 if (!this.enableHighlight) { defs.enableHighlight = this.enableHighlight; }
2662 if (this.highlightState) { defs.highlightState = this.highlightState; }
2663 if (this.propagateHighlightUp) { defs.propagateHighlightUp = this.propagateHighlightUp; }
2664 if (this.propagateHighlightDown) { defs.propagateHighlightDown = this.propagateHighlightDown; }
2665 defs.type = this._type;
2669 for (var i = 0; i < this.children.length;i++) {
2670 def = this.children[i].getNodeDefinition();
2671 if (def === false) { return false;}
2674 if (children.length) { defs.children = children; }
2680 * Generates the link that will invoke this node's toggle method
2681 * @method getToggleLink
2682 * @return {string} the javascript url for toggling this node
2684 getToggleLink: function() {
2685 return 'return false;';
2689 * Sets the value of property for this node and all loaded descendants.
2690 * Only public and defined properties can be set, not methods.
2691 * Values for unknown properties will be assigned to the refNode.data object
2692 * @method setNodesProperty
2693 * @param name {string} Name of the property to be set
2694 * @param value {any} value to be set
2695 * @param refresh {boolean} if present and true, it does a refresh
2697 setNodesProperty: function(name, value, refresh) {
2698 if (name.charAt(0) != '_' && !Lang.isUndefined(this[name]) && !Lang.isFunction(this[name]) ) {
2701 this.data[name] = value;
2703 for (var i = 0; i < this.children.length;i++) {
2704 this.children[i].setNodesProperty(name,value);
2711 * Toggles the highlighted state of a Node
2712 * @method toggleHighlight
2714 toggleHighlight: function() {
2715 if (this.enableHighlight) {
2716 // unhighlights only if fully highligthed. For not or partially highlighted it will highlight
2717 if (this.highlightState == 1) {
2726 * Turns highlighting on node.
2728 * @param _silent {boolean} optional, don't fire the highlightEvent
2730 highlight: function(_silent) {
2731 if (this.enableHighlight) {
2732 if (this.tree.singleNodeHighlight) {
2733 if (this.tree._currentlyHighlighted) {
2734 this.tree._currentlyHighlighted.unhighlight(_silent);
2736 this.tree._currentlyHighlighted = this;
2738 this.highlightState = 1;
2739 this._setHighlightClassName();
2740 if (!this.tree.singleNodeHighlight) {
2741 if (this.propagateHighlightDown) {
2742 for (var i = 0;i < this.children.length;i++) {
2743 this.children[i].highlight(true);
2746 if (this.propagateHighlightUp) {
2748 this.parent._childrenHighlighted();
2753 this.tree.fireEvent('highlightEvent',this);
2758 * Turns highlighting off a node.
2759 * @method unhighlight
2760 * @param _silent {boolean} optional, don't fire the highlightEvent
2762 unhighlight: function(_silent) {
2763 if (this.enableHighlight) {
2764 // might have checked singleNodeHighlight but it wouldn't really matter either way
2765 this.tree._currentlyHighlighted = null;
2766 this.highlightState = 0;
2767 this._setHighlightClassName();
2768 if (!this.tree.singleNodeHighlight) {
2769 if (this.propagateHighlightDown) {
2770 for (var i = 0;i < this.children.length;i++) {
2771 this.children[i].unhighlight(true);
2774 if (this.propagateHighlightUp) {
2776 this.parent._childrenHighlighted();
2781 this.tree.fireEvent('highlightEvent',this);
2786 * Checks whether all or part of the children of a node are highlighted and
2787 * sets the node highlight to full, none or partial highlight.
2788 * If set to propagate it will further call the parent
2789 * @method _childrenHighlighted
2792 _childrenHighlighted: function() {
2793 var yes = false, no = false;
2794 if (this.enableHighlight) {
2795 for (var i = 0;i < this.children.length;i++) {
2796 switch(this.children[i].highlightState) {
2809 this.highlightState = 2;
2811 this.highlightState = 1;
2813 this.highlightState = 0;
2815 this._setHighlightClassName();
2816 if (this.propagateHighlightUp) {
2818 this.parent._childrenHighlighted();
2825 * Changes the classNames on the toggle and content containers to reflect the current highlighting
2826 * @method _setHighlightClassName
2829 _setHighlightClassName: function() {
2830 var el = Dom.get('ygtvtableel' + this.index);
2832 el.className = el.className.replace(/\bygtv-highlight\d\b/gi,'ygtv-highlight' + this.highlightState);
2838 YAHOO.augment(YAHOO.widget.Node, YAHOO.util.EventProvider);
2841 * A custom YAHOO.widget.Node that handles the unique nature of
2842 * the virtual, presentationless root node.
2843 * @namespace YAHOO.widget
2845 * @extends YAHOO.widget.Node
2846 * @param oTree {YAHOO.widget.TreeView} The tree instance this node belongs to
2849 YAHOO.widget.RootNode = function(oTree) {
2850 // Initialize the node with null params. The root node is a
2851 // special case where the node has no presentation. So we have
2852 // to alter the standard properties a bit.
2853 this.init(null, null, true);
2856 * For the root node, we get the tree reference from as a param
2857 * to the constructor instead of from the parent element.
2862 YAHOO.extend(YAHOO.widget.RootNode, YAHOO.widget.Node, {
2869 * @default "RootNode"
2873 // overrides YAHOO.widget.Node
2874 getNodeHtml: function() {
2878 toString: function() {
2882 loadComplete: function() {
2887 * Count of nodes in tree.
2888 * It overrides Nodes.getNodeCount because the root node should not be counted.
2889 * @method getNodeCount
2890 * @return {int} number of nodes in the tree
2892 getNodeCount: function() {
2893 for (var i = 0, count = 0;i< this.children.length;i++) {
2894 count += this.children[i].getNodeCount();
2900 * Returns an object which could be used to build a tree out of this node and its children.
2901 * It can be passed to the tree constructor to reproduce this node as a tree.
2902 * Since the RootNode is automatically created by treeView,
2903 * its own definition is excluded from the returned node definition
2904 * which only contains its children.
2905 * @method getNodeDefinition
2906 * @return {Object | false} definition of the tree or false if any child node is defined as dynamic
2908 getNodeDefinition: function() {
2910 for (var def, defs = [], i = 0; i < this.children.length;i++) {
2911 def = this.children[i].getNodeDefinition();
2912 if (def === false) { return false;}
2918 collapse: function() {},
2919 expand: function() {},
2920 getSiblings: function() { return null; },
2921 focus: function () {}
2925 var Dom = YAHOO.util.Dom,
2927 Event = YAHOO.util.Event;
2929 * The default node presentation. The first parameter should be
2930 * either a string that will be used as the node's label, or an object
2931 * that has at least a string property called label. By default, clicking the
2932 * label will toggle the expanded/collapsed state of the node. By
2933 * setting the href property of the instance, this behavior can be
2934 * changed so that the label will go to the specified href.
2935 * @namespace YAHOO.widget
2937 * @extends YAHOO.widget.Node
2939 * @param oData {object} a string or object containing the data that will
2940 * be used to render this node.
2941 * Providing a string is the same as providing an object with a single property named label.
2942 * All values in the oData will be used to set equally named properties in the node
2943 * as long as the node does have such properties, they are not undefined, private or functions.
2944 * All attributes are made available in noderef.data, which
2945 * can be used to store custom attributes. TreeView.getNode(s)ByProperty
2946 * can be used to retrieve a node by one of the attributes.
2947 * @param oParent {YAHOO.widget.Node} this node's parent node
2948 * @param expanded {boolean} the initial expanded/collapsed state (deprecated; use oData.expanded)
2950 YAHOO.widget.TextNode = function(oData, oParent, expanded) {
2953 if (Lang.isString(oData)) {
2954 oData = { label: oData };
2956 this.init(oData, oParent, expanded);
2957 this.setUpLabel(oData);
2962 YAHOO.extend(YAHOO.widget.TextNode, YAHOO.widget.Node, {
2965 * The CSS class for the label href. Defaults to ygtvlabel, but can be
2966 * overridden to provide a custom presentation for a specific node.
2967 * @property labelStyle
2970 labelStyle: "ygtvlabel",
2973 * The derived element id of the label for this node
2974 * @property labelElId
2980 * The text for the label. It is assumed that the oData parameter will
2981 * either be a string that will be used as the label, or an object that
2982 * has a property called "label" that we will use.
2989 * The text for the title (tooltip) for the label element
2996 * The href for the node's label. If one is not specified, the href will
2997 * be set so that it toggles the node.
3004 * The label href target, defaults to current window
3015 * @default "TextNode"
3021 * Sets up the node label
3022 * @method setUpLabel
3023 * @param oData string containing the label, or an object with a label property
3025 setUpLabel: function(oData) {
3027 if (Lang.isString(oData)) {
3033 this.labelStyle = oData.style;
3037 this.label = oData.label;
3039 this.labelElId = "ygtvlabelel" + this.index;
3044 * Returns the label element
3045 * @for YAHOO.widget.TextNode
3046 * @method getLabelEl
3047 * @return {object} the element
3049 getLabelEl: function() {
3050 return Dom.get(this.labelElId);
3053 // overrides YAHOO.widget.Node
3054 getContentHtml: function() {
3056 sb[sb.length] = this.href ? '<a' : '<span';
3057 sb[sb.length] = ' id="' + Lang.escapeHTML(this.labelElId) + '"';
3058 sb[sb.length] = ' class="' + Lang.escapeHTML(this.labelStyle) + '"';
3060 sb[sb.length] = ' href="' + Lang.escapeHTML(this.href) + '"';
3061 sb[sb.length] = ' target="' + Lang.escapeHTML(this.target) + '"';
3064 sb[sb.length] = ' title="' + Lang.escapeHTML(this.title) + '"';
3066 sb[sb.length] = ' >';
3067 sb[sb.length] = Lang.escapeHTML(this.label);
3068 sb[sb.length] = this.href?'</a>':'</span>';
3075 * Returns an object which could be used to build a tree out of this node and its children.
3076 * It can be passed to the tree constructor to reproduce this node as a tree.
3077 * It will return false if the node or any descendant loads dynamically, regardless of whether it is loaded or not.
3078 * @method getNodeDefinition
3079 * @return {Object | false} definition of the tree or false if this node or any descendant is defined as dynamic
3081 getNodeDefinition: function() {
3082 var def = YAHOO.widget.TextNode.superclass.getNodeDefinition.call(this);
3083 if (def === false) { return false; }
3085 // Node specific properties
3086 def.label = this.label;
3087 if (this.labelStyle != 'ygtvlabel') { def.style = this.labelStyle; }
3088 if (this.title) { def.title = this.title; }
3089 if (this.href) { def.href = this.href; }
3090 if (this.target != '_self') { def.target = this.target; }
3096 toString: function() {
3097 return YAHOO.widget.TextNode.superclass.toString.call(this) + ": " + this.label;
3101 onLabelClick: function() {
3104 refresh: function() {
3105 YAHOO.widget.TextNode.superclass.refresh.call(this);
3106 var label = this.getLabelEl();
3107 label.innerHTML = this.label;
3108 if (label.tagName.toUpperCase() == 'A') {
3109 label.href = this.href;
3110 label.target = this.target;
3120 * A menu-specific implementation that differs from TextNode in that only
3121 * one sibling can be expanded at a time.
3122 * @namespace YAHOO.widget
3124 * @extends YAHOO.widget.TextNode
3125 * @param oData {object} a string or object containing the data that will
3126 * be used to render this node.
3127 * Providing a string is the same as providing an object with a single property named label.
3128 * All values in the oData will be used to set equally named properties in the node
3129 * as long as the node does have such properties, they are not undefined, private or functions.
3130 * All attributes are made available in noderef.data, which
3131 * can be used to store custom attributes. TreeView.getNode(s)ByProperty
3132 * can be used to retrieve a node by one of the attributes.
3133 * @param oParent {YAHOO.widget.Node} this node's parent node
3134 * @param expanded {boolean} the initial expanded/collapsed state (deprecated; use oData.expanded)
3137 YAHOO.widget.MenuNode = function(oData, oParent, expanded) {
3138 YAHOO.widget.MenuNode.superclass.constructor.call(this,oData,oParent,expanded);
3141 * Menus usually allow only one branch to be open at a time.
3143 this.multiExpand = false;
3147 YAHOO.extend(YAHOO.widget.MenuNode, YAHOO.widget.TextNode, {
3153 * @default "MenuNode"
3159 var Dom = YAHOO.util.Dom,
3161 Event = YAHOO.util.Event;
3164 * This implementation takes either a string or object for the
3165 * oData argument. If is it a string, it will use it for the display
3166 * of this node (and it can contain any html code). If the parameter
3167 * is an object,it looks for a parameter called "html" that will be
3168 * used for this node's display.
3169 * @namespace YAHOO.widget
3171 * @extends YAHOO.widget.Node
3173 * @param oData {object} a string or object containing the data that will
3174 * be used to render this node.
3175 * Providing a string is the same as providing an object with a single property named html.
3176 * All values in the oData will be used to set equally named properties in the node
3177 * as long as the node does have such properties, they are not undefined, private or functions.
3178 * All other attributes are made available in noderef.data, which
3179 * can be used to store custom attributes. TreeView.getNode(s)ByProperty
3180 * can be used to retrieve a node by one of the attributes.
3181 * @param oParent {YAHOO.widget.Node} this node's parent node
3182 * @param expanded {boolean} the initial expanded/collapsed state (deprecated; use oData.expanded)
3183 * @param hasIcon {boolean} specifies whether or not leaf nodes should
3184 * be rendered with or without a horizontal line and/or toggle icon. If the icon
3185 * is not displayed, the content fills the space it would have occupied.
3186 * This option operates independently of the leaf node presentation logic
3187 * for dynamic nodes.
3188 * (deprecated; use oData.hasIcon)
3190 var HN = function(oData, oParent, expanded, hasIcon) {
3192 this.init(oData, oParent, expanded);
3193 this.initContent(oData, hasIcon);
3198 YAHOO.widget.HTMLNode = HN;
3199 YAHOO.extend(HN, YAHOO.widget.Node, {
3202 * The CSS class for the html content container. Defaults to ygtvhtml, but
3203 * can be overridden to provide a custom presentation for a specific node.
3204 * @property contentStyle
3207 contentStyle: "ygtvhtml",
3211 * The HTML content to use for this node's display
3222 * @default "HTMLNode"
3227 * Sets up the node label
3228 * @method initContent
3229 * @param oData {object} An html string or object containing an html property
3230 * @param hasIcon {boolean} determines if the node will be rendered with an
3233 initContent: function(oData, hasIcon) {
3234 this.setHtml(oData);
3235 this.contentElId = "ygtvcontentel" + this.index;
3236 if (!Lang.isUndefined(hasIcon)) { this.hasIcon = hasIcon; }
3241 * Synchronizes the node.html, and the node's content
3243 * @param o {object |string | HTMLElement } An html string, an object containing an html property or an HTML element
3245 setHtml: function(o) {
3246 this.html = (Lang.isObject(o) && 'html' in o) ? o.html : o;
3248 var el = this.getContentEl();
3250 if (o.nodeType && o.nodeType == 1 && o.tagName) {
3253 el.innerHTML = this.html;
3259 // overrides YAHOO.widget.Node
3260 // If property html is a string, it sets the innerHTML for the node
3261 // If it is an HTMLElement, it defers appending it to the tree until the HTML basic structure is built
3262 getContentHtml: function() {
3263 if (typeof this.html === "string") {
3267 HN._deferredNodes.push(this);
3269 HN._timer = window.setTimeout(function () {
3271 while((n = HN._deferredNodes.pop())) {
3272 n.getContentEl().appendChild(n.html);
3282 * Returns an object which could be used to build a tree out of this node and its children.
3283 * It can be passed to the tree constructor to reproduce this node as a tree.
3284 * It will return false if any node loads dynamically, regardless of whether it is loaded or not.
3285 * @method getNodeDefinition
3286 * @return {Object | false} definition of the tree or false if any node is defined as dynamic
3288 getNodeDefinition: function() {
3289 var def = HN.superclass.getNodeDefinition.call(this);
3290 if (def === false) { return false; }
3291 def.html = this.html;
3298 * An array of HTMLNodes created with HTML Elements that had their rendering
3299 * deferred until the basic tree structure is rendered.
3300 * @property _deferredNodes
3301 * @type YAHOO.widget.HTMLNode[]
3306 HN._deferredNodes = [];
3308 * A system timer value used to mark whether a deferred operation is pending.
3310 * @type System Timer
3318 var Dom = YAHOO.util.Dom,
3320 Event = YAHOO.util.Event,
3321 Calendar = YAHOO.widget.Calendar;
3324 * A Date-specific implementation that differs from TextNode in that it uses
3325 * YAHOO.widget.Calendar as an in-line editor, if available
3326 * If Calendar is not available, it behaves as a plain TextNode.
3327 * @namespace YAHOO.widget
3329 * @extends YAHOO.widget.TextNode
3330 * @param oData {object} a string or object containing the data that will
3331 * be used to render this node.
3332 * Providing a string is the same as providing an object with a single property named label.
3333 * All values in the oData will be used to set equally named properties in the node
3334 * as long as the node does have such properties, they are not undefined, private nor functions.
3335 * All attributes are made available in noderef.data, which
3336 * can be used to store custom attributes. TreeView.getNode(s)ByProperty
3337 * can be used to retrieve a node by one of the attributes.
3338 * @param oParent {YAHOO.widget.Node} this node's parent node
3339 * @param expanded {boolean} the initial expanded/collapsed state (deprecated; use oData.expanded)
3342 YAHOO.widget.DateNode = function(oData, oParent, expanded) {
3343 YAHOO.widget.DateNode.superclass.constructor.call(this,oData, oParent, expanded);
3346 YAHOO.extend(YAHOO.widget.DateNode, YAHOO.widget.TextNode, {
3353 * @default "DateNode"
3358 * Configuration object for the Calendar editor, if used.
3359 * See <a href="http://developer.yahoo.com/yui/calendar/#internationalization">http://developer.yahoo.com/yui/calendar/#internationalization</a>
3360 * @property calendarConfig
3362 calendarConfig: null,
3367 * If YAHOO.widget.Calendar is available, it will pop up a Calendar to enter a new date. Otherwise, it falls back to a plain <input> textbox
3368 * @method fillEditorContainer
3369 * @param editorData {YAHOO.widget.TreeView.editorData} a shortcut to the static object holding editing information
3372 fillEditorContainer: function (editorData) {
3374 var cal, container = editorData.inputContainer;
3376 if (Lang.isUndefined(Calendar)) {
3377 Dom.replaceClass(editorData.editorPanel,'ygtv-edit-DateNode','ygtv-edit-TextNode');
3378 YAHOO.widget.DateNode.superclass.fillEditorContainer.call(this, editorData);
3382 if (editorData.nodeType != this._type) {
3383 editorData.nodeType = this._type;
3384 editorData.saveOnEnter = false;
3386 editorData.node.destroyEditorContents(editorData);
3388 editorData.inputObject = cal = new Calendar(container.appendChild(document.createElement('div')));
3389 if (this.calendarConfig) {
3390 cal.cfg.applyConfig(this.calendarConfig,true);
3391 cal.cfg.fireQueue();
3393 cal.selectEvent.subscribe(function () {
3394 this.tree._closeEditor(true);
3397 cal = editorData.inputObject;
3400 editorData.oldValue = this.label;
3401 cal.cfg.setProperty("selected",this.label, false);
3403 var delim = cal.cfg.getProperty('DATE_FIELD_DELIMITER');
3404 var pageDate = this.label.split(delim);
3405 cal.cfg.setProperty('pagedate',pageDate[cal.cfg.getProperty('MDY_MONTH_POSITION') -1] + delim + pageDate[cal.cfg.getProperty('MDY_YEAR_POSITION') -1]);
3406 cal.cfg.fireQueue();
3409 cal.oDomContainer.focus();
3412 * Returns the value from the input element.
3413 * Overrides Node.getEditorValue.
3414 * @method getEditorValue
3415 * @param editorData {YAHOO.widget.TreeView.editorData} a shortcut to the static object holding editing information
3416 * @return {string} date entered
3419 getEditorValue: function (editorData) {
3420 if (Lang.isUndefined(Calendar)) {
3421 return editorData.inputElement.value;
3423 var cal = editorData.inputObject,
3424 date = cal.getSelectedDates()[0],
3427 dd[cal.cfg.getProperty('MDY_DAY_POSITION') -1] = date.getDate();
3428 dd[cal.cfg.getProperty('MDY_MONTH_POSITION') -1] = date.getMonth() + 1;
3429 dd[cal.cfg.getProperty('MDY_YEAR_POSITION') -1] = date.getFullYear();
3430 return dd.join(cal.cfg.getProperty('DATE_FIELD_DELIMITER'));
3435 * Finally displays the newly entered date in the tree.
3436 * Overrides Node.displayEditedValue.
3437 * @method displayEditedValue
3438 * @param value {HTML} date to be displayed and stored in the node.
3439 * This data is added to the node unescaped via the innerHTML property.
3440 * @param editorData {YAHOO.widget.TreeView.editorData} a shortcut to the static object holding editing information
3442 displayEditedValue: function (value,editorData) {
3443 var node = editorData.node;
3445 node.getLabelEl().innerHTML = value;
3449 * Returns an object which could be used to build a tree out of this node and its children.
3450 * It can be passed to the tree constructor to reproduce this node as a tree.
3451 * It will return false if the node or any descendant loads dynamically, regardless of whether it is loaded or not.
3452 * @method getNodeDefinition
3453 * @return {Object | false} definition of the node or false if this node or any descendant is defined as dynamic
3455 getNodeDefinition: function() {
3456 var def = YAHOO.widget.DateNode.superclass.getNodeDefinition.call(this);
3457 if (def === false) { return false; }
3458 if (this.calendarConfig) { def.calendarConfig = this.calendarConfig; }
3466 var Dom = YAHOO.util.Dom,
3468 Event = YAHOO.util.Event,
3469 TV = YAHOO.widget.TreeView,
3470 TVproto = TV.prototype;
3473 * An object to store information used for in-line editing
3474 * for all Nodes of all TreeViews. It contains:
3476 * <li>active {boolean}, whether there is an active cell editor </li>
3477 * <li>whoHasIt {YAHOO.widget.TreeView} TreeView instance that is currently using the editor</li>
3478 * <li>nodeType {string} value of static Node._type property, allows reuse of input element if node is of the same type.</li>
3479 * <li>editorPanel {HTMLelement (<div>)} element holding the in-line editor</li>
3480 * <li>inputContainer {HTMLelement (<div>)} element which will hold the type-specific input element(s) to be filled by the fillEditorContainer method</li>
3481 * <li>buttonsContainer {HTMLelement (<div>)} element which holds the <button> elements for Ok/Cancel. If you don't want any of the buttons, hide it via CSS styles, don't destroy it</li>
3482 * <li>node {YAHOO.widget.Node} reference to the Node being edited</li>
3483 * <li>saveOnEnter {boolean}, whether the Enter key should be accepted as a Save command (Esc. is always taken as Cancel), disable for multi-line input elements </li>
3484 * <li>oldValue {any} value before editing</li>
3486 * Editors are free to use this object to store additional data.
3487 * @property editorData
3489 * @for YAHOO.widget.TreeView
3493 whoHasIt:null, // which TreeView has it
3496 inputContainer:null,
3497 buttonsContainer:null,
3498 node:null, // which Node is being edited
3501 // Each node type is free to add its own properties to this as it sees fit.
3505 * Validator function for edited data, called from the TreeView instance scope,
3506 * receives the arguments (newValue, oldValue, nodeInstance)
3507 * and returns either the validated (or type-converted) value or undefined.
3508 * An undefined return will prevent the editor from closing
3509 * @property validator
3512 * @for YAHOO.widget.TreeView
3514 TVproto.validator = null;
3517 * Entry point for initializing the editing plug-in.
3518 * TreeView will call this method on initializing if it exists
3519 * @method _initEditor
3520 * @for YAHOO.widget.TreeView
3524 TVproto._initEditor = function () {
3526 * Fires when the user clicks on the ok button of a node editor
3527 * @event editorSaveEvent
3529 * @param oArgs.newValue {mixed} the new value just entered
3530 * @param oArgs.oldValue {mixed} the value originally in the tree
3531 * @param oArgs.node {YAHOO.widget.Node} the node that has the focus
3532 * @for YAHOO.widget.TreeView
3534 this.createEvent("editorSaveEvent", this);
3537 * Fires when the user clicks on the cancel button of a node editor
3538 * @event editorCancelEvent
3540 * @param {YAHOO.widget.Node} node the node that has the focus
3541 * @for YAHOO.widget.TreeView
3543 this.createEvent("editorCancelEvent", this);
3548 * Entry point of the editing plug-in.
3549 * TreeView will call this method if it exists when a node label is clicked
3550 * @method _nodeEditing
3551 * @param node {YAHOO.widget.Node} the node to be edited
3552 * @return {Boolean} true to indicate that the node is editable and prevent any further bubbling of the click.
3553 * @for YAHOO.widget.TreeView
3559 TVproto._nodeEditing = function (node) {
3560 if (node.fillEditorContainer && node.editable) {
3561 var ed, topLeft, buttons, button, editorData = TV.editorData;
3562 editorData.active = true;
3563 editorData.whoHasIt = this;
3564 if (!editorData.nodeType) {
3565 // Fixes: http://yuilibrary.com/projects/yui2/ticket/2528945
3566 editorData.editorPanel = ed = this.getEl().appendChild(document.createElement('div'));
3567 Dom.addClass(ed,'ygtv-label-editor');
3570 buttons = editorData.buttonsContainer = ed.appendChild(document.createElement('div'));
3571 Dom.addClass(buttons,'ygtv-button-container');
3572 button = buttons.appendChild(document.createElement('button'));
3573 Dom.addClass(button,'ygtvok');
3574 button.innerHTML = ' ';
3575 button = buttons.appendChild(document.createElement('button'));
3576 Dom.addClass(button,'ygtvcancel');
3577 button.innerHTML = ' ';
3578 Event.on(buttons, 'click', function (ev) {
3579 var target = Event.getTarget(ev),
3580 editorData = TV.editorData,
3581 node = editorData.node,
3582 self = editorData.whoHasIt;
3583 if (Dom.hasClass(target,'ygtvok')) {
3584 Event.stopEvent(ev);
3585 self._closeEditor(true);
3587 if (Dom.hasClass(target,'ygtvcancel')) {
3588 Event.stopEvent(ev);
3589 self._closeEditor(false);
3593 editorData.inputContainer = ed.appendChild(document.createElement('div'));
3594 Dom.addClass(editorData.inputContainer,'ygtv-input');
3596 Event.on(ed,'keydown',function (ev) {
3597 var editorData = TV.editorData,
3598 KEY = YAHOO.util.KeyListener.KEY,
3599 self = editorData.whoHasIt;
3600 switch (ev.keyCode) {
3602 Event.stopEvent(ev);
3603 if (editorData.saveOnEnter) {
3604 self._closeEditor(true);
3608 Event.stopEvent(ev);
3609 self._closeEditor(false);
3617 ed = editorData.editorPanel;
3619 editorData.node = node;
3620 if (editorData.nodeType) {
3621 Dom.removeClass(ed,'ygtv-edit-' + editorData.nodeType);
3623 Dom.addClass(ed,' ygtv-edit-' + node._type);
3624 // Fixes: http://yuilibrary.com/projects/yui2/ticket/2528945
3625 Dom.setStyle(ed,'display','block');
3626 Dom.setXY(ed,Dom.getXY(node.getContentEl()));
3629 node.fillEditorContainer(editorData);
3631 return true; // If inline editor available, don't do anything else.
3636 * Method to be associated with an event (clickEvent, dblClickEvent or enterKeyPressed) to pop up the contents editor
3637 * It calls the corresponding node editNode method.
3638 * @method onEventEditNode
3639 * @param oArgs {object} Object passed as arguments to TreeView event listeners
3640 * @for YAHOO.widget.TreeView
3643 TVproto.onEventEditNode = function (oArgs) {
3644 if (oArgs instanceof YAHOO.widget.Node) {
3646 } else if (oArgs.node instanceof YAHOO.widget.Node) {
3647 oArgs.node.editNode();
3653 * Method to be called when the inline editing is finished and the editor is to be closed
3654 * @method _closeEditor
3655 * @param save {Boolean} true if the edited value is to be saved, false if discarded
3657 * @for YAHOO.widget.TreeView
3660 TVproto._closeEditor = function (save) {
3661 var ed = TV.editorData,
3664 // http://yuilibrary.com/projects/yui2/ticket/2528946
3665 // _closeEditor might now be called at any time, even when there is no label editor open
3666 // so we need to ensure there is one.
3667 if (!node || !ed.active) { return; }
3669 close = ed.node.saveEditorValue(ed) !== false;
3671 this.fireEvent( 'editorCancelEvent', node);
3675 Dom.setStyle(ed.editorPanel,'display','none');
3682 * Entry point for TreeView's destroy method to destroy whatever the editing plug-in has created
3683 * @method _destroyEditor
3685 * @for YAHOO.widget.TreeView
3687 TVproto._destroyEditor = function() {
3688 var ed = TV.editorData;
3689 if (ed && ed.nodeType && (!ed.active || ed.whoHasIt === this)) {
3690 Event.removeListener(ed.editorPanel,'keydown');
3691 Event.removeListener(ed.buttonContainer,'click');
3692 ed.node.destroyEditorContents(ed);
3693 document.body.removeChild(ed.editorPanel);
3694 ed.nodeType = ed.editorPanel = ed.inputContainer = ed.buttonsContainer = ed.whoHasIt = ed.node = null;
3699 var Nproto = YAHOO.widget.Node.prototype;
3702 * Signals if the label is editable. (Ignored on TextNodes with href set.)
3703 * @property editable
3705 * @for YAHOO.widget.Node
3707 Nproto.editable = false;
3710 * pops up the contents editor, if there is one and the node is declared editable
3712 * @for YAHOO.widget.Node
3715 Nproto.editNode = function () {
3716 this.tree._nodeEditing(this);
3720 /** Placeholder for a function that should provide the inline node label editor.
3721 * Leaving it set to null will indicate that this node type is not editable.
3722 * It should be overridden by nodes that provide inline editing.
3723 * The Node-specific editing element (input box, textarea or whatever) should be inserted into editorData.inputContainer.
3724 * @method fillEditorContainer
3725 * @param editorData {YAHOO.widget.TreeView.editorData} a shortcut to the static object holding editing information
3727 * @for YAHOO.widget.Node
3729 Nproto.fillEditorContainer = null;
3733 * Node-specific destroy function to empty the contents of the inline editor panel.
3734 * This function is the worst case alternative that will purge all possible events and remove the editor contents.
3735 * Method Event.purgeElement is somewhat costly so if it can be replaced by specifc Event.removeListeners, it is better to do so.
3736 * @method destroyEditorContents
3737 * @param editorData {YAHOO.widget.TreeView.editorData} a shortcut to the static object holding editing information
3738 * @for YAHOO.widget.Node
3740 Nproto.destroyEditorContents = function (editorData) {
3741 // In the worst case, if the input editor (such as the Calendar) has no destroy method
3742 // we can only try to remove all possible events on it.
3743 Event.purgeElement(editorData.inputContainer,true);
3744 editorData.inputContainer.innerHTML = '';
3748 * Saves the value entered into the editor.
3749 * @method saveEditorValue
3750 * @param editorData {YAHOO.widget.TreeView.editorData} a shortcut to the static object holding editing information
3751 * @return {false or none} a return of exactly false will prevent the editor from closing
3752 * @for YAHOO.widget.Node
3754 Nproto.saveEditorValue = function (editorData) {
3755 var node = editorData.node,
3757 validator = node.tree.validator;
3759 value = this.getEditorValue(editorData);
3761 if (Lang.isFunction(validator)) {
3762 value = validator(value,editorData.oldValue,node);
3763 if (Lang.isUndefined(value)) {
3768 if (this.tree.fireEvent( 'editorSaveEvent', {
3770 oldValue:editorData.oldValue,
3773 this.displayEditedValue(value,editorData);
3779 * Returns the value(s) from the input element(s) .
3780 * Should be overridden by each node type.
3781 * @method getEditorValue
3782 * @param editorData {YAHOO.widget.TreeView.editorData} a shortcut to the static object holding editing information
3783 * @return {any} value entered
3784 * @for YAHOO.widget.Node
3787 Nproto.getEditorValue = function (editorData) {
3791 * Finally displays the newly edited value(s) in the tree.
3792 * Should be overridden by each node type.
3793 * @method displayEditedValue
3794 * @param value {HTML} value to be displayed and stored in the node
3795 * This data is added to the node unescaped via the innerHTML property.
3796 * @param editorData {YAHOO.widget.TreeView.editorData} a shortcut to the static object holding editing information
3797 * @for YAHOO.widget.Node
3799 Nproto.displayEditedValue = function (value,editorData) {
3802 var TNproto = YAHOO.widget.TextNode.prototype;
3807 * Places an <input> textbox in the input container and loads the label text into it.
3808 * @method fillEditorContainer
3809 * @param editorData {YAHOO.widget.TreeView.editorData} a shortcut to the static object holding editing information
3811 * @for YAHOO.widget.TextNode
3813 TNproto.fillEditorContainer = function (editorData) {
3816 // If last node edited is not of the same type as this one, delete it and fill it with our editor
3817 if (editorData.nodeType != this._type) {
3818 editorData.nodeType = this._type;
3819 editorData.saveOnEnter = true;
3820 editorData.node.destroyEditorContents(editorData);
3822 editorData.inputElement = input = editorData.inputContainer.appendChild(document.createElement('input'));
3825 // if the last node edited was of the same time, reuse the input element.
3826 input = editorData.inputElement;
3828 editorData.oldValue = this.label;
3829 input.value = this.label;
3835 * Returns the value from the input element.
3836 * Overrides Node.getEditorValue.
3837 * @method getEditorValue
3838 * @param editorData {YAHOO.widget.TreeView.editorData} a shortcut to the static object holding editing information
3839 * @return {string} value entered
3840 * @for YAHOO.widget.TextNode
3843 TNproto.getEditorValue = function (editorData) {
3844 return editorData.inputElement.value;
3848 * Finally displays the newly edited value in the tree.
3849 * Overrides Node.displayEditedValue.
3850 * @method displayEditedValue
3851 * @param value {string} value to be displayed and stored in the node
3852 * @param editorData {YAHOO.widget.TreeView.editorData} a shortcut to the static object holding editing information
3853 * @for YAHOO.widget.TextNode
3855 TNproto.displayEditedValue = function (value,editorData) {
3856 var node = editorData.node;
3858 node.getLabelEl().innerHTML = value;
3862 * Destroys the contents of the inline editor panel.
3863 * Overrides Node.destroyEditorContent.
3864 * Since we didn't set any event listeners on this inline editor, it is more efficient to avoid the generic method in Node.
3865 * @method destroyEditorContents
3866 * @param editorData {YAHOO.widget.TreeView.editorData} a shortcut to the static object holding editing information
3867 * @for YAHOO.widget.TextNode
3869 TNproto.destroyEditorContents = function (editorData) {
3870 editorData.inputContainer.innerHTML = '';
3874 * A static factory class for tree view expand/collapse animations
3878 YAHOO.widget.TVAnim = function() {
3881 * Constant for the fade in animation
3886 FADE_IN: "TVFadeIn",
3889 * Constant for the fade out animation
3890 * @property FADE_OUT
3894 FADE_OUT: "TVFadeOut",
3897 * Returns a ygAnim instance of the given type
3899 * @param type {string} the type of animation
3900 * @param el {HTMLElement} the element to element (probably the children div)
3901 * @param callback {function} function to invoke when the animation is done.
3902 * @return {YAHOO.util.Animation} the animation instance
3905 getAnim: function(type, el, callback) {
3906 if (YAHOO.widget[type]) {
3907 return new YAHOO.widget[type](el, callback);
3914 * Returns true if the specified animation class is available
3916 * @param type {string} the type of animation
3917 * @return {boolean} true if valid, false if not
3920 isValid: function(type) {
3921 return (YAHOO.widget[type]);
3926 * A 1/2 second fade-in animation.
3929 * @param el {HTMLElement} the element to animate
3930 * @param callback {function} function to invoke when the animation is finished
3932 YAHOO.widget.TVFadeIn = function(el, callback) {
3934 * The element to animate
3941 * the callback to invoke when the animation is complete
3942 * @property callback
3945 this.callback = callback;
3949 YAHOO.widget.TVFadeIn.prototype = {
3951 * Performs the animation
3954 animate: function() {
3957 var s = this.el.style;
3959 s.filter = "alpha(opacity=10)";
3963 var a = new YAHOO.util.Anim(this.el, {opacity: {from: 0.1, to: 1, unit:""}}, dur);
3964 a.onComplete.subscribe( function() { tvanim.onComplete(); } );
3969 * Clean up and invoke callback
3970 * @method onComplete
3972 onComplete: function() {
3979 * @return {string} the string representation of the instance
3981 toString: function() {
3986 * A 1/2 second fade out animation.
3989 * @param el {HTMLElement} the element to animate
3990 * @param callback {Function} function to invoke when the animation is finished
3992 YAHOO.widget.TVFadeOut = function(el, callback) {
3994 * The element to animate
4001 * the callback to invoke when the animation is complete
4002 * @property callback
4005 this.callback = callback;
4009 YAHOO.widget.TVFadeOut.prototype = {
4011 * Performs the animation
4014 animate: function() {
4017 var a = new YAHOO.util.Anim(this.el, {opacity: {from: 1, to: 0.1, unit:""}}, dur);
4018 a.onComplete.subscribe( function() { tvanim.onComplete(); } );
4023 * Clean up and invoke callback
4024 * @method onComplete
4026 onComplete: function() {
4027 var s = this.el.style;
4030 s.filter = "alpha(opacity=100)";
4037 * @return {string} the string representation of the instance
4039 toString: function() {
4043 YAHOO.register("treeview", YAHOO.widget.TreeView, {version: "2.9.0", build: "2800"});