WebInspector.AuditsPanel = function()
{
WebInspector.Panel.call(this, "audits");
this.registerRequiredCSS("panelEnablerView.css");
this.registerRequiredCSS("auditsPanel.css");
this.createSidebarViewWithTree();
this.auditsTreeElement = new WebInspector.SidebarSectionTreeElement("", {}, true);
this.sidebarTree.appendChild(this.auditsTreeElement);
this.auditsTreeElement.listItemElement.addStyleClass("hidden");
this.auditsItemTreeElement = new WebInspector.AuditsSidebarTreeElement(this);
this.auditsTreeElement.appendChild(this.auditsItemTreeElement);
this.auditResultsTreeElement = new WebInspector.SidebarSectionTreeElement(WebInspector.UIString("RESULTS"), {}, true);
this.sidebarTree.appendChild(this.auditResultsTreeElement);
this.auditResultsTreeElement.expand();
this.clearResultsButton = new WebInspector.StatusBarButton(WebInspector.UIString("Clear audit results."), "clear-status-bar-item");
this.clearResultsButton.addEventListener("click", this._clearButtonClicked, this);
this.viewsContainerElement = this.splitView.mainElement;
this._constructCategories();
this._auditController = new WebInspector.AuditController(this);
this._launcherView = new WebInspector.AuditLauncherView(this._auditController);
for (var id in this.categoriesById)
this._launcherView.addCategory(this.categoriesById[id]);
}
WebInspector.AuditsPanel.prototype = {
statusBarItems: function()
{
return [this.clearResultsButton.element];
},
get categoriesById()
{
return this._auditCategoriesById;
},
addCategory: function(category)
{
this.categoriesById[category.id] = category;
this._launcherView.addCategory(category);
},
getCategory: function(id)
{
return this.categoriesById[id];
},
_constructCategories: function()
{
this._auditCategoriesById = {};
for (var categoryCtorID in WebInspector.AuditCategories) {
var auditCategory = new WebInspector.AuditCategories[categoryCtorID]();
auditCategory._id = categoryCtorID;
this.categoriesById[categoryCtorID] = auditCategory;
}
},
auditFinishedCallback: function(mainResourceURL, results)
{
var children = this.auditResultsTreeElement.children;
var ordinal = 1;
for (var i = 0; i < children.length; ++i) {
if (children[i].mainResourceURL === mainResourceURL)
ordinal++;
}
var resultTreeElement = new WebInspector.AuditResultSidebarTreeElement(this, results, mainResourceURL, ordinal);
this.auditResultsTreeElement.appendChild(resultTreeElement);
resultTreeElement.revealAndSelect();
},
showResults: function(categoryResults)
{
if (!categoryResults._resultView)
categoryResults._resultView = new WebInspector.AuditResultView(categoryResults);
this.visibleView = categoryResults._resultView;
},
showLauncherView: function()
{
this.visibleView = this._launcherView;
},
get visibleView()
{
return this._visibleView;
},
set visibleView(x)
{
if (this._visibleView === x)
return;
if (this._visibleView)
this._visibleView.detach();
this._visibleView = x;
if (x)
x.show(this.viewsContainerElement);
},
wasShown: function()
{
WebInspector.Panel.prototype.wasShown.call(this);
if (!this._visibleView)
this.auditsItemTreeElement.select();
},
_clearButtonClicked: function()
{
this.auditsItemTreeElement.revealAndSelect();
this.auditResultsTreeElement.removeChildren();
},
__proto__: WebInspector.Panel.prototype
}
WebInspector.AuditCategory = function(displayName)
{
this._displayName = displayName;
this._rules = [];
}
WebInspector.AuditCategory.prototype = {
get id()
{
return this._id;
},
get displayName()
{
return this._displayName;
},
addRule: function(rule, severity)
{
rule.severity = severity;
this._rules.push(rule);
},
run: function(requests, ruleResultCallback, categoryDoneCallback, progress)
{
this._ensureInitialized();
var remainingRulesCount = this._rules.length;
progress.setTotalWork(remainingRulesCount);
function callbackWrapper(result)
{
ruleResultCallback(result);
progress.worked();
if (!--remainingRulesCount)
categoryDoneCallback();
}
for (var i = 0; i < this._rules.length; ++i)
this._rules[i].run(requests, callbackWrapper, progress);
},
_ensureInitialized: function()
{
if (!this._initialized) {
if ("initialize" in this)
this.initialize();
this._initialized = true;
}
}
}
WebInspector.AuditRule = function(id, displayName)
{
this._id = id;
this._displayName = displayName;
}
WebInspector.AuditRule.Severity = {
Info: "info",
Warning: "warning",
Severe: "severe"
}
WebInspector.AuditRule.SeverityOrder = {
"info": 3,
"warning": 2,
"severe": 1
}
WebInspector.AuditRule.prototype = {
get id()
{
return this._id;
},
get displayName()
{
return this._displayName;
},
set severity(severity)
{
this._severity = severity;
},
run: function(requests, callback, progress)
{
if (progress.isCanceled())
return;
var result = new WebInspector.AuditRuleResult(this.displayName);
result.severity = this._severity;
this.doRun(requests, result, callback, progress);
},
doRun: function(requests, result, callback, progress)
{
throw new Error("doRun() not implemented");
}
}
WebInspector.AuditCategoryResult = function(category)
{
this.title = category.displayName;
this.ruleResults = [];
}
WebInspector.AuditCategoryResult.prototype = {
addRuleResult: function(ruleResult)
{
this.ruleResults.push(ruleResult);
}
}
WebInspector.AuditRuleResult = function(value, expanded, className)
{
this.value = value;
this.className = className;
this.expanded = expanded;
this.violationCount = 0;
this._formatters = {
r: WebInspector.AuditRuleResult.linkifyDisplayName
};
var standardFormatters = Object.keys(String.standardFormatters);
for (var i = 0; i < standardFormatters.length; ++i)
this._formatters[standardFormatters[i]] = String.standardFormatters[standardFormatters[i]];
}
WebInspector.AuditRuleResult.linkifyDisplayName = function(url)
{
return WebInspector.linkifyURLAsNode(url, WebInspector.displayNameForURL(url));
}
WebInspector.AuditRuleResult.resourceDomain = function(domain)
{
return domain || WebInspector.UIString("[empty domain]");
}
WebInspector.AuditRuleResult.prototype = {
addChild: function(value, expanded, className)
{
if (!this.children)
this.children = [];
var entry = new WebInspector.AuditRuleResult(value, expanded, className);
this.children.push(entry);
return entry;
},
addURL: function(url)
{
this.addChild(WebInspector.AuditRuleResult.linkifyDisplayName(url));
},
addURLs: function(urls)
{
for (var i = 0; i < urls.length; ++i)
this.addURL(urls[i]);
},
addSnippet: function(snippet)
{
this.addChild(snippet, false, "source-code");
},
addFormatted: function(format, vararg)
{
var substitutions = Array.prototype.slice.call(arguments, 1);
var fragment = document.createDocumentFragment();
function append(a, b)
{
if (!(b instanceof Node))
b = document.createTextNode(b);
a.appendChild(b);
return a;
}
var formattedResult = String.format(format, substitutions, this._formatters, fragment, append).formattedResult;
if (formattedResult instanceof Node)
formattedResult.normalize();
return this.addChild(formattedResult);
}
}
WebInspector.AuditsSidebarTreeElement = function(panel)
{
this._panel = panel;
this.small = false;
WebInspector.SidebarTreeElement.call(this, "audits-sidebar-tree-item", WebInspector.UIString("Audits"), "", null, false);
}
WebInspector.AuditsSidebarTreeElement.prototype = {
onattach: function()
{
WebInspector.SidebarTreeElement.prototype.onattach.call(this);
},
onselect: function()
{
this._panel.showLauncherView();
},
get selectable()
{
return true;
},
refresh: function()
{
this.refreshTitles();
},
__proto__: WebInspector.SidebarTreeElement.prototype
}
WebInspector.AuditResultSidebarTreeElement = function(panel, results, mainResourceURL, ordinal)
{
this._panel = panel;
this.results = results;
this.mainResourceURL = mainResourceURL;
WebInspector.SidebarTreeElement.call(this, "audit-result-sidebar-tree-item", String.sprintf("%s (%d)", mainResourceURL, ordinal), "", {}, false);
}
WebInspector.AuditResultSidebarTreeElement.prototype = {
onselect: function()
{
this._panel.showResults(this.results);
},
get selectable()
{
return true;
},
__proto__: WebInspector.SidebarTreeElement.prototype
}
WebInspector.AuditRules = {};
WebInspector.AuditCategories = {};
importScript("AuditCategories.js");
importScript("AuditController.js");
importScript("AuditFormatters.js");
importScript("AuditLauncherView.js");
importScript("AuditResultView.js");
importScript("AuditRules.js");