Web Inspector: Support passing extra arguments to ContentViews during construction
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / ContentView.js
1 /*
2  * Copyright (C) 2013 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  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 WebInspector.ContentView = function(representedObject, extraArguments)
27 {
28     if (this.constructor === WebInspector.ContentView) {
29         // When instantiated directly return an instance of a type-based concrete subclass.
30
31         console.assert(representedObject);
32
33         if (representedObject instanceof WebInspector.Frame)
34             return new WebInspector.FrameContentView(representedObject, extraArguments);
35
36         if (representedObject instanceof WebInspector.Resource)
37             return new WebInspector.ResourceClusterContentView(representedObject, extraArguments);
38
39         if (representedObject instanceof WebInspector.Script)
40             return new WebInspector.ScriptContentView(representedObject, extraArguments);
41
42         if (representedObject instanceof WebInspector.TimelineRecording)
43             return new WebInspector.TimelineRecordingContentView(representedObject, extraArguments);
44
45         if (representedObject instanceof WebInspector.Timeline) {
46             var timelineType = representedObject.type;
47             if (timelineType === WebInspector.TimelineRecord.Type.Network)
48                 return new WebInspector.NetworkTimelineView(representedObject, extraArguments);
49
50             if (timelineType === WebInspector.TimelineRecord.Type.Layout)
51                 return new WebInspector.LayoutTimelineView(representedObject, extraArguments);
52
53             if (timelineType === WebInspector.TimelineRecord.Type.Script)
54                 return new WebInspector.ScriptTimelineView(representedObject, extraArguments);
55
56             if (timelineType === WebInspector.TimelineRecord.Type.RenderingFrame)
57                 return new WebInspector.RenderingFrameTimelineView(representedObject, extraArguments);
58         }
59
60         if (representedObject instanceof WebInspector.DOMStorageObject)
61             return new WebInspector.DOMStorageContentView(representedObject, extraArguments);
62
63         if (representedObject instanceof WebInspector.CookieStorageObject)
64             return new WebInspector.CookieStorageContentView(representedObject, extraArguments);
65
66         if (representedObject instanceof WebInspector.DatabaseTableObject)
67             return new WebInspector.DatabaseTableContentView(representedObject, extraArguments);
68
69         if (representedObject instanceof WebInspector.DatabaseObject)
70             return new WebInspector.DatabaseContentView(representedObject, extraArguments);
71
72         if (representedObject instanceof WebInspector.IndexedDatabaseObjectStore)
73             return new WebInspector.IndexedDatabaseObjectStoreContentView(representedObject, extraArguments);
74
75         if (representedObject instanceof WebInspector.IndexedDatabaseObjectStoreIndex)
76             return new WebInspector.IndexedDatabaseObjectStoreContentView(representedObject, extraArguments);
77
78         if (representedObject instanceof WebInspector.ApplicationCacheFrame)
79             return new WebInspector.ApplicationCacheFrameContentView(representedObject, extraArguments);
80
81         if (representedObject instanceof WebInspector.DOMTree)
82             return new WebInspector.FrameDOMTreeContentView(representedObject, extraArguments);
83
84         if (representedObject instanceof WebInspector.LogObject)
85             return new WebInspector.LogContentView(representedObject, extraArguments);
86
87         if (representedObject instanceof WebInspector.ContentFlow)
88             return new WebInspector.ContentFlowDOMTreeContentView(representedObject, extraArguments);
89
90         if (typeof representedObject === "string" || representedObject instanceof String)
91             return new WebInspector.TextContentView(representedObject, extraArguments);
92
93         console.assert(!WebInspector.ContentView.isViewable(representedObject));
94
95         throw "Can't make a ContentView for an unknown representedObject.";
96     }
97
98     // Concrete object instantiation.
99     console.assert(this.constructor !== WebInspector.ContentView && this instanceof WebInspector.ContentView);
100     console.assert(!representedObject || WebInspector.ContentView.isViewable(representedObject));
101
102     // FIXME: Convert this to a WebInspector.Object subclass, and call super().
103     // WebInspector.Object.call(this);
104
105     this._representedObject = representedObject;
106
107     this._element = document.createElement("div");
108     this._element.classList.add(WebInspector.ContentView.StyleClassName);
109
110     this._parentContainer = null;
111 };
112
113 // FIXME: Move to a WebInspector.Object subclass and we can remove this.
114 WebInspector.Object.deprecatedAddConstructorFunctions(WebInspector.ContentView);
115
116 WebInspector.ContentView.isViewable = function(representedObject)
117 {
118     if (representedObject instanceof WebInspector.Frame)
119         return true;
120     if (representedObject instanceof WebInspector.Resource)
121         return true;
122     if (representedObject instanceof WebInspector.Script)
123         return true;
124     if (representedObject instanceof WebInspector.TimelineRecording)
125         return true;
126     if (representedObject instanceof WebInspector.Timeline)
127         return true;
128     if (representedObject instanceof WebInspector.DOMStorageObject)
129         return true;
130     if (representedObject instanceof WebInspector.CookieStorageObject)
131         return true;
132     if (representedObject instanceof WebInspector.DatabaseTableObject)
133         return true;
134     if (representedObject instanceof WebInspector.DatabaseObject)
135         return true;
136     if (representedObject instanceof WebInspector.IndexedDatabaseObjectStore)
137         return true;
138     if (representedObject instanceof WebInspector.IndexedDatabaseObjectStoreIndex)
139         return true;
140     if (representedObject instanceof WebInspector.ApplicationCacheFrame)
141         return true;
142     if (representedObject instanceof WebInspector.DOMTree)
143         return true;
144     if (representedObject instanceof WebInspector.LogObject)
145         return true;
146     if (representedObject instanceof WebInspector.ContentFlow)
147         return true;
148     if (typeof representedObject === "string" || representedObject instanceof String)
149         return true;
150     return false;
151 };
152
153 WebInspector.ContentView.StyleClassName = "content-view";
154
155 WebInspector.ContentView.Event = {
156     SelectionPathComponentsDidChange: "content-view-selection-path-components-did-change",
157     SupplementalRepresentedObjectsDidChange: "content-view-supplemental-represented-objects-did-change",
158     NumberOfSearchResultsDidChange: "content-view-number-of-search-results-did-change",
159     NavigationItemsDidChange: "content-view-navigation-items-did-change"
160 };
161
162 WebInspector.ContentView.prototype = {
163     constructor: WebInspector.ContentView,
164
165     // Public
166
167     get representedObject()
168     {
169         return this._representedObject;
170     },
171
172     get navigationItems()
173     {
174         // Navigation items that will be displayed by the ContentBrowser instance,
175         // meant to be subclassed. Implemented by subclasses.
176         return [];
177     },
178
179     get allowedNavigationSidebarPanels()
180     {
181         // Allow any navigation sidebar panel.
182         return [];
183     },
184
185     get element()
186     {
187         return this._element;
188     },
189
190     get parentContainer()
191     {
192         return this._parentContainer;
193     },
194
195     get visible()
196     {
197         return this._visible;
198     },
199
200     set visible(flag)
201     {
202         this._visible = flag;
203     },
204
205     get scrollableElements()
206     {
207         // Implemented by subclasses.
208         return [];
209     },
210
211     get shouldKeepElementsScrolledToBottom()
212     {
213         // Implemented by subclasses.
214         return false;
215     },
216
217     get selectionPathComponents()
218     {
219         // Implemented by subclasses.
220         return [];
221     },
222
223     get supplementalRepresentedObjects()
224     {
225         // Implemented by subclasses.
226         return [];
227     },
228
229     get supportsSplitContentBrowser()
230     {
231         // Implemented by subclasses.
232         return true;
233     },
234
235     updateLayout: function()
236     {
237         // Implemented by subclasses.
238     },
239
240     shown: function()
241     {
242         // Implemented by subclasses.
243     },
244
245     hidden: function()
246     {
247         // Implemented by subclasses.
248     },
249
250     closed: function()
251     {
252         // Implemented by subclasses.
253     },
254
255     saveToCookie: function(cookie)
256     {
257         // Implemented by subclasses.
258     },
259
260     restoreFromCookie: function(cookie)
261     {
262         // Implemented by subclasses.
263     },
264
265     canGoBack: function()
266     {
267         // Implemented by subclasses.
268         return false;
269     },
270
271     canGoForward: function()
272     {
273         // Implemented by subclasses.
274         return false;
275     },
276
277     goBack: function()
278     {
279         // Implemented by subclasses.
280     },
281
282     goForward: function()
283     {
284         // Implemented by subclasses.
285     },
286
287     get supportsSearch()
288     {
289         // Implemented by subclasses.
290         return false;
291     },
292
293     get numberOfSearchResults()
294     {
295         // Implemented by subclasses.
296         return null;
297     },
298
299     get hasPerformedSearch()
300     {
301         // Implemented by subclasses.
302         return false;
303     },
304
305     set automaticallyRevealFirstSearchResult(reveal)
306     {
307         // Implemented by subclasses.
308     },
309
310     performSearch: function(query)
311     {
312         // Implemented by subclasses.
313     },
314
315     searchCleared: function()
316     {
317         // Implemented by subclasses.
318     },
319
320     searchQueryWithSelection: function()
321     {
322         // Implemented by subclasses.
323         return null;
324     },
325
326     revealPreviousSearchResult: function(changeFocus)
327     {
328         // Implemented by subclasses.
329     },
330
331     revealNextSearchResult: function(changeFocus)
332     {
333         // Implemented by subclasses.
334     }
335 };
336
337 WebInspector.ContentView.prototype.__proto__ = WebInspector.Object.prototype;