home *** CD-ROM | disk | FTP | other *** search
/ Computer Active Guide 2009 July / CAG7.ISO / Internetas / SafariSetup.exe / AppleApplicationSupport.msi / WebKit.resources_inspector_AuditLauncherView.js < prev    next >
Encoding:
Text File  |  2010-06-03  |  11.1 KB  |  285 lines

  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.AuditLauncherView = function(categoriesById, runnerCallback)
  32. {
  33.     WebInspector.View.call(this);
  34.     this._categoriesById = categoriesById;
  35.     this._runnerCallback = runnerCallback;
  36.     this._categoryIdPrefix = "audit-category-item-";
  37.     this._auditRunning = false;
  38.  
  39.     this.element.addStyleClass("audit-launcher-view");
  40.  
  41.     this._contentElement = document.createElement("div");
  42.     this._contentElement.className = "audit-launcher-view-content";
  43.     this.element.appendChild(this._contentElement);
  44.  
  45.     this._resetResourceCount();
  46.  
  47.     function categorySortFunction(a, b)
  48.     {
  49.         var aTitle = a.displayName || "";
  50.         var bTitle = b.displayName || "";
  51.         return aTitle.localeCompare(bTitle);
  52.     }
  53.     var sortedCategories = [];
  54.     for (var id in this._categoriesById)
  55.         sortedCategories.push(this._categoriesById[id]);
  56.     sortedCategories.sort(categorySortFunction);
  57.  
  58.     if (!sortedCategories.length) {
  59.         this._headerElement = document.createElement("h1");
  60.         this._headerElement.className = "no-audits";
  61.         this._headerElement.textContent = WebInspector.UIString("No audits to run");
  62.         this._contentElement.appendChild(this._headerElement);
  63.     } else
  64.         this._createLauncherUI(sortedCategories);
  65. }
  66.  
  67. WebInspector.AuditLauncherView.prototype = {
  68.     updateResourceTrackingState: function(isTracking)
  69.     {
  70.         if (!this._auditPresentStateLabelElement)
  71.             return;
  72.  
  73.         if (isTracking) {
  74.             this._auditPresentStateLabelElement.nodeValue = WebInspector.UIString("Audit Present State");
  75.             this._auditPresentStateElement.disabled = false;
  76.             this._auditPresentStateElement.parentElement.removeStyleClass("disabled");
  77.         } else {
  78.             this._resetResourceCount();
  79.             this._auditPresentStateLabelElement.nodeValue = WebInspector.UIString("Audit Present State (Resource Tracking must be enabled)");
  80.             this._auditPresentStateElement.disabled = true;
  81.             this._auditPresentStateElement.parentElement.addStyleClass("disabled");
  82.             this.auditReloadedStateElement.checked = true;
  83.         }
  84.     },
  85.  
  86.     get totalResources()
  87.     {
  88.         return this._totalResources;
  89.     },
  90.  
  91.     set totalResources(x)
  92.     {
  93.         if (this._totalResources === x)
  94.             return;
  95.         this._totalResources = x;
  96.         this._updateResourceProgress();
  97.     },
  98.  
  99.     get loadedResources()
  100.     {
  101.         return this._loadedResources;
  102.     },
  103.  
  104.     set loadedResources(x)
  105.     {
  106.         if (this._loadedResources === x)
  107.             return;
  108.         this._loadedResources = x;
  109.         this._updateResourceProgress();
  110.     },
  111.  
  112.     _resetResourceCount: function()
  113.     {
  114.         this.loadedResources = 0;
  115.  
  116.         // We never receive a resourceStarted notification for the main resource
  117.         // (see InspectorController.willSendRequest())
  118.         this.totalResources = 1;
  119.     },
  120.  
  121.     resourceStarted: function(resource)
  122.     {
  123.         ++this.totalResources;
  124.     },
  125.  
  126.     resourceFinished: function(resource)
  127.     {
  128.         ++this.loadedResources;
  129.     },
  130.  
  131.     reset: function()
  132.     {
  133.         this._resetResourceCount();
  134.     },
  135.  
  136.     _setAuditRunning: function(auditRunning)
  137.     {
  138.         if (this._auditRunning === auditRunning)
  139.             return;
  140.         this._auditRunning = auditRunning;
  141.         this._updateButton();
  142.         this._updateResourceProgress();
  143.     },
  144.  
  145.     _launchButtonClicked: function(event)
  146.     {
  147.         var catIds = [];
  148.         var childNodes = this._categoriesElement.childNodes;
  149.         for (var id in this._categoriesById) {
  150.             if (this._categoriesById[id]._checkboxElement.checked)
  151.                 catIds.push(id);
  152.         }
  153.         this._setAuditRunning(true);
  154.         this._runnerCallback(catIds, this._auditPresentStateElement.checked, this._setAuditRunning.bind(this, false));
  155.     },
  156.  
  157.     _selectAllClicked: function(checkCategories)
  158.     {
  159.         var childNodes = this._categoriesElement.childNodes;
  160.         for (var i = 0, length = childNodes.length; i < length; ++i)
  161.             childNodes[i].firstChild.checked = checkCategories;
  162.         this._currentCategoriesCount = checkCategories ? this._totalCategoriesCount : 0;
  163.         this._updateButton();
  164.     },
  165.  
  166.     _categoryClicked: function(event)
  167.     {
  168.         this._currentCategoriesCount += event.target.checked ? 1 : -1;
  169.         this._selectAllCheckboxElement.checked = this._currentCategoriesCount === this._totalCategoriesCount;
  170.         this._updateButton();
  171.     },
  172.  
  173.     _createCategoryElement: function(title, id)
  174.     {
  175.         var labelElement = document.createElement("label");
  176.         labelElement.id = this._categoryIdPrefix + id;
  177.  
  178.         var element = document.createElement("input");
  179.         element.type = "checkbox";
  180.         labelElement.appendChild(element);
  181.         labelElement.appendChild(document.createTextNode(title));
  182.  
  183.         return labelElement;
  184.     },
  185.  
  186.     _createLauncherUI: function(sortedCategories)
  187.     {
  188.         this._headerElement = document.createElement("h1");
  189.         this._headerElement.textContent = WebInspector.UIString("Select audits to run");
  190.         this._contentElement.appendChild(this._headerElement);
  191.  
  192.         function handleSelectAllClick(event)
  193.         {
  194.             this._selectAllClicked(event.target.checked);
  195.         }
  196.         var categoryElement = this._createCategoryElement(WebInspector.UIString("Select All"), "");
  197.         categoryElement.id = "audit-launcher-selectall";
  198.         this._selectAllCheckboxElement = categoryElement.firstChild;
  199.         this._selectAllCheckboxElement.checked = true;
  200.         this._selectAllCheckboxElement.addEventListener("click", handleSelectAllClick.bind(this), false);
  201.         this._contentElement.appendChild(categoryElement);
  202.  
  203.         this._categoriesElement = document.createElement("div");
  204.         this._categoriesElement.className = "audit-categories-container";
  205.         this._contentElement.appendChild(this._categoriesElement);
  206.  
  207.         var boundCategoryClickListener = this._categoryClicked.bind(this);
  208.  
  209.         for (var i = 0; i < sortedCategories.length; ++i) {
  210.             categoryElement = this._createCategoryElement(sortedCategories[i].displayName, sortedCategories[i].id);
  211.             categoryElement.firstChild.addEventListener("click", boundCategoryClickListener, false);
  212.             sortedCategories[i]._checkboxElement = categoryElement.firstChild;
  213.             this._categoriesElement.appendChild(categoryElement);
  214.         }
  215.  
  216.         this._totalCategoriesCount = this._categoriesElement.childNodes.length;
  217.         this._currentCategoriesCount = 0;
  218.  
  219.         var flexibleSpaceElement = document.createElement("div");
  220.         flexibleSpaceElement.className = "flexible-space";
  221.         this._contentElement.appendChild(flexibleSpaceElement);
  222.  
  223.         this._buttonContainerElement = document.createElement("div");
  224.         this._buttonContainerElement.className = "button-container";
  225.  
  226.         var labelElement = document.createElement("label");
  227.         this._auditPresentStateElement = document.createElement("input");
  228.         this._auditPresentStateElement.name = "audit-mode";
  229.         this._auditPresentStateElement.type = "radio";
  230.         this._auditPresentStateElement.checked = true;
  231.         this._auditPresentStateLabelElement = document.createTextNode("");
  232.         labelElement.appendChild(this._auditPresentStateElement);
  233.         labelElement.appendChild(this._auditPresentStateLabelElement);
  234.         this._buttonContainerElement.appendChild(labelElement);
  235.  
  236.         labelElement = document.createElement("label");
  237.         this.auditReloadedStateElement = document.createElement("input");
  238.         this.auditReloadedStateElement.name = "audit-mode";
  239.         this.auditReloadedStateElement.type = "radio";
  240.         labelElement.appendChild(this.auditReloadedStateElement);
  241.         labelElement.appendChild(document.createTextNode("Reload Page and Audit on Load"));
  242.         this._buttonContainerElement.appendChild(labelElement);
  243.  
  244.         this._launchButton = document.createElement("button");
  245.         this._launchButton.type = "button";
  246.         this._launchButton.textContent = WebInspector.UIString("Run");
  247.         this._launchButton.addEventListener("click", this._launchButtonClicked.bind(this), false);
  248.         this._buttonContainerElement.appendChild(this._launchButton);
  249.  
  250.         this._resourceProgressContainer = document.createElement("span");
  251.         this._resourceProgressContainer.className = "resource-progress";
  252.         var resourceProgressImage = document.createElement("img");
  253.         this._resourceProgressContainer.appendChild(resourceProgressImage);
  254.         this._resourceProgressTextElement = document.createElement("span");
  255.         this._resourceProgressContainer.appendChild(this._resourceProgressTextElement);
  256.         this._buttonContainerElement.appendChild(this._resourceProgressContainer);
  257.  
  258.         this._contentElement.appendChild(this._buttonContainerElement);
  259.  
  260.         this._selectAllClicked(this._selectAllCheckboxElement.checked);
  261.         this.updateResourceTrackingState();
  262.         this._updateButton();
  263.     },
  264.  
  265.     _updateResourceProgress: function()
  266.     {
  267.         if (!this._resourceProgressContainer)
  268.             return;
  269.  
  270.         if (!this._auditRunning) {
  271.             this._resetResourceCount();
  272.             this._resourceProgressContainer.addStyleClass("hidden");
  273.         } else
  274.             this._resourceProgressContainer.removeStyleClass("hidden");
  275.         this._resourceProgressTextElement.textContent = WebInspector.UIString("Loading (%d of %d)", this.loadedResources, this.totalResources);
  276.     },
  277.  
  278.     _updateButton: function()
  279.     {
  280.         this._launchButton.disabled = !this._currentCategoriesCount || this._auditRunning;
  281.     }
  282. }
  283.  
  284. WebInspector.AuditLauncherView.prototype.__proto__ = WebInspector.View.prototype;
  285.