initial import
[vuplus_webkit] / Source / WebCore / inspector / front-end / Section.js
1 /*
2  * Copyright (C) 2007 Apple Inc.  All rights reserved.
3  * Copyright (C) 2009 Google Inc.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 WebInspector.Section = function(title, subtitle)
31 {
32     this.element = document.createElement("div");
33     this.element.className = "section";
34     this.element._section = this;
35
36     this.headerElement = document.createElement("div");
37     this.headerElement.className = "header";
38
39     this.titleElement = document.createElement("div");
40     this.titleElement.className = "title";
41
42     this.subtitleElement = document.createElement("div");
43     this.subtitleElement.className = "subtitle";
44
45     this.headerElement.appendChild(this.subtitleElement);
46     this.headerElement.appendChild(this.titleElement);
47
48     this.headerElement.addEventListener("click", this.handleClick.bind(this), false);
49     this.element.appendChild(this.headerElement);
50
51     this.title = title;
52     this.subtitle = subtitle;
53     this._expanded = false;
54 }
55
56 WebInspector.Section.prototype = {
57     get title()
58     {
59         return this._title;
60     },
61
62     set title(x)
63     {
64         if (this._title === x)
65             return;
66         this._title = x;
67
68         if (x instanceof Node) {
69             this.titleElement.removeChildren();
70             this.titleElement.appendChild(x);
71         } else
72           this.titleElement.textContent = x;
73     },
74
75     get subtitle()
76     {
77         return this._subtitle;
78     },
79
80     set subtitle(x)
81     {
82         if (this._subtitle === x)
83             return;
84         this._subtitle = x;
85         this.subtitleElement.textContent = x;
86     },
87
88     get subtitleAsTextForTest()
89     {
90         var result = this.subtitleElement.textContent;
91         var child = this.subtitleElement.querySelector("[data-uncopyable]");
92         if (child) {
93             var linkData = child.getAttribute("data-uncopyable");
94             if (linkData)
95                 result += linkData;
96         }
97         return result;
98     },
99
100     get expanded()
101     {
102         return this._expanded;
103     },
104
105     set expanded(x)
106     {
107         if (x)
108             this.expand();
109         else
110             this.collapse();
111     },
112
113     get populated()
114     {
115         return this._populated;
116     },
117
118     set populated(x)
119     {
120         this._populated = x;
121         if (!x && this.onpopulate && this._expanded) {
122             this.onpopulate(this);
123             this._populated = true;
124         }
125     },
126
127     get firstSibling()
128     {
129         var parent = this.element.parentElement;
130         if (!parent)
131             return null;
132
133         var childElement = parent.firstChild;
134         while (childElement) {
135             if (childElement._section)
136                 return childElement._section;
137             childElement = childElement.nextSibling;
138         }
139
140         return null;
141     },
142
143     get lastSibling()
144     {
145         var parent = this.element.parentElement;
146         if (!parent)
147             return null;
148
149         var childElement = parent.lastChild;
150         while (childElement) {
151             if (childElement._section)
152                 return childElement._section;
153             childElement = childElement.previousSibling;
154         }
155
156         return null;
157     },
158
159     get nextSibling()
160     {
161         var curElement = this.element;
162         do {
163             curElement = curElement.nextSibling;
164         } while (curElement && !curElement._section);
165
166         return curElement ? curElement._section : null;
167     },
168
169     get previousSibling()
170     {
171         var curElement = this.element;
172         do {
173             curElement = curElement.previousSibling;
174         } while (curElement && !curElement._section);
175
176         return curElement ? curElement._section : null;
177     },
178
179     expand: function()
180     {
181         if (this._expanded)
182             return;
183         this._expanded = true;
184         this.element.addStyleClass("expanded");
185
186         if (!this._populated && this.onpopulate) {
187             this.onpopulate(this);
188             this._populated = true;
189         }
190     },
191
192     collapse: function()
193     {
194         if (!this._expanded)
195             return;
196         this._expanded = false;
197         this.element.removeStyleClass("expanded");
198     },
199
200     toggleExpanded: function()
201     {
202         this.expanded = !this.expanded;
203     },
204
205     handleClick: function(e)
206     {
207         this.toggleExpanded();
208         e.stopPropagation();
209     }
210 }