-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathRowFormatter.ts
More file actions
147 lines (134 loc) · 4.73 KB
/
RowFormatter.ts
File metadata and controls
147 lines (134 loc) · 4.73 KB
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
function entitiesEncode(str) {
return String(str).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
}
function safeEncode(item) {
switch (Object.prototype.toString.call(item)) {
case "[object Undefined]":
case "[object Boolean]":
case "[object Number]":
return item;
case "[object String]":
return entitiesEncode(item);
default:
console.warn("Unknown cell type: " + Object.prototype.toString.call(item));
}
return item;
}
function isArray(obj: any): obj is any[] {
return obj instanceof Array;
}
const LINE_SPLITTER = "<br><hr class='dgrid-fakeline'>";
const LINE_SPLITTER2 = "<br><hr class='dgrid-fakeline' style='visibility: hidden'>";
export interface ColumnType {
field: string;
leafID: string;
label: string;
idx: number;
className: string;
sortable: boolean;
hidden: boolean;
isSet: boolean;
width?: number;
formatter?: CellFormatter;
renderCell?: CellRenderer;
children?: ColumnType[];
}
export interface RowType {
__hpcc_id: number;
__origRow: any[];
[colIdx: number]: any;
}
export type CellFormatter = (this: ColumnType, cell: any, row: RowType) => string;
export type CellRenderer = (this: ColumnType, row: RowType, cell: any, cellElement: HTMLElement) => HTMLElement | void;
export class RowFormatter {
private _columns: ColumnType[];
private _flattenedColumns = [];
private _columnIdx = {};
private _formattedRow = {};
constructor(columns: ColumnType[], protected _renderHtml) {
this._columns = columns;
this.flattenColumns(columns);
}
flattenColumns(columns: ColumnType[]) {
for (const column of columns) {
this.flattenColumn(column);
}
}
flattenColumn(column: ColumnType) {
if (column.children) {
for (const childColumn of column.children) this.flattenColumn(childColumn);
} else {
this._columnIdx[column.field] = this._flattenedColumns.length;
this._flattenedColumns.push(column.field);
}
}
format(row) {
this._formattedRow = {};
this.formatRow(this._columns, row);
return this.row();
}
calcDepth(columns: ColumnType[], row) {
let maxChildDepth = 1;
for (const column of columns) {
if (column.children && row[column.leafID]) {
let childDepth = 0;
let childRows = [];
if (isArray(row[column.leafID])) {
childRows = row[column.leafID];
} else if (isArray(row[column.leafID].Row)) {
childRows = row[column.leafID].Row;
}
for (const childRow of childRows) {
childDepth += this.calcDepth(column.children, childRow);
}
maxChildDepth = Math.max(maxChildDepth, childDepth);
}
}
return maxChildDepth;
}
formatCell(column: ColumnType, cell, maxChildDepth) {
if (column.children) {
let childDepth = 0;
if (!isArray(cell)) {
if (isArray(cell.Row)) {
cell = cell.Row;
} else {
cell = [cell];
}
}
for (const row of cell) {
childDepth = Math.max(childDepth, this.formatRow(column.children, row));
}
} else {
if (column.isSet) {
cell = JSON.stringify(cell.Item);
}
if (this._formattedRow[column.field] === undefined) {
this._formattedRow[column.field] = "" + (cell === undefined ? "" : (this._renderHtml ? cell : safeEncode(cell)));
} else {
this._formattedRow[column.field] += LINE_SPLITTER;
this._formattedRow[column.field] += "" + (cell === undefined ? "" : (this._renderHtml ? cell : safeEncode(cell)));
}
if (maxChildDepth > 1) {
const paddingArr = [];
paddingArr.length = maxChildDepth;
const padding = paddingArr.join(LINE_SPLITTER2);
this._formattedRow[column.field] += padding;
}
}
}
formatRow(columns: ColumnType[], row: { [key: string]: any } = [], rowIdx: number = 0) {
const maxChildDepth = this.calcDepth(columns, row);
for (const column of columns) {
this.formatCell(column, row[column.leafID], maxChildDepth);
}
return maxChildDepth;
}
row() {
const retVal = {};
for (const column of this._flattenedColumns) {
retVal[column] = this._formattedRow[column];
}
return retVal;
}
}