Make sure we don't call findTreeElement with the same representedObject
[WebKit-https.git] / WebCore / page / inspector / treeoutline.js
1 /*
2  * Copyright (C) 2007 Apple Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer. 
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution. 
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission. 
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 function TreeOutline(listNode)
30 {
31     this.children = [];
32     this.selectedTreeElement = null;
33     this._childrenListNode = listNode;
34     this._childrenListNode.removeChildren();
35     this._knownTreeElements = [];
36     this._treeElementsExpandedState = [];
37     this.expandTreeElementsWhenArrowing = false;
38     this.root = true;
39     this.hasChildren = false;
40     this.expanded = true;
41     this.selected = false;
42     this.treeOutline = this;
43 }
44
45 TreeOutline._knownTreeElementNextIdentifier = 1;
46
47 TreeOutline._appendChild = function(child)
48 {
49     if (!child)
50         throw("child can't be undefined or null");
51
52     var lastChild = this.children[this.children.length - 1];
53     if (lastChild) {
54         lastChild.nextSibling = child;
55         child.previousSibling = lastChild;
56     } else {
57         child.previousSibling = null;
58         child.nextSibling = null;
59     }
60
61     this.children.push(child);
62     this.hasChildren = true;
63     child.parent = this;
64     child.treeOutline = this.treeOutline;
65     child.treeOutline._rememberTreeElement(child);
66
67     var current = child.children[0];
68     while (current) {
69         current.treeOutline = this.treeOutline;
70         current.treeOutline._rememberTreeElement(current);
71         current = current.traverseNextTreeElement(false, child, true);
72     }
73
74     if (child.hasChildren && child.treeOutline._treeElementsExpandedState[child.identifier] !== undefined)
75         child.expanded = child.treeOutline._treeElementsExpandedState[child.identifier];
76
77     if (!this._childrenListNode) {
78         this._childrenListNode = this.treeOutline._childrenListNode.ownerDocument.createElement("ol");
79         this._childrenListNode.parentTreeElement = this;
80         this._childrenListNode.addStyleClass("children");
81         if (this.hidden)
82             this._childrenListNode.addStyleClass("hidden");
83     }
84
85     child._attach();
86 }
87
88 TreeOutline._insertChild = function(child, index)
89 {
90     if (!child)
91         throw("child can't be undefined or null");
92
93     var previousChild = (index > 0 ? this.children[index - 1] : null);
94     if (previousChild) {
95         previousChild.nextSibling = child;
96         child.previousSibling = previousChild;
97     } else {
98         child.previousSibling = null;
99     }
100
101     var nextChild = this.children[index];
102     if (nextChild) {
103         nextChild.previousSibling = child;
104         child.nextSibling = nextChild;
105     } else {
106         child.nextSibling = null;
107     }
108
109     this.children.splice(index, 0, child);
110     this.hasChildren = true;
111     child.parent = this;
112     child.treeOutline = this.treeOutline;
113     child.treeOutline._rememberTreeElement(child);
114
115     var current = child.children[0];
116     while (current) {
117         current.treeOutline = this.treeOutline;
118         current.treeOutline._rememberTreeElement(current);
119         current = current.traverseNextTreeElement(false, child, true);
120     }
121
122     if (child.hasChildren && child.treeOutline._treeElementsExpandedState[child.identifier] !== undefined)
123         child.expanded = child.treeOutline._treeElementsExpandedState[child.identifier];
124
125     if (!this._childrenListNode) {
126         this._childrenListNode = this.treeOutline._childrenListNode.ownerDocument.createElement("ol");
127         this._childrenListNode.parentTreeElement = this;
128         this._childrenListNode.addStyleClass("children");
129         if (this.hidden)
130             this._childrenListNode.addStyleClass("hidden");
131     }
132
133     child._attach();
134 }
135
136 TreeOutline._removeChild = function(child)
137 {
138     if (!child)
139         throw("child can't be undefined or null");
140
141     for (var i = 0; i < this.children.length; ++i) {
142         if (this.children[i] === child) {
143             this.children.splice(i, 1);
144             break;
145         }
146     }
147
148     child.deselect();
149
150     if (child.previousSibling)
151         child.previousSibling.nextSibling = child.nextSibling;
152     if (child.nextSibling)
153         child.nextSibling.previousSibling = child.previousSibling;
154
155     if (child.treeOutline)
156         child.treeOutline._forgetTreeElement(child);
157     child._detach();
158     child.treeOutline = null;
159     child.parent = null;
160     child.nextSibling = null;
161     child.previousSibling = null;
162 }
163
164 TreeOutline._removeChildren = function()
165 {
166     for (var i = 0; i < this.children.length; ++i) {
167         var child = this.children[i];
168         child.deselect();
169         if (child.treeOutline)
170             child.treeOutline._forgetTreeElement(child);
171         child._detach();
172         child.treeOutline = null;
173         child.parent = null;
174         child.nextSibling = null;
175         child.previousSibling = null;
176     }
177
178     this.children = [];
179
180     if (this._childrenListNode)
181         this._childrenListNode.offsetTop; // force layout
182 }
183
184 TreeOutline._removeChildrenRecursive = function()
185 {
186     var childrenToRemove = this.children;
187
188     var child = this.children[0];
189     while (child) {
190         if (child.children.length)
191             childrenToRemove = childrenToRemove.concat(child.children);
192         child = child.traverseNextTreeElement(false, this, true);
193     }
194
195     for (var i = 0; i < childrenToRemove.length; ++i) {
196         var child = childrenToRemove[i];
197         child.deselect();
198         if (child.treeOutline)
199             child.treeOutline._forgetTreeElement(child);
200         child._detach();
201         child.children = [];
202         child.treeOutline = null;
203         child.parent = null;
204         child.nextSibling = null;
205         child.previousSibling = null;
206     }
207
208     this.children = [];
209 }
210
211 TreeOutline.prototype._rememberTreeElement = function(element)
212 {
213     if (!this._knownTreeElements[element.identifier])
214         this._knownTreeElements[element.identifier] = [];
215
216     // check if the element is already known
217     var elements = this._knownTreeElements[element.identifier];
218     for (var i = 0; i < elements.length; ++i)
219         if (elements[i] === element)
220             return;
221
222     // add the element
223     elements.push(element);
224 }
225
226 TreeOutline.prototype._forgetTreeElement = function(element)
227 {
228     if (!this._knownTreeElements[element.identifier])
229         return;
230
231     var elements = this._knownTreeElements[element.identifier];
232     for (var i = 0; i < elements.length; ++i) {
233         if (elements[i] === element) {
234             elements.splice(i, 1);
235             break;
236         }
237     }
238 }
239
240 TreeOutline.prototype.findTreeElement = function(representedObject, isAncestor, getParent)
241 {
242     if (!representedObject)
243         return null;
244
245     if ("__treeElementIdentifier" in representedObject) {
246         // If this representedObject has a tree element identifier, and it is a known TreeElement
247         // in our tree we can just return that tree element.
248         var elements = this._knownTreeElements[representedObject.__treeElementIdentifier];
249         if (elements) {
250             for (var i = 0; i < elements.length; ++i)
251                 if (elements[i].representedObject === representedObject)
252                     return elements[i];
253         }
254     }
255
256     if (!isAncestor || !(isAncestor instanceof Function) || !getParent || !(getParent instanceof Function))
257         return null;
258
259     // The representedObject isn't know, so we start at the top of the tree and work down to find the first
260     // tree element that represents representedObject or one of its ancestors.
261     var item;
262     var found = false;
263     for (var i = 0; i < this.children.length; ++i) {
264         item = this.children[i];
265         if (item.representedObject === representedObject || isAncestor(item.representedObject, representedObject)) {
266             found = true;
267             break;
268         }
269     }
270
271     if (!found)
272         return null;
273
274     // Make sure the item that we found is connected to the root of the tree.
275     // Build up a list of representedObject's ancestors that aren't already in our tree.
276     var ancestors = [];
277     var currentObject = representedObject;
278     while (currentObject) {
279         ancestors.unshift(currentObject);
280         if (currentObject === item.representedObject)
281             break;
282         currentObject = getParent(currentObject);
283     }
284
285     // For each of those ancestors we populate them to fill in the tree.
286     for (var i = 0; i < ancestors.length; ++i) {
287         // Make sure we don't call findTreeElement with the same representedObject
288         // again, to prevent infinite recursion.
289         if (ancestors[i] === representedObject)
290             continue;
291         // FIXME: we could do something faster than findTreeElement since we will know the next
292         // ancestor exists in the tree.
293         item = this.findTreeElement(ancestors[i], isAncestor, getParent);
294         if (item && item.onpopulate)
295             item.onpopulate(item);
296     }
297
298     return item;
299 }
300
301 TreeOutline.prototype.treeElementFromPoint = function(x, y)
302 {
303     var node = this._childrenListNode.ownerDocument.elementFromPoint(x, y);
304     var listNode = node.enclosingNodeOrSelfWithNodeNameInArray(["ol", "li"]);
305     if (listNode)
306         return listNode.parentTreeElement || listNode.treeElement;
307     return null;
308 }
309
310 TreeOutline.prototype.handleKeyEvent = function(event)
311 {
312     if (!this.selectedTreeElement || event.shiftKey || event.metaKey || event.ctrlKey)
313         return false;
314
315     var handled = false;
316     var nextSelectedElement;
317     if (event.keyIdentifier === "Up" && !event.altKey) {
318         nextSelectedElement = this.selectedTreeElement.traversePreviousTreeElement(true);
319         while (nextSelectedElement && !nextSelectedElement.selectable)
320             nextSelectedElement = nextSelectedElement.traversePreviousTreeElement(!this.expandTreeElementsWhenArrowing);
321         handled = nextSelectedElement ? true : false;
322     } else if (event.keyIdentifier === "Down" && !event.altKey) {
323         nextSelectedElement = this.selectedTreeElement.traverseNextTreeElement(true);
324         while (nextSelectedElement && !nextSelectedElement.selectable)
325             nextSelectedElement = nextSelectedElement.traverseNextTreeElement(!this.expandTreeElementsWhenArrowing);
326         handled = nextSelectedElement ? true : false;
327     } else if (event.keyIdentifier === "Left") {
328         if (this.selectedTreeElement.expanded) {
329             if (event.altKey)
330                 this.selectedTreeElement.collapseRecursively();
331             else
332                 this.selectedTreeElement.collapse();
333             handled = true;
334         } else if (this.selectedTreeElement.parent && !this.selectedTreeElement.parent.root) {
335             handled = true;
336             if (this.selectedTreeElement.parent.selectable) {
337                 nextSelectedElement = this.selectedTreeElement.parent;
338                 handled = nextSelectedElement ? true : false;
339             } else if (this.selectedTreeElement.parent)
340                 this.selectedTreeElement.parent.collapse();
341         }
342     } else if (event.keyIdentifier === "Right") {
343         if (!this.selectedTreeElement.revealed()) {
344             this.selectedTreeElement.reveal();
345             handled = true;
346         } else if (this.selectedTreeElement.hasChildren) {
347             handled = true;
348             if (this.selectedTreeElement.expanded) {
349                 nextSelectedElement = this.selectedTreeElement.children[0];
350                 handled = nextSelectedElement ? true : false;
351             } else {
352                 if (event.altKey)
353                     this.selectedTreeElement.expandRecursively();
354                 else
355                     this.selectedTreeElement.expand();
356             }
357         }
358     }
359
360     if (nextSelectedElement) {
361         nextSelectedElement.reveal();
362         nextSelectedElement.select();
363     }
364
365     if (handled) {
366         event.preventDefault();
367         event.stopPropagation();
368     }
369
370     return handled;
371 }
372
373 TreeOutline.prototype.expand = function()
374 {
375     // this is the root, do nothing
376 }
377
378 TreeOutline.prototype.collapse = function()
379 {
380     // this is the root, do nothing
381 }
382
383 TreeOutline.prototype.revealed = function()
384 {
385     return true;
386 }
387
388 TreeOutline.prototype.reveal = function()
389 {
390     // this is the root, do nothing
391 }
392
393 TreeOutline.prototype.appendChild = TreeOutline._appendChild;
394 TreeOutline.prototype.insertChild = TreeOutline._insertChild;
395 TreeOutline.prototype.removeChild = TreeOutline._removeChild;
396 TreeOutline.prototype.removeChildren = TreeOutline._removeChildren;
397 TreeOutline.prototype.removeChildrenRecursive = TreeOutline._removeChildrenRecursive;
398
399 function TreeElement(title, representedObject, hasChildren)
400 {
401     this._title = title;
402     this.representedObject = (representedObject || {});
403
404     if (this.representedObject.__treeElementIdentifier)
405         this.identifier = this.representedObject.__treeElementIdentifier;
406     else {
407         this.identifier = TreeOutline._knownTreeElementNextIdentifier++;
408         this.representedObject.__treeElementIdentifier = this.identifier;
409     }
410
411     this._hidden = false;
412     this.expanded = false;
413     this.selected = false;
414     this.hasChildren = hasChildren;
415     this.children = [];
416     this.treeOutline = null;
417     this.parent = null;
418     this.previousSibling = null;
419     this.nextSibling = null;
420     this._listItemNode = null;
421 }
422
423 TreeElement.prototype = {
424     selectable: true,
425     arrowToggleWidth: 10,
426
427     get listItemElement() {
428         return this._listItemNode;
429     },
430
431     get childrenListElement() {
432         return this._childrenListNode;
433     },
434
435     get title() {
436         return this._title;
437     },
438
439     set title(x) {
440         this._title = x;
441         if (this._listItemNode)
442             this._listItemNode.innerHTML = x;
443     },
444
445     get tooltip() {
446         return this._tooltip;
447     },
448
449     set tooltip(x) {
450         this._tooltip = x;
451         if (this._listItemNode)
452             this._listItemNode.title = x ? x : "";
453     },
454
455     get hidden() {
456         return this._hidden;
457     },
458
459     set hidden(x) {
460         if (this._hidden === x)
461             return;
462
463         this._hidden = x;
464
465         if (x) {
466             if (this._listItemNode)
467                 this._listItemNode.addStyleClass("hidden");
468             if (this._childrenListNode)
469                 this._childrenListNode.addStyleClass("hidden");
470         } else {
471             if (this._listItemNode)
472                 this._listItemNode.removeStyleClass("hidden");
473             if (this._childrenListNode)
474                 this._childrenListNode.removeStyleClass("hidden");
475         }
476     },
477
478     get shouldRefreshChildren() {
479         return this._shouldRefreshChildren;
480     },
481
482     set shouldRefreshChildren(x) {
483         this._shouldRefreshChildren = x;
484         if (x && this.expanded)
485             this.expand();
486     }
487 }
488
489 TreeElement.prototype.appendChild = TreeOutline._appendChild;
490 TreeElement.prototype.insertChild = TreeOutline._insertChild;
491 TreeElement.prototype.removeChild = TreeOutline._removeChild;
492 TreeElement.prototype.removeChildren = TreeOutline._removeChildren;
493 TreeElement.prototype.removeChildrenRecursive = TreeOutline._removeChildrenRecursive;
494
495 TreeElement.prototype._attach = function()
496 {
497     if (!this._listItemNode || this.parent._shouldRefreshChildren) {
498         if (this._listItemNode && this._listItemNode.parentNode)
499             this._listItemNode.parentNode.removeChild(this._listItemNode);
500
501         this._listItemNode = this.treeOutline._childrenListNode.ownerDocument.createElement("li");
502         this._listItemNode.treeElement = this;
503         this._listItemNode.innerHTML = this._title;
504         this._listItemNode.title = this._tooltip ? this._tooltip : "";
505
506         if (this.hidden)
507             this._listItemNode.addStyleClass("hidden");
508         if (this.hasChildren)
509             this._listItemNode.addStyleClass("parent");
510         if (this.expanded)
511             this._listItemNode.addStyleClass("expanded");
512         if (this.selected)
513             this._listItemNode.addStyleClass("selected");
514
515         this._listItemNode.addEventListener("mousedown", TreeElement.treeElementSelected, false);
516         this._listItemNode.addEventListener("click", TreeElement.treeElementToggled, false);
517         this._listItemNode.addEventListener("dblclick", TreeElement.treeElementDoubleClicked, false);
518
519         if (this.onattach)
520             this.onattach(this);
521     }
522
523     this.parent._childrenListNode.insertBefore(this._listItemNode, (this.nextSibling ? this.nextSibling._listItemNode : null));
524     if (this._childrenListNode)
525         this.parent._childrenListNode.insertBefore(this._childrenListNode, this._listItemNode.nextSibling);
526     if (this.selected)
527         this.select();
528     if (this.expanded)
529         this.expand();
530 }
531
532 TreeElement.prototype._detach = function()
533 {
534     if (this._listItemNode && this._listItemNode.parentNode)
535         this._listItemNode.parentNode.removeChild(this._listItemNode);
536     if (this._childrenListNode && this._childrenListNode.parentNode)
537         this._childrenListNode.parentNode.removeChild(this._childrenListNode);
538 }
539
540 TreeElement.treeElementSelected = function(event)
541 {
542     var element = event.currentTarget;
543     if (!element || !element.treeElement || !element.treeElement.selectable)
544         return;
545
546     if (element.treeElement.isEventWithinDisclosureTriangle(event))
547         return;
548
549     element.treeElement.select();
550 }
551
552 TreeElement.treeElementToggled = function(event)
553 {
554     var element = event.currentTarget;
555     if (!element || !element.treeElement)
556         return;
557
558     if (!element.treeElement.isEventWithinDisclosureTriangle(event))
559         return;
560
561     if (element.treeElement.expanded) {
562         if (event.altKey)
563             element.treeElement.collapseRecursively();
564         else
565             element.treeElement.collapse();
566     } else {
567         if (event.altKey)
568             element.treeElement.expandRecursively();
569         else
570             element.treeElement.expand();
571     }
572 }
573
574 TreeElement.treeElementDoubleClicked = function(event)
575 {
576     var element = event.currentTarget;
577     if (!element || !element.treeElement)
578         return;
579
580     if (element.treeElement.ondblclick)
581         element.treeElement.ondblclick(element.treeElement, event);
582     else if (element.treeElement.hasChildren && !element.treeElement.expanded)
583         element.treeElement.expand();
584 }
585
586 TreeElement.prototype.collapse = function()
587 {
588     if (this._listItemNode)
589         this._listItemNode.removeStyleClass("expanded");
590     if (this._childrenListNode)
591         this._childrenListNode.removeStyleClass("expanded");
592
593     this.expanded = false;
594     if (this.treeOutline)
595         this.treeOutline._treeElementsExpandedState[this.identifier] = true;
596
597     if (this.oncollapse)
598         this.oncollapse(this);
599 }
600
601 TreeElement.prototype.collapseRecursively = function()
602 {
603     var item = this;
604     while (item) {
605         if (item.expanded)
606             item.collapse();
607         item = item.traverseNextTreeElement(false, this, true);
608     }
609 }
610
611 TreeElement.prototype.expand = function()
612 {
613     if (!this.hasChildren || (this.expanded && !this._shouldRefreshChildren && this._childrenListNode))
614         return;
615
616     if (!this._childrenListNode || this._shouldRefreshChildren) {
617         if (this._childrenListNode && this._childrenListNode.parentNode)
618             this._childrenListNode.parentNode.removeChild(this._childrenListNode);
619
620         this._childrenListNode = this.treeOutline._childrenListNode.ownerDocument.createElement("ol");
621         this._childrenListNode.parentTreeElement = this;
622         this._childrenListNode.addStyleClass("children");
623
624         if (this.hidden)
625             this._childrenListNode.addStyleClass("hidden");
626
627         if (this.onpopulate)
628             this.onpopulate(this);
629
630         for (var i = 0; i < this.children.length; ++i)
631             this.children[i]._attach();
632
633         delete this._shouldRefreshChildren;
634     }
635
636     if (this._listItemNode) {
637         this._listItemNode.addStyleClass("expanded");
638         if (this._childrenListNode.parentNode != this._listItemNode.parentNode)
639             this.parent._childrenListNode.insertBefore(this._childrenListNode, this._listItemNode.nextSibling);
640     }
641
642     if (this._childrenListNode)
643         this._childrenListNode.addStyleClass("expanded");
644
645     this.expanded = true;
646     if (this.treeOutline)
647         this.treeOutline._treeElementsExpandedState[this.identifier] = true;
648
649     if (this.onexpand)
650         this.onexpand(this);
651 }
652
653 TreeElement.prototype.expandRecursively = function(maxDepth)
654 {
655     var item = this;
656     var info = {};
657     var depth = 0;
658
659     // The Inspector uses TreeOutlines to represents object properties, so recursive expansion
660     // in some case can be infinite, since JavaScript objects can hold circular references.
661     // So default to a recursion cap of 3 levels, since that gives fairly good results.
662     if (typeof maxDepth === "undefined" || typeof maxDepth === "null")
663         maxDepth = 3;
664
665     while (item) {
666         if (depth < maxDepth)
667             item.expand();
668         item = item.traverseNextTreeElement(false, this, (depth >= maxDepth), info);
669         depth += info.depthChange;
670     }
671 }
672
673 TreeElement.prototype.reveal = function()
674 {
675     var currentAncestor = this.parent;
676     while (currentAncestor && !currentAncestor.root) {
677         if (!currentAncestor.expanded)
678             currentAncestor.expand();
679         currentAncestor = currentAncestor.parent;
680     }
681
682     if (this.onreveal)
683         this.onreveal(this);
684 }
685
686 TreeElement.prototype.revealed = function()
687 {
688     var currentAncestor = this.parent;
689     while (currentAncestor && !currentAncestor.root) {
690         if (!currentAncestor.expanded)
691             return false;
692         currentAncestor = currentAncestor.parent;
693     }
694
695     return true;
696 }
697
698 TreeElement.prototype.select = function(supressOnSelect)
699 {
700     if (!this.treeOutline || !this.selectable || this.selected)
701         return;
702
703     if (this.treeOutline.selectedTreeElement)
704         this.treeOutline.selectedTreeElement.deselect();
705
706     this.selected = true;
707     this.treeOutline.selectedTreeElement = this;
708     if (this._listItemNode)
709         this._listItemNode.addStyleClass("selected");
710
711     if (this.onselect && !supressOnSelect)
712         this.onselect(this);
713 }
714
715 TreeElement.prototype.deselect = function(supressOnDeselect)
716 {
717     if (!this.treeOutline || this.treeOutline.selectedTreeElement !== this || !this.selected)
718         return;
719
720     this.selected = false;
721     this.treeOutline.selectedTreeElement = null;
722     if (this._listItemNode)
723         this._listItemNode.removeStyleClass("selected");
724
725     if (this.ondeselect && !supressOnDeselect)
726         this.ondeselect(this);
727 }
728
729 TreeElement.prototype.traverseNextTreeElement = function(skipHidden, stayWithin, dontPopulate, info)
730 {
731     if (!dontPopulate && this.hasChildren && this.onpopulate)
732         this.onpopulate(this);
733
734     if (info)
735         info.depthChange = 0;
736
737     var element = skipHidden ? (this.revealed() ? this.children[0] : null) : this.children[0];
738     if (element && (!skipHidden || (skipHidden && this.expanded))) {
739         if (info)
740             info.depthChange = 1;
741         return element;
742     }
743
744     if (this === stayWithin)
745         return null;
746
747     element = skipHidden ? (this.revealed() ? this.nextSibling : null) : this.nextSibling;
748     if (element)
749         return element;
750
751     element = this;
752     while (element && !element.root && !(skipHidden ? (element.revealed() ? element.nextSibling : null) : element.nextSibling) && element.parent !== stayWithin) {
753         if (info)
754             info.depthChange -= 1;
755         element = element.parent;
756     }
757
758     if (!element)
759         return null;
760
761     return (skipHidden ? (element.revealed() ? element.nextSibling : null) : element.nextSibling);
762 }
763
764 TreeElement.prototype.traversePreviousTreeElement = function(skipHidden, dontPopulate)
765 {
766     var element = skipHidden ? (this.revealed() ? this.previousSibling : null) : this.previousSibling;
767     if (!dontPopulate && element && element.hasChildren && element.onpopulate)
768         element.onpopulate(element);
769
770     while (element && (skipHidden ? (element.revealed() && element.expanded ? element.children[element.children.length - 1] : null) : element.children[element.children.length - 1])) {
771         if (!dontPopulate && element.hasChildren && element.onpopulate)
772             element.onpopulate(element);
773         element = (skipHidden ? (element.revealed() && element.expanded ? element.children[element.children.length - 1] : null) : element.children[element.children.length - 1]);
774     }
775
776     if (element)
777         return element;
778
779     if (!this.parent || this.parent.root)
780         return null;
781
782     return this.parent;
783 }
784
785 TreeElement.prototype.isEventWithinDisclosureTriangle = function(event)
786 {
787     var left = this._listItemNode.totalOffsetLeft;
788     return event.pageX >= left && event.pageX <= left + this.arrowToggleWidth && this.hasChildren;
789 }