@hpcc-js/dgrid 3.5.9 → 3.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,146 +1,147 @@
1
- function entitiesEncode(str) {
2
- return String(str).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
3
- }
4
-
5
- function safeEncode(item) {
6
- switch (Object.prototype.toString.call(item)) {
7
- case "[object Undefined]":
8
- case "[object Boolean]":
9
- case "[object Number]":
10
- return item;
11
- case "[object String]":
12
- return entitiesEncode(item);
13
- default:
14
- console.warn("Unknown cell type: " + Object.prototype.toString.call(item));
15
- }
16
- return item;
17
- }
18
-
19
- function isArray(obj: any): obj is any[] {
20
- return obj instanceof Array;
21
- }
22
-
23
- const LINE_SPLITTER = "<br><hr class='dgrid-fakeline'>";
24
- const LINE_SPLITTER2 = "<br><hr class='dgrid-fakeline' style='visibility: hidden'>";
25
-
26
- export interface ColumnType {
27
- field: string;
28
- leafID: string;
29
- label: string;
30
- idx: number;
31
- className: string;
32
- sortable: boolean;
33
- isSet: boolean;
34
- width?: number;
35
- formatter?: CellFormatter;
36
- renderCell?: CellRenderer;
37
- children?: ColumnType[];
38
- }
39
- export interface RowType {
40
- __hpcc_id: number;
41
- __origRow: any[];
42
- [colIdx: number]: any;
43
- }
44
- export type CellFormatter = (this: ColumnType, cell: any, row: RowType) => string;
45
- export type CellRenderer = (this: ColumnType, row: RowType, cell: any, cellElement: HTMLElement) => HTMLElement | void;
46
-
47
- export class RowFormatter {
48
- private _columns: ColumnType[];
49
- private _flattenedColumns = [];
50
- private _columnIdx = {};
51
- private _formattedRow = {};
52
-
53
- constructor(columns: ColumnType[], protected _renderHtml) {
54
- this._columns = columns;
55
- this.flattenColumns(columns);
56
- }
57
-
58
- flattenColumns(columns: ColumnType[]) {
59
- for (const column of columns) {
60
- this.flattenColumn(column);
61
- }
62
- }
63
-
64
- flattenColumn(column: ColumnType) {
65
- if (column.children) {
66
- for (const childColumn of column.children) this.flattenColumn(childColumn);
67
- } else {
68
- this._columnIdx[column.field] = this._flattenedColumns.length;
69
- this._flattenedColumns.push(column.field);
70
- }
71
- }
72
-
73
- format(row) {
74
- this._formattedRow = {};
75
- this.formatRow(this._columns, row);
76
- return this.row();
77
- }
78
-
79
- calcDepth(columns: ColumnType[], row) {
80
- let maxChildDepth = 1;
81
- for (const column of columns) {
82
- if (column.children && row[column.leafID]) {
83
- let childDepth = 0;
84
- let childRows = [];
85
- if (isArray(row[column.leafID])) {
86
- childRows = row[column.leafID];
87
- } else if (isArray(row[column.leafID].Row)) {
88
- childRows = row[column.leafID].Row;
89
- }
90
- for (const childRow of childRows) {
91
- childDepth += this.calcDepth(column.children, childRow);
92
- }
93
- maxChildDepth = Math.max(maxChildDepth, childDepth);
94
- }
95
- }
96
- return maxChildDepth;
97
- }
98
-
99
- formatCell(column: ColumnType, cell, maxChildDepth) {
100
- if (column.children) {
101
- let childDepth = 0;
102
- if (!isArray(cell)) {
103
- if (isArray(cell.Row)) {
104
- cell = cell.Row;
105
- } else {
106
- cell = [cell];
107
- }
108
- }
109
- for (const row of cell) {
110
- childDepth = Math.max(childDepth, this.formatRow(column.children, row));
111
- }
112
- } else {
113
- if (column.isSet) {
114
- cell = JSON.stringify(cell.Item);
115
- }
116
- if (this._formattedRow[column.field] === undefined) {
117
- this._formattedRow[column.field] = "" + (cell === undefined ? "" : (this._renderHtml ? cell : safeEncode(cell)));
118
- } else {
119
- this._formattedRow[column.field] += LINE_SPLITTER;
120
- this._formattedRow[column.field] += "" + (cell === undefined ? "" : (this._renderHtml ? cell : safeEncode(cell)));
121
- }
122
- if (maxChildDepth > 1) {
123
- const paddingArr = [];
124
- paddingArr.length = maxChildDepth;
125
- const padding = paddingArr.join(LINE_SPLITTER2);
126
- this._formattedRow[column.field] += padding;
127
- }
128
- }
129
- }
130
-
131
- formatRow(columns: ColumnType[], row: { [key: string]: any } = [], rowIdx: number = 0) {
132
- const maxChildDepth = this.calcDepth(columns, row);
133
- for (const column of columns) {
134
- this.formatCell(column, row[column.leafID], maxChildDepth);
135
- }
136
- return maxChildDepth;
137
- }
138
-
139
- row() {
140
- const retVal = {};
141
- for (const column of this._flattenedColumns) {
142
- retVal[column] = this._formattedRow[column];
143
- }
144
- return retVal;
145
- }
146
- }
1
+ function entitiesEncode(str) {
2
+ return String(str).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
3
+ }
4
+
5
+ function safeEncode(item) {
6
+ switch (Object.prototype.toString.call(item)) {
7
+ case "[object Undefined]":
8
+ case "[object Boolean]":
9
+ case "[object Number]":
10
+ return item;
11
+ case "[object String]":
12
+ return entitiesEncode(item);
13
+ default:
14
+ console.warn("Unknown cell type: " + Object.prototype.toString.call(item));
15
+ }
16
+ return item;
17
+ }
18
+
19
+ function isArray(obj: any): obj is any[] {
20
+ return obj instanceof Array;
21
+ }
22
+
23
+ const LINE_SPLITTER = "<br><hr class='dgrid-fakeline'>";
24
+ const LINE_SPLITTER2 = "<br><hr class='dgrid-fakeline' style='visibility: hidden'>";
25
+
26
+ export interface ColumnType {
27
+ field: string;
28
+ leafID: string;
29
+ label: string;
30
+ idx: number;
31
+ className: string;
32
+ sortable: boolean;
33
+ hidden: boolean;
34
+ isSet: boolean;
35
+ width?: number;
36
+ formatter?: CellFormatter;
37
+ renderCell?: CellRenderer;
38
+ children?: ColumnType[];
39
+ }
40
+ export interface RowType {
41
+ __hpcc_id: number;
42
+ __origRow: any[];
43
+ [colIdx: number]: any;
44
+ }
45
+ export type CellFormatter = (this: ColumnType, cell: any, row: RowType) => string;
46
+ export type CellRenderer = (this: ColumnType, row: RowType, cell: any, cellElement: HTMLElement) => HTMLElement | void;
47
+
48
+ export class RowFormatter {
49
+ private _columns: ColumnType[];
50
+ private _flattenedColumns = [];
51
+ private _columnIdx = {};
52
+ private _formattedRow = {};
53
+
54
+ constructor(columns: ColumnType[], protected _renderHtml) {
55
+ this._columns = columns;
56
+ this.flattenColumns(columns);
57
+ }
58
+
59
+ flattenColumns(columns: ColumnType[]) {
60
+ for (const column of columns) {
61
+ this.flattenColumn(column);
62
+ }
63
+ }
64
+
65
+ flattenColumn(column: ColumnType) {
66
+ if (column.children) {
67
+ for (const childColumn of column.children) this.flattenColumn(childColumn);
68
+ } else {
69
+ this._columnIdx[column.field] = this._flattenedColumns.length;
70
+ this._flattenedColumns.push(column.field);
71
+ }
72
+ }
73
+
74
+ format(row) {
75
+ this._formattedRow = {};
76
+ this.formatRow(this._columns, row);
77
+ return this.row();
78
+ }
79
+
80
+ calcDepth(columns: ColumnType[], row) {
81
+ let maxChildDepth = 1;
82
+ for (const column of columns) {
83
+ if (column.children && row[column.leafID]) {
84
+ let childDepth = 0;
85
+ let childRows = [];
86
+ if (isArray(row[column.leafID])) {
87
+ childRows = row[column.leafID];
88
+ } else if (isArray(row[column.leafID].Row)) {
89
+ childRows = row[column.leafID].Row;
90
+ }
91
+ for (const childRow of childRows) {
92
+ childDepth += this.calcDepth(column.children, childRow);
93
+ }
94
+ maxChildDepth = Math.max(maxChildDepth, childDepth);
95
+ }
96
+ }
97
+ return maxChildDepth;
98
+ }
99
+
100
+ formatCell(column: ColumnType, cell, maxChildDepth) {
101
+ if (column.children) {
102
+ let childDepth = 0;
103
+ if (!isArray(cell)) {
104
+ if (isArray(cell.Row)) {
105
+ cell = cell.Row;
106
+ } else {
107
+ cell = [cell];
108
+ }
109
+ }
110
+ for (const row of cell) {
111
+ childDepth = Math.max(childDepth, this.formatRow(column.children, row));
112
+ }
113
+ } else {
114
+ if (column.isSet) {
115
+ cell = JSON.stringify(cell.Item);
116
+ }
117
+ if (this._formattedRow[column.field] === undefined) {
118
+ this._formattedRow[column.field] = "" + (cell === undefined ? "" : (this._renderHtml ? cell : safeEncode(cell)));
119
+ } else {
120
+ this._formattedRow[column.field] += LINE_SPLITTER;
121
+ this._formattedRow[column.field] += "" + (cell === undefined ? "" : (this._renderHtml ? cell : safeEncode(cell)));
122
+ }
123
+ if (maxChildDepth > 1) {
124
+ const paddingArr = [];
125
+ paddingArr.length = maxChildDepth;
126
+ const padding = paddingArr.join(LINE_SPLITTER2);
127
+ this._formattedRow[column.field] += padding;
128
+ }
129
+ }
130
+ }
131
+
132
+ formatRow(columns: ColumnType[], row: { [key: string]: any } = [], rowIdx: number = 0) {
133
+ const maxChildDepth = this.calcDepth(columns, row);
134
+ for (const column of columns) {
135
+ this.formatCell(column, row[column.leafID], maxChildDepth);
136
+ }
137
+ return maxChildDepth;
138
+ }
139
+
140
+ row() {
141
+ const retVal = {};
142
+ for (const column of this._flattenedColumns) {
143
+ retVal[column] = this._formattedRow[column];
144
+ }
145
+ return retVal;
146
+ }
147
+ }