Web Inspector: sidebar panels shouldn't be added as subviews unless visible
[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 WI.Sidebar = class Sidebar extends WI.View
27 {
28     constructor(element, side, sidebarPanels, role, label, hasNavigationBar)
29     {
30         super(element);
31
32         console.assert(!side || side === WI.Sidebar.Sides.Left || side === WI.Sidebar.Sides.Right);
33         this._side = side || WI.Sidebar.Sides.Left;
34         this._collapsed = true;
35
36         this.element.classList.add("sidebar", this._side, WI.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 WI.SidebarNavigationBar(null, null, "tablist");
46             this._navigationBar.addEventListener(WI.NavigationBar.Event.NavigationItemSelected, this._navigationItemSelected, this);
47             this.addSubview(this._navigationBar);
48         }
49
50         this._resizer = new WI.Resizer(WI.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         this.insertSidebarPanel(sidebarPanel, this._sidebarPanels.length);
66     }
67
68     insertSidebarPanel(sidebarPanel, index)
69     {
70         console.assert(sidebarPanel instanceof WI.SidebarPanel);
71         if (!(sidebarPanel instanceof WI.SidebarPanel))
72             return;
73
74         console.assert(!sidebarPanel.parentSidebar);
75         if (sidebarPanel.parentSidebar)
76             return;
77
78         console.assert(index >= 0 && index <= this._sidebarPanels.length);
79         this._sidebarPanels.splice(index, 0, sidebarPanel);
80
81         if (this._navigationBar) {
82             console.assert(sidebarPanel.navigationItem);
83             this._navigationBar.insertNavigationItem(sidebarPanel.navigationItem, index);
84         }
85     }
86
87     removeSidebarPanel(sidebarPanelOrIdentifierOrIndex)
88     {
89         var sidebarPanel = this.findSidebarPanel(sidebarPanelOrIdentifierOrIndex);
90         if (!sidebarPanel)
91             return;
92
93         if (sidebarPanel.visible) {
94             sidebarPanel.hidden();
95             sidebarPanel.visibilityDidChange();
96         }
97
98         sidebarPanel.selected = false;
99
100         if (this._selectedSidebarPanel === sidebarPanel) {
101             var index = this._sidebarPanels.indexOf(sidebarPanel);
102             this.selectedSidebarPanel = this._sidebarPanels[index - 1] || this._sidebarPanels[index + 1] || null;
103         }
104
105         this._sidebarPanels.remove(sidebarPanel);
106
107         if (this._navigationBar) {
108             console.assert(sidebarPanel.navigationItem);
109             this._navigationBar.removeNavigationItem(sidebarPanel.navigationItem);
110         }
111     }
112
113     get selectedSidebarPanel()
114     {
115         return this._selectedSidebarPanel || null;
116     }
117
118     set selectedSidebarPanel(sidebarPanelOrIdentifierOrIndex)
119     {
120         var sidebarPanel = this.findSidebarPanel(sidebarPanelOrIdentifierOrIndex);
121         if (this._selectedSidebarPanel === sidebarPanel)
122             return;
123
124         if (this._selectedSidebarPanel) {
125             this._selectedSidebarPanel.hidden();
126             this._selectedSidebarPanel.visibilityDidChange();
127             this._selectedSidebarPanel.selected = false;
128             this.removeSubview(this._selectedSidebarPanel);
129         }
130
131         this._selectedSidebarPanel = sidebarPanel || null;
132
133         if (this._navigationBar)
134             this._navigationBar.selectedNavigationItem = sidebarPanel ? sidebarPanel.navigationItem : null;
135
136         if (this._selectedSidebarPanel) {
137             this.addSubview(this._selectedSidebarPanel);
138             this._selectedSidebarPanel.selected = true;
139             this._selectedSidebarPanel.shown();
140             this._selectedSidebarPanel.visibilityDidChange();
141         }
142
143         this.dispatchEventToListeners(WI.Sidebar.Event.SidebarPanelSelected);
144     }
145
146     get minimumWidth()
147     {
148         if (this._navigationBar)
149             return Math.max(WI.Sidebar.AbsoluteMinimumWidth, this._navigationBar.minimumWidth);
150         if (this._selectedSidebarPanel)
151             return Math.max(WI.Sidebar.AbsoluteMinimumWidth, this._selectedSidebarPanel.minimumWidth);
152         return WI.Sidebar.AbsoluteMinimumWidth;
153     }
154
155     get maximumWidth()
156     {
157         return WI.getMaximumSidebarWidth(this);
158     }
159
160     get width()
161     {
162         return this.element.offsetWidth;
163     }
164
165     set width(newWidth)
166     {
167         if (newWidth === this.width)
168             return;
169
170         this._recalculateWidth(newWidth);
171     }
172
173     get collapsed()
174     {
175         return this._collapsed;
176     }
177
178     set collapsed(flag)
179     {
180         if (flag === this._collapsed)
181             return;
182
183         this._collapsed = flag || false;
184         this.element.classList.toggle(WI.Sidebar.CollapsedStyleClassName);
185
186         if (!this._collapsed && this._navigationBar)
187             this._navigationBar.needsLayout();
188
189         if (this._selectedSidebarPanel) {
190             if (this._selectedSidebarPanel.visible)
191                 this._selectedSidebarPanel.shown();
192             else
193                 this._selectedSidebarPanel.hidden();
194
195             this._selectedSidebarPanel.visibilityDidChange();
196         }
197
198         this.dispatchEventToListeners(WI.Sidebar.Event.CollapsedStateDidChange);
199         this.dispatchEventToListeners(WI.Sidebar.Event.WidthDidChange);
200     }
201
202     get sidebarPanels()
203     {
204         return this._sidebarPanels;
205     }
206
207     get side()
208     {
209         return this._side;
210     }
211
212     findSidebarPanel(sidebarPanelOrIdentifierOrIndex)
213     {
214         var sidebarPanel = null;
215
216         if (sidebarPanelOrIdentifierOrIndex instanceof WI.SidebarPanel) {
217             if (this._sidebarPanels.includes(sidebarPanelOrIdentifierOrIndex))
218                 sidebarPanel = sidebarPanelOrIdentifierOrIndex;
219         } else if (typeof sidebarPanelOrIdentifierOrIndex === "number") {
220             sidebarPanel = this._sidebarPanels[sidebarPanelOrIdentifierOrIndex];
221         } else if (typeof sidebarPanelOrIdentifierOrIndex === "string") {
222             for (var i = 0; i < this._sidebarPanels.length; ++i) {
223                 if (this._sidebarPanels[i].identifier === sidebarPanelOrIdentifierOrIndex) {
224                     sidebarPanel = this._sidebarPanels[i];
225                     break;
226                 }
227             }
228         }
229
230         return sidebarPanel;
231     }
232
233     // Protected
234
235     resizerDragStarted(resizer)
236     {
237         this._widthBeforeResize = this.width;
238     }
239
240     resizerDragging(resizer, positionDelta)
241     {
242         if (this._side === WI.Sidebar.Sides.Left)
243             positionDelta *= -1;
244
245         if (WI.resolvedLayoutDirection() === WI.LayoutDirection.RTL)
246             positionDelta *= -1;
247
248         var newWidth = positionDelta + this._widthBeforeResize;
249         this.width = newWidth;
250         this.collapsed = newWidth < (this.minimumWidth / 2);
251     }
252
253     resizerDragEnded(resizer)
254     {
255         if (this._widthBeforeResize === this.width)
256             return;
257
258         if (!this.collapsed && this._navigationBar)
259             this._navigationBar.sizeDidChange();
260
261         if (!this.collapsed && this._selectedSidebarPanel)
262             this._selectedSidebarPanel.sizeDidChange();
263     }
264
265     // Private
266
267     _recalculateWidth(newWidth = this.width)
268     {
269         // Need to add 1 because of the 1px border-right.
270         newWidth = Math.ceil(Number.constrain(newWidth, this.minimumWidth + 1, this.maximumWidth));
271         this.element.style.width = `${newWidth}px`;
272
273         if (this.collapsed)
274             return;
275
276         if (this._navigationBar)
277             this._navigationBar.updateLayoutIfNeeded(WI.View.LayoutReason.Resize);
278
279         if (this._selectedSidebarPanel)
280             this._selectedSidebarPanel.updateLayoutIfNeeded(WI.View.LayoutReason.Resize);
281
282         this.dispatchEventToListeners(WI.Sidebar.Event.WidthDidChange, {newWidth});
283     }
284
285     _navigationItemSelected(event)
286     {
287         this.selectedSidebarPanel = event.target.selectedNavigationItem ? event.target.selectedNavigationItem.identifier : null;
288     }
289 };
290
291 WI.Sidebar.CollapsedStyleClassName = "collapsed";
292 WI.Sidebar.AbsoluteMinimumWidth = 200;
293
294 WI.Sidebar.Sides = {
295     Right: "right",
296     Left: "left"
297 };
298
299 WI.Sidebar.Event = {
300     SidebarPanelSelected: "sidebar-panel-selected",
301     CollapsedStateDidChange: "sidebar-collapsed-state-did-change",
302     WidthDidChange: "sidebar-width-did-change",
303 };