Web Inspector: Audit: allow audits to be enabled/disabled
[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         if (WI.auditManager.editing) {
40             let contentPlaceholder = WI.createMessageTextView(WI.UIString("Editing audits"));
41             contentPlaceholder.classList.add("finish-editing-audits-placeholder");
42             contentView.element.appendChild(contentPlaceholder);
43
44             let finishEditingNavigationItem = new WI.ButtonNavigationItem("finish-editing-audits", WI.UIString("Done"));
45             finishEditingNavigationItem.addEventListener(WI.ButtonNavigationItem.Event.Clicked, (event) => {
46                 WI.auditManager.editing = false;
47             });
48
49             let importHelpElement = WI.createNavigationItemHelp(WI.UIString("Press %s to stop editing"), finishEditingNavigationItem);
50             contentPlaceholder.appendChild(importHelpElement);
51         } else {
52             let contentPlaceholder = WI.createMessageTextView(WI.UIString("No audit selected"));
53             contentView.element.appendChild(contentPlaceholder);
54
55              let importNavigationItem = new WI.ButtonNavigationItem("import-audit", WI.UIString("Import"), "Images/Import.svg", 15, 15);
56             importNavigationItem.buttonStyle = WI.ButtonNavigationItem.Style.ImageAndText;
57             importNavigationItem.addEventListener(WI.ButtonNavigationItem.Event.Clicked, this._handleImportButtonNavigationItemClicked, this);
58
59              let importHelpElement = WI.createNavigationItemHelp(WI.UIString("Press %s to import a test or result file"), importNavigationItem);
60             contentPlaceholder.appendChild(importHelpElement);
61         }
62
63         this.contentBrowser.showContentView(contentView);
64     }
65
66     // Protected
67
68     initialLayout()
69     {
70         super.initialLayout();
71
72         this.contentTreeOutline.allowsRepeatSelection = false;
73
74         let controlsNavigationBar = new WI.NavigationBar;
75
76         this._startStopButtonNavigationItem = new WI.ToggleButtonNavigationItem("audit-start-stop", WI.UIString("Start"), WI.UIString("Stop"), "Images/AuditStart.svg", "Images/AuditStop.svg", 13, 13);
77         this._startStopButtonNavigationItem.buttonStyle = WI.ButtonNavigationItem.Style.ImageAndText;
78         this._updateStartStopButtonNavigationItemState();
79         this._startStopButtonNavigationItem.addEventListener(WI.ButtonNavigationItem.Event.Clicked, this._handleStartStopButtonNavigationItemClicked, this);
80         controlsNavigationBar.addNavigationItem(this._startStopButtonNavigationItem);
81
82         controlsNavigationBar.addNavigationItem(new WI.DividerNavigationItem);
83
84         let importButtonNavigationItem = new WI.ButtonNavigationItem("audit-import", WI.UIString("Import"), "Images/Import.svg", 15, 15);
85         importButtonNavigationItem.buttonStyle = WI.ButtonNavigationItem.Style.ImageAndText;
86         importButtonNavigationItem.visibilityPriority = WI.NavigationItem.VisibilityPriority.Low;
87         importButtonNavigationItem.addEventListener(WI.ButtonNavigationItem.Event.Clicked, this._handleImportButtonNavigationItemClicked, this);
88         controlsNavigationBar.addNavigationItem(importButtonNavigationItem);
89
90         this.addSubview(controlsNavigationBar);
91
92         let editNavigationbar = new WI.NavigationBar;
93
94         this._editButtonNavigationItem = new WI.ActivateButtonNavigationItem("edit-audits", WI.UIString("Edit"), WI.UIString("Done"));
95         this._editButtonNavigationItem.addEventListener(WI.ButtonNavigationItem.Event.Clicked, this._handleEditButtonNavigationItemClicked, this);
96         editNavigationbar.addNavigationItem(this._editButtonNavigationItem);
97
98         this.contentView.addSubview(editNavigationbar);
99
100         for (let test of WI.auditManager.tests)
101             this._addTest(test);
102
103         WI.auditManager.results.forEach((result, i) => {
104             this._addResult(result, i);
105         });
106
107         WI.auditManager.addEventListener(WI.AuditManager.Event.EditingChanged, this._handleAuditManagerEditingChanged, this);
108         WI.auditManager.addEventListener(WI.AuditManager.Event.TestAdded, this._handleAuditTestAdded, this);
109         WI.auditManager.addEventListener(WI.AuditManager.Event.TestCompleted, this._handleAuditTestCompleted, this);
110         WI.auditManager.addEventListener(WI.AuditManager.Event.TestRemoved, this._handleAuditTestRemoved, this);
111         WI.auditManager.addEventListener(WI.AuditManager.Event.TestScheduled, this._handleAuditTestScheduled, this);
112
113         this.contentTreeOutline.addEventListener(WI.TreeOutline.Event.SelectionDidChange, this._treeSelectionDidChange, this);
114     }
115
116     closed()
117     {
118         super.closed();
119
120         WI.auditManager.removeEventListener(null, null, this);
121     }
122
123     updateFilter()
124     {
125         super.updateFilter();
126
127         if (!this.hasActiveFilters)
128             this._updateNoAuditsPlaceholder();
129     }
130
131     hasCustomFilters()
132     {
133         return true;
134     }
135
136     matchTreeElementAgainstCustomFilters(treeElement, flags)
137     {
138         if (WI.auditManager.editing) {
139             if (treeElement.representedObject instanceof WI.AuditTestResultBase || treeElement.hasAncestor(this._resultsFolderTreeElement) || treeElement === this._resultsFolderTreeElement)
140                 return false;
141         } else {
142             if (treeElement.representedObject instanceof WI.AuditTestBase && treeElement.representedObject.disabled)
143                 return false;
144         }
145
146         return super.matchTreeElementAgainstCustomFilters(treeElement, flags);
147     }
148
149     // Private
150
151     _addTest(test)
152     {
153         this.element.classList.add("has-tests");
154
155         let treeElement = new WI.AuditTreeElement(test);
156
157         if (this._resultsFolderTreeElement) {
158             this.contentTreeOutline.insertChild(treeElement, this.contentTreeOutline.children.indexOf(this._resultsFolderTreeElement));
159             this._resultsFolderTreeElement.hidden = !this._resultsFolderTreeElement.children.length;
160         } else
161             this.contentTreeOutline.appendChild(treeElement);
162
163         this._updateStartStopButtonNavigationItemState();
164         this._updateEditButtonNavigationItemState();
165
166         this.hideEmptyContentPlaceholder();
167     }
168
169     _addResult(result, index)
170     {
171         this.element.classList.add("has-results");
172
173         if (!this._resultsFolderTreeElement) {
174             this._resultsFolderTreeElement = new WI.FolderTreeElement(WI.UIString("Results"));
175             this.contentTreeOutline.appendChild(this._resultsFolderTreeElement);
176         }
177
178         this._resultsFolderTreeElement.expand();
179
180         let resultFolderTreeElement = new WI.FolderTreeElement(WI.UIString("Run %d").format(index + 1));
181         if (result instanceof WI.AuditTestResultBase) {
182             resultFolderTreeElement.subtitle = WI.UIString("Imported");
183             result = [result];
184         }
185         this._resultsFolderTreeElement.appendChild(resultFolderTreeElement);
186
187         console.assert(this._resultsFolderTreeElement.children.length === WI.auditManager.results.length);
188
189         for (let resultItem of result)
190             resultFolderTreeElement.appendChild(new WI.AuditTreeElement(resultItem));
191
192         this._updateStartStopButtonNavigationItemState();
193         this._updateEditButtonNavigationItemState();
194     }
195
196     _updateStartStopButtonNavigationItemState()
197     {
198         this._startStopButtonNavigationItem.toggled = WI.auditManager.runningState === WI.AuditManager.RunningState.Active || WI.auditManager.runningState === WI.AuditManager.RunningState.Stopping;
199         this._startStopButtonNavigationItem.enabled = WI.auditManager.tests.length && (WI.auditManager.runningState === WI.AuditManager.RunningState.Inactive || WI.auditManager.runningState === WI.AuditManager.RunningState.Active);
200     }
201
202      _updateEditButtonNavigationItemState()
203     {
204         this._editButtonNavigationItem.label = WI.auditManager.editing ? this._editButtonNavigationItem.activatedToolTip : this._editButtonNavigationItem.defaultToolTip;
205         this._editButtonNavigationItem.activated = WI.auditManager.editing;
206         this._editButtonNavigationItem.enabled = WI.auditManager.tests.length && (WI.auditManager.editing || WI.auditManager.runningState === WI.AuditManager.RunningState.Inactive);
207     }
208
209     _updateNoAuditsPlaceholder()
210     {
211         if (WI.auditManager.tests.length)
212             return;
213
214         let contentPlaceholder = WI.createMessageTextView(WI.UIString("No Audits"));
215
216         let defaultButtonElement = contentPlaceholder.appendChild(document.createElement("button"));
217         defaultButtonElement.textContent = WI.UIString("Add Default Audits");
218         defaultButtonElement.addEventListener("click", () => {
219             WI.auditManager.addDefaultTestsIfNeeded();
220         });
221
222         contentPlaceholder = this.showEmptyContentPlaceholder(contentPlaceholder);
223
224         if (WI.auditManager.results.length) {
225             console.assert(this.contentTreeOutline.children[0] === this._resultsFolderTreeElement);
226
227             // Move the placeholder to be the first element in the content area, where it will
228             // be styled such that only the button is visible.
229             this.contentView.element.insertBefore(contentPlaceholder, this.contentView.element.firstChild);
230         }
231
232         this._updateEditButtonNavigationItemState();
233     }
234
235     _handleAuditManagerEditingChanged(event)
236     {
237         if (WI.auditManager.editing) {
238             console.assert(!this._selectedTreeElementBeforeEditing);
239             this._selectedTreeElementBeforeEditing = this.contentTreeOutline.selectedTreeElement;
240             if (this._selectedTreeElementBeforeEditing)
241                 this._selectedTreeElementBeforeEditing.deselect();
242         } else if (this._selectedTreeElementBeforeEditing) {
243             if (!(this._selectedTreeElementBeforeEditing.representedObject instanceof WI.AuditTestBase) || !this._selectedTreeElementBeforeEditing.representedObject.disabled)
244                 this._selectedTreeElementBeforeEditing.select();
245             this._selectedTreeElementBeforeEditing = null;
246         }
247
248         if (!this.contentTreeOutline.selectedTreeElement)
249             this.showDefaultContentView();
250
251         this._updateStartStopButtonNavigationItemState();
252         this._updateEditButtonNavigationItemState();
253
254         this.updateFilter();
255     }
256
257     _handleAuditTestAdded(event)
258     {
259         this._addTest(event.data.test);
260     }
261
262     _handleAuditTestCompleted(event)
263     {
264         let {result, index} = event.data;
265         this._addResult(result, index);
266     }
267
268     _handleAuditTestRemoved(event)
269     {
270         let {test} = event.data;
271         let treeElement = this.treeElementForRepresentedObject(test);
272         this.contentTreeOutline.removeChild(treeElement);
273
274         this.element.classList.toggle("has-tests", !!WI.auditManager.tests.length);
275
276         this._updateStartStopButtonNavigationItemState();
277         this._updateNoAuditsPlaceholder();
278     }
279
280     _handleAuditTestScheduled(event)
281     {
282         this._updateStartStopButtonNavigationItemState();
283         this._updateEditButtonNavigationItemState();
284     }
285
286     _treeSelectionDidChange(event)
287     {
288         if (!this.selected)
289             return;
290
291         let treeElement = this.contentTreeOutline.selectedTreeElement;
292         if (!treeElement || treeElement instanceof WI.FolderTreeElement) {
293             this.showDefaultContentView();
294             return;
295         }
296
297         console.assert(!WI.auditManager.editing);
298         if (WI.auditManager.editing)
299             return;
300
301         let representedObject = treeElement.representedObject;
302         if (representedObject instanceof WI.AuditTestCase || representedObject instanceof WI.AuditTestGroup
303             || representedObject instanceof WI.AuditTestCaseResult || representedObject instanceof WI.AuditTestGroupResult) {
304             WI.showRepresentedObject(representedObject);
305             return;
306         }
307
308         console.error("Unknown tree element", treeElement);
309     }
310
311     _handleStartStopButtonNavigationItemClicked(event)
312     {
313         if (WI.auditManager.runningState === WI.AuditManager.RunningState.Inactive)
314             WI.auditManager.start();
315         else if (WI.auditManager.runningState === WI.AuditManager.RunningState.Active)
316             WI.auditManager.stop();
317
318         this._updateStartStopButtonNavigationItemState();
319     }
320
321     _handleImportButtonNavigationItemClicked(event)
322     {
323         WI.FileUtilities.importJSON((result) => WI.auditManager.processJSON(result));
324     }
325
326     _handleEditButtonNavigationItemClicked(event)
327     {
328         WI.auditManager.editing = !WI.auditManager.editing;
329     }
330 };