@hpcc-js/dgrid 3.5.7 → 3.5.9

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 type { DDL2 } from "@hpcc-js/ddl-shim";
2
- import { Deferred, QueryResults } from "./dgrid-shim.ts";
3
- import { ColumnType, RowFormatter } from "./RowFormatter.ts";
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 type { DDL2 } from "@hpcc-js/ddl-shim";
2
+ import { Deferred, QueryResults } from "./dgrid-shim.ts";
3
+ import { ColumnType, RowFormatter } from "./RowFormatter.ts";
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,62 +1,62 @@
1
- import { Widget } from "@hpcc-js/common";
2
- import { Memory } from "./dgrid-shim.ts";
3
- import { Common } from "./Common.ts";
4
- import { DatasourceStore, IDatasource } from "./DatasourceStore.ts";
5
-
6
- export class DatasourceTable extends Common {
7
- _prevDatasource?: IDatasource;
8
-
9
- constructor() {
10
- super();
11
- }
12
-
13
- invalidate(): this {
14
- delete this._prevDatasource;
15
- return this;
16
- }
17
-
18
- enter(domNode, element) {
19
- super.enter(domNode, element);
20
- }
21
-
22
- update(domNode, element) {
23
- super.update(domNode, element);
24
- }
25
- render(callback?: (w: Widget) => void): this {
26
- return super.render(w => {
27
- if (this._prevDatasource !== this.datasource()) {
28
- this._dgrid.set("collection", new Memory());
29
- this._dgrid.set("columns", []);
30
- this._prevDatasource = this.datasource();
31
- if (this._prevDatasource) {
32
- const store = new DatasourceStore(this._prevDatasource, this.renderHtml());
33
- this._dgrid.set("columns", store.columns());
34
- this._dgrid.set("collection", store);
35
- if (callback) {
36
- callback(w);
37
- }
38
- } else {
39
- if (callback) {
40
- callback(w);
41
- }
42
- }
43
- } else {
44
- if (callback) {
45
- callback(w);
46
- }
47
- }
48
- });
49
- }
50
-
51
- click(row, col, sel) {
52
- }
53
- }
54
- DatasourceTable.prototype._class += " dgrid_DatasourceTable";
55
-
56
- export interface DatasourceTable {
57
- datasource(): IDatasource;
58
- datasource(_: IDatasource): this;
59
- }
60
-
61
- DatasourceTable.prototype.publish("datasource", null, "object", "Datasource");
62
-
1
+ import { Widget } from "@hpcc-js/common";
2
+ import { Memory } from "./dgrid-shim.ts";
3
+ import { Common } from "./Common.ts";
4
+ import { DatasourceStore, IDatasource } from "./DatasourceStore.ts";
5
+
6
+ export class DatasourceTable extends Common {
7
+ _prevDatasource?: IDatasource;
8
+
9
+ constructor() {
10
+ super();
11
+ }
12
+
13
+ invalidate(): this {
14
+ delete this._prevDatasource;
15
+ return this;
16
+ }
17
+
18
+ enter(domNode, element) {
19
+ super.enter(domNode, element);
20
+ }
21
+
22
+ update(domNode, element) {
23
+ super.update(domNode, element);
24
+ }
25
+ render(callback?: (w: Widget) => void): this {
26
+ return super.render(w => {
27
+ if (this._prevDatasource !== this.datasource()) {
28
+ this._dgrid.set("collection", new Memory());
29
+ this._dgrid.set("columns", []);
30
+ this._prevDatasource = this.datasource();
31
+ if (this._prevDatasource) {
32
+ const store = new DatasourceStore(this._prevDatasource, this.renderHtml());
33
+ this._dgrid.set("columns", store.columns());
34
+ this._dgrid.set("collection", store);
35
+ if (callback) {
36
+ callback(w);
37
+ }
38
+ } else {
39
+ if (callback) {
40
+ callback(w);
41
+ }
42
+ }
43
+ } else {
44
+ if (callback) {
45
+ callback(w);
46
+ }
47
+ }
48
+ });
49
+ }
50
+
51
+ click(row, col, sel) {
52
+ }
53
+ }
54
+ DatasourceTable.prototype._class += " dgrid_DatasourceTable";
55
+
56
+ export interface DatasourceTable {
57
+ datasource(): IDatasource;
58
+ datasource(_: IDatasource): this;
59
+ }
60
+
61
+ DatasourceTable.prototype.publish("datasource", null, "object", "Datasource");
62
+
@@ -1,146 +1,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
- 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
+ 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
+ }