OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 /** |
| 6 * @fileoverview Helper functions for generating tables |
| 7 */ |
| 8 |
| 9 /** |
| 10 * Given an array of strings (or anything that will be cast to a string) |
| 11 * generate an HTML <tr> element that contains <td>s that contain an element |
| 12 * in the array. |
| 13 * |
| 14 * Meant to be used with appendRow() |
| 15 */ |
| 16 function genRow(arr) { |
| 17 "use strict"; |
| 18 |
| 19 var tr = document.createElement('tr'); |
| 20 |
| 21 arr.forEach(function (text) { |
| 22 var td = document.createElement('td'); |
| 23 var textNode = document.createTextNode(text); |
| 24 td.appendChild(textNode); |
| 25 tr.appendChild(td); |
| 26 }); |
| 27 return tr; |
| 28 } |
| 29 |
| 30 function genLogRow(logObj) { |
| 31 "use strict"; |
| 32 var tr = document.createElement("tr"); |
| 33 |
| 34 // Unrolled for performance. |
| 35 // This function gets called potentially thousands of times per second |
| 36 var td1 = document.createElement('td'); |
| 37 var td2 = document.createElement('td'); |
| 38 var td3 = document.createElement('td'); |
| 39 |
| 40 var text1 = document.createTextNode(goog.time.millisToString(logObj.time)); |
| 41 var text2 = document.createTextNode(logObj.key); |
| 42 var text3 = document.createTextNode(logObj.value); |
| 43 |
| 44 td1.appendChild(text1); |
| 45 td2.appendChild(text2); |
| 46 td3.appendChild(text3); |
| 47 |
| 48 tr.appendChild(td1); |
| 49 tr.appendChild(td2); |
| 50 tr.appendChild(td3); |
| 51 |
| 52 return tr; |
| 53 } |
| 54 |
| 55 /** |
| 56 * Creates an HTML <table> element that contains <thead> |
| 57 * and <tbody> elements. If arguments are applied to this function, |
| 58 * those arguments are inserted into the <thead> section. |
| 59 */ |
| 60 function makeTable(headers) { |
| 61 "use strict"; |
| 62 |
| 63 var table = document.createElement('table'); |
| 64 |
| 65 if (arguments.length > 0) { |
| 66 var thead = document.createElement('thead'); |
| 67 var tr = document.createElement('tr'); |
| 68 thead.appendChild(tr); |
| 69 table.appendChild(thead); |
| 70 |
| 71 (Array.prototype.slice.call(arguments)).forEach(function (arg) { |
| 72 var td = document.createElement('td'); |
| 73 var text = document.createTextNode(arg); |
| 74 td.appendChild(text); |
| 75 tr.appendChild(td); |
| 76 }); |
| 77 } |
| 78 |
| 79 var tbody = document.createElement('tbody'); |
| 80 table.appendChild(tbody); |
| 81 return table; |
| 82 } |
| 83 |
| 84 /** |
| 85 * Adds a row to a table |
| 86 */ |
| 87 function appendRow(table, row) { |
| 88 "use strict"; |
| 89 // last child should be the <tbody> |
| 90 table.lastChild.appendChild(row); |
| 91 } |
| 92 |
| 93 function removeChildren(element) { |
| 94 "use strict"; |
| 95 |
| 96 // console.trace(); |
| 97 // throw "error"; |
| 98 |
| 99 while (element.hasChildNodes()) { |
| 100 element.removeChild(element.lastChild); |
| 101 } |
| 102 } |
| 103 |
| 104 |
| 105 /* If you are treating a table like it is a map where the first <td> |
| 106 * is the name, and the remaining <td>s are properties of that object, then |
| 107 * it might be useful to update that table. |
| 108 */ |
| 109 function modRow(table, key, valueMult) { |
| 110 "use strict"; |
| 111 |
| 112 // Since we can get multiple values as arguments, make a list of them. |
| 113 // leaving off the first two args. |
| 114 // Then reverse beause we pop them off in the opposite order. |
| 115 var values = goog.array.toArray(arguments).splice(2).reverse(); |
| 116 |
| 117 var tbody = table.querySelector("tbody"); |
| 118 var rows = tbody.querySelectorAll("tr"); |
| 119 |
| 120 rows.forEach(function (row) { |
| 121 // We need to copy them because we are going to pop them out to |
| 122 // populate the columns |
| 123 var modValues = goog.array.toArray(values); |
| 124 var firstColumn = row.querySelector("td"); |
| 125 if (firstColumn.textContent.trim() === key) { |
| 126 var remainingColumns = goog.array.toArray( |
| 127 row.querySelectorAll("td")).slice(1); |
| 128 remainingColumns.forEach(function (column) { |
| 129 var replacement = modValues.pop(); |
| 130 // If the value is either null or undefined, |
| 131 // don't update the text. |
| 132 if (replacement) { |
| 133 column.textContent = replacement; |
| 134 } |
| 135 }); |
| 136 } |
| 137 }); |
| 138 } |
OLD | NEW |