Web Inspector: Creating the CSSStyleDetailsSidebarPanel takes about 50ms (20%) of...
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / Sidebar.js
1 /*
2  * Copyright (C) 2013, 2015 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.Sidebar = class Sidebar extends WebInspector.View
27 {
28     constructor(element, side, sidebarPanels, role, label, hasNavigationBar)
29     {
30         super(element);
31
32         console.assert(!side || side === WebInspector.Sidebar.Sides.Left || side === WebInspector.Sidebar.Sides.Right);
33         this._side = side || WebInspector.Sidebar.Sides.Left;
34         this._collapsed = true;
35
36         this.element.classList.add("sidebar", this._side, WebInspector.Sidebar.CollapsedStyleClassName);
37
38         this.element.setAttribute("role", role || "group");
39         if (label)
40             this.element.setAttribute("aria-label", label);
41
42         if (hasNavigationBar) {
43             this.element.classList.add("has-navigation-bar");
44
45             this._navigationBar = new WebInspector.NavigationBar(null, null, "tablist");
46             this._navigationBar.addEventListener(WebInspector.NavigationBar.Event.NavigationItemSelected, this._navigationItemSelected, this);
47             this.addSubview(this._navigationBar);
48         }
49
50         this._resizer = new WebInspector.Resizer(WebInspector.Resizer.RuleOrientation.Vertical, this);
51         this.element.insertBefore(this._resizer.element, this.element.firstChild);
52
53         this._sidebarPanels = [];
54
55         if (sidebarPanels) {
56             for (let sidebarPanel of sidebarPanels)
57                 this.addSidebarPanel(sidebarPanel);
58         }
59     }
60
61     // Public
62
63     addSidebarPanel(sidebarPanel)
64     {
65         console.assert(sidebarPanel instanceof WebInspector.SidebarPanel);
66         if (!(sidebarPanel instanceof WebInspector.SidebarPanel))
67             return null;
68
69         console.assert(!sidebarPanel.parentSidebar);
70         if (sidebarPanel.parentSidebar)
71             return null;
72
73         this._sidebarPanels.push(sidebarPanel);
74         this.addSubview(sidebarPanel);
75
76         if (this._navigationBar) {
77             console.assert(sidebarPanel.navigationItem);
78             this._navigationBar.addNavigationItem(sidebarPanel.navigationItem);
79         }
80
81         sidebarPanel.added();
82
83         return sidebarPanel;
84     }
85
86     removeSidebarPanel(sidebarPanelOrIdentifierOrIndex)
87     {
88         var sidebarPanel = this.findSidebarPanel(sidebarPanelOrIdentifierOrIndex);
89         if (!sidebarPanel)
90             return null;
91
92         sidebarPanel.selected = false;
93
94         if (sidebarPanel.visible) {
95             sidebarPanel.hidden();
96             sidebarPanel.visibilityDidChange();
97         }
98
99         if (this._selectedSidebarPanel === sidebarPanel) {
100             var index = this._sidebarPanels.indexOf(sidebarPanel);
101             this.selectedSidebarPanel = this._sidebarPanels[index - 1] || this._sidebarPanels[index + 1] || null;
102         }
103
104         this._sidebarPanels.remove(sidebarPanel);
105         this.removeSubview(sidebarPanel);
106
107         if (this._navigationBar) {
108             console.assert(sidebarPanel.navigationItem);
109             this._navigationBar.removeNavigationItem(sidebarPanel.navigationItem);
110         }
111
112         sidebarPanel.removed();
113
114         return sidebarPanel;
115     }
116
117     get selectedSidebarPanel()
118     {
119         return this._selectedSidebarPanel || null;
120     }
121
122     set selectedSidebarPanel(sidebarPanelOrIdentifierOrIndex)
123     {
124         var sidebarPanel = this.findSidebarPanel(sidebarPanelOrIdentifierOrIndex);
125         if (this._selectedSidebarPanel === sidebarPanel)
126             return;
127
128         if (this._selectedSidebarPanel) {
129             var wasVisible = this._selectedSidebarPanel.visible;
130
131             this._selectedSidebarPanel.selected = false;
132
133             if (wasVisible) {
134                 this._selectedSidebarPanel.hidden();
135                 this._selectedSidebarPanel.visibilityDidChange();
136             }
137         }
138
139         this._selectedSidebarPanel = sidebarPanel || null;
140
141         if (this._navigationBar)
142             this._navigationBar.selectedNavigationItem = sidebarPanel ? sidebarPanel.navigationItem : null;
143
144         if (this._selectedSidebarPanel) {
145             this._selectedSidebarPanel.selected = true;
146
147             if (this._selectedSidebarPanel.visible) {
148                 this._selectedSidebarPanel.shown();
149                 this._selectedSidebarPanel.visibilityDidChange();
150                 this._recalculateWidth(this._selectedSidebarPanel.savedWidth);
151             }
152         }
153
154         this.dispatchEventToListeners(WebInspector.Sidebar.Event.SidebarPanelSelected);
155     }
156
157     get minimumWidth()
158     {
159         if (this._navigationBar)
160             return Math.max(WebInspector.Sidebar.AbsoluteMinimumWidth, this._navigationBar.minimumWidth);
161         if (this._selectedSidebarPanel)
162             return Math.max(WebInspector.Sidebar.AbsoluteMinimumWidth, this._selectedSidebarPanel.minimumWidth);
163         return WebInspector.Sidebar.AbsoluteMinimumWidth;
164     }
165
166     get maximumWidth()
167     {
168         // FIXME: This is kind of arbitrary and ideally would be a more complex calculation based on the
169         // available space for the sibling elements.
170         return Math.round(window.innerWidth / 3);
171     }
172
173     get width()
174     {
175         return this.element.offsetWidth;
176     }
177
178     set width(newWidth)
179     {
180         if (newWidth === this.width)
181             return;
182
183         this._recalculateWidth(newWidth);
184     }
185
186     get collapsed()
187     {
188         return this._collapsed;
189     }
190
191     set collapsed(flag)
192     {
193         if (flag === this._collapsed)
194             return;
195
196         this._collapsed = flag || false;
197         this.element.classList.toggle(WebInspector.Sidebar.CollapsedStyleClassName);
198
199         if (!this._collapsed && this._navigationBar)
200             this._navigationBar.needsLayout();
201
202         if (this._selectedSidebarPanel) {
203             if (this._selectedSidebarPanel.visible) {
204                 this._selectedSidebarPanel.shown();
205                 this._recalculateWidth(this._selectedSidebarPanel.savedWidth);
206             } else
207                 this._selectedSidebarPanel.hidden();
208
209             this._selectedSidebarPanel.visibilityDidChange();
210         }
211
212         this.dispatchEventToListeners(WebInspector.Sidebar.Event.CollapsedStateDidChange);
213         this.dispatchEventToListeners(WebInspector.Sidebar.Event.WidthDidChange);
214     }
215
216     get sidebarPanels()
217     {
218         return this._sidebarPanels;
219     }
220
221     get side()
222     {
223         return this._side;
224     }
225
226     findSidebarPanel(sidebarPanelOrIdentifierOrIndex)
227     {
228         var sidebarPanel = null;
229
230         if (sidebarPanelOrIdentifierOrIndex instanceof WebInspector.SidebarPanel) {
231             if (this._sidebarPanels.includes(sidebarPanelOrIdentifierOrIndex))
232                 sidebarPanel = sidebarPanelOrIdentifierOrIndex;
233         } else if (typeof sidebarPanelOrIdentifierOrIndex === "number") {
234             sidebarPanel = this._sidebarPanels[sidebarPanelOrIdentifierOrIndex];
235         } else if (typeof sidebarPanelOrIdentifierOrIndex === "string") {
236             for (var i = 0; i < this._sidebarPanels.length; ++i) {
237                 if (this._sidebarPanels[i].identifier === sidebarPanelOrIdentifierOrIndex) {
238                     sidebarPanel = this._sidebarPanels[i];
239                     break;
240                 }
241             }
242         }
243
244         return sidebarPanel;
245     }
246
247     // Protected
248
249     resizerDragStarted(resizer)
250     {
251         this._widthBeforeResize = this.width;
252     }
253
254     resizerDragging(resizer, positionDelta)
255     {
256         if (this._side === WebInspector.Sidebar.Sides.Left)
257             positionDelta *= -1;
258
259         var newWidth = positionDelta + this._widthBeforeResize;
260         this.width = newWidth;
261         this.collapsed = (newWidth < (this.minimumWidth / 2));
262     }
263
264     // Private
265
266     _recalculateWidth(newWidth = this.width)
267     {
268         // Need to add 1 because of the 1px border-right.
269         newWidth = Number.constrain(newWidth, this.minimumWidth + 1, this.maximumWidth);
270         this.element.style.width = Math.ceil(newWidth) + "px";
271
272         if (!this.collapsed && this._navigationBar)
273             this._navigationBar.needsLayout();
274
275         if (!this.collapsed && this._selectedSidebarPanel)
276             this._selectedSidebarPanel.updateLayout(WebInspector.View.LayoutReason.Resize);
277
278         this.dispatchEventToListeners(WebInspector.Sidebar.Event.WidthDidChange);
279     }
280
281     _navigationItemSelected(event)
282     {
283         this.selectedSidebarPanel = event.target.selectedNavigationItem ? event.target.selectedNavigationItem.identifier : null;
284     }
285 };
286
287 WebInspector.Sidebar.CollapsedStyleClassName = "collapsed";
288 WebInspector.Sidebar.AbsoluteMinimumWidth = 200;
289
290 WebInspector.Sidebar.Sides = {
291     Right: "right",
292     Left: "left"
293 };
294
295 WebInspector.Sidebar.Event = {
296     SidebarPanelSelected: "sidebar-panel-selected",
297     CollapsedStateDidChange: "sidebar-collapsed-state-did-change",
298     WidthDidChange: "sidebar-width-did-change",
299 };