Web Inspector: Use initialLayout for Settings tab
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / SettingsTabContentView.js
1 /*
2  * Copyright (C) 2015-2017 Apple Inc. All rights reserved.
3  * Copyright (C) 2016 Devin Rousso <dcrousso+webkit@gmail.com>. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24  * THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 WebInspector.SettingsTabContentView = class SettingsTabContentView extends WebInspector.TabContentView
28 {
29     constructor(identifier)
30     {
31         let tabBarItem = new WebInspector.PinnedTabBarItem("Images/Gear.svg", WebInspector.UIString("Open Settings"));
32
33         super(identifier || "settings", "settings", tabBarItem);
34
35         // Ensures that the Settings tab is displayable from a pinned tab bar item.
36         tabBarItem.representedObject = this;
37
38         this._selectedSettingsView = null;
39         this._settingsViews = [];
40     }
41
42     static tabInfo()
43     {
44         return {
45             image: "Images/Gear.svg",
46             title: WebInspector.UIString("Settings"),
47         };
48     }
49
50     static isEphemeral()
51     {
52         return true;
53     }
54
55     static shouldSaveTab()
56     {
57         return false;
58     }
59
60     // Public
61
62     get type() { return WebInspector.SettingsTabContentView.Type; }
63
64     get selectedSettingsView()
65     {
66         return this._selectedSettingsView;
67     }
68
69     set selectedSettingsView(settingsView)
70     {
71         if (this._selectedSettingsView === settingsView)
72             return;
73
74         if (this._selectedSettingsView)
75             this.replaceSubview(this._selectedSettingsView, settingsView);
76         else
77             this.addSubview(settingsView);
78
79         this._selectedSettingsView = settingsView;
80         this._selectedSettingsView.updateLayout();
81
82         let navigationItem = this._navigationBar.findNavigationItem(settingsView.identifier);
83         console.assert(navigationItem, "Missing navigation item for settings view.", settingsView)
84         if (!navigationItem)
85             return;
86
87         this._navigationBar.selectedNavigationItem = navigationItem;
88     }
89
90     addSettingsView(settingsView)
91     {
92         if (this._settingsViews.includes(settingsView)) {
93             console.assert(false, "SettingsView already exists.", settingsView);
94             return;
95         }
96
97         this._settingsViews.push(settingsView);
98         this._navigationBar.addNavigationItem(new WebInspector.RadioButtonNavigationItem(settingsView.identifier, settingsView.displayName));
99
100         this._updateNavigationBarVisibility();
101     }
102
103     setSettingsViewVisible(settingsView, visible)
104     {
105         let navigationItem = this._navigationBar.findNavigationItem(settingsView.identifier);
106         console.assert(navigationItem, "Missing NavigationItem for identifier: " + settingsView.identifier);
107         if (!navigationItem)
108             return;
109
110         if (navigationItem.hidden === !visible)
111             return;
112
113         navigationItem.hidden = !visible;
114         settingsView.element.classList.toggle("hidden", !visible);
115
116         this._updateNavigationBarVisibility();
117
118         if (!this.selectedSettingsView) {
119             if (visible)
120                 this.selectedSettingsView = settingsView;
121             return;
122         }
123
124         if (this.selectedSettingsView !== settingsView)
125             return;
126
127         let index = this._settingsViews.indexOf(settingsView);
128         console.assert(index !== -1, "SettingsView not found.", settingsView)
129         if (index === -1)
130             return;
131
132         let previousIndex = index;
133         while (--previousIndex >= 0) {
134             let previousNavigationItem = this._navigationBar.navigationItems[previousIndex];
135             console.assert(previousNavigationItem);
136             if (!previousNavigationItem || previousNavigationItem.hidden)
137                 continue;
138
139             this.selectedSettingsView = this._settingsViews[previousIndex];
140             return;
141         }
142
143         let nextIndex = index;
144         while (++nextIndex < this._settingsViews.length) {
145             let nextNavigationItem = this._navigationBar.navigationItems[nextIndex];
146             console.assert(nextNavigationItem);
147             if (!nextNavigationItem || nextNavigationItem.hidden)
148                 continue;
149
150             this.selectedSettingsView = this._settingsViews[nextIndex];
151             return;
152         }
153     }
154
155     // Protected
156
157     initialLayout()
158     {
159         this._navigationBar = new WebInspector.NavigationBar;
160         this._navigationBar.addEventListener(WebInspector.NavigationBar.Event.NavigationItemSelected, this._navigationItemSelected, this);
161
162         this.addSubview(this._navigationBar);
163
164         let generalSettingsView = new WebInspector.GeneralSettingsView;
165         this.addSettingsView(generalSettingsView);
166
167         this.selectedSettingsView = generalSettingsView;
168
169         WebInspector.notifications.addEventListener(WebInspector.Notification.DebugUIEnabledDidChange, this.needsLayout.bind(this, WebInspector.View.LayoutReason.Dirty));
170     }
171
172     // Private
173
174     _updateNavigationBarVisibility()
175     {
176         let visibleItems = 0;
177         for (let item of this._navigationBar.navigationItems) {
178             if (!item.hidden && ++visibleItems > 1) {
179                 this._navigationBar.element.classList.remove("invisible");
180                 return;
181             }
182         }
183
184         this._navigationBar.element.classList.add("invisible");
185     }
186
187     _navigationItemSelected(event)
188     {
189         let navigationItem = event.target.selectedNavigationItem;
190         if (!navigationItem)
191             return;
192
193         let settingsView = this._settingsViews.find((view) => view.identifier === navigationItem.identifier);
194         console.assert(settingsView, "Missing SettingsView for identifier " + navigationItem.identifier);
195         if (!settingsView)
196             return;
197
198         this.selectedSettingsView = settingsView;
199     }
200 };
201
202 WebInspector.SettingsTabContentView.Type = "settings";