forked from catapult-project/catapult
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_printer.js
290 lines (261 loc) · 8.69 KB
/
table_printer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* TablePrinter is a helper to format a table as ASCII art or an HTML table.
*
* Usage: call addRow() and addCell() repeatedly to specify the data.
*
* addHeaderCell() can optionally be called to specify header cells for a
* single header row. The header row appears at the top of an HTML formatted
* table, and uses thead and th tags. In ascii tables, the header is separated
* from the table body by a partial row of dashes.
*
* setTitle() can optionally be used to set a title that is displayed before
* the header row. In HTML tables, it uses the title class and in ascii tables
* it's between two rows of dashes.
*
* Once all the fields have been input, call toText() to format it as text or
* toHTML() to format it as HTML.
*/
var TablePrinter = (function() {
'use strict';
/**
* @constructor
*/
function TablePrinter() {
this.rows_ = [];
this.hasHeaderRow_ = false;
this.title_ = null;
// Number of cells automatically added at the start of new rows.
this.newRowCellIndent_ = 0;
}
TablePrinter.prototype = {
/**
* Sets the number of blank cells to add after each call to addRow.
*/
setNewRowCellIndent: function(newRowCellIndent) {
this.newRowCellIndent_ = newRowCellIndent;
},
/**
* Starts a new row.
*/
addRow: function() {
this.rows_.push([]);
for (var i = 0; i < this.newRowCellIndent_; ++i)
this.addCell('');
},
/**
* Adds a column to the current row, setting its value to cellText.
*
* @return {!TablePrinterCell} the cell that was added.
*/
addCell: function(cellText) {
var r = this.rows_[this.rows_.length - 1];
var cell = new TablePrinterCell(cellText);
r.push(cell);
return cell;
},
/**
* Sets the title displayed at the top of a table. Titles are optional.
*/
setTitle: function(title) {
this.title_ = title;
},
/**
* Adds a header row, if not already present, and adds a new column to it,
* setting its contents to |headerText|.
*
* @return {!TablePrinterCell} the cell that was added.
*/
addHeaderCell: function(headerText) {
// Insert empty new row at start of |rows_| if currently no header row.
if (!this.hasHeaderRow_) {
this.rows_.splice(0, 0, []);
this.hasHeaderRow_ = true;
}
var cell = new TablePrinterCell(headerText);
this.rows_[0].push(cell);
return cell;
},
/**
* Returns the maximum number of columns this table contains.
*/
getNumColumns: function() {
var numColumns = 0;
for (var i = 0; i < this.rows_.length; ++i) {
numColumns = Math.max(numColumns, this.rows_[i].length);
}
return numColumns;
},
/**
* Returns the cell at position (rowIndex, columnIndex), or null if there is
* no such cell.
*/
getCell_: function(rowIndex, columnIndex) {
if (rowIndex >= this.rows_.length)
return null;
var row = this.rows_[rowIndex];
if (columnIndex >= row.length)
return null;
return row[columnIndex];
},
/**
* Returns true if searchString can be found entirely within a cell.
* Case insensitive.
*
* @param {string} string String to search for, must be lowercase.
* @return {boolean} True if some cell contains searchString.
*/
search: function(searchString) {
var numColumns = this.getNumColumns();
for (var r = 0; r < this.rows_.length; ++r) {
for (var c = 0; c < numColumns; ++c) {
var cell = this.getCell_(r, c);
if (!cell)
continue;
if (cell.text.toLowerCase().indexOf(searchString) != -1)
return true;
}
}
return false;
},
/**
* Prints a formatted text representation of the table data to the
* node |parent|. |spacing| indicates number of extra spaces, if any,
* to add between columns.
*/
toText: function(spacing, parent) {
var pre = addNode(parent, 'pre');
var numColumns = this.getNumColumns();
// Figure out the maximum width of each column.
var columnWidths = [];
columnWidths.length = numColumns;
for (var i = 0; i < numColumns; ++i)
columnWidths[i] = 0;
// If header row is present, temporarily add a spacer row to |rows_|.
if (this.hasHeaderRow_) {
var headerSpacerRow = [];
for (var c = 0; c < numColumns; ++c) {
var cell = this.getCell_(0, c);
if (!cell)
continue;
var spacerStr = makeRepeatedString('-', cell.text.length);
headerSpacerRow.push(new TablePrinterCell(spacerStr));
}
this.rows_.splice(1, 0, headerSpacerRow);
}
var numRows = this.rows_.length;
for (var c = 0; c < numColumns; ++c) {
for (var r = 0; r < numRows; ++r) {
var cell = this.getCell_(r, c);
if (cell && !cell.allowOverflow) {
columnWidths[c] = Math.max(columnWidths[c], cell.text.length);
}
}
}
var out = [];
// Print title, if present.
if (this.title_) {
var titleSpacerStr = makeRepeatedString('-', this.title_.length);
out.push(titleSpacerStr);
out.push('\n');
out.push(this.title_);
out.push('\n');
out.push(titleSpacerStr);
out.push('\n');
}
// Print each row.
var spacingStr = makeRepeatedString(' ', spacing);
for (var r = 0; r < numRows; ++r) {
for (var c = 0; c < numColumns; ++c) {
var cell = this.getCell_(r, c);
if (cell) {
// Pad the cell with spaces to make it fit the maximum column width.
var padding = columnWidths[c] - cell.text.length;
var paddingStr = makeRepeatedString(' ', padding);
if (cell.alignRight)
out.push(paddingStr);
if (cell.link) {
// Output all previous text, and clear |out|.
addTextNode(pre, out.join(''));
out = [];
var linkNode = addNodeWithText(pre, 'a', cell.text);
linkNode.href = cell.link;
} else {
out.push(cell.text);
}
if (!cell.alignRight)
out.push(paddingStr);
out.push(spacingStr);
}
}
out.push('\n');
}
// Remove spacer row under the header row, if one was added.
if (this.hasHeaderRow_)
this.rows_.splice(1, 1);
addTextNode(pre, out.join(''));
},
/**
* Adds a new HTML table to the node |parent| using the specified style.
*/
toHTML: function(parent, style) {
var numRows = this.rows_.length;
var numColumns = this.getNumColumns();
var table = addNode(parent, 'table');
table.setAttribute('class', style);
var thead = addNode(table, 'thead');
var tbody = addNode(table, 'tbody');
// Add title, if needed.
if (this.title_) {
var tableTitleRow = addNode(thead, 'tr');
var tableTitle = addNodeWithText(tableTitleRow, 'th', this.title_);
tableTitle.colSpan = numColumns;
tableTitle.classList.add('title');
}
// Fill table body, adding header row first, if needed.
for (var r = 0; r < numRows; ++r) {
var cellType;
var row;
if (r == 0 && this.hasHeaderRow_) {
row = addNode(thead, 'tr');
cellType = 'th';
} else {
row = addNode(tbody, 'tr');
cellType = 'td';
}
for (var c = 0; c < numColumns; ++c) {
var cell = this.getCell_(r, c);
if (cell) {
var tableCell = addNode(row, cellType, cell.text);
if (cell.alignRight)
tableCell.alignRight = true;
// If allowing overflow on the rightmost cell of a row,
// make the cell span the rest of the columns. Otherwise,
// ignore the flag.
if (cell.allowOverflow && !this.getCell_(r, c + 1))
tableCell.colSpan = numColumns - c;
if (cell.link) {
var linkNode = addNodeWithText(tableCell, 'a', cell.text);
linkNode.href = cell.link;
} else {
addTextNode(tableCell, cell.text);
}
}
}
}
return table;
}
};
/**
* Links are only used in HTML tables.
*/
function TablePrinterCell(value) {
this.text = '' + value;
this.link = null;
this.alignRight = false;
this.allowOverflow = false;
}
return TablePrinter;
})();