OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 * Helper that binds the |this| object to a method to create a callback. |
| 7 */ |
| 8 Function.prototype.bind = function(thisObj) { |
| 9 var func = this; |
| 10 var args = Array.prototype.slice.call(arguments, 1); |
| 11 return function() { |
| 12 return func.apply(thisObj, |
| 13 args.concat(Array.prototype.slice.call(arguments, 0))) |
| 14 }; |
| 15 }; |
| 16 |
| 17 /** |
| 18 * Inherit the prototype methods from one constructor into another. |
| 19 */ |
| 20 function inherits(childCtor, parentCtor) { |
| 21 function tempCtor() {}; |
| 22 tempCtor.prototype = parentCtor.prototype; |
| 23 childCtor.superClass_ = parentCtor.prototype; |
| 24 childCtor.prototype = new tempCtor(); |
| 25 childCtor.prototype.constructor = childCtor; |
| 26 }; |
| 27 |
| 28 /** |
| 29 * Sets the width (in pixels) on a DOM node. |
| 30 */ |
| 31 function setNodeWidth(node, widthPx) { |
| 32 node.style.width = widthPx.toFixed(0) + "px"; |
| 33 } |
| 34 |
| 35 /** |
| 36 * Sets the height (in pixels) on a DOM node. |
| 37 */ |
| 38 function setNodeHeight(node, heightPx) { |
| 39 node.style.height = heightPx.toFixed(0) + "px"; |
| 40 } |
| 41 |
| 42 /** |
| 43 * Sets the position and size of a DOM node (in pixels). |
| 44 */ |
| 45 function setNodePosition(node, leftPx, topPx, widthPx, heightPx) { |
| 46 node.style.left = leftPx.toFixed(0) + "px"; |
| 47 node.style.top = topPx.toFixed(0) + "px"; |
| 48 setNodeWidth(node, widthPx); |
| 49 setNodeHeight(node, heightPx); |
| 50 } |
| 51 |
| 52 /** |
| 53 * Sets the visibility for a DOM node. |
| 54 */ |
| 55 function setNodeDisplay(node, isVisible) { |
| 56 node.style.display = isVisible ? '' : 'none'; |
| 57 } |
| 58 |
| 59 /** |
| 60 * Adds a node to |parentNode|, of type |tagName|. |
| 61 */ |
| 62 function addNode(parentNode, tagName) { |
| 63 var elem = parentNode.ownerDocument.createElement(tagName); |
| 64 parentNode.appendChild(elem); |
| 65 return elem; |
| 66 } |
| 67 |
| 68 /** |
| 69 * Adds |text| to node |parentNode|. |
| 70 */ |
| 71 function addTextNode(parentNode, text) { |
| 72 var textNode = parentNode.ownerDocument.createTextNode(text); |
| 73 parentNode.appendChild(textNode); |
| 74 return textNode; |
| 75 } |
| 76 |
| 77 /** |
| 78 * Adds a node to |parentNode|, of type |tagName|. Then adds |
| 79 * |text| to the new node. |
| 80 */ |
| 81 function addNodeWithText(parentNode, tagName, text) { |
| 82 var elem = parentNode.ownerDocument.createElement(tagName); |
| 83 parentNode.appendChild(elem); |
| 84 addTextNode(elem, text); |
| 85 return elem; |
| 86 } |
| 87 |
| 88 /** |
| 89 * Adds or removes a CSS class to |node|. |
| 90 */ |
| 91 function changeClassName(node, classNameToAddOrRemove, isAdd) { |
| 92 // Multiple classes can be separated by spaces. |
| 93 var currentNames = node.className.split(" "); |
| 94 |
| 95 if (isAdd) { |
| 96 if (!(classNameToAddOrRemove in currentNames)) { |
| 97 currentNames.push(classNameToAddOrRemove); |
| 98 } |
| 99 } else { |
| 100 for (var i = 0; i < currentNames.length; ++i) { |
| 101 if (currentNames[i] == classNameToAddOrRemove) { |
| 102 currentNames.splice(i, 1); |
| 103 break; |
| 104 } |
| 105 } |
| 106 } |
| 107 |
| 108 node.className = currentNames.join(" "); |
| 109 } |
| 110 |
| 111 function getKeyWithValue(map, value) { |
| 112 for (key in map) { |
| 113 if (map[key] == value) |
| 114 return key; |
| 115 } |
| 116 return '?'; |
| 117 } |
| 118 |
| 119 /** |
| 120 * Builds a string by repeating |str| |count| times. |
| 121 */ |
| 122 function makeRepeatedString(str, count) { |
| 123 var out = []; |
| 124 for (var i = 0; i < count; ++i) |
| 125 out.push(str); |
| 126 return out.join(''); |
| 127 } |
| 128 |
| 129 /** |
| 130 * TablePrinter is a helper to format a table as ascii art. |
| 131 * |
| 132 * Usage: call addRow() and addCell() repeatedly to specify the data. Ones |
| 133 * all the fields have been inputted, call toText() to format it as text. |
| 134 */ |
| 135 function TablePrinter() { |
| 136 this.rows_ = []; |
| 137 } |
| 138 |
| 139 function TablePrinterCell(value) { |
| 140 this.text = '' + value; |
| 141 this.alignRight = false; |
| 142 this.allowOverflow = false; |
| 143 } |
| 144 |
| 145 /** |
| 146 * Starts a new row. |
| 147 */ |
| 148 TablePrinter.prototype.addRow = function() { |
| 149 this.rows_.push([]); |
| 150 }; |
| 151 |
| 152 /** |
| 153 * Adds a column to the current row, setting its value to cellText. |
| 154 * |
| 155 * @returns {!TablePrinterCell} the cell that was added. |
| 156 */ |
| 157 TablePrinter.prototype.addCell = function(cellText) { |
| 158 var r = this.rows_[this.rows_.length - 1]; |
| 159 var cell = new TablePrinterCell(cellText); |
| 160 r.push(cell); |
| 161 return cell; |
| 162 }; |
| 163 |
| 164 /** |
| 165 * Returns the maximum number of columns this table contains. |
| 166 */ |
| 167 TablePrinter.prototype.getNumColumns = function() { |
| 168 var numColumns = 0; |
| 169 for (var i = 0; i < this.rows_.length; ++i) { |
| 170 numColumns = Math.max(numColumns, this.rows_[i].length); |
| 171 } |
| 172 return numColumns; |
| 173 } |
| 174 |
| 175 /** |
| 176 * Returns the cell at position (rowIndex, columnIndex), or null if there is |
| 177 * no such cell. |
| 178 */ |
| 179 TablePrinter.prototype.getCell_ = function(rowIndex, columnIndex) { |
| 180 if (rowIndex >= this.rows_.length) |
| 181 return null; |
| 182 var row = this.rows_[rowIndex]; |
| 183 if (columnIndex >= row.length) |
| 184 return null; |
| 185 return row[columnIndex]; |
| 186 }; |
| 187 |
| 188 /** |
| 189 * Returns a formatted text representation of the table data. |
| 190 */ |
| 191 TablePrinter.prototype.toText = function() { |
| 192 var numRows = this.rows_.length; |
| 193 var numColumns = this.getNumColumns(); |
| 194 |
| 195 // Figure out the maximum width of each column. |
| 196 var columnWidths = []; |
| 197 columnWidths.length = numColumns; |
| 198 for (var i = 0; i < numColumns; ++i) |
| 199 columnWidths[i] = 0; |
| 200 |
| 201 for (var c = 0; c < numColumns; ++c) { |
| 202 for (var r = 0; r < numRows; ++r) { |
| 203 var cell = this.getCell_(r, c); |
| 204 if (cell && !cell.allowOverflow) { |
| 205 columnWidths[c] = Math.max(columnWidths[c], cell.text.length); |
| 206 } |
| 207 } |
| 208 } |
| 209 |
| 210 // Print each row. |
| 211 var out = []; |
| 212 for (var r = 0; r < numRows; ++r) { |
| 213 for (var c = 0; c < numColumns; ++c) { |
| 214 var cell = this.getCell_(r, c); |
| 215 if (cell) { |
| 216 // Padd the cell with spaces to make it fit the maximum column width. |
| 217 var padding = columnWidths[c] - cell.text.length; |
| 218 var paddingStr = makeRepeatedString(' ', padding); |
| 219 |
| 220 if (cell.alignRight) { |
| 221 out.push(paddingStr); |
| 222 out.push(cell.text); |
| 223 } else { |
| 224 out.push(cell.text); |
| 225 out.push(paddingStr); |
| 226 } |
| 227 } |
| 228 } |
| 229 out.push('\n'); |
| 230 } |
| 231 |
| 232 return out.join(''); |
| 233 }; |
| 234 |
| 235 /** |
| 236 * The browser gives us times in terms of "time ticks" in milliseconds. |
| 237 * This function converts the tick count to a Date() object. |
| 238 * |
| 239 * @param {String} timeTicks. |
| 240 * @returns {Date} The time that |timeTicks| represents. |
| 241 */ |
| 242 function convertTimeTicksToDate(timeTicks) { |
| 243 // Note that the subtraction by 0 is to cast to a number (probably a float |
| 244 // since the numbers are big). |
| 245 var timeStampMs = (this.timeTickOffset_ - 0) + (timeTicks - 0); |
| 246 var d = new Date(); |
| 247 d.setTime(timeStampMs); |
| 248 return d; |
| 249 }; |
| 250 |
OLD | NEW |