5658da7cf8cffee52d744afb0d804dab32c1fca4
[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._updateStartStopButtonNavigationItemState();
109
110         this.contentTreeOutline.insertChild(new WI.AuditTreeElement(test), this.contentTreeOutline.children.indexOf(this._resultsFolderTreeElement));
111
112         this._resultsFolderTreeElement.hidden = !this._resultsFolderTreeElement.children.length;
113     }
114
115     _addResult(result, index)
116     {
117         this._updateStartStopButtonNavigationItemState();
118
119         this._resultsFolderTreeElement.hidden = false;
120
121         let resultFolderTreeElement = new WI.FolderTreeElement(WI.UIString("Run %d").format(index + 1));
122         if (result instanceof WI.AuditTestResultBase) {
123             resultFolderTreeElement.subtitle = WI.UIString("Imported");
124             result = [result];
125         }
126         this._resultsFolderTreeElement.appendChild(resultFolderTreeElement);
127
128         for (let resultItem of result)
129             resultFolderTreeElement.appendChild(new WI.AuditTreeElement(resultItem));
130     }
131
132     _updateStartStopButtonNavigationItemState()
133     {
134         this._startStopButtonNavigationItem.toggled = WI.auditManager.runningState !== WI.AuditManager.RunningState.Inactive;
135         this._startStopButtonNavigationItem.enabled = WI.auditManager.tests.length && WI.auditManager.runningState !== WI.AuditManager.RunningState.Stopping;
136     }
137
138     _handleAuditTestAdded(event)
139     {
140         this._addTest(event.data.test);
141     }
142
143     _handleAuditTestCompleted(event)
144     {
145         let {result, index} = event.data;
146         this._addResult(result, index);
147     }
148
149     _handleAuditTestRemoved(event)
150     {
151         let {test} = event.data;
152         let treeElement = this.treeElementForRepresentedObject(test);
153         this.contentTreeOutline.removeChild(treeElement);
154
155         this._updateStartStopButtonNavigationItemState();
156     }
157
158     _handleAuditTestScheduled(event)
159     {
160         this._updateStartStopButtonNavigationItemState();
161     }
162
163     _treeSelectionDidChange(event)
164     {
165         if (!this.selected)
166             return;
167
168         let treeElement = event.data.selectedElement;
169         if (!treeElement || treeElement instanceof WI.FolderTreeElement) {
170             this.showDefaultContentView();
171             return;
172         }
173
174         let representedObject = treeElement.representedObject;
175         if (representedObject instanceof WI.AuditTestCase || representedObject instanceof WI.AuditTestGroup
176             || representedObject instanceof WI.AuditTestCaseResult || representedObject instanceof WI.AuditTestGroupResult) {
177             WI.showRepresentedObject(representedObject);
178             return;
179         }
180
181         console.error("Unknown tree element", treeElement);
182     }
183
184     _handleStartStopButtonNavigationItemClicked(event)
185     {
186         if (WI.auditManager.runningState === WI.AuditManager.RunningState.Inactive)
187             WI.auditManager.start();
188         else if (WI.auditManager.runningState === WI.AuditManager.RunningState.Active)
189             WI.auditManager.stop();
190
191         this._updateStartStopButtonNavigationItemState();
192     }
193
194     _handleImportButtonNavigationItemClicked(event)
195     {
196         WI.FileUtilities.importJSON((result) => WI.auditManager.processJSON(result));
197     }
198 };