tizen beta release
[profile/ivi/webkit-efl.git] / debian / tmp / usr / share / ewebkit-0 / webinspector / AuditLauncherView.js
1 /*
2  * Copyright (C) 2011 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.View}
34  */
35 WebInspector.AuditLauncherView = function(runnerCallback)
36 {
37     WebInspector.View.call(this);
38     this._runnerCallback = runnerCallback;
39     this._categoryIdPrefix = "audit-category-item-";
40     this._auditRunning = false;
41
42     this.element.addStyleClass("audit-launcher-view");
43
44     this._contentElement = document.createElement("div");
45     this._contentElement.className = "audit-launcher-view-content";
46     this.element.appendChild(this._contentElement);
47     this._boundCategoryClickListener = this._categoryClicked.bind(this);
48
49     this._resetResourceCount();
50
51     this._sortedCategories = [];
52
53     this._headerElement = document.createElement("h1");
54     this._headerElement.className = "no-audits";
55     this._headerElement.textContent = WebInspector.UIString("No audits to run");
56     this._contentElement.appendChild(this._headerElement);
57
58     WebInspector.networkManager.addEventListener(WebInspector.NetworkManager.EventTypes.ResourceStarted, this._onResourceStarted, this);
59     WebInspector.networkManager.addEventListener(WebInspector.NetworkManager.EventTypes.ResourceFinished, this._onResourceFinished, this);
60 }
61
62 WebInspector.AuditLauncherView.prototype = {
63     _resetResourceCount: function()
64     {
65         this._loadedResources = 0;
66         this._totalResources = 0;
67     },
68
69     _onResourceStarted: function(event)
70     {
71         var resource = event.data;
72         // Ignore long-living WebSockets for the sake of progress indicator, as we won't be waiting them anyway.
73         if (resource.type === WebInspector.Resource.Type.WebSocket)
74             return;
75         ++this._totalResources;
76         this._updateResourceProgress();
77     },
78
79     _onResourceFinished: function(event)
80     {
81         var resource = event.data;
82         // See resorceStarted for details.
83         if (resource.type === WebInspector.Resource.Type.WebSocket)
84             return;
85         ++this._loadedResources;
86         this._updateResourceProgress();
87     },
88
89     addCategory: function(category)
90     {
91         if (!this._sortedCategories.length)
92             this._createLauncherUI();
93
94         var categoryElement = this._createCategoryElement(category.displayName, category.id);
95         category._checkboxElement = categoryElement.firstChild;
96         if (this._selectAllCheckboxElement.checked) {
97             category._checkboxElement.checked = true;
98             ++this._currentCategoriesCount;
99         }
100
101         function compareCategories(a, b)
102         {
103             var aTitle = a.displayName || "";
104             var bTitle = b.displayName || "";
105             return aTitle.localeCompare(bTitle);
106         }
107         var insertBefore = insertionIndexForObjectInListSortedByFunction(category, this._sortedCategories, compareCategories);
108         this._categoriesElement.insertBefore(categoryElement, this._categoriesElement.children[insertBefore]);
109         this._sortedCategories.splice(insertBefore, 0, category);
110         this._updateButton();
111     },
112
113     _setAuditRunning: function(auditRunning)
114     {
115         if (this._auditRunning === auditRunning)
116             return;
117         this._auditRunning = auditRunning;
118         this._updateButton();
119         this._updateResourceProgress();
120     },
121
122     _launchButtonClicked: function(event)
123     {
124         var catIds = [];
125         var childNodes = this._categoriesElement.childNodes;
126         for (var category = 0; category < this._sortedCategories.length; ++category) {
127             if (this._sortedCategories[category]._checkboxElement.checked)
128                 catIds.push(this._sortedCategories[category].id);
129         }
130
131         this._setAuditRunning(true);
132         this._runnerCallback(catIds, this._auditPresentStateElement.checked, this._setAuditRunning.bind(this, false));
133     },
134
135     _selectAllClicked: function(checkCategories)
136     {
137         var childNodes = this._categoriesElement.childNodes;
138         for (var i = 0, length = childNodes.length; i < length; ++i)
139             childNodes[i].firstChild.checked = checkCategories;
140         this._currentCategoriesCount = checkCategories ? this._sortedCategories.length : 0;
141         this._updateButton();
142     },
143
144     _categoryClicked: function(event)
145     {
146         this._currentCategoriesCount += event.target.checked ? 1 : -1;
147         this._selectAllCheckboxElement.checked = this._currentCategoriesCount === this._sortedCategories.length;
148         this._updateButton();
149     },
150
151     _createCategoryElement: function(title, id)
152     {
153         var labelElement = document.createElement("label");
154         labelElement.id = this._categoryIdPrefix + id;
155
156         var element = document.createElement("input");
157         element.type = "checkbox";
158         if (id !== "")
159             element.addEventListener("click", this._boundCategoryClickListener, false);
160         labelElement.appendChild(element);
161         labelElement.appendChild(document.createTextNode(title));
162
163         return labelElement;
164     },
165
166     _createLauncherUI: function()
167     {
168         this._headerElement = document.createElement("h1");
169         this._headerElement.textContent = WebInspector.UIString("Select audits to run");
170
171         for (var child = 0; child < this._contentElement.children.length; ++child)
172             this._contentElement.removeChild(this._contentElement.children[child]);
173
174         this._contentElement.appendChild(this._headerElement);
175
176         function handleSelectAllClick(event)
177         {
178             this._selectAllClicked(event.target.checked);
179         }
180         var categoryElement = this._createCategoryElement(WebInspector.UIString("Select All"), "");
181         categoryElement.id = "audit-launcher-selectall";
182         this._selectAllCheckboxElement = categoryElement.firstChild;
183         this._selectAllCheckboxElement.checked = true;
184         this._selectAllCheckboxElement.addEventListener("click", handleSelectAllClick.bind(this), false);
185         this._contentElement.appendChild(categoryElement);
186
187         this._categoriesElement = document.createElement("div");
188         this._categoriesElement.className = "audit-categories-container";
189         this._contentElement.appendChild(this._categoriesElement);
190
191         this._currentCategoriesCount = 0;
192
193         var flexibleSpaceElement = document.createElement("div");
194         flexibleSpaceElement.className = "flexible-space";
195         this._contentElement.appendChild(flexibleSpaceElement);
196
197         this._buttonContainerElement = document.createElement("div");
198         this._buttonContainerElement.className = "button-container";
199
200         var labelElement = document.createElement("label");
201         this._auditPresentStateElement = document.createElement("input");
202         this._auditPresentStateElement.name = "audit-mode";
203         this._auditPresentStateElement.type = "radio";
204         this._auditPresentStateElement.checked = true;
205         this._auditPresentStateLabelElement = document.createTextNode(WebInspector.UIString("Audit Present State"));
206         labelElement.appendChild(this._auditPresentStateElement);
207         labelElement.appendChild(this._auditPresentStateLabelElement);
208         this._buttonContainerElement.appendChild(labelElement);
209
210         labelElement = document.createElement("label");
211         this.auditReloadedStateElement = document.createElement("input");
212         this.auditReloadedStateElement.name = "audit-mode";
213         this.auditReloadedStateElement.type = "radio";
214         labelElement.appendChild(this.auditReloadedStateElement);
215         labelElement.appendChild(document.createTextNode("Reload Page and Audit on Load"));
216         this._buttonContainerElement.appendChild(labelElement);
217
218         this._launchButton = document.createElement("button");
219         this._launchButton.type = "button";
220         this._launchButton.textContent = WebInspector.UIString("Run");
221         this._launchButton.addEventListener("click", this._launchButtonClicked.bind(this), false);
222         this._buttonContainerElement.appendChild(this._launchButton);
223
224         this._resourceProgressContainer = document.createElement("span");
225         this._resourceProgressContainer.className = "resource-progress";
226         var resourceProgressImage = document.createElement("img");
227         this._resourceProgressContainer.appendChild(resourceProgressImage);
228         this._resourceProgressTextElement = document.createElement("span");
229         this._resourceProgressContainer.appendChild(this._resourceProgressTextElement);
230         this._buttonContainerElement.appendChild(this._resourceProgressContainer);
231
232         this._contentElement.appendChild(this._buttonContainerElement);
233
234         this._selectAllClicked(this._selectAllCheckboxElement.checked);
235         this._updateButton();
236         this._updateResourceProgress();
237     },
238
239     _updateResourceProgress: function()
240     {
241         if (!this._resourceProgressContainer)
242             return;
243
244         if (!this._auditRunning) {
245             this._resetResourceCount();
246             this._resourceProgressContainer.addStyleClass("hidden");
247         } else
248             this._resourceProgressContainer.removeStyleClass("hidden");
249         this._resourceProgressTextElement.textContent = WebInspector.UIString("Loading (%d of %d)", this._loadedResources, this._totalResources);
250     },
251
252     _updateButton: function()
253     {
254         this._launchButton.disabled = !this._currentCategoriesCount || this._auditRunning;
255     }
256 }
257
258 WebInspector.AuditLauncherView.prototype.__proto__ = WebInspector.View.prototype;