AuditResultView.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (C) 2009 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. * @extends {WebInspector.SidebarPaneStack}
  33. * @param {!Array.<!WebInspector.AuditCategoryResult>} categoryResults
  34. */
  35. WebInspector.AuditResultView = function(categoryResults)
  36. {
  37. WebInspector.SidebarPaneStack.call(this);
  38. this.element.addStyleClass("audit-result-view");
  39. function categorySorter(a, b) {
  40. return (a.title || "").localeCompare(b.title || "");
  41. }
  42. categoryResults.sort(categorySorter);
  43. for (var i = 0; i < categoryResults.length; ++i)
  44. this.addPane(new WebInspector.AuditCategoryResultPane(categoryResults[i]));
  45. }
  46. WebInspector.AuditResultView.prototype = {
  47. __proto__: WebInspector.SidebarPaneStack.prototype
  48. }
  49. /**
  50. * @constructor
  51. * @extends {WebInspector.SidebarPane}
  52. * @param {!WebInspector.AuditCategoryResult} categoryResult
  53. */
  54. WebInspector.AuditCategoryResultPane = function(categoryResult)
  55. {
  56. WebInspector.SidebarPane.call(this, categoryResult.title);
  57. var treeOutlineElement = document.createElement("ol");
  58. this.bodyElement.addStyleClass("audit-result-tree");
  59. this.bodyElement.appendChild(treeOutlineElement);
  60. this._treeOutline = new TreeOutline(treeOutlineElement);
  61. this._treeOutline.expandTreeElementsWhenArrowing = true;
  62. function ruleSorter(a, b)
  63. {
  64. var result = WebInspector.AuditRule.SeverityOrder[a.severity || 0] - WebInspector.AuditRule.SeverityOrder[b.severity || 0];
  65. if (!result)
  66. result = (a.value || "").localeCompare(b.value || "");
  67. return result;
  68. }
  69. categoryResult.ruleResults.sort(ruleSorter);
  70. for (var i = 0; i < categoryResult.ruleResults.length; ++i) {
  71. var ruleResult = categoryResult.ruleResults[i];
  72. var treeElement = this._appendResult(this._treeOutline, ruleResult);
  73. treeElement.listItemElement.addStyleClass("audit-result");
  74. if (ruleResult.severity) {
  75. var severityElement = document.createElement("div");
  76. severityElement.className = "severity-" + ruleResult.severity;
  77. treeElement.listItemElement.appendChild(severityElement);
  78. }
  79. }
  80. this.expand();
  81. }
  82. WebInspector.AuditCategoryResultPane.prototype = {
  83. /**
  84. * @param {(TreeOutline|TreeElement)} parentTreeElement
  85. * @param {!WebInspector.AuditRuleResult} result
  86. */
  87. _appendResult: function(parentTreeElement, result)
  88. {
  89. var title = "";
  90. if (typeof result.value === "string") {
  91. title = result.value;
  92. if (result.violationCount)
  93. title = String.sprintf("%s (%d)", title, result.violationCount);
  94. }
  95. var treeElement = new TreeElement(null, null, !!result.children);
  96. treeElement.title = title;
  97. parentTreeElement.appendChild(treeElement);
  98. if (result.className)
  99. treeElement.listItemElement.addStyleClass(result.className);
  100. if (typeof result.value !== "string")
  101. treeElement.listItemElement.appendChild(WebInspector.auditFormatters.apply(result.value));
  102. if (result.children) {
  103. for (var i = 0; i < result.children.length; ++i)
  104. this._appendResult(treeElement, result.children[i]);
  105. }
  106. if (result.expanded) {
  107. treeElement.listItemElement.removeStyleClass("parent");
  108. treeElement.listItemElement.addStyleClass("parent-expanded");
  109. treeElement.expand();
  110. }
  111. return treeElement;
  112. },
  113. __proto__: WebInspector.SidebarPane.prototype
  114. }