Web Inspector: Remove SidebarPanel show/hide and added/removed
[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.NavigationBar(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         let referenceView = this._sidebarPanels[index + 1] || null;
82         this.insertSubviewBefore(sidebarPanel, referenceView);
83
84         if (this._navigationBar) {
85             console.assert(sidebarPanel.navigationItem);
86             this._navigationBar.insertNavigationItem(sidebarPanel.navigationItem, index);
87         }
88     }
89
90     removeSidebarPanel(sidebarPanelOrIdentifierOrIndex)
91     {
92         var sidebarPanel = this.findSidebarPanel(sidebarPanelOrIdentifierOrIndex);
93         if (!sidebarPanel)
94             return;
95
96         sidebarPanel.selected = false;
97
98         if (sidebarPanel.visible) {
99             sidebarPanel.hidden();
100             sidebarPanel.visibilityDidChange();
101         }
102
103         if (this._selectedSidebarPanel === sidebarPanel) {
104             var index = this._sidebarPanels.indexOf(sidebarPanel);
105             this.selectedSidebarPanel = this._sidebarPanels[index - 1] || this._sidebarPanels[index + 1] || null;
106         }
107
108         this._sidebarPanels.remove(sidebarPanel);
109         this.removeSubview(sidebarPanel);
110
111         if (this._navigationBar) {
112             console.assert(sidebarPanel.navigationItem);
113             this._navigationBar.removeNavigationItem(sidebarPanel.navigationItem);
114         }
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             }
151         }
152
153         this.dispatchEventToListeners(WI.Sidebar.Event.SidebarPanelSelected);
154     }
155
156     get minimumWidth()
157     {
158         if (this._navigationBar)
159             return Math.max(WI.Sidebar.AbsoluteMinimumWidth, this._navigationBar.minimumWidth);
160         if (this._selectedSidebarPanel)
161             return Math.max(WI.Sidebar.AbsoluteMinimumWidth, this._selectedSidebarPanel.minimumWidth);
162         return WI.Sidebar.AbsoluteMinimumWidth;
163     }
164
165     get maximumWidth()
166     {
167         return WI.getMaximumSidebarWidth(this);
168     }
169
170     get width()
171     {
172         return this.element.offsetWidth;
173     }
174
175     set width(newWidth)
176     {
177         if (newWidth === this.width)
178             return;
179
180         this._recalculateWidth(newWidth);
181     }
182
183     get collapsed()
184     {
185         return this._collapsed;
186     }
187
188     set collapsed(flag)
189     {
190         if (flag === this._collapsed)
191             return;
192
193         this._collapsed = flag || false;
194         this.element.classList.toggle(WI.Sidebar.CollapsedStyleClassName);
195
196         if (!this._collapsed && this._navigationBar)
197             this._navigationBar.needsLayout();
198
199         if (this._selectedSidebarPanel) {
200             if (this._selectedSidebarPanel.visible)
201                 this._selectedSidebarPanel.shown();
202             else
203                 this._selectedSidebarPanel.hidden();
204
205             this._selectedSidebarPanel.visibilityDidChange();
206         }
207
208         this.dispatchEventToListeners(WI.Sidebar.Event.CollapsedStateDidChange);
209         this.dispatchEventToListeners(WI.Sidebar.Event.WidthDidChange);
210     }
211
212     get sidebarPanels()
213     {
214         return this._sidebarPanels;
215     }
216
217     get side()
218     {
219         return this._side;
220     }
221
222     findSidebarPanel(sidebarPanelOrIdentifierOrIndex)
223     {
224         var sidebarPanel = null;
225
226         if (sidebarPanelOrIdentifierOrIndex instanceof WI.SidebarPanel) {
227             if (this._sidebarPanels.includes(sidebarPanelOrIdentifierOrIndex))
228                 sidebarPanel = sidebarPanelOrIdentifierOrIndex;
229         } else if (typeof sidebarPanelOrIdentifierOrIndex === "number") {
230             sidebarPanel = this._sidebarPanels[sidebarPanelOrIdentifierOrIndex];
231         } else if (typeof sidebarPanelOrIdentifierOrIndex === "string") {
232             for (var i = 0; i < this._sidebarPanels.length; ++i) {
233                 if (this._sidebarPanels[i].identifier === sidebarPanelOrIdentifierOrIndex) {
234                     sidebarPanel = this._sidebarPanels[i];
235                     break;
236                 }
237             }
238         }
239
240         return sidebarPanel;
241     }
242
243     // Protected
244
245     resizerDragStarted(resizer)
246     {
247         this._widthBeforeResize = this.width;
248     }
249
250     resizerDragging(resizer, positionDelta)
251     {
252         if (this._side === WI.Sidebar.Sides.Left)
253             positionDelta *= -1;
254
255         if (WI.resolvedLayoutDirection() === WI.LayoutDirection.RTL)
256             positionDelta *= -1;
257
258         var newWidth = positionDelta + this._widthBeforeResize;
259         this.width = newWidth;
260         this.collapsed = (newWidth < (this.minimumWidth / 2));
261     }
262
263     resizerDragEnded(resizer)
264     {
265         if (this._widthBeforeResize === this.width)
266             return;
267
268         if (!this.collapsed && this._navigationBar)
269             this._navigationBar.sizeDidChange();
270
271         if (!this.collapsed && this._selectedSidebarPanel)
272             this._selectedSidebarPanel.sizeDidChange();
273     }
274
275     // Private
276
277     _recalculateWidth(newWidth = this.width)
278     {
279         // Need to add 1 because of the 1px border-right.
280         newWidth = Math.ceil(Number.constrain(newWidth, this.minimumWidth + 1, this.maximumWidth));
281         this.element.style.width = `${newWidth}px`;
282
283         if (this.collapsed)
284             return;
285
286         if (this._navigationBar)
287             this._navigationBar.updateLayoutIfNeeded(WI.View.LayoutReason.Resize);
288
289         if (this._selectedSidebarPanel)
290             this._selectedSidebarPanel.updateLayoutIfNeeded(WI.View.LayoutReason.Resize);
291
292         this.dispatchEventToListeners(WI.Sidebar.Event.WidthDidChange, {newWidth});
293     }
294
295     _navigationItemSelected(event)
296     {
297         this.selectedSidebarPanel = event.target.selectedNavigationItem ? event.target.selectedNavigationItem.identifier : null;
298     }
299 };
300
301 WI.Sidebar.CollapsedStyleClassName = "collapsed";
302 WI.Sidebar.AbsoluteMinimumWidth = 200;
303
304 WI.Sidebar.Sides = {
305     Right: "right",
306     Left: "left"
307 };
308
309 WI.Sidebar.Event = {
310     SidebarPanelSelected: "sidebar-panel-selected",
311     CollapsedStateDidChange: "sidebar-collapsed-state-did-change",
312     WidthDidChange: "sidebar-width-did-change",
313 };