62431d81f946f1bf8a3062bf0a3d0accfaaceda9
[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         sidebarPanel.added();
90     }
91
92     removeSidebarPanel(sidebarPanelOrIdentifierOrIndex)
93     {
94         var sidebarPanel = this.findSidebarPanel(sidebarPanelOrIdentifierOrIndex);
95         if (!sidebarPanel)
96             return;
97
98         sidebarPanel.selected = false;
99
100         if (sidebarPanel.visible) {
101             sidebarPanel.hidden();
102             sidebarPanel.visibilityDidChange();
103         }
104
105         if (this._selectedSidebarPanel === sidebarPanel) {
106             var index = this._sidebarPanels.indexOf(sidebarPanel);
107             this.selectedSidebarPanel = this._sidebarPanels[index - 1] || this._sidebarPanels[index + 1] || null;
108         }
109
110         this._sidebarPanels.remove(sidebarPanel);
111         this.removeSubview(sidebarPanel);
112
113         if (this._navigationBar) {
114             console.assert(sidebarPanel.navigationItem);
115             this._navigationBar.removeNavigationItem(sidebarPanel.navigationItem);
116         }
117
118         sidebarPanel.removed();
119     }
120
121     get selectedSidebarPanel()
122     {
123         return this._selectedSidebarPanel || null;
124     }
125
126     set selectedSidebarPanel(sidebarPanelOrIdentifierOrIndex)
127     {
128         var sidebarPanel = this.findSidebarPanel(sidebarPanelOrIdentifierOrIndex);
129         if (this._selectedSidebarPanel === sidebarPanel)
130             return;
131
132         if (this._selectedSidebarPanel) {
133             var wasVisible = this._selectedSidebarPanel.visible;
134
135             this._selectedSidebarPanel.selected = false;
136
137             if (wasVisible) {
138                 this._selectedSidebarPanel.hidden();
139                 this._selectedSidebarPanel.visibilityDidChange();
140             }
141         }
142
143         this._selectedSidebarPanel = sidebarPanel || null;
144
145         if (this._navigationBar)
146             this._navigationBar.selectedNavigationItem = sidebarPanel ? sidebarPanel.navigationItem : null;
147
148         if (this._selectedSidebarPanel) {
149             this._selectedSidebarPanel.selected = true;
150
151             if (this._selectedSidebarPanel.visible) {
152                 this._selectedSidebarPanel.shown();
153                 this._selectedSidebarPanel.visibilityDidChange();
154             }
155         }
156
157         this.dispatchEventToListeners(WI.Sidebar.Event.SidebarPanelSelected);
158     }
159
160     get minimumWidth()
161     {
162         if (this._navigationBar)
163             return Math.max(WI.Sidebar.AbsoluteMinimumWidth, this._navigationBar.minimumWidth);
164         if (this._selectedSidebarPanel)
165             return Math.max(WI.Sidebar.AbsoluteMinimumWidth, this._selectedSidebarPanel.minimumWidth);
166         return WI.Sidebar.AbsoluteMinimumWidth;
167     }
168
169     get maximumWidth()
170     {
171         return WI.getMaximumSidebarWidth(this);
172     }
173
174     get width()
175     {
176         return this.element.offsetWidth;
177     }
178
179     set width(newWidth)
180     {
181         if (newWidth === this.width)
182             return;
183
184         this._recalculateWidth(newWidth);
185     }
186
187     get collapsed()
188     {
189         return this._collapsed;
190     }
191
192     set collapsed(flag)
193     {
194         if (flag === this._collapsed)
195             return;
196
197         this._collapsed = flag || false;
198         this.element.classList.toggle(WI.Sidebar.CollapsedStyleClassName);
199
200         if (!this._collapsed && this._navigationBar)
201             this._navigationBar.needsLayout();
202
203         if (this._selectedSidebarPanel) {
204             if (this._selectedSidebarPanel.visible)
205                 this._selectedSidebarPanel.shown();
206             else
207                 this._selectedSidebarPanel.hidden();
208
209             this._selectedSidebarPanel.visibilityDidChange();
210         }
211
212         this.dispatchEventToListeners(WI.Sidebar.Event.CollapsedStateDidChange);
213         this.dispatchEventToListeners(WI.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 WI.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 === WI.Sidebar.Sides.Left)
257             positionDelta *= -1;
258
259         if (WI.resolvedLayoutDirection() === WI.LayoutDirection.RTL)
260             positionDelta *= -1;
261
262         var newWidth = positionDelta + this._widthBeforeResize;
263         this.width = newWidth;
264         this.collapsed = (newWidth < (this.minimumWidth / 2));
265     }
266
267     resizerDragEnded(resizer)
268     {
269         if (this._widthBeforeResize === this.width)
270             return;
271
272         if (!this.collapsed && this._navigationBar)
273             this._navigationBar.sizeDidChange();
274
275         if (!this.collapsed && this._selectedSidebarPanel)
276             this._selectedSidebarPanel.sizeDidChange();
277     }
278
279     // Private
280
281     _recalculateWidth(newWidth = this.width)
282     {
283         // Need to add 1 because of the 1px border-right.
284         newWidth = Math.ceil(Number.constrain(newWidth, this.minimumWidth + 1, this.maximumWidth));
285         this.element.style.width = `${newWidth}px`;
286
287         if (this.collapsed)
288             return;
289
290         if (this._navigationBar)
291             this._navigationBar.updateLayoutIfNeeded(WI.View.LayoutReason.Resize);
292
293         if (this._selectedSidebarPanel)
294             this._selectedSidebarPanel.updateLayoutIfNeeded(WI.View.LayoutReason.Resize);
295
296         this.dispatchEventToListeners(WI.Sidebar.Event.WidthDidChange, {newWidth});
297     }
298
299     _navigationItemSelected(event)
300     {
301         this.selectedSidebarPanel = event.target.selectedNavigationItem ? event.target.selectedNavigationItem.identifier : null;
302     }
303 };
304
305 WI.Sidebar.CollapsedStyleClassName = "collapsed";
306 WI.Sidebar.AbsoluteMinimumWidth = 200;
307
308 WI.Sidebar.Sides = {
309     Right: "right",
310     Left: "left"
311 };
312
313 WI.Sidebar.Event = {
314     SidebarPanelSelected: "sidebar-panel-selected",
315     CollapsedStateDidChange: "sidebar-collapsed-state-did-change",
316     WidthDidChange: "sidebar-width-did-change",
317 };