@hpcc-js/dgrid 2.34.2 → 2.34.4

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,126 +1,126 @@
1
- import { DDL2 } from "@hpcc-js/ddl-shim";
2
- import { Deferred, QueryResults } from "@hpcc-js/dgrid-shim";
3
- import { ColumnType, RowFormatter } from "./RowFormatter";
4
-
5
- export interface IDatasource {
6
- id: () => string;
7
- hash: () => string;
8
- label: () => string;
9
-
10
- outFields: () => DDL2.IField[];
11
- total: () => number;
12
- fetch: (from: number, count: number) => Promise<ReadonlyArray<object>>;
13
- }
14
-
15
- export class DatasourceCache implements IDatasource {
16
- protected _datasource: IDatasource;
17
- _prevHash: string;
18
- _fetchCache: { [key: string]: Promise<ReadonlyArray<object>> } = {};
19
-
20
- constructor(datasource: IDatasource) {
21
- this._datasource = datasource;
22
- }
23
-
24
- validateCache() {
25
- const hash = this.hash();
26
- if (this._prevHash !== hash) {
27
- this._prevHash = hash;
28
- this._fetchCache = {};
29
- }
30
- }
31
-
32
- id() { return this._datasource.id(); }
33
- hash() { return this._datasource.hash(); }
34
- label() { return this._datasource.label(); }
35
-
36
- outFields() { return this._datasource.outFields(); }
37
- total() { return this._datasource.total(); }
38
- fetch(from: number, count: number): Promise<ReadonlyArray<object>> {
39
- this.validateCache();
40
- const cacheID = `${from}->${count}`;
41
- let retVal = this._fetchCache[cacheID];
42
- if (!retVal) {
43
- retVal = this._datasource.fetch(from, count);
44
- this._fetchCache[cacheID] = retVal;
45
- }
46
- return retVal;
47
- }
48
- }
49
-
50
- export class DatasourceStore {
51
- _datasource: DatasourceCache;
52
- _columnsIdx: { [key: string]: number } = {};
53
- _columns;
54
-
55
- private rowFormatter: RowFormatter;
56
-
57
- constructor(datasource: IDatasource, renderHtml: boolean) {
58
- this._datasource = new DatasourceCache(datasource);
59
-
60
- this._columnsIdx = {};
61
- this._columns = this.db2Columns(this._datasource.outFields()).map((column, idx) => {
62
- this._columnsIdx[column.field] = idx;
63
- return column;
64
- });
65
- this.rowFormatter = new RowFormatter(this._columns, renderHtml);
66
- }
67
-
68
- columns() {
69
- return this._columns;
70
- }
71
-
72
- db2Columns(fields: DDL2.IField[], prefix = ""): ColumnType[] {
73
- if (!fields) return [];
74
- return fields.map((field, idx) => {
75
- const column: ColumnType = {
76
- field: prefix + field.id,
77
- leafID: field.id,
78
- label: field.id,
79
- idx,
80
- className: "resultGridCell",
81
- sortable: true,
82
- isSet: false
83
- };
84
- if (field.type === "dataset") {
85
- column.children = this.db2Columns(field.children, prefix + field.id + "_");
86
- } else {
87
- column.formatter = (cell, row) => {
88
- switch (typeof cell) {
89
- case "string":
90
- return cell.replace(/\t/g, "&nbsp;&nbsp;&nbsp;&nbsp;");
91
- }
92
- return cell;
93
- };
94
- }
95
- return column;
96
- });
97
- }
98
-
99
- getIdentity(row) {
100
- return row.__hpcc_id;
101
- }
102
-
103
- _request(start, end): Promise<{ totalLength: number, data: any[] }> {
104
- if (!this._datasource) return Promise.resolve({ totalLength: 0, data: [] });
105
- const retVal = this._datasource.fetch(start, end - start).then(response => {
106
- return {
107
- totalLength: this._datasource.total(),
108
- data: response.map((row, idx) => {
109
- const formattedRow: any = this.rowFormatter.format(row);
110
- formattedRow.__hpcc_id = start + idx;
111
- formattedRow.__origRow = row;
112
- return formattedRow;
113
- })
114
- };
115
- });
116
- return retVal;
117
- }
118
-
119
- fetchRange(options): Promise<any[]> {
120
- const retVal = new Deferred();
121
- this._request(options.start, options.end).then(response => retVal.resolve(response));
122
- return new QueryResults(retVal.then(response => response.data), {
123
- totalLength: retVal.then(response => response.totalLength)
124
- });
125
- }
126
- }
1
+ import { DDL2 } from "@hpcc-js/ddl-shim";
2
+ import { Deferred, QueryResults } from "@hpcc-js/dgrid-shim";
3
+ import { ColumnType, RowFormatter } from "./RowFormatter";
4
+
5
+ export interface IDatasource {
6
+ id: () => string;
7
+ hash: () => string;
8
+ label: () => string;
9
+
10
+ outFields: () => DDL2.IField[];
11
+ total: () => number;
12
+ fetch: (from: number, count: number) => Promise<ReadonlyArray<object>>;
13
+ }
14
+
15
+ export class DatasourceCache implements IDatasource {
16
+ protected _datasource: IDatasource;
17
+ _prevHash: string;
18
+ _fetchCache: { [key: string]: Promise<ReadonlyArray<object>> } = {};
19
+
20
+ constructor(datasource: IDatasource) {
21
+ this._datasource = datasource;
22
+ }
23
+
24
+ validateCache() {
25
+ const hash = this.hash();
26
+ if (this._prevHash !== hash) {
27
+ this._prevHash = hash;
28
+ this._fetchCache = {};
29
+ }
30
+ }
31
+
32
+ id() { return this._datasource.id(); }
33
+ hash() { return this._datasource.hash(); }
34
+ label() { return this._datasource.label(); }
35
+
36
+ outFields() { return this._datasource.outFields(); }
37
+ total() { return this._datasource.total(); }
38
+ fetch(from: number, count: number): Promise<ReadonlyArray<object>> {
39
+ this.validateCache();
40
+ const cacheID = `${from}->${count}`;
41
+ let retVal = this._fetchCache[cacheID];
42
+ if (!retVal) {
43
+ retVal = this._datasource.fetch(from, count);
44
+ this._fetchCache[cacheID] = retVal;
45
+ }
46
+ return retVal;
47
+ }
48
+ }
49
+
50
+ export class DatasourceStore {
51
+ _datasource: DatasourceCache;
52
+ _columnsIdx: { [key: string]: number } = {};
53
+ _columns;
54
+
55
+ private rowFormatter: RowFormatter;
56
+
57
+ constructor(datasource: IDatasource, renderHtml: boolean) {
58
+ this._datasource = new DatasourceCache(datasource);
59
+
60
+ this._columnsIdx = {};
61
+ this._columns = this.db2Columns(this._datasource.outFields()).map((column, idx) => {
62
+ this._columnsIdx[column.field] = idx;
63
+ return column;
64
+ });
65
+ this.rowFormatter = new RowFormatter(this._columns, renderHtml);
66
+ }
67
+
68
+ columns() {
69
+ return this._columns;
70
+ }
71
+
72
+ db2Columns(fields: DDL2.IField[], prefix = ""): ColumnType[] {
73
+ if (!fields) return [];
74
+ return fields.map((field, idx) => {
75
+ const column: ColumnType = {
76
+ field: prefix + field.id,
77
+ leafID: field.id,
78
+ label: field.id,
79
+ idx,
80
+ className: "resultGridCell",
81
+ sortable: true,
82
+ isSet: false
83
+ };
84
+ if (field.type === "dataset") {
85
+ column.children = this.db2Columns(field.children, prefix + field.id + "_");
86
+ } else {
87
+ column.formatter = (cell, row) => {
88
+ switch (typeof cell) {
89
+ case "string":
90
+ return cell.replace(/\t/g, "&nbsp;&nbsp;&nbsp;&nbsp;");
91
+ }
92
+ return cell;
93
+ };
94
+ }
95
+ return column;
96
+ });
97
+ }
98
+
99
+ getIdentity(row) {
100
+ return row.__hpcc_id;
101
+ }
102
+
103
+ _request(start, end): Promise<{ totalLength: number, data: any[] }> {
104
+ if (!this._datasource) return Promise.resolve({ totalLength: 0, data: [] });
105
+ const retVal = this._datasource.fetch(start, end - start).then(response => {
106
+ return {
107
+ totalLength: this._datasource.total(),
108
+ data: response.map((row, idx) => {
109
+ const formattedRow: any = this.rowFormatter.format(row);
110
+ formattedRow.__hpcc_id = start + idx;
111
+ formattedRow.__origRow = row;
112
+ return formattedRow;
113
+ })
114
+ };
115
+ });
116
+ return retVal;
117
+ }
118
+
119
+ fetchRange(options): Promise<any[]> {
120
+ const retVal = new Deferred();
121
+ this._request(options.start, options.end).then(response => retVal.resolve(response));
122
+ return new QueryResults(retVal.then(response => response.data), {
123
+ totalLength: retVal.then(response => response.totalLength)
124
+ });
125
+ }
126
+ }
@@ -1,57 +1,57 @@
1
- import { publish, Widget } from "@hpcc-js/common";
2
- import { Memory } from "@hpcc-js/dgrid-shim";
3
- import { Common } from "./Common";
4
- import { DatasourceStore, IDatasource } from "./DatasourceStore";
5
-
6
- export class DatasourceTable extends Common {
7
- _prevDatasource: IDatasource;
8
-
9
- constructor() {
10
- super();
11
- }
12
-
13
- @publish(null, "object", "Datasource")
14
- datasource: { (): IDatasource; (_: IDatasource): DatasourceTable };
15
-
16
- invalidate(): this {
17
- delete this._prevDatasource;
18
- return this;
19
- }
20
-
21
- enter(domNode, element) {
22
- super.enter(domNode, element);
23
- }
24
-
25
- update(domNode, element) {
26
- super.update(domNode, element);
27
- }
28
- render(callback?: (w: Widget) => void): this {
29
- return super.render(w => {
30
- if (this._prevDatasource !== this.datasource()) {
31
- this._dgrid.set("collection", new Memory());
32
- this._dgrid.set("columns", []);
33
- this._prevDatasource = this.datasource();
34
- if (this._prevDatasource) {
35
- const store = new DatasourceStore(this._prevDatasource, this.renderHtml());
36
- this._dgrid.set("columns", store.columns());
37
- this._dgrid.set("collection", store);
38
- if (callback) {
39
- callback(w);
40
- }
41
- } else {
42
- if (callback) {
43
- callback(w);
44
- }
45
- }
46
- } else {
47
- if (callback) {
48
- callback(w);
49
- }
50
- }
51
- });
52
- }
53
-
54
- click(row, col, sel) {
55
- }
56
- }
57
- DatasourceTable.prototype._class += " dgrid_DatasourceTable";
1
+ import { publish, Widget } from "@hpcc-js/common";
2
+ import { Memory } from "@hpcc-js/dgrid-shim";
3
+ import { Common } from "./Common";
4
+ import { DatasourceStore, IDatasource } from "./DatasourceStore";
5
+
6
+ export class DatasourceTable extends Common {
7
+ _prevDatasource: IDatasource;
8
+
9
+ constructor() {
10
+ super();
11
+ }
12
+
13
+ @publish(null, "object", "Datasource")
14
+ datasource: { (): IDatasource; (_: IDatasource): DatasourceTable };
15
+
16
+ invalidate(): this {
17
+ delete this._prevDatasource;
18
+ return this;
19
+ }
20
+
21
+ enter(domNode, element) {
22
+ super.enter(domNode, element);
23
+ }
24
+
25
+ update(domNode, element) {
26
+ super.update(domNode, element);
27
+ }
28
+ render(callback?: (w: Widget) => void): this {
29
+ return super.render(w => {
30
+ if (this._prevDatasource !== this.datasource()) {
31
+ this._dgrid.set("collection", new Memory());
32
+ this._dgrid.set("columns", []);
33
+ this._prevDatasource = this.datasource();
34
+ if (this._prevDatasource) {
35
+ const store = new DatasourceStore(this._prevDatasource, this.renderHtml());
36
+ this._dgrid.set("columns", store.columns());
37
+ this._dgrid.set("collection", store);
38
+ if (callback) {
39
+ callback(w);
40
+ }
41
+ } else {
42
+ if (callback) {
43
+ callback(w);
44
+ }
45
+ }
46
+ } else {
47
+ if (callback) {
48
+ callback(w);
49
+ }
50
+ }
51
+ });
52
+ }
53
+
54
+ click(row, col, sel) {
55
+ }
56
+ }
57
+ DatasourceTable.prototype._class += " dgrid_DatasourceTable";
@@ -1,139 +1,139 @@
1
-
2
- function entitiesEncode(str) {
3
- return String(str).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
4
- }
5
-
6
- function safeEncode(item) {
7
- switch (Object.prototype.toString.call(item)) {
8
- case "[object Undefined]":
9
- case "[object Boolean]":
10
- case "[object Number]":
11
- return item;
12
- case "[object String]":
13
- return entitiesEncode(item);
14
- default:
15
- console.warn("Unknown cell type: " + Object.prototype.toString.call(item));
16
- }
17
- return item;
18
- }
19
-
20
- const LINE_SPLITTER = "<br><hr class='dgrid-fakeline'>";
21
- const LINE_SPLITTER2 = "<br><hr class='dgrid-fakeline' style='visibility: hidden'>";
22
-
23
- export interface ColumnType {
24
- field: string;
25
- leafID: string;
26
- label: string;
27
- idx: number;
28
- className: string;
29
- sortable: boolean;
30
- isSet: boolean;
31
- width?: number;
32
- formatter?: CellFormatter;
33
- renderCell?: CellRenderer;
34
- children?: ColumnType[];
35
- }
36
- export interface RowType {
37
- __hpcc_id: number;
38
- __origRow: any[];
39
- [colIdx: number]: any;
40
- }
41
- export type CellFormatter = (this: ColumnType, cell: any, row: RowType) => string;
42
- export type CellRenderer = (this: ColumnType, row: RowType, cell: any, cellElement: HTMLElement) => HTMLElement | void;
43
-
44
- export class RowFormatter {
45
- private _columns: ColumnType[];
46
- private _flattenedColumns = [];
47
- private _columnIdx = {};
48
- private _formattedRow = {};
49
-
50
- constructor(columns: ColumnType[], protected _renderHtml) {
51
- this._columns = columns;
52
- this.flattenColumns(columns);
53
- }
54
-
55
- flattenColumns(columns: ColumnType[]) {
56
- for (const column of columns) {
57
- this.flattenColumn(column);
58
- }
59
- }
60
-
61
- flattenColumn(column: ColumnType) {
62
- if (column.children) {
63
- for (const childColumn of column.children) this.flattenColumn(childColumn);
64
- } else {
65
- this._columnIdx[column.field] = this._flattenedColumns.length;
66
- this._flattenedColumns.push(column.field);
67
- }
68
- }
69
-
70
- format(row) {
71
- this._formattedRow = {};
72
- this.formatRow(this._columns, row);
73
- return this.row();
74
- }
75
-
76
- calcDepth(columns: ColumnType[], row) {
77
- let maxChildDepth = 1;
78
- for (const column of columns) {
79
- if (column.children && row[column.leafID]) {
80
- let childDepth = 0;
81
- for (const childRow of row[column.leafID]) {
82
- if (childRow instanceof Array) {
83
- }
84
- childDepth += this.calcDepth(column.children, childRow);
85
- }
86
- maxChildDepth = Math.max(maxChildDepth, childDepth);
87
- }
88
- }
89
- return maxChildDepth;
90
- }
91
-
92
- formatCell(column: ColumnType, cell, maxChildDepth) {
93
- if (column.children) {
94
- let childDepth = 0;
95
- if (!(cell instanceof Array)) {
96
- if (cell instanceof Object && cell.Row instanceof Array) { // Push fix in comms?
97
- cell = cell.Row;
98
- } else {
99
- cell = [cell];
100
- }
101
- }
102
- for (const row of cell) {
103
- childDepth = Math.max(childDepth, this.formatRow(column.children, row));
104
- }
105
- } else {
106
- if (column.isSet) {
107
- cell = JSON.stringify(cell.Item);
108
- }
109
- if (this._formattedRow[column.field] === undefined) {
110
- this._formattedRow[column.field] = "" + cell === undefined ? "" : (this._renderHtml ? cell : safeEncode(cell));
111
- } else {
112
- this._formattedRow[column.field] += LINE_SPLITTER;
113
- this._formattedRow[column.field] += "" + cell === undefined ? "" : (this._renderHtml ? cell : safeEncode(cell));
114
- }
115
- if (maxChildDepth > 1) {
116
- const paddingArr = [];
117
- paddingArr.length = maxChildDepth;
118
- const padding = paddingArr.join(LINE_SPLITTER2);
119
- this._formattedRow[column.field] += padding;
120
- }
121
- }
122
- }
123
-
124
- formatRow(columns: ColumnType[], row: { [key: string]: any } = [], rowIdx: number = 0) {
125
- const maxChildDepth = this.calcDepth(columns, row);
126
- for (const column of columns) {
127
- this.formatCell(column, row[column.leafID], maxChildDepth);
128
- }
129
- return maxChildDepth;
130
- }
131
-
132
- row() {
133
- const retVal = {};
134
- for (const column of this._flattenedColumns) {
135
- retVal[column] = this._formattedRow[column];
136
- }
137
- return retVal;
138
- }
139
- }
1
+
2
+ function entitiesEncode(str) {
3
+ return String(str).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
4
+ }
5
+
6
+ function safeEncode(item) {
7
+ switch (Object.prototype.toString.call(item)) {
8
+ case "[object Undefined]":
9
+ case "[object Boolean]":
10
+ case "[object Number]":
11
+ return item;
12
+ case "[object String]":
13
+ return entitiesEncode(item);
14
+ default:
15
+ console.warn("Unknown cell type: " + Object.prototype.toString.call(item));
16
+ }
17
+ return item;
18
+ }
19
+
20
+ const LINE_SPLITTER = "<br><hr class='dgrid-fakeline'>";
21
+ const LINE_SPLITTER2 = "<br><hr class='dgrid-fakeline' style='visibility: hidden'>";
22
+
23
+ export interface ColumnType {
24
+ field: string;
25
+ leafID: string;
26
+ label: string;
27
+ idx: number;
28
+ className: string;
29
+ sortable: boolean;
30
+ isSet: boolean;
31
+ width?: number;
32
+ formatter?: CellFormatter;
33
+ renderCell?: CellRenderer;
34
+ children?: ColumnType[];
35
+ }
36
+ export interface RowType {
37
+ __hpcc_id: number;
38
+ __origRow: any[];
39
+ [colIdx: number]: any;
40
+ }
41
+ export type CellFormatter = (this: ColumnType, cell: any, row: RowType) => string;
42
+ export type CellRenderer = (this: ColumnType, row: RowType, cell: any, cellElement: HTMLElement) => HTMLElement | void;
43
+
44
+ export class RowFormatter {
45
+ private _columns: ColumnType[];
46
+ private _flattenedColumns = [];
47
+ private _columnIdx = {};
48
+ private _formattedRow = {};
49
+
50
+ constructor(columns: ColumnType[], protected _renderHtml) {
51
+ this._columns = columns;
52
+ this.flattenColumns(columns);
53
+ }
54
+
55
+ flattenColumns(columns: ColumnType[]) {
56
+ for (const column of columns) {
57
+ this.flattenColumn(column);
58
+ }
59
+ }
60
+
61
+ flattenColumn(column: ColumnType) {
62
+ if (column.children) {
63
+ for (const childColumn of column.children) this.flattenColumn(childColumn);
64
+ } else {
65
+ this._columnIdx[column.field] = this._flattenedColumns.length;
66
+ this._flattenedColumns.push(column.field);
67
+ }
68
+ }
69
+
70
+ format(row) {
71
+ this._formattedRow = {};
72
+ this.formatRow(this._columns, row);
73
+ return this.row();
74
+ }
75
+
76
+ calcDepth(columns: ColumnType[], row) {
77
+ let maxChildDepth = 1;
78
+ for (const column of columns) {
79
+ if (column.children && row[column.leafID]) {
80
+ let childDepth = 0;
81
+ for (const childRow of row[column.leafID]) {
82
+ if (childRow instanceof Array) {
83
+ }
84
+ childDepth += this.calcDepth(column.children, childRow);
85
+ }
86
+ maxChildDepth = Math.max(maxChildDepth, childDepth);
87
+ }
88
+ }
89
+ return maxChildDepth;
90
+ }
91
+
92
+ formatCell(column: ColumnType, cell, maxChildDepth) {
93
+ if (column.children) {
94
+ let childDepth = 0;
95
+ if (!(cell instanceof Array)) {
96
+ if (cell instanceof Object && cell.Row instanceof Array) { // Push fix in comms?
97
+ cell = cell.Row;
98
+ } else {
99
+ cell = [cell];
100
+ }
101
+ }
102
+ for (const row of cell) {
103
+ childDepth = Math.max(childDepth, this.formatRow(column.children, row));
104
+ }
105
+ } else {
106
+ if (column.isSet) {
107
+ cell = JSON.stringify(cell.Item);
108
+ }
109
+ if (this._formattedRow[column.field] === undefined) {
110
+ this._formattedRow[column.field] = "" + cell === undefined ? "" : (this._renderHtml ? cell : safeEncode(cell));
111
+ } else {
112
+ this._formattedRow[column.field] += LINE_SPLITTER;
113
+ this._formattedRow[column.field] += "" + cell === undefined ? "" : (this._renderHtml ? cell : safeEncode(cell));
114
+ }
115
+ if (maxChildDepth > 1) {
116
+ const paddingArr = [];
117
+ paddingArr.length = maxChildDepth;
118
+ const padding = paddingArr.join(LINE_SPLITTER2);
119
+ this._formattedRow[column.field] += padding;
120
+ }
121
+ }
122
+ }
123
+
124
+ formatRow(columns: ColumnType[], row: { [key: string]: any } = [], rowIdx: number = 0) {
125
+ const maxChildDepth = this.calcDepth(columns, row);
126
+ for (const column of columns) {
127
+ this.formatCell(column, row[column.leafID], maxChildDepth);
128
+ }
129
+ return maxChildDepth;
130
+ }
131
+
132
+ row() {
133
+ const retVal = {};
134
+ for (const column of this._flattenedColumns) {
135
+ retVal[column] = this._formattedRow[column];
136
+ }
137
+ return retVal;
138
+ }
139
+ }