OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 /** | 5 /** |
6 * TablePrinter is a helper to format a table as ASCII art or an HTML table. | 6 * TablePrinter is a helper to format a table as ASCII art or an HTML table. |
7 * | 7 * |
8 * Usage: call addRow() and addCell() repeatedly to specify the data. | 8 * Usage: call addRow() and addCell() repeatedly to specify the data. |
9 * | 9 * |
10 * addHeaderCell() can optionally be called to specify header cells for a | 10 * addHeaderCell() can optionally be called to specify header cells for a |
(...skipping 11 matching lines...) Expand all Loading... | |
22 var TablePrinter = (function() { | 22 var TablePrinter = (function() { |
23 'use strict'; | 23 'use strict'; |
24 | 24 |
25 /** | 25 /** |
26 * @constructor | 26 * @constructor |
27 */ | 27 */ |
28 function TablePrinter() { | 28 function TablePrinter() { |
29 this.rows_ = []; | 29 this.rows_ = []; |
30 this.hasHeaderRow_ = false; | 30 this.hasHeaderRow_ = false; |
31 this.title_ = null; | 31 this.title_ = null; |
32 // Number of cells automatically added at the start of new rows. | |
33 this.newRowCellIndent_ = 0; | |
32 } | 34 } |
33 | 35 |
34 TablePrinter.prototype = { | 36 TablePrinter.prototype = { |
35 /** | 37 /** |
38 * Sets the number of blank cells to add after each call to addRow. | |
39 */ | |
40 setNewRowCellIndent: function(newRowCellIndent) { | |
41 this.newRowCellIndent_ = newRowCellIndent; | |
42 }, | |
43 | |
44 /** | |
36 * Starts a new row. | 45 * Starts a new row. |
37 */ | 46 */ |
38 addRow: function() { | 47 addRow: function() { |
39 this.rows_.push([]); | 48 this.rows_.push([]); |
49 for (var i = 0; i < this.newRowCellIndent_; ++i) | |
50 this.addCell(''); | |
40 }, | 51 }, |
41 | 52 |
42 /** | 53 /** |
43 * Adds a column to the current row, setting its value to cellText. | 54 * Adds a column to the current row, setting its value to cellText. |
44 * | 55 * |
45 * @returns {!TablePrinterCell} the cell that was added. | 56 * @returns {!TablePrinterCell} the cell that was added. |
46 */ | 57 */ |
47 addCell: function(cellText) { | 58 addCell: function(cellText) { |
48 var r = this.rows_[this.rows_.length - 1]; | 59 var r = this.rows_[this.rows_.length - 1]; |
49 var cell = new TablePrinterCell(cellText); | 60 var cell = new TablePrinterCell(cellText); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
93 getCell_: function(rowIndex, columnIndex) { | 104 getCell_: function(rowIndex, columnIndex) { |
94 if (rowIndex >= this.rows_.length) | 105 if (rowIndex >= this.rows_.length) |
95 return null; | 106 return null; |
96 var row = this.rows_[rowIndex]; | 107 var row = this.rows_[rowIndex]; |
97 if (columnIndex >= row.length) | 108 if (columnIndex >= row.length) |
98 return null; | 109 return null; |
99 return row[columnIndex]; | 110 return row[columnIndex]; |
100 }, | 111 }, |
101 | 112 |
102 /** | 113 /** |
103 * Returns a formatted text representation of the table data. | 114 * Returns a formatted text representation of the table data. |
eroman
2011/12/08 22:21:06
Update this comment -- the return value is no long
mmenke
2011/12/08 22:28:37
Done.
| |
104 * |spacing| indicates number of extra spaces, if any, to add between | 115 * |spacing| indicates number of extra spaces, if any, to add between |
105 * columns. | 116 * columns. |
106 */ | 117 */ |
107 toText: function(spacing) { | 118 toText: function(spacing, parent) { |
119 var pre = addNode(parent, 'pre'); | |
108 var numColumns = this.getNumColumns(); | 120 var numColumns = this.getNumColumns(); |
109 | 121 |
110 // Figure out the maximum width of each column. | 122 // Figure out the maximum width of each column. |
111 var columnWidths = []; | 123 var columnWidths = []; |
112 columnWidths.length = numColumns; | 124 columnWidths.length = numColumns; |
113 for (var i = 0; i < numColumns; ++i) | 125 for (var i = 0; i < numColumns; ++i) |
114 columnWidths[i] = 0; | 126 columnWidths[i] = 0; |
115 | 127 |
116 // If header row is present, temporarily add a spacer row to |rows_|. | 128 // If header row is present, temporarily add a spacer row to |rows_|. |
117 if (this.hasHeaderRow_) { | 129 if (this.hasHeaderRow_) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
152 // Print each row. | 164 // Print each row. |
153 var spacingStr = makeRepeatedString(' ', spacing); | 165 var spacingStr = makeRepeatedString(' ', spacing); |
154 for (var r = 0; r < numRows; ++r) { | 166 for (var r = 0; r < numRows; ++r) { |
155 for (var c = 0; c < numColumns; ++c) { | 167 for (var c = 0; c < numColumns; ++c) { |
156 var cell = this.getCell_(r, c); | 168 var cell = this.getCell_(r, c); |
157 if (cell) { | 169 if (cell) { |
158 // Pad the cell with spaces to make it fit the maximum column width. | 170 // Pad the cell with spaces to make it fit the maximum column width. |
159 var padding = columnWidths[c] - cell.text.length; | 171 var padding = columnWidths[c] - cell.text.length; |
160 var paddingStr = makeRepeatedString(' ', padding); | 172 var paddingStr = makeRepeatedString(' ', padding); |
161 | 173 |
162 if (cell.alignRight) { | 174 if (cell.alignRight) |
163 out.push(paddingStr); | 175 out.push(paddingStr); |
164 out.push(cell.text); | 176 if (cell.link) { |
177 // Output all previous text, and clear |out|. | |
178 addTextNode(pre, out.join('')); | |
179 out = []; | |
180 | |
181 var linkNode = addNodeWithText(pre, 'a', cell.text); | |
182 linkNode.href = cell.link; | |
165 } else { | 183 } else { |
166 out.push(cell.text); | 184 out.push(cell.text); |
185 } | |
186 if (!cell.alignRight) | |
167 out.push(paddingStr); | 187 out.push(paddingStr); |
168 } | |
169 out.push(spacingStr); | 188 out.push(spacingStr); |
170 } | 189 } |
171 } | 190 } |
172 out.push('\n'); | 191 out.push('\n'); |
173 } | 192 } |
174 | 193 |
175 // Remove spacer row under the header row, if one was added. | 194 // Remove spacer row under the header row, if one was added. |
176 if (this.hasHeaderRow_) | 195 if (this.hasHeaderRow_) |
177 this.rows_.splice(1, 1); | 196 this.rows_.splice(1, 1); |
178 | 197 |
179 return out.join(''); | 198 addTextNode(pre, out.join('')); |
180 }, | 199 }, |
181 | 200 |
182 /** | 201 /** |
183 * Adds a new HTML table to the node |parent| using the specified style. | 202 * Adds a new HTML table to the node |parent| using the specified style. |
184 */ | 203 */ |
185 toHTML: function(parent, style) { | 204 toHTML: function(parent, style) { |
186 var numRows = this.rows_.length; | 205 var numRows = this.rows_.length; |
187 var numColumns = this.getNumColumns(); | 206 var numColumns = this.getNumColumns(); |
188 | 207 |
189 var table = addNode(parent, 'table'); | 208 var table = addNode(parent, 'table'); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
240 */ | 259 */ |
241 function TablePrinterCell(value) { | 260 function TablePrinterCell(value) { |
242 this.text = '' + value; | 261 this.text = '' + value; |
243 this.link = null; | 262 this.link = null; |
244 this.alignRight = false; | 263 this.alignRight = false; |
245 this.allowOverflow = false; | 264 this.allowOverflow = false; |
246 } | 265 } |
247 | 266 |
248 return TablePrinter; | 267 return TablePrinter; |
249 })(); | 268 })(); |
OLD | NEW |