Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: chrome/browser/resources/about_stats.js

Issue 10010019: JS style nits (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: couple more Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/resources/certificate_viewer.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /* Counter accessor for Name Node. */ 5 /* Counter accessor for Name Node. */
6 function getCounterNameFromCounterNode(node) { 6 function getCounterNameFromCounterNode(node) {
7 return node.childNodes[1]; 7 return node.childNodes[1];
8 } 8 }
9 9
10 /* Counter accessor for Value Node. */ 10 /* Counter accessor for Value Node. */
11 function getCounterValueFromCounterNode(node) { 11 function getCounterValueFromCounterNode(node) {
(...skipping 23 matching lines...) Expand all
35 /* Timer accessor for Average Time Node. */ 35 /* Timer accessor for Average Time Node. */
36 function getTimerAvgTimeFromTimerNode(node) { 36 function getTimerAvgTimeFromTimerNode(node) {
37 return node.childNodes[7]; 37 return node.childNodes[7];
38 } 38 }
39 39
40 /* Do the filter work. Hide all nodes matching node.*/ 40 /* Do the filter work. Hide all nodes matching node.*/
41 function filterMatching(text, nodelist, functionToGetNameNode) { 41 function filterMatching(text, nodelist, functionToGetNameNode) {
42 var showAll = text.length == 0; 42 var showAll = text.length == 0;
43 for (var i = 0, node; node = nodelist[i]; i++) { 43 for (var i = 0, node; node = nodelist[i]; i++) {
44 var name = functionToGetNameNode(node).innerHTML.toLowerCase(); 44 var name = functionToGetNameNode(node).innerHTML.toLowerCase();
45 if (showAll || name.indexOf(text) >= 0) { 45 if (showAll || name.indexOf(text) >= 0)
46 node.style.display = "table-row"; 46 node.style.display = 'table-row';
47 } else { 47 else
48 node.style.display = "none"; 48 node.style.display = 'none';
49 }
50 } 49 }
51 } 50 }
52 51
53 /* Hides or shows counters based on the user's current filter selection. */ 52 /* Hides or shows counters based on the user's current filter selection. */
54 function doFilter() { 53 function doFilter() {
55 var filter = document.getElementById("filter"); 54 var filter = document.getElementById('filter');
56 var text = filter.value.toLowerCase(); 55 var text = filter.value.toLowerCase();
57 var nodes = document.getElementsByName("counter"); 56 var nodes = document.getElementsByName('counter');
58 filterMatching(text, nodes, getCounterNameFromCounterNode); 57 filterMatching(text, nodes, getCounterNameFromCounterNode);
59 var nodes = document.getElementsByName("timer"); 58 var nodes = document.getElementsByName('timer');
60 filterMatching(text, nodes, getTimerNameFromTimerNode); 59 filterMatching(text, nodes, getTimerNameFromTimerNode);
61 } 60 }
62 61
63 /* Colors the counters based on increasing or decreasing value. */ 62 /* Colors the counters based on increasing or decreasing value. */
64 function doColor() { 63 function doColor() {
65 var nodes = document.getElementsByName("counter"); 64 var nodes = document.getElementsByName('counter');
66 for (var i = 0, node; node = nodes[i]; i++) { 65 for (var i = 0, node; node = nodes[i]; i++) {
67 var child = getCounterDeltaFromCounterNode(node); 66 var child = getCounterDeltaFromCounterNode(node);
68 var delta = child.innerHTML; 67 var delta = child.innerHTML;
69 if (delta > 0) { 68 if (delta > 0)
70 child.style.color = "Green"; 69 child.style.color = 'Green';
71 } else if (delta == 0) { 70 else if (delta == 0)
72 child.style.color = "Black"; 71 child.style.color = 'Black';
73 } else { 72 else
74 child.style.color = "Red"; 73 child.style.color = 'Red';
75 }
76 } 74 }
77 } 75 }
78 76
79 /* Counters with no values are null. Remove them. */ 77 /* Counters with no values are null. Remove them. */
80 function removeNullValues() { 78 function removeNullValues() {
81 var nodes = document.getElementsByName("counter"); 79 var nodes = document.getElementsByName('counter');
82 for (var i = nodes.length - 1; i >= 0; i--) { 80 for (var i = nodes.length - 1; i >= 0; i--) {
83 var node = nodes[i]; 81 var node = nodes[i];
84 var value = getCounterValueFromCounterNode(node).innerHTML; 82 var value = getCounterValueFromCounterNode(node).innerHTML;
85 if (value == "null") { 83 if (value == 'null')
86 node.parentNode.removeChild(node); 84 node.parentNode.removeChild(node);
87 }
88 } 85 }
89 var nodes = document.getElementsByName("timer"); 86 var nodes = document.getElementsByName('timer');
90 for (var i = 0, node; node = nodes[i]; i++) { 87 for (var i = 0, node; node = nodes[i]; i++) {
91 var value_node = getTimerValueFromTimerNode(node); 88 var value_node = getTimerValueFromTimerNode(node);
92 if (value_node.innerHTML == "null") { 89 if (value_node.innerHTML == 'null')
93 value_node.innerHTML = ""; 90 value_node.innerHTML = '';
94 }
95 } 91 }
96 } 92 }
97 93
98 /* Compute the average time for timers */ 94 /* Compute the average time for timers */
99 function computeTimes() { 95 function computeTimes() {
100 var nodes = document.getElementsByName("timer"); 96 var nodes = document.getElementsByName('timer');
101 for (var i = 0, node; node = nodes[i]; i++) { 97 for (var i = 0, node; node = nodes[i]; i++) {
102 var count = getTimerValueFromTimerNode(node).innerHTML; 98 var count = getTimerValueFromTimerNode(node).innerHTML;
103 if (count.length > 0) { 99 if (count.length > 0) {
104 var time = getTimerTimeFromTimerNode(node).innerHTML; 100 var time = getTimerTimeFromTimerNode(node).innerHTML;
105 var avg = getTimerAvgTimeFromTimerNode(node); 101 var avg = getTimerAvgTimeFromTimerNode(node);
106 avg.innerHTML = Math.round(time / count * 100) / 100; 102 avg.innerHTML = Math.round(time / count * 100) / 100;
107 } 103 }
108 } 104 }
109 } 105 }
110 106
111 /* All the work we do onload. */ 107 /* All the work we do onload. */
112 function onLoadWork() { 108 function onLoadWork() {
113 // This is the javascript code that processes the template: 109 // This is the javascript code that processes the template:
114 var input = new JsEvalContext(templateData); 110 var input = new JsEvalContext(templateData);
115 var output = document.getElementById('t'); 111 var output = document.getElementById('t');
116 jstProcess(input, output); 112 jstProcess(input, output);
117 113
118 // Add handlers to dynamically created HTML elements. 114 // Add handlers to dynamically created HTML elements.
119 var elements = document.getElementsByName("string-sort"); 115 var elements = document.getElementsByName('string-sort');
120 for (var i = 0; i < elements.length; ++i) 116 for (var i = 0; i < elements.length; ++i)
121 elements[i].onclick = function () { sort_table("string"); }; 117 elements[i].onclick = function() { sort_table('string'); };
122 118
123 elements = document.getElementsByName("number-sort"); 119 elements = document.getElementsByName('number-sort');
124 for (i = 0; i < elements.length; ++i) 120 for (i = 0; i < elements.length; ++i)
125 elements[i].onclick = function () { sort_table("number"); }; 121 elements[i].onclick = function() { sort_table('number'); };
126 122
127 doColor(); 123 doColor();
128 removeNullValues(); 124 removeNullValues();
129 computeTimes(); 125 computeTimes();
130 document.getElementById("filter").focus(); 126 document.getElementById('filter').focus();
131 } 127 }
132 128
133 // The function should only be used as the event handler 129 // The function should only be used as the event handler
134 // on a table cell element. To use it, put it in a <td> element: 130 // on a table cell element. To use it, put it in a <td> element:
135 // <td onclick="sort('string')" ...> 131 // <td onclick="sort('string')" ...>
136 // 132 //
137 // The function sorts rows after the row with onclick event handler. 133 // The function sorts rows after the row with onclick event handler.
138 // 134 //
139 // type: the data type, 'string', 'number' 135 // type: the data type, 'string', 'number'
140 function sort_table(type){ 136 function sort_table(type) {
141 var cell = event.target; 137 var cell = event.target;
142 var cnum = cell.cellIndex; 138 var cnum = cell.cellIndex;
143 139
144 var row = cell.parentNode; 140 var row = cell.parentNode;
145 var start_index = row.rowIndex + 1; 141 var start_index = row.rowIndex + 1;
146 142
147 var tbody = row.parentNode; 143 var tbody = row.parentNode;
148 var table = tbody.parentNode; 144 var table = tbody.parentNode;
149 145
150 var rows = new Array(); 146 var rows = new Array();
151 147
152 var indexes = new Array(); 148 var indexes = new Array();
153 // skip the first row 149 // skip the first row
154 for (var i = start_index; i < table.rows.length; i++) 150 for (var i = start_index; i < table.rows.length; i++)
155 rows.push(table.rows[i]); 151 rows.push(table.rows[i]);
156 152
157 // a, b are strings 153 // a, b are strings
158 function compare_strings(a,b) { 154 function compare_strings(a, b) {
159 if (a == b) return 0; 155 if (a == b) return 0;
160 if (a < b) return -1; 156 if (a < b) return -1;
161 return 1; 157 return 1;
162 } 158 }
163 159
164 // a, b are numbers 160 // a, b are numbers
165 function compare_numbers(a,b) { 161 function compare_numbers(a, b) {
166 var x = isNaN(a) ? 0 : a; 162 var x = isNaN(a) ? 0 : a;
167 var y = isNaN(b) ? 0 : b; 163 var y = isNaN(b) ? 0 : b;
168 return x - y; 164 return x - y;
169 } 165 }
170 166
171 var sort_func = undefined; 167 var sort_func;
172 if (type === 'string') { 168 if (type === 'string') {
173 sort_func = function(a, b) { 169 sort_func = function(a, b) {
174 var x = a.cells[cnum].innerText; 170 var x = a.cells[cnum].innerText;
175 var y = b.cells[cnum].innerText; 171 var y = b.cells[cnum].innerText;
176 return compare_strings(x, y); 172 return compare_strings(x, y);
177 } ; 173 };
178 174
179 } else if (type === 'number') { 175 } else if (type === 'number') {
180 sort_func = function(a, b) { 176 sort_func = function(a, b) {
181 var x = parseFloat(a.cells[cnum].innerText); 177 var x = parseFloat(a.cells[cnum].innerText);
182 var y = parseFloat(b.cells[cnum].innerText); 178 var y = parseFloat(b.cells[cnum].innerText);
183 return compare_numbers(x, y); 179 return compare_numbers(x, y);
184 } 180 };
185 } 181 }
186 182
187 rows.sort(sort_func); 183 rows.sort(sort_func);
188 184
189 // change tables 185 // change tables
190 if (cell._reverse) { 186 if (cell._reverse) {
191 for (var i = rows.length - 1; i >= 0; i--) 187 for (var i = rows.length - 1; i >= 0; i--)
192 tbody.appendChild(rows[i]); 188 tbody.appendChild(rows[i]);
193 cell._reverse = false; 189 cell._reverse = false;
194 } else { 190 } else {
195 for (var i = 0; i < rows.length; i++) 191 for (var i = 0; i < rows.length; i++)
196 tbody.appendChild(rows[i]); 192 tbody.appendChild(rows[i]);
197 cell._reverse = true; 193 cell._reverse = true;
198 } 194 }
199 } 195 }
200 196
201 document.addEventListener('DOMContentLoaded', onLoadWork); 197 document.addEventListener('DOMContentLoaded', onLoadWork);
202
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/resources/certificate_viewer.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698