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