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