2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * @param {!WebInspector.AuditController} auditController
34 * @extends {WebInspector.VBox}
36 WebInspector.AuditLauncherView = function(auditController)
38 WebInspector.VBox.call(this);
39 this.setMinimumSize(100, 25);
41 this._auditController = auditController;
43 this._categoryIdPrefix = "audit-category-item-";
44 this._auditRunning = false;
46 this.element.classList.add("audit-launcher-view");
47 this.element.classList.add("panel-enabler-view");
49 this._contentElement = document.createElement("div");
50 this._contentElement.className = "audit-launcher-view-content";
51 this.element.appendChild(this._contentElement);
52 this._boundCategoryClickListener = this._categoryClicked.bind(this);
54 this._resetResourceCount();
56 this._sortedCategories = [];
58 this._headerElement = document.createElement("h1");
59 this._headerElement.className = "no-audits";
60 this._headerElement.textContent = WebInspector.UIString("No audits to run");
61 this._contentElement.appendChild(this._headerElement);
63 WebInspector.networkManager.addEventListener(WebInspector.NetworkManager.EventTypes.RequestStarted, this._onRequestStarted, this);
64 WebInspector.networkManager.addEventListener(WebInspector.NetworkManager.EventTypes.RequestFinished, this._onRequestFinished, this);
66 var defaultSelectedAuditCategory = {};
67 defaultSelectedAuditCategory[WebInspector.AuditLauncherView.AllCategoriesKey] = true;
68 this._selectedCategoriesSetting = WebInspector.settings.createSetting("selectedAuditCategories", defaultSelectedAuditCategory);
71 WebInspector.AuditLauncherView.AllCategoriesKey = "__AllCategories";
73 WebInspector.AuditLauncherView.prototype = {
74 _resetResourceCount: function()
76 this._loadedResources = 0;
77 this._totalResources = 0;
80 _onRequestStarted: function(event)
82 var request = /** @type {!WebInspector.NetworkRequest} */ (event.data);
83 // Ignore long-living WebSockets for the sake of progress indicator, as we won't be waiting them anyway.
84 if (request.type === WebInspector.resourceTypes.WebSocket)
86 ++this._totalResources;
87 this._updateResourceProgress();
90 _onRequestFinished: function(event)
92 var request = /** @type {!WebInspector.NetworkRequest} */ (event.data);
93 // See resorceStarted for details.
94 if (request.type === WebInspector.resourceTypes.WebSocket)
96 ++this._loadedResources;
97 this._updateResourceProgress();
101 * @param {!WebInspector.AuditCategory} category
103 addCategory: function(category)
105 if (!this._sortedCategories.length)
106 this._createLauncherUI();
108 var selectedCategories = this._selectedCategoriesSetting.get();
109 var categoryElement = this._createCategoryElement(category.displayName, category.id);
110 category._checkboxElement = categoryElement.firstChild;
111 if (this._selectAllCheckboxElement.checked || selectedCategories[category.displayName]) {
112 category._checkboxElement.checked = true;
113 ++this._currentCategoriesCount;
117 * @param {!WebInspector.AuditCategory} a
118 * @param {!WebInspector.AuditCategory} b
121 function compareCategories(a, b)
123 var aTitle = a.displayName || "";
124 var bTitle = b.displayName || "";
125 return aTitle.localeCompare(bTitle);
127 var insertBefore = insertionIndexForObjectInListSortedByFunction(category, this._sortedCategories, compareCategories);
128 this._categoriesElement.insertBefore(categoryElement, this._categoriesElement.children[insertBefore]);
129 this._sortedCategories.splice(insertBefore, 0, category);
130 this._selectedCategoriesUpdated();
134 * @param {boolean} auditRunning
136 _setAuditRunning: function(auditRunning)
138 if (this._auditRunning === auditRunning)
140 this._auditRunning = auditRunning;
141 this._updateButton();
142 this._toggleUIComponents(this._auditRunning);
143 if (this._auditRunning)
149 _startAudit: function()
152 for (var category = 0; category < this._sortedCategories.length; ++category) {
153 if (this._sortedCategories[category]._checkboxElement.checked)
154 catIds.push(this._sortedCategories[category].id);
157 this._resetResourceCount();
158 this._progressIndicator = new WebInspector.ProgressIndicator();
159 this._buttonContainerElement.appendChild(this._progressIndicator.element);
160 this._displayResourceLoadingProgress = true;
163 * @this {WebInspector.AuditLauncherView}
165 function onAuditStarted()
167 this._displayResourceLoadingProgress = false;
169 this._auditController.initiateAudit(catIds, this._progressIndicator, this._auditPresentStateElement.checked, onAuditStarted.bind(this), this._setAuditRunning.bind(this, false));
172 _stopAudit: function()
174 this._displayResourceLoadingProgress = false;
175 this._progressIndicator.cancel();
176 this._progressIndicator.done();
177 delete this._progressIndicator;
181 * @param {boolean} disable
183 _toggleUIComponents: function(disable)
185 this._selectAllCheckboxElement.disabled = disable;
186 this._categoriesElement.disabled = disable;
187 this._auditPresentStateElement.disabled = disable;
188 this._auditReloadedStateElement.disabled = disable;
191 _launchButtonClicked: function(event)
193 this._setAuditRunning(!this._auditRunning);
196 _clearButtonClicked: function()
198 this._auditController.clearResults();
202 * @param {boolean} checkCategories
203 * @param {boolean=} userGesture
205 _selectAllClicked: function(checkCategories, userGesture)
207 var childNodes = this._categoriesElement.childNodes;
208 for (var i = 0, length = childNodes.length; i < length; ++i)
209 childNodes[i].firstChild.checked = checkCategories;
210 this._currentCategoriesCount = checkCategories ? this._sortedCategories.length : 0;
211 this._selectedCategoriesUpdated(userGesture);
214 _categoryClicked: function(event)
216 this._currentCategoriesCount += event.target.checked ? 1 : -1;
217 this._selectAllCheckboxElement.checked = this._currentCategoriesCount === this._sortedCategories.length;
218 this._selectedCategoriesUpdated(true);
222 * @param {string} title
225 _createCategoryElement: function(title, id)
227 var labelElement = document.createElement("label");
228 labelElement.id = this._categoryIdPrefix + id;
230 var element = document.createElement("input");
231 element.type = "checkbox";
233 element.addEventListener("click", this._boundCategoryClickListener, false);
234 labelElement.appendChild(element);
235 labelElement.appendChild(document.createTextNode(title));
236 labelElement.__displayName = title;
241 _createLauncherUI: function()
243 this._headerElement = document.createElement("h1");
244 this._headerElement.textContent = WebInspector.UIString("Select audits to run");
246 for (var child = 0; child < this._contentElement.children.length; ++child)
247 this._contentElement.removeChild(this._contentElement.children[child]);
249 this._contentElement.appendChild(this._headerElement);
252 * @param {?Event} event
253 * @this {WebInspector.AuditLauncherView}
255 function handleSelectAllClick(event)
257 this._selectAllClicked(event.target.checked, true);
259 var categoryElement = this._createCategoryElement(WebInspector.UIString("Select All"), "");
260 categoryElement.id = "audit-launcher-selectall";
261 this._selectAllCheckboxElement = categoryElement.firstChild;
262 this._selectAllCheckboxElement.checked = this._selectedCategoriesSetting.get()[WebInspector.AuditLauncherView.AllCategoriesKey];
263 this._selectAllCheckboxElement.addEventListener("click", handleSelectAllClick.bind(this), false);
264 this._contentElement.appendChild(categoryElement);
266 this._categoriesElement = this._contentElement.createChild("fieldset", "audit-categories-container");
267 this._currentCategoriesCount = 0;
269 this._contentElement.createChild("div", "flexible-space");
271 this._buttonContainerElement = this._contentElement.createChild("div", "button-container");
273 var labelElement = this._buttonContainerElement.createChild("label");
274 this._auditPresentStateElement = labelElement.createChild("input");
275 this._auditPresentStateElement.name = "audit-mode";
276 this._auditPresentStateElement.type = "radio";
277 this._auditPresentStateElement.checked = true;
278 this._auditPresentStateLabelElement = document.createTextNode(WebInspector.UIString("Audit Present State"));
279 labelElement.appendChild(this._auditPresentStateLabelElement);
281 labelElement = this._buttonContainerElement.createChild("label");
282 this._auditReloadedStateElement = labelElement.createChild("input");
283 this._auditReloadedStateElement.name = "audit-mode";
284 this._auditReloadedStateElement.type = "radio";
285 labelElement.appendChild(document.createTextNode("Reload Page and Audit on Load"));
287 this._launchButton = this._buttonContainerElement.createChild("button");
288 this._launchButton.textContent = WebInspector.UIString("Run");
289 this._launchButton.addEventListener("click", this._launchButtonClicked.bind(this), false);
291 this._clearButton = this._buttonContainerElement.createChild("button");
292 this._clearButton.textContent = WebInspector.UIString("Clear");
293 this._clearButton.addEventListener("click", this._clearButtonClicked.bind(this), false);
295 this._selectAllClicked(this._selectAllCheckboxElement.checked);
298 _updateResourceProgress: function()
300 if (this._displayResourceLoadingProgress)
301 this._progressIndicator.setTitle(WebInspector.UIString("Loading (%d of %d)", this._loadedResources, this._totalResources));
305 * @param {boolean=} userGesture
307 _selectedCategoriesUpdated: function(userGesture)
309 // Save present categories only upon user gesture to clean up junk from past versions and removed extensions.
310 // Do not remove old categories if not handling a user gesture, as there's chance categories will be added
311 // later during start-up.
312 var selectedCategories = userGesture ? {} : this._selectedCategoriesSetting.get();
313 var childNodes = this._categoriesElement.childNodes;
314 for (var i = 0, length = childNodes.length; i < length; ++i)
315 selectedCategories[childNodes[i].__displayName] = childNodes[i].firstChild.checked;
316 selectedCategories[WebInspector.AuditLauncherView.AllCategoriesKey] = this._selectAllCheckboxElement.checked;
317 this._selectedCategoriesSetting.set(selectedCategories);
318 this._updateButton();
321 _updateButton: function()
323 this._launchButton.textContent = this._auditRunning ? WebInspector.UIString("Stop") : WebInspector.UIString("Run");
324 this._launchButton.disabled = !this._currentCategoriesCount;
327 __proto__: WebInspector.VBox.prototype