Web Inspector: Elements: Styles: add icons for various CSS rule types
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / NetworkDetailView.js
1 /*
2  * Copyright (C) 2018 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.NetworkDetailView = class NetworkDetailView extends WI.View
27 {
28     constructor(representedObject, delegate)
29     {
30         super();
31
32         this._representedObject = representedObject;
33         this._delegate = delegate || null;
34
35         this.element.classList.add("network-detail");
36
37         this._contentBrowser = null;
38
39         this._detailNavigationItemMap = new Map;
40
41         this._contentViewCookie = null;
42     }
43
44     // Public
45
46     get representedObject() { return this._representedObject; }
47
48     shown()
49     {
50         if (!this._contentBrowser)
51             return;
52
53         this._showPreferredContentView();
54
55         if (this._contentViewCookie) {
56             this._contentBrowser.showContentView(this._contentBrowser.currentContentView, this._contentViewCookie);
57             this._contentViewCookie = null;
58         }
59
60         this._contentBrowser.shown();
61     }
62
63     hidden()
64     {
65         this._contentBrowser.hidden();
66     }
67
68     dispose()
69     {
70         this._delegate = null;
71
72         this._contentBrowser.contentViewContainer.closeAllContentViews();
73     }
74
75     willShowWithCookie(cookie)
76     {
77         this._contentViewCookie = cookie;
78     }
79
80     // Protected
81
82     initialLayout()
83     {
84         let closeNavigationItem = new WI.ButtonNavigationItem("close", WI.UIString("Close detail view"), "Images/CloseLarge.svg", 16, 16);
85         closeNavigationItem.addEventListener(WI.ButtonNavigationItem.Event.Clicked, this._handleCloseButton.bind(this));
86         closeNavigationItem.visibilityPriority = WI.NavigationItem.VisibilityPriority.High;
87
88         let contentViewNavigationItemsGroup = new WI.GroupNavigationItem;
89         let contentViewNavigationItemsFlexItem = new WI.FlexibleSpaceNavigationItem(contentViewNavigationItemsGroup, WI.FlexibleSpaceNavigationItem.Align.End);
90         contentViewNavigationItemsFlexItem.visibilityPriority = WI.NavigationItem.VisibilityPriority.Low;
91
92         const element = null;
93         const disableBackForward = true;
94         const disableFindBanner = false;
95         this._contentBrowser = new WI.ContentBrowser(element, this, disableBackForward, disableFindBanner, contentViewNavigationItemsFlexItem, contentViewNavigationItemsGroup);
96
97         // Insert all of our custom navigation items at the start of the ContentBrowser's NavigationBar.
98         let index = 0;
99         this._contentBrowser.navigationBar.insertNavigationItem(closeNavigationItem, index++);
100         this._contentBrowser.navigationBar.insertNavigationItem(new WI.FlexibleSpaceNavigationItem, index++);
101         for (let detailNavigationItem of this._detailNavigationItemMap.values())
102             this._contentBrowser.navigationBar.insertNavigationItem(detailNavigationItem, index++);
103
104         this._contentBrowser.navigationBar.addEventListener(WI.NavigationBar.Event.NavigationItemSelected, this._navigationItemSelected, this);
105
106         this.addSubview(this._contentBrowser);
107
108         this._showPreferredContentView();
109     }
110
111     createDetailNavigationItem(identifier, toolTip)
112     {
113         this._detailNavigationItemMap.set(identifier, new WI.RadioButtonNavigationItem(identifier, toolTip));
114     }
115
116     detailNavigationItemForIdentifier(identifier)
117     {
118         return this._detailNavigationItemMap.get(identifier);
119     }
120
121     showContentViewForIdentifier(identifier)
122     {
123         // Implemented by subclasses.
124     }
125
126     // Private
127
128     _showPreferredContentView()
129     {
130         let detailNavigationItems = Array.from(this._detailNavigationItemMap.values());
131
132         // Restore the preferred navigation item.
133         let firstNavigationItem = null;
134         let defaultIdentifier = WI.settings.selectedNetworkDetailContentViewIdentifier.value;
135         for (let navigationItem of this._contentBrowser.navigationBar.navigationItems) {
136             if (!(navigationItem instanceof WI.RadioButtonNavigationItem))
137                 continue;
138
139             if (!detailNavigationItems.includes(navigationItem))
140                 continue;
141
142             if (!firstNavigationItem)
143                 firstNavigationItem = navigationItem;
144
145             if (navigationItem.identifier === defaultIdentifier) {
146                 this._contentBrowser.navigationBar.selectedNavigationItem = navigationItem;
147                 return;
148             }
149         }
150
151         console.assert(firstNavigationItem, "Should have found at least one navigation item above");
152         this._contentBrowser.navigationBar.selectedNavigationItem = firstNavigationItem;
153     }
154
155     _navigationItemSelected(event)
156     {
157         let navigationItem = event.target.selectedNavigationItem;
158         if (!(navigationItem instanceof WI.RadioButtonNavigationItem))
159             return;
160
161         this.showContentViewForIdentifier(navigationItem.identifier);
162
163         console.assert(navigationItem.identifier);
164         WI.settings.selectedNetworkDetailContentViewIdentifier.value = navigationItem.identifier;
165     }
166
167     _handleCloseButton(event)
168     {
169         if (this._delegate && this._delegate.networkDetailViewClose)
170             this._delegate.networkDetailViewClose(this);
171     }
172 };