Makes setting shouldRefreshChildren on a TreeElement perform the refresh
[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         var elements = this._knownTreeElements[representedObject.__treeElementIdentifier];
247         if (elements) {
248             for (var i = 0; i < elements.length; ++i)
249                 if (elements[i].representedObject === representedObject)
250                     return elements[i];
251         }
252     }
253
254     if (!isAncestor || !(isAncestor instanceof Function) || !getParent || !(getParent instanceof Function))
255         return null;
256
257     var item;
258     var found = false;
259     for (var i = 0; i < this.children.length; ++i) {
260         item = this.children[i];
261         if (item.representedObject === representedObject || isAncestor(item.representedObject, representedObject)) {
262             found = true;
263             break;
264         }
265     }
266
267     if (!found)
268         return null;
269
270     var ancestors = [];
271     var currentObject = representedObject;
272     while (currentObject) {
273         ancestors.unshift(currentObject);
274         if (currentObject === item.representedObject)
275             break;
276         currentObject = getParent(currentObject);
277     }
278
279     for (var i = 0; i < ancestors.length; ++i) {
280         item = this.findTreeElement(ancestors[i], isAncestor, getParent);
281         if (ancestors[i] !== representedObject && item && item.onpopulate)
282             item.onpopulate(item);
283     }
284
285     return item;
286 }
287
288 TreeOutline.prototype.treeElementFromPoint = function(x, y)
289 {
290     var node = this._childrenListNode.ownerDocument.elementFromPoint(x, y);
291     var listNode = node.enclosingNodeOrSelfWithNodeNameInArray(["ol", "li"]);
292     if (listNode)
293         return listNode.parentTreeElement || listNode.treeElement;
294     return null;
295 }
296
297 TreeOutline.prototype.handleKeyEvent = function(event)
298 {
299     if (!this.selectedTreeElement || event.shiftKey || event.metaKey || event.ctrlKey)
300         return false;
301
302     var handled = false;
303     var nextSelectedElement;
304     if (event.keyIdentifier === "Up" && !event.altKey) {
305         nextSelectedElement = this.selectedTreeElement.traversePreviousTreeElement(true);
306         while (nextSelectedElement && !nextSelectedElement.selectable)
307             nextSelectedElement = nextSelectedElement.traversePreviousTreeElement(!this.expandTreeElementsWhenArrowing);
308         handled = nextSelectedElement ? true : false;
309     } else if (event.keyIdentifier === "Down" && !event.altKey) {
310         nextSelectedElement = this.selectedTreeElement.traverseNextTreeElement(true);
311         while (nextSelectedElement && !nextSelectedElement.selectable)
312             nextSelectedElement = nextSelectedElement.traverseNextTreeElement(!this.expandTreeElementsWhenArrowing);
313         handled = nextSelectedElement ? true : false;
314     } else if (event.keyIdentifier === "Left") {
315         if (this.selectedTreeElement.expanded) {
316             if (event.altKey)
317                 this.selectedTreeElement.collapseRecursively();
318             else
319                 this.selectedTreeElement.collapse();
320             handled = true;
321         } else if (this.selectedTreeElement.parent && !this.selectedTreeElement.parent.root) {
322             handled = true;
323             if (this.selectedTreeElement.parent.selectable) {
324                 nextSelectedElement = this.selectedTreeElement.parent;
325                 handled = nextSelectedElement ? true : false;
326             } else if (this.selectedTreeElement.parent)
327                 this.selectedTreeElement.parent.collapse();
328         }
329     } else if (event.keyIdentifier === "Right") {
330         if (!this.selectedTreeElement.revealed()) {
331             this.selectedTreeElement.reveal();
332             handled = true;
333         } else if (this.selectedTreeElement.hasChildren) {
334             handled = true;
335             if (this.selectedTreeElement.expanded) {
336                 nextSelectedElement = this.selectedTreeElement.children[0];
337                 handled = nextSelectedElement ? true : false;
338             } else {
339                 if (event.altKey)
340                     this.selectedTreeElement.expandRecursively();
341                 else
342                     this.selectedTreeElement.expand();
343             }
344         }
345     }
346
347     if (nextSelectedElement) {
348         nextSelectedElement.reveal();
349         nextSelectedElement.select();
350     }
351
352     if (handled) {
353         event.preventDefault();
354         event.stopPropagation();
355     }
356
357     return handled;
358 }
359
360 TreeOutline.prototype.expand = function()
361 {
362     // this is the root, do nothing
363 }
364
365 TreeOutline.prototype.collapse = function()
366 {
367     // this is the root, do nothing
368 }
369
370 TreeOutline.prototype.revealed = function()
371 {
372     return true;
373 }
374
375 TreeOutline.prototype.reveal = function()
376 {
377     // this is the root, do nothing
378 }
379
380 TreeOutline.prototype.appendChild = TreeOutline._appendChild;
381 TreeOutline.prototype.insertChild = TreeOutline._insertChild;
382 TreeOutline.prototype.removeChild = TreeOutline._removeChild;
383 TreeOutline.prototype.removeChildren = TreeOutline._removeChildren;
384 TreeOutline.prototype.removeChildrenRecursive = TreeOutline._removeChildrenRecursive;
385
386 function TreeElement(title, representedObject, hasChildren)
387 {
388     this._title = title;
389     this.representedObject = (representedObject || {});
390
391     if (this.representedObject.__treeElementIdentifier)
392         this.identifier = this.representedObject.__treeElementIdentifier;
393     else {
394         this.identifier = TreeOutline._knownTreeElementNextIdentifier++;
395         this.representedObject.__treeElementIdentifier = this.identifier;
396     }
397
398     this._hidden = false;
399     this.expanded = false;
400     this.selected = false;
401     this.hasChildren = hasChildren;
402     this.children = [];
403     this.treeOutline = null;
404     this.parent = null;
405     this.previousSibling = null;
406     this.nextSibling = null;
407     this._listItemNode = null;
408 }
409
410 TreeElement.prototype = {
411     selectable: true,
412     arrowToggleWidth: 10,
413
414     get listItemElement() {
415         return this._listItemNode;
416     },
417
418     get childrenListElement() {
419         return this._childrenListNode;
420     },
421
422     get title() {
423         return this._title;
424     },
425
426     set title(x) {
427         this._title = x;
428         if (this._listItemNode)
429             this._listItemNode.innerHTML = x;
430     },
431
432     get tooltip() {
433         return this._tooltip;
434     },
435
436     set tooltip(x) {
437         this._tooltip = x;
438         if (this._listItemNode)
439             this._listItemNode.title = x ? x : "";
440     },
441
442     get hidden() {
443         return this._hidden;
444     },
445
446     set hidden(x) {
447         if (this._hidden === x)
448             return;
449
450         this._hidden = x;
451
452         if (x) {
453             if (this._listItemNode)
454                 this._listItemNode.addStyleClass("hidden");
455             if (this._childrenListNode)
456                 this._childrenListNode.addStyleClass("hidden");
457         } else {
458             if (this._listItemNode)
459                 this._listItemNode.removeStyleClass("hidden");
460             if (this._childrenListNode)
461                 this._childrenListNode.removeStyleClass("hidden");
462         }
463     },
464
465     get shouldRefreshChildren() {
466         return this._shouldRefreshChildren;
467     },
468
469     set shouldRefreshChildren(x) {
470         this._shouldRefreshChildren = x;
471         if (x && this.expanded)
472             this.expand();
473     }
474 }
475
476 TreeElement.prototype.appendChild = TreeOutline._appendChild;
477 TreeElement.prototype.insertChild = TreeOutline._insertChild;
478 TreeElement.prototype.removeChild = TreeOutline._removeChild;
479 TreeElement.prototype.removeChildren = TreeOutline._removeChildren;
480 TreeElement.prototype.removeChildrenRecursive = TreeOutline._removeChildrenRecursive;
481
482 TreeElement.prototype._attach = function()
483 {
484     if (!this._listItemNode || this.parent._shouldRefreshChildren) {
485         if (this._listItemNode && this._listItemNode.parentNode)
486             this._listItemNode.parentNode.removeChild(this._listItemNode);
487
488         this._listItemNode = this.treeOutline._childrenListNode.ownerDocument.createElement("li");
489         this._listItemNode.treeElement = this;
490         this._listItemNode.innerHTML = this._title;
491         this._listItemNode.title = this._tooltip ? this._tooltip : "";
492
493         if (this.hidden)
494             this._listItemNode.addStyleClass("hidden");
495         if (this.hasChildren)
496             this._listItemNode.addStyleClass("parent");
497         if (this.expanded)
498             this._listItemNode.addStyleClass("expanded");
499         if (this.selected)
500             this._listItemNode.addStyleClass("selected");
501
502         this._listItemNode.addEventListener("mousedown", TreeElement.treeElementSelected, false);
503         this._listItemNode.addEventListener("click", TreeElement.treeElementToggled, false);
504         this._listItemNode.addEventListener("dblclick", TreeElement.treeElementDoubleClicked, false);
505
506         if (this.onattach)
507             this.onattach(this);
508     }
509
510     this.parent._childrenListNode.insertBefore(this._listItemNode, (this.nextSibling ? this.nextSibling._listItemNode : null));
511     if (this._childrenListNode)
512         this.parent._childrenListNode.insertBefore(this._childrenListNode, this._listItemNode.nextSibling);
513     if (this.selected)
514         this.select();
515     if (this.expanded)
516         this.expand();
517 }
518
519 TreeElement.prototype._detach = function()
520 {
521     if (this._listItemNode && this._listItemNode.parentNode)
522         this._listItemNode.parentNode.removeChild(this._listItemNode);
523     if (this._childrenListNode && this._childrenListNode.parentNode)
524         this._childrenListNode.parentNode.removeChild(this._childrenListNode);
525 }
526
527 TreeElement.treeElementSelected = function(event)
528 {
529     var element = event.currentTarget;
530     if (!element || !element.treeElement || !element.treeElement.selectable)
531         return;
532
533     if (element.treeElement.isEventWithinDisclosureTriangle(event))
534         return;
535
536     element.treeElement.select();
537 }
538
539 TreeElement.treeElementToggled = function(event)
540 {
541     var element = event.currentTarget;
542     if (!element || !element.treeElement)
543         return;
544
545     if (!element.treeElement.isEventWithinDisclosureTriangle(event))
546         return;
547
548     if (element.treeElement.expanded) {
549         if (event.altKey)
550             element.treeElement.collapseRecursively();
551         else
552             element.treeElement.collapse();
553     } else {
554         if (event.altKey)
555             element.treeElement.expandRecursively();
556         else
557             element.treeElement.expand();
558     }
559 }
560
561 TreeElement.treeElementDoubleClicked = function(event)
562 {
563     var element = event.currentTarget;
564     if (!element || !element.treeElement)
565         return;
566
567     if (element.treeElement.ondblclick)
568         element.treeElement.ondblclick(element.treeElement, event);
569     else if (element.treeElement.hasChildren && !element.treeElement.expanded)
570         element.treeElement.expand();
571 }
572
573 TreeElement.prototype.collapse = function()
574 {
575     if (this._listItemNode)
576         this._listItemNode.removeStyleClass("expanded");
577     if (this._childrenListNode)
578         this._childrenListNode.removeStyleClass("expanded");
579
580     this.expanded = false;
581     if (this.treeOutline)
582         this.treeOutline._treeElementsExpandedState[this.identifier] = true;
583
584     if (this.oncollapse)
585         this.oncollapse(this);
586 }
587
588 TreeElement.prototype.collapseRecursively = function()
589 {
590     var item = this;
591     while (item) {
592         if (item.expanded)
593             item.collapse();
594         item = item.traverseNextTreeElement(false, this, true);
595     }
596 }
597
598 TreeElement.prototype.expand = function()
599 {
600     if (!this.hasChildren || (this.expanded && !this._shouldRefreshChildren && this._childrenListNode))
601         return;
602
603     if (!this._childrenListNode || this._shouldRefreshChildren) {
604         if (this._childrenListNode && this._childrenListNode.parentNode)
605             this._childrenListNode.parentNode.removeChild(this._childrenListNode);
606
607         this._childrenListNode = this.treeOutline._childrenListNode.ownerDocument.createElement("ol");
608         this._childrenListNode.parentTreeElement = this;
609         this._childrenListNode.addStyleClass("children");
610
611         if (this.hidden)
612             this._childrenListNode.addStyleClass("hidden");
613
614         if (this.onpopulate)
615             this.onpopulate(this);
616
617         for (var i = 0; i < this.children.length; ++i)
618             this.children[i]._attach();
619
620         delete this._shouldRefreshChildren;
621     }
622
623     if (this._listItemNode) {
624         this._listItemNode.addStyleClass("expanded");
625         if (this._childrenListNode.parentNode != this._listItemNode.parentNode)
626             this.parent._childrenListNode.insertBefore(this._childrenListNode, this._listItemNode.nextSibling);
627     }
628
629     if (this._childrenListNode)
630         this._childrenListNode.addStyleClass("expanded");
631
632     this.expanded = true;
633     if (this.treeOutline)
634         this.treeOutline._treeElementsExpandedState[this.identifier] = true;
635
636     if (this.onexpand)
637         this.onexpand(this);
638 }
639
640 TreeElement.prototype.expandRecursively = function()
641 {
642     var item = this;
643     while (item) {
644         item.expand();
645         item = item.traverseNextTreeElement(false, this);
646     }
647 }
648
649 TreeElement.prototype.reveal = function()
650 {
651     var currentAncestor = this.parent;
652     while (currentAncestor && !currentAncestor.root) {
653         if (!currentAncestor.expanded)
654             currentAncestor.expand();
655         currentAncestor = currentAncestor.parent;
656     }
657
658     if (this.onreveal)
659         this.onreveal(this);
660 }
661
662 TreeElement.prototype.revealed = function()
663 {
664     var currentAncestor = this.parent;
665     while (currentAncestor && !currentAncestor.root) {
666         if (!currentAncestor.expanded)
667             return false;
668         currentAncestor = currentAncestor.parent;
669     }
670
671     return true;
672 }
673
674 TreeElement.prototype.select = function(supressOnSelect)
675 {
676     if (!this.treeOutline || !this.selectable || this.selected)
677         return;
678
679     if (this.treeOutline.selectedTreeElement)
680         this.treeOutline.selectedTreeElement.deselect();
681
682     this.selected = true;
683     this.treeOutline.selectedTreeElement = this;
684     if (this._listItemNode)
685         this._listItemNode.addStyleClass("selected");
686
687     if (this.onselect && !supressOnSelect)
688         this.onselect(this);
689 }
690
691 TreeElement.prototype.deselect = function(supressOnDeselect)
692 {
693     if (!this.treeOutline || this.treeOutline.selectedTreeElement !== this || !this.selected)
694         return;
695
696     this.selected = false;
697     this.treeOutline.selectedTreeElement = null;
698     if (this._listItemNode)
699         this._listItemNode.removeStyleClass("selected");
700
701     if (this.ondeselect && !supressOnDeselect)
702         this.ondeselect(this);
703 }
704
705 TreeElement.prototype.traverseNextTreeElement = function(skipHidden, stayWithin, dontPopulate)
706 {
707     if (!dontPopulate && this.hasChildren && this.onpopulate)
708         this.onpopulate(this);
709
710     var element = skipHidden ? (this.revealed() ? this.children[0] : null) : this.children[0];
711     if (element && (!skipHidden || (skipHidden && this.expanded)))
712         return element;
713
714     if (this === stayWithin)
715         return null;
716
717     element = skipHidden ? (this.revealed() ? this.nextSibling : null) : this.nextSibling;
718     if (element)
719         return element;
720
721     element = this;
722     while (element && !element.root && !(skipHidden ? (element.revealed() ? element.nextSibling : null) : element.nextSibling) && element.parent !== stayWithin)
723         element = element.parent;
724
725     if (!element)
726         return null;
727
728     return (skipHidden ? (element.revealed() ? element.nextSibling : null) : element.nextSibling);
729 }
730
731 TreeElement.prototype.traversePreviousTreeElement = function(skipHidden, dontPopulate)
732 {
733     var element = skipHidden ? (this.revealed() ? this.previousSibling : null) : this.previousSibling;
734     if (!dontPopulate && element && element.hasChildren && element.onpopulate)
735         element.onpopulate(element);
736
737     while (element && (skipHidden ? (element.revealed() && element.expanded ? element.children[element.children.length - 1] : null) : element.children[element.children.length - 1])) {
738         if (!dontPopulate && element.hasChildren && element.onpopulate)
739             element.onpopulate(element);
740         element = (skipHidden ? (element.revealed() && element.expanded ? element.children[element.children.length - 1] : null) : element.children[element.children.length - 1]);
741     }
742
743     if (element)
744         return element;
745
746     if (!this.parent || this.parent.root)
747         return null;
748
749     return this.parent;
750 }
751
752 TreeElement.prototype.isEventWithinDisclosureTriangle = function(event)
753 {
754     var left = this._listItemNode.totalOffsetLeft;
755     return event.pageX >= left && event.pageX <= left + this.arrowToggleWidth && this.hasChildren;
756 }