Web Inspector: Remove parameters from TreeOutline SelectionDidChange event
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / AuditNavigationSidebarPanel.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.AuditNavigationSidebarPanel = class AuditNavigationSidebarPanel extends WI.NavigationSidebarPanel
27 {
28     constructor()
29     {
30         super("audit", WI.UIString("Audits"));
31     }
32
33     // Public
34
35     showDefaultContentView()
36     {
37         let contentView = new WI.ContentView;
38
39         let contentPlaceholder = WI.createMessageTextView(WI.UIString("No audit selected"));
40         contentView.element.appendChild(contentPlaceholder);
41
42         let importNavigationItem = new WI.ButtonNavigationItem("import-audit", WI.UIString("Import"), "Images/Import.svg", 15, 15);
43         importNavigationItem.buttonStyle = WI.ButtonNavigationItem.Style.ImageAndText;
44         importNavigationItem.addEventListener(WI.ButtonNavigationItem.Event.Clicked, this._handleImportButtonNavigationItemClicked, this);
45
46         let importHelpElement = WI.createNavigationItemHelp(WI.UIString("Press %s to import a test or result file"), importNavigationItem);
47         contentPlaceholder.appendChild(importHelpElement);
48
49         this.contentBrowser.showContentView(contentView);
50     }
51
52     // Protected
53
54     initialLayout()
55     {
56         super.initialLayout();
57
58         this.contentTreeOutline.allowsRepeatSelection = false;
59
60         this._resultsFolderTreeElement = new WI.FolderTreeElement(WI.UIString("Results"));
61         this.contentTreeOutline.appendChild(this._resultsFolderTreeElement);
62         this._resultsFolderTreeElement.hidden = true;
63         this._resultsFolderTreeElement.expand();
64
65         let navigationBar = new WI.NavigationBar;
66
67         this._startStopButtonNavigationItem = new WI.ToggleButtonNavigationItem("audit-start-stop", WI.UIString("Start"), WI.UIString("Stop"), "Images/AuditStart.svg", "Images/AuditStop.svg", 13, 13);
68         this._startStopButtonNavigationItem.buttonStyle = WI.ButtonNavigationItem.Style.ImageAndText;
69         this._updateStartStopButtonNavigationItemState();
70         this._startStopButtonNavigationItem.addEventListener(WI.ButtonNavigationItem.Event.Clicked, this._handleStartStopButtonNavigationItemClicked, this);
71         navigationBar.addNavigationItem(this._startStopButtonNavigationItem);
72
73         navigationBar.addNavigationItem(new WI.DividerNavigationItem);
74
75         let importButtonNavigationItem = new WI.ButtonNavigationItem("audit-import", WI.UIString("Import"), "Images/Import.svg", 15, 15);
76         importButtonNavigationItem.buttonStyle = WI.ButtonNavigationItem.Style.ImageAndText;
77         importButtonNavigationItem.visibilityPriority = WI.NavigationItem.VisibilityPriority.Low;
78         importButtonNavigationItem.addEventListener(WI.ButtonNavigationItem.Event.Clicked, this._handleImportButtonNavigationItemClicked, this);
79         navigationBar.addNavigationItem(importButtonNavigationItem);
80
81         this.addSubview(navigationBar);
82
83         for (let test of WI.auditManager.tests)
84             this._addTest(test);
85
86         for (let result of WI.auditManager.results)
87             this._addResult(result);
88
89         WI.auditManager.addEventListener(WI.AuditManager.Event.TestAdded, this._handleAuditTestAdded, this);
90         WI.auditManager.addEventListener(WI.AuditManager.Event.TestCompleted, this._handleAuditTestCompleted, this);
91         WI.auditManager.addEventListener(WI.AuditManager.Event.TestRemoved, this._handleAuditTestRemoved, this);
92         WI.auditManager.addEventListener(WI.AuditManager.Event.TestScheduled, this._handleAuditTestScheduled, this);
93
94         this.contentTreeOutline.addEventListener(WI.TreeOutline.Event.SelectionDidChange, this._treeSelectionDidChange, this);
95     }
96
97     closed()
98     {
99         super.closed();
100
101         WI.auditManager.removeEventListener(null, null, this);
102     }
103
104     // Private
105
106     _addTest(test)
107     {
108         this.element.classList.add("has-tests");
109
110         this._updateStartStopButtonNavigationItemState();
111
112         this.contentTreeOutline.insertChild(new WI.AuditTreeElement(test), this.contentTreeOutline.children.indexOf(this._resultsFolderTreeElement));
113
114         this._resultsFolderTreeElement.hidden = !this._resultsFolderTreeElement.children.length;
115
116         this.hideEmptyContentPlaceholder();
117     }
118
119     _addResult(result, index)
120     {
121         this.element.classList.add("has-results");
122
123         this._updateStartStopButtonNavigationItemState();
124
125         this._resultsFolderTreeElement.hidden = false;
126
127         let resultFolderTreeElement = new WI.FolderTreeElement(WI.UIString("Run %d").format(index + 1));
128         if (result instanceof WI.AuditTestResultBase) {
129             resultFolderTreeElement.subtitle = WI.UIString("Imported");
130             result = [result];
131         }
132         this._resultsFolderTreeElement.appendChild(resultFolderTreeElement);
133
134         for (let resultItem of result)
135             resultFolderTreeElement.appendChild(new WI.AuditTreeElement(resultItem));
136     }
137
138     _updateStartStopButtonNavigationItemState()
139     {
140         this._startStopButtonNavigationItem.toggled = WI.auditManager.runningState !== WI.AuditManager.RunningState.Inactive;
141         this._startStopButtonNavigationItem.enabled = WI.auditManager.tests.length && WI.auditManager.runningState !== WI.AuditManager.RunningState.Stopping;
142     }
143
144     _handleAuditTestAdded(event)
145     {
146         this._addTest(event.data.test);
147     }
148
149     _handleAuditTestCompleted(event)
150     {
151         let {result, index} = event.data;
152         this._addResult(result, index);
153     }
154
155     _handleAuditTestRemoved(event)
156     {
157         let {test} = event.data;
158         let treeElement = this.treeElementForRepresentedObject(test);
159         this.contentTreeOutline.removeChild(treeElement);
160
161         this.element.classList.toggle("has-tests", !!WI.auditManager.tests.length);
162
163         if (!WI.auditManager.tests.length) {
164             let contentPlaceholder = WI.createMessageTextView(WI.UIString("No audits"));
165
166             let defaultButtonElement = contentPlaceholder.appendChild(document.createElement("button"));
167             defaultButtonElement.textContent = WI.UIString("Add Default Audits");
168             defaultButtonElement.addEventListener("click", () => {
169                 WI.auditManager.addDefaultTestsIfNeeded();
170             });
171
172             contentPlaceholder = this.showEmptyContentPlaceholder(contentPlaceholder);
173
174             if (WI.auditManager.results.length) {
175                 console.assert(this.contentTreeOutline.children[0] === this._resultsFolderTreeElement);
176
177                 // Move the placeholder to be the first element in the content area, where it will
178                 // be styled such that only the button is visible.
179                 this.contentView.element.insertBefore(contentPlaceholder, this.contentView.element.firstChild);
180             }
181         }
182
183         this._updateStartStopButtonNavigationItemState();
184     }
185
186     _handleAuditTestScheduled(event)
187     {
188         this._updateStartStopButtonNavigationItemState();
189     }
190
191     _treeSelectionDidChange(event)
192     {
193         if (!this.selected)
194             return;
195
196         let treeElement = this.contentTreeOutline.selectedTreeElement;
197         if (!treeElement || treeElement instanceof WI.FolderTreeElement) {
198             this.showDefaultContentView();
199             return;
200         }
201
202         let representedObject = treeElement.representedObject;
203         if (representedObject instanceof WI.AuditTestCase || representedObject instanceof WI.AuditTestGroup
204             || representedObject instanceof WI.AuditTestCaseResult || representedObject instanceof WI.AuditTestGroupResult) {
205             WI.showRepresentedObject(representedObject);
206             return;
207         }
208
209         console.error("Unknown tree element", treeElement);
210     }
211
212     _handleStartStopButtonNavigationItemClicked(event)
213     {
214         if (WI.auditManager.runningState === WI.AuditManager.RunningState.Inactive)
215             WI.auditManager.start();
216         else if (WI.auditManager.runningState === WI.AuditManager.RunningState.Active)
217             WI.auditManager.stop();
218
219         this._updateStartStopButtonNavigationItemState();
220     }
221
222     _handleImportButtonNavigationItemClicked(event)
223     {
224         WI.FileUtilities.importJSON((result) => WI.auditManager.processJSON(result));
225     }
226 };