AuditLauncherView.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Copyright (C) 2011 Google 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 are
  6. * met:
  7. *
  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
  13. * distribution.
  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.
  17. *
  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.
  29. */
  30. /**
  31. * @constructor
  32. * @param {WebInspector.AuditController} auditController
  33. * @extends {WebInspector.View}
  34. */
  35. WebInspector.AuditLauncherView = function(auditController)
  36. {
  37. WebInspector.View.call(this);
  38. this._auditController = auditController;
  39. this._categoryIdPrefix = "audit-category-item-";
  40. this._auditRunning = false;
  41. this.element.addStyleClass("audit-launcher-view");
  42. this.element.addStyleClass("panel-enabler-view");
  43. this._contentElement = document.createElement("div");
  44. this._contentElement.className = "audit-launcher-view-content";
  45. this.element.appendChild(this._contentElement);
  46. this._boundCategoryClickListener = this._categoryClicked.bind(this);
  47. this._resetResourceCount();
  48. this._sortedCategories = [];
  49. this._headerElement = document.createElement("h1");
  50. this._headerElement.className = "no-audits";
  51. this._headerElement.textContent = WebInspector.UIString("No audits to run");
  52. this._contentElement.appendChild(this._headerElement);
  53. WebInspector.networkManager.addEventListener(WebInspector.NetworkManager.EventTypes.RequestStarted, this._onRequestStarted, this);
  54. WebInspector.networkManager.addEventListener(WebInspector.NetworkManager.EventTypes.RequestFinished, this._onRequestFinished, this);
  55. var defaultSelectedAuditCategory = {};
  56. defaultSelectedAuditCategory[WebInspector.AuditLauncherView.AllCategoriesKey] = true;
  57. this._selectedCategoriesSetting = WebInspector.settings.createSetting("selectedAuditCategories", defaultSelectedAuditCategory);
  58. }
  59. WebInspector.AuditLauncherView.AllCategoriesKey = "__AllCategories";
  60. WebInspector.AuditLauncherView.prototype = {
  61. _resetResourceCount: function()
  62. {
  63. this._loadedResources = 0;
  64. this._totalResources = 0;
  65. },
  66. _onRequestStarted: function(event)
  67. {
  68. var request = /** @type {WebInspector.NetworkRequest} */ (event.data);
  69. // Ignore long-living WebSockets for the sake of progress indicator, as we won't be waiting them anyway.
  70. if (request.type === WebInspector.resourceTypes.WebSocket)
  71. return;
  72. ++this._totalResources;
  73. this._updateResourceProgress();
  74. },
  75. _onRequestFinished: function(event)
  76. {
  77. var request = /** @type {WebInspector.NetworkRequest} */ (event.data);
  78. // See resorceStarted for details.
  79. if (request.type === WebInspector.resourceTypes.WebSocket)
  80. return;
  81. ++this._loadedResources;
  82. this._updateResourceProgress();
  83. },
  84. /**
  85. * @param {!WebInspector.AuditCategory} category
  86. */
  87. addCategory: function(category)
  88. {
  89. if (!this._sortedCategories.length)
  90. this._createLauncherUI();
  91. var selectedCategories = this._selectedCategoriesSetting.get();
  92. var categoryElement = this._createCategoryElement(category.displayName, category.id);
  93. category._checkboxElement = categoryElement.firstChild;
  94. if (this._selectAllCheckboxElement.checked || selectedCategories[category.displayName]) {
  95. category._checkboxElement.checked = true;
  96. ++this._currentCategoriesCount;
  97. }
  98. /**
  99. * @param {!WebInspector.AuditCategory} a
  100. * @param {!WebInspector.AuditCategory} b
  101. * @return {number}
  102. */
  103. function compareCategories(a, b)
  104. {
  105. var aTitle = a.displayName || "";
  106. var bTitle = b.displayName || "";
  107. return aTitle.localeCompare(bTitle);
  108. }
  109. var insertBefore = insertionIndexForObjectInListSortedByFunction(category, this._sortedCategories, compareCategories);
  110. this._categoriesElement.insertBefore(categoryElement, this._categoriesElement.children[insertBefore]);
  111. this._sortedCategories.splice(insertBefore, 0, category);
  112. this._selectedCategoriesUpdated();
  113. },
  114. /**
  115. * @param {boolean} auditRunning
  116. */
  117. _setAuditRunning: function(auditRunning)
  118. {
  119. if (this._auditRunning === auditRunning)
  120. return;
  121. this._auditRunning = auditRunning;
  122. this._updateButton();
  123. this._toggleUIComponents(this._auditRunning);
  124. if (this._auditRunning)
  125. this._startAudit();
  126. else
  127. this._stopAudit();
  128. },
  129. _startAudit: function()
  130. {
  131. var catIds = [];
  132. for (var category = 0; category < this._sortedCategories.length; ++category) {
  133. if (this._sortedCategories[category]._checkboxElement.checked)
  134. catIds.push(this._sortedCategories[category].id);
  135. }
  136. this._resetResourceCount();
  137. this._progressIndicator = new WebInspector.ProgressIndicator();
  138. this._buttonContainerElement.appendChild(this._progressIndicator.element);
  139. this._displayResourceLoadingProgress = true;
  140. function onAuditStarted()
  141. {
  142. this._displayResourceLoadingProgress = false;
  143. }
  144. this._auditController.initiateAudit(catIds, this._progressIndicator, this._auditPresentStateElement.checked, onAuditStarted.bind(this), this._setAuditRunning.bind(this, false));
  145. },
  146. _stopAudit: function()
  147. {
  148. this._displayResourceLoadingProgress = false;
  149. this._progressIndicator.cancel();
  150. this._progressIndicator.done();
  151. delete this._progressIndicator;
  152. },
  153. /**
  154. * @param {boolean} disable
  155. */
  156. _toggleUIComponents: function(disable)
  157. {
  158. this._selectAllCheckboxElement.disabled = disable;
  159. this._categoriesElement.disabled = disable;
  160. this._auditPresentStateElement.disabled = disable;
  161. this._auditReloadedStateElement.disabled = disable;
  162. },
  163. _launchButtonClicked: function(event)
  164. {
  165. this._setAuditRunning(!this._auditRunning);
  166. },
  167. /**
  168. * @param {boolean} checkCategories
  169. * @param {boolean=} userGesture
  170. */
  171. _selectAllClicked: function(checkCategories, userGesture)
  172. {
  173. var childNodes = this._categoriesElement.childNodes;
  174. for (var i = 0, length = childNodes.length; i < length; ++i)
  175. childNodes[i].firstChild.checked = checkCategories;
  176. this._currentCategoriesCount = checkCategories ? this._sortedCategories.length : 0;
  177. this._selectedCategoriesUpdated(userGesture);
  178. },
  179. _categoryClicked: function(event)
  180. {
  181. this._currentCategoriesCount += event.target.checked ? 1 : -1;
  182. this._selectAllCheckboxElement.checked = this._currentCategoriesCount === this._sortedCategories.length;
  183. this._selectedCategoriesUpdated(true);
  184. },
  185. /**
  186. * @param {string} title
  187. * @param {string} id
  188. */
  189. _createCategoryElement: function(title, id)
  190. {
  191. var labelElement = document.createElement("label");
  192. labelElement.id = this._categoryIdPrefix + id;
  193. var element = document.createElement("input");
  194. element.type = "checkbox";
  195. if (id !== "")
  196. element.addEventListener("click", this._boundCategoryClickListener, false);
  197. labelElement.appendChild(element);
  198. labelElement.appendChild(document.createTextNode(title));
  199. labelElement.__displayName = title;
  200. return labelElement;
  201. },
  202. _createLauncherUI: function()
  203. {
  204. this._headerElement = document.createElement("h1");
  205. this._headerElement.textContent = WebInspector.UIString("Select audits to run");
  206. for (var child = 0; child < this._contentElement.children.length; ++child)
  207. this._contentElement.removeChild(this._contentElement.children[child]);
  208. this._contentElement.appendChild(this._headerElement);
  209. function handleSelectAllClick(event)
  210. {
  211. this._selectAllClicked(event.target.checked, true);
  212. }
  213. var categoryElement = this._createCategoryElement(WebInspector.UIString("Select All"), "");
  214. categoryElement.id = "audit-launcher-selectall";
  215. this._selectAllCheckboxElement = categoryElement.firstChild;
  216. this._selectAllCheckboxElement.checked = this._selectedCategoriesSetting.get()[WebInspector.AuditLauncherView.AllCategoriesKey];
  217. this._selectAllCheckboxElement.addEventListener("click", handleSelectAllClick.bind(this), false);
  218. this._contentElement.appendChild(categoryElement);
  219. this._categoriesElement = this._contentElement.createChild("fieldset", "audit-categories-container");
  220. this._currentCategoriesCount = 0;
  221. this._contentElement.createChild("div", "flexible-space");
  222. this._buttonContainerElement = this._contentElement.createChild("div", "button-container");
  223. var labelElement = this._buttonContainerElement.createChild("label");
  224. this._auditPresentStateElement = labelElement.createChild("input");
  225. this._auditPresentStateElement.name = "audit-mode";
  226. this._auditPresentStateElement.type = "radio";
  227. this._auditPresentStateElement.checked = true;
  228. this._auditPresentStateLabelElement = document.createTextNode(WebInspector.UIString("Audit Present State"));
  229. labelElement.appendChild(this._auditPresentStateLabelElement);
  230. labelElement = this._buttonContainerElement.createChild("label");
  231. this._auditReloadedStateElement = labelElement.createChild("input");
  232. this._auditReloadedStateElement.name = "audit-mode";
  233. this._auditReloadedStateElement.type = "radio";
  234. labelElement.appendChild(document.createTextNode("Reload Page and Audit on Load"));
  235. this._launchButton = this._buttonContainerElement.createChild("button");
  236. this._launchButton.textContent = WebInspector.UIString("Run");
  237. this._launchButton.addEventListener("click", this._launchButtonClicked.bind(this), false);
  238. this._selectAllClicked(this._selectAllCheckboxElement.checked);
  239. },
  240. _updateResourceProgress: function()
  241. {
  242. if (this._displayResourceLoadingProgress)
  243. this._progressIndicator.setTitle(WebInspector.UIString("Loading (%d of %d)", this._loadedResources, this._totalResources));
  244. },
  245. /**
  246. * @param {boolean=} userGesture
  247. */
  248. _selectedCategoriesUpdated: function(userGesture)
  249. {
  250. // Save present categories only upon user gesture to clean up junk from past versions and removed extensions.
  251. // Do not remove old categories if not handling a user gesture, as there's chance categories will be added
  252. // later during start-up.
  253. var selectedCategories = userGesture ? {} : this._selectedCategoriesSetting.get();
  254. var childNodes = this._categoriesElement.childNodes;
  255. for (var i = 0, length = childNodes.length; i < length; ++i)
  256. selectedCategories[childNodes[i].__displayName] = childNodes[i].firstChild.checked;
  257. selectedCategories[WebInspector.AuditLauncherView.AllCategoriesKey] = this._selectAllCheckboxElement.checked;
  258. this._selectedCategoriesSetting.set(selectedCategories);
  259. this._updateButton();
  260. },
  261. _updateButton: function()
  262. {
  263. this._launchButton.textContent = this._auditRunning ? WebInspector.UIString("Stop") : WebInspector.UIString("Run");
  264. this._launchButton.disabled = !this._currentCategoriesCount;
  265. },
  266. __proto__: WebInspector.View.prototype
  267. }