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