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
3 // found in the LICENSE file.
5 /* Counter accessor for Name Node. */
6 function getCounterNameFromCounterNode(node) {
7 return node.childNodes[1];
10 /* Counter accessor for Value Node. */
11 function getCounterValueFromCounterNode(node) {
12 return node.childNodes[3];
15 /* Counter accessor for Delta Node. */
16 function getCounterDeltaFromCounterNode(node) {
17 return node.childNodes[5];
20 /* Timer accessor for Name Node. */
21 function getTimerNameFromTimerNode(node) {
22 return node.childNodes[1];
25 /* Timer accessor for Value Node. */
26 function getTimerValueFromTimerNode(node) {
27 return node.childNodes[3];
30 /* Timer accessor for Time Node. */
31 function getTimerTimeFromTimerNode(node) {
32 return node.childNodes[5];
35 /* Timer accessor for Average Time Node. */
36 function getTimerAvgTimeFromTimerNode(node) {
37 return node.childNodes[7];
40 /* Do the filter work. Hide all nodes matching node.*/
41 function filterMatching(text, nodelist, functionToGetNameNode) {
42 var showAll = text.length == 0;
43 for (var i = 0, node; node = nodelist[i]; i++) {
44 var name = functionToGetNameNode(node).innerHTML.toLowerCase();
45 if (showAll || name.indexOf(text) >= 0)
46 node.style.display = 'table-row';
48 node.style.display = 'none';
52 /* Hides or shows counters based on the user's current filter selection. */
54 var filter = $('filter');
55 var text = filter.value.toLowerCase();
56 var nodes = document.getElementsByName('counter');
57 filterMatching(text, nodes, getCounterNameFromCounterNode);
58 var nodes = document.getElementsByName('timer');
59 filterMatching(text, nodes, getTimerNameFromTimerNode);
62 /* Colors the counters based on increasing or decreasing value. */
64 var nodes = document.getElementsByName('counter');
65 for (var i = 0, node; node = nodes[i]; i++) {
66 var child = getCounterDeltaFromCounterNode(node);
67 var delta = child.innerHTML;
69 child.style.color = 'Green';
71 child.style.color = 'Black';
73 child.style.color = 'Red';
77 /* Counters with no values are null. Remove them. */
78 function removeNullValues() {
79 var nodes = document.getElementsByName('counter');
80 for (var i = nodes.length - 1; i >= 0; i--) {
82 var value = getCounterValueFromCounterNode(node).innerHTML;
84 node.parentNode.removeChild(node);
86 var nodes = document.getElementsByName('timer');
87 for (var i = 0, node; node = nodes[i]; i++) {
88 var valueNode = getTimerValueFromTimerNode(node);
89 if (valueNode.innerHTML == 'null')
90 valueNode.innerHTML = '';
94 /* Compute the average time for timers */
95 function computeTimes() {
96 var nodes = document.getElementsByName('timer');
97 for (var i = 0, node; node = nodes[i]; i++) {
98 var count = getTimerValueFromTimerNode(node).innerHTML;
99 if (count.length > 0) {
100 var time = getTimerTimeFromTimerNode(node).innerHTML;
101 var avg = getTimerAvgTimeFromTimerNode(node);
102 avg.innerHTML = Math.round(time / count * 100) / 100;
107 /* All the work we do onload. */
108 function onLoadWork() {
109 // This is the javascript code that processes the template:
110 var input = new JsEvalContext(templateData);
112 jstProcess(input, output);
114 // Add handlers to dynamically created HTML elements.
115 var elements = document.getElementsByName('string-sort');
116 for (var i = 0; i < elements.length; ++i)
117 elements[i].onclick = function() { sortTable('string'); };
119 elements = document.getElementsByName('number-sort');
120 for (i = 0; i < elements.length; ++i)
121 elements[i].onclick = function() { sortTable('number'); };
127 var filter = $('filter');
128 filter.onkeyup = doFilter;
132 // The function should only be used as the event handler
133 // on a table cell element. To use it, put it in a <td> element:
134 // <td onclick="sort('string')" ...>
136 // The function sorts rows after the row with onclick event handler.
138 // type: the data type, 'string', 'number'
139 function sortTable(type) {
140 var cell = event.target;
141 var cnum = cell.cellIndex;
143 var row = cell.parentNode;
144 var startIndex = row.rowIndex + 1;
146 var tbody = row.parentNode;
147 var table = tbody.parentNode;
149 var rows = new Array();
151 var indexes = new Array();
152 // skip the first row
153 for (var i = startIndex; i < table.rows.length; i++)
154 rows.push(table.rows[i]);
157 function compareStrings(a, b) {
158 if (a == b) return 0;
159 if (a < b) return -1;
164 function compareNumbers(a, b) {
165 var x = isNaN(a) ? 0 : a;
166 var y = isNaN(b) ? 0 : b;
171 if (type === 'string') {
172 sortFunc = function(a, b) {
173 var x = a.cells[cnum].innerText;
174 var y = b.cells[cnum].innerText;
175 return compareStrings(x, y);
178 } else if (type === 'number') {
179 sortFunc = function(a, b) {
180 var x = parseFloat(a.cells[cnum].innerText);
181 var y = parseFloat(b.cells[cnum].innerText);
182 return compareNumbers(x, y);
190 for (var i = rows.length - 1; i >= 0; i--)
191 tbody.appendChild(rows[i]);
192 cell._reverse = false;
194 for (var i = 0; i < rows.length; i++)
195 tbody.appendChild(rows[i]);
196 cell._reverse = true;
200 document.addEventListener('DOMContentLoaded', onLoadWork);