@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.
- package/LICENSE +43 -43
- package/README.md +31 -31
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/index.umd.cjs +2 -2
- package/dist/index.umd.cjs.map +1 -1
- package/package.json +6 -8
- package/src/Common.css +587 -494
- package/src/Common.ts +178 -166
- package/src/DBStore.ts +91 -90
- package/src/DatasourceStore.ts +127 -126
- package/src/DatasourceTable.ts +62 -62
- package/src/RowFormatter.ts +147 -146
- package/src/Table.ts +263 -246
- package/src/__package__.ts +3 -3
- package/src/__tests__/index.ts +1 -1
- package/src/__tests__/test1.ts +50 -50
- package/src/dgrid-shim.ts +20 -12
- package/src/i18n.js +11 -11
- package/src/index.ts +10 -10
- package/types/Common.d.ts +8 -5
- package/types/RowFormatter.d.ts +1 -0
- package/types/Table.d.ts +7 -0
- package/types/dgrid-shim.d.ts +1 -1
package/src/Common.ts
CHANGED
|
@@ -1,166 +1,178 @@
|
|
|
1
|
-
import { HTMLWidget } from "@hpcc-js/common";
|
|
2
|
-
import { Grid, PagingGrid } from "./dgrid-shim.ts";
|
|
3
|
-
import { DBStore } from "./DBStore.ts";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
protected
|
|
10
|
-
protected
|
|
11
|
-
protected
|
|
12
|
-
protected
|
|
13
|
-
|
|
14
|
-
private
|
|
15
|
-
private
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
selection(
|
|
30
|
-
selection(_
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
this.
|
|
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
|
-
this.
|
|
72
|
-
this.
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
this.
|
|
76
|
-
this.
|
|
77
|
-
this.
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
this.
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
this.
|
|
99
|
-
|
|
100
|
-
if (
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
if (
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
;
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
this.
|
|
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
|
-
|
|
1
|
+
import { HTMLWidget, Selection } from "@hpcc-js/common";
|
|
2
|
+
import { Grid, PagingGrid } from "./dgrid-shim.ts";
|
|
3
|
+
import { DBStore } from "./DBStore.ts";
|
|
4
|
+
import { type ColumnType } from "./RowFormatter.ts";
|
|
5
|
+
|
|
6
|
+
import "../src/Common.css";
|
|
7
|
+
|
|
8
|
+
export class Common extends HTMLWidget {
|
|
9
|
+
protected _columns: ColumnType[] = [];
|
|
10
|
+
protected _store = new DBStore(this._db);
|
|
11
|
+
protected _dgridDiv: Selection<HTMLDivElement, unknown, HTMLElement, unknown>;
|
|
12
|
+
protected _dgrid: typeof PagingGrid | typeof Grid;
|
|
13
|
+
protected _prevPaging: boolean;
|
|
14
|
+
private _prevSortBy: string;
|
|
15
|
+
private _prevSortByDescending: boolean;
|
|
16
|
+
private _prevMultiSelect: boolean;
|
|
17
|
+
|
|
18
|
+
constructor() {
|
|
19
|
+
super();
|
|
20
|
+
this._tag = "div";
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
protected formatSortBy(): [{ property: string, descending: boolean }] | undefined {
|
|
24
|
+
const idx = this.columns().indexOf(this.sortBy());
|
|
25
|
+
return idx >= 0 ? [{ property: idx.toString(), descending: this.sortByDescending() }] : undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
protected _supressEvents;
|
|
29
|
+
selection(): any[];
|
|
30
|
+
selection(_: any[]): this;
|
|
31
|
+
selection(_?: any[]): any[] | this {
|
|
32
|
+
if (!arguments.length) {
|
|
33
|
+
const retVal = [];
|
|
34
|
+
for (const id in this._dgrid.selection) {
|
|
35
|
+
if (this._dgrid.selection[id]) {
|
|
36
|
+
const storeItem = this._store.get(+id);
|
|
37
|
+
retVal.push(this.rowToObj(storeItem));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return retVal;
|
|
41
|
+
}
|
|
42
|
+
this._supressEvents = true;
|
|
43
|
+
this._dgrid?.clearSelection();
|
|
44
|
+
let first = true;
|
|
45
|
+
this.data().forEach((row, idx) => {
|
|
46
|
+
if (_.indexOf(row) >= 0) {
|
|
47
|
+
const row = this._dgrid?.row(idx);
|
|
48
|
+
if (row.element && first) {
|
|
49
|
+
first = false;
|
|
50
|
+
row.element.scrollIntoView();
|
|
51
|
+
}
|
|
52
|
+
this._dgrid?.select(idx);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
this._supressEvents = false;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
enter(domNode, element) {
|
|
59
|
+
super.enter(domNode, element);
|
|
60
|
+
this._dgridDiv = element.append("div")
|
|
61
|
+
.attr("class", "flat")
|
|
62
|
+
;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
update(domNode, element) {
|
|
66
|
+
super.update(domNode, element);
|
|
67
|
+
|
|
68
|
+
this._store.renderHtml(this.renderHtml());
|
|
69
|
+
|
|
70
|
+
if (!this._dgrid || this._prevPaging !== this.pagination() ||
|
|
71
|
+
this._prevSortBy !== this.sortBy() ||
|
|
72
|
+
this._prevSortByDescending !== this.sortByDescending() ||
|
|
73
|
+
this._prevMultiSelect !== this.multiSelect()) {
|
|
74
|
+
|
|
75
|
+
this._prevPaging = this.pagination();
|
|
76
|
+
this._prevSortBy = this.sortBy();
|
|
77
|
+
this._prevSortByDescending = this.sortByDescending();
|
|
78
|
+
this._prevMultiSelect = this.multiSelect();
|
|
79
|
+
if (this._dgrid) {
|
|
80
|
+
this._dgrid.destroy();
|
|
81
|
+
this._dgridDiv = element.append("div")
|
|
82
|
+
.attr("class", "flat")
|
|
83
|
+
;
|
|
84
|
+
}
|
|
85
|
+
this._dgrid = new (this._prevPaging ? PagingGrid : Grid)({
|
|
86
|
+
columns: this._columns,
|
|
87
|
+
collection: this._store,
|
|
88
|
+
sort: this.formatSortBy(),
|
|
89
|
+
selectionMode: this.multiSelect() ? "extended" : "single",
|
|
90
|
+
deselectOnRefresh: true,
|
|
91
|
+
cellNavigation: false,
|
|
92
|
+
pagingLinks: 1,
|
|
93
|
+
pagingTextBox: true,
|
|
94
|
+
previousNextArrows: true,
|
|
95
|
+
firstLastArrows: true,
|
|
96
|
+
rowsPerPage: this.pageSize(),
|
|
97
|
+
pageSizeOptions: [1, 10, 25, 50, 100, 1000]
|
|
98
|
+
}, this._dgridDiv.node());
|
|
99
|
+
this._dgrid.on("dgrid-select", (evt) => {
|
|
100
|
+
if (this._supressEvents) return;
|
|
101
|
+
if (evt.rows && evt.rows.length && evt.rows[0].data) {
|
|
102
|
+
this.click(this.rowToObj(evt.rows[0].data.__origRow), "", true, { selection: this.selection() });
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
this._dgrid.on("dgrid-deselect", (evt) => {
|
|
106
|
+
if (this._supressEvents) return;
|
|
107
|
+
if (evt.rows && evt.rows.length && evt.rows[0].data) {
|
|
108
|
+
this.click(this.rowToObj(evt.rows[0].data.__origRow), "", false, { selection: this.selection() });
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
this._dgrid.on("dgrid-column-autofit", (evt) => {
|
|
112
|
+
if (this._supressEvents) return;
|
|
113
|
+
if (evt.detail?.label) {
|
|
114
|
+
const column = this._columns.find(c => c.label === evt.detail.label);
|
|
115
|
+
if (!column) return;
|
|
116
|
+
this.dblclickColResize(column.label, column);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
this._dgrid.refresh({});
|
|
120
|
+
}
|
|
121
|
+
this._dgrid.noDataMessage = `<span class='dojoxGridNoData'>${this.noDataMessage()}</span>`;
|
|
122
|
+
this._dgrid.loadingMessage = `<span class='dojoxGridNoData'>${this.loadingMessage()}</span>`;
|
|
123
|
+
|
|
124
|
+
this._dgridDiv
|
|
125
|
+
.style("width", this.width() + "px")
|
|
126
|
+
.style("height", this.height() - 2 + "px")
|
|
127
|
+
;
|
|
128
|
+
this._dgrid.resize();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
exit(domNode, element) {
|
|
132
|
+
delete this._prevPaging;
|
|
133
|
+
if (this._dgrid) {
|
|
134
|
+
this._dgrid.destroy();
|
|
135
|
+
delete this._dgrid;
|
|
136
|
+
}
|
|
137
|
+
super.exit(domNode, element);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
click(row, col, sel, more) {
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
dblclickColResize(column, dgridColumn) {
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
Common.prototype._class += " dgrid_Common";
|
|
147
|
+
|
|
148
|
+
export interface Common {
|
|
149
|
+
noDataMessage(): string;
|
|
150
|
+
noDataMessage(_: string): this;
|
|
151
|
+
loadingMessage(): string;
|
|
152
|
+
loadingMessage(_: string): this;
|
|
153
|
+
pagination(): boolean;
|
|
154
|
+
pagination(_: boolean): this;
|
|
155
|
+
pageSize(): number;
|
|
156
|
+
pageSize(_: number): this;
|
|
157
|
+
sortable(): boolean;
|
|
158
|
+
sortable(_: boolean): this;
|
|
159
|
+
sortBy(): string;
|
|
160
|
+
sortBy(_: string): this;
|
|
161
|
+
sortByDescending(): boolean;
|
|
162
|
+
sortByDescending(_: boolean): this;
|
|
163
|
+
multiSelect(): boolean;
|
|
164
|
+
multiSelect(_: boolean): this;
|
|
165
|
+
renderHtml(): boolean;
|
|
166
|
+
renderHtml(_: boolean): this;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
Common.prototype.publish("noDataMessage", "...empty...", "string", "No Data Message");
|
|
170
|
+
Common.prototype.publish("loadingMessage", "loading...", "string", "Loading Message");
|
|
171
|
+
Common.prototype.publish("pagination", false, "boolean", "Enable paging");
|
|
172
|
+
Common.prototype.publish("pageSize", 25, "number", "Page size");
|
|
173
|
+
Common.prototype.publish("sortable", false, "boolean", "Enable sorting by column");
|
|
174
|
+
Common.prototype.publish("sortBy", null, "set", "Default 'sort by' Column ID", function (this: Common) { return this.columns(); }, { optional: true });
|
|
175
|
+
Common.prototype.publish("sortByDescending", false, "boolean", "Default 'sort by' descending", undefined, { disable: self => !self.sortBy() });
|
|
176
|
+
Common.prototype.publish("multiSelect", false, "boolean", "Multiple Selection");
|
|
177
|
+
Common.prototype.publish("renderHtml", true, "boolean", "Render HTML");
|
|
178
|
+
|
package/src/DBStore.ts
CHANGED
|
@@ -1,90 +1,91 @@
|
|
|
1
|
-
import { Database } from "@hpcc-js/common";
|
|
2
|
-
import { Deferred } from "./dgrid-shim.ts";
|
|
3
|
-
import { CellFormatter, CellRenderer, ColumnType, RowFormatter } from "./RowFormatter.ts";
|
|
4
|
-
|
|
5
|
-
export class DBStore {
|
|
6
|
-
private _db: Database.Grid;
|
|
7
|
-
|
|
8
|
-
Model: null;
|
|
9
|
-
idProperty: "__hpcc_id";
|
|
10
|
-
|
|
11
|
-
constructor(db: Database.Grid) {
|
|
12
|
-
this._db = db;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
protected _renderHtml = true;
|
|
16
|
-
renderHtml(_: boolean) {
|
|
17
|
-
this._renderHtml = _;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
db2Columns(sortable: boolean, fields: Database.Field[], prefix = "", formatter?: CellFormatter, renderCell?: CellRenderer): ColumnType[] {
|
|
21
|
-
if (!fields) return [];
|
|
22
|
-
return fields.map((field, idx) => {
|
|
23
|
-
const label = field.label();
|
|
24
|
-
const column: ColumnType = {
|
|
25
|
-
label,
|
|
26
|
-
leafID: "" + idx,
|
|
27
|
-
field: prefix + idx,
|
|
28
|
-
idx,
|
|
29
|
-
className: "resultGridCell",
|
|
30
|
-
sortable,
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
column.
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
column.
|
|
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
|
-
const
|
|
73
|
-
retVal
|
|
74
|
-
retVal.
|
|
75
|
-
retVal.
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if ((l[idx]
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
1
|
+
import { Database } from "@hpcc-js/common";
|
|
2
|
+
import { Deferred } from "./dgrid-shim.ts";
|
|
3
|
+
import { CellFormatter, CellRenderer, ColumnType, RowFormatter } from "./RowFormatter.ts";
|
|
4
|
+
|
|
5
|
+
export class DBStore {
|
|
6
|
+
private _db: Database.Grid;
|
|
7
|
+
|
|
8
|
+
Model: null;
|
|
9
|
+
idProperty: "__hpcc_id";
|
|
10
|
+
|
|
11
|
+
constructor(db: Database.Grid) {
|
|
12
|
+
this._db = db;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
protected _renderHtml = true;
|
|
16
|
+
renderHtml(_: boolean) {
|
|
17
|
+
this._renderHtml = _;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
db2Columns(sortable: boolean, fields: Database.Field[], prefix = "", formatter?: CellFormatter, renderCell?: CellRenderer): ColumnType[] {
|
|
21
|
+
if (!fields) return [];
|
|
22
|
+
return fields.map((field, idx) => {
|
|
23
|
+
const label = field.label();
|
|
24
|
+
const column: ColumnType = {
|
|
25
|
+
label,
|
|
26
|
+
leafID: "" + idx,
|
|
27
|
+
field: prefix + idx,
|
|
28
|
+
idx,
|
|
29
|
+
className: "resultGridCell",
|
|
30
|
+
sortable,
|
|
31
|
+
hidden: false,
|
|
32
|
+
isSet: false
|
|
33
|
+
};
|
|
34
|
+
switch (field.type()) {
|
|
35
|
+
case "nested":
|
|
36
|
+
column.children = this.db2Columns(false, field.children(), prefix + idx + "_", formatter);
|
|
37
|
+
column.sortable = false;
|
|
38
|
+
break;
|
|
39
|
+
default:
|
|
40
|
+
column.formatter = formatter;
|
|
41
|
+
column.renderCell = renderCell;
|
|
42
|
+
}
|
|
43
|
+
return column;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
columns(sortable: boolean, formatter?: CellFormatter, renderCell?: CellRenderer) {
|
|
48
|
+
return this.db2Columns(sortable, this._db.fields(), "", formatter, renderCell);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
getIdentity(object) {
|
|
52
|
+
return object.__hpcc_id;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
get(row: number) {
|
|
56
|
+
return this._db.row(row + 1);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
_fetchRange(opts: { start: number, end: number }): object[] {
|
|
60
|
+
const rowFormatter = new RowFormatter(this.columns(false), this._renderHtml);
|
|
61
|
+
return this._db.data().slice(opts.start, opts.end).map((row, idx) => {
|
|
62
|
+
const formattedRow: any = rowFormatter.format(row);
|
|
63
|
+
return {
|
|
64
|
+
...formattedRow,
|
|
65
|
+
__hpcc_id: opts.start + idx,
|
|
66
|
+
__origRow: row
|
|
67
|
+
};
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
fetchRange(opts: { start: number, end: number }): Promise<object[]> {
|
|
72
|
+
const data = this._fetchRange(opts);
|
|
73
|
+
const retVal: any = new Deferred();
|
|
74
|
+
retVal.totalLength = new Deferred();
|
|
75
|
+
retVal.resolve(data);
|
|
76
|
+
retVal.totalLength.resolve(this._db.length() - 1);
|
|
77
|
+
return retVal;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
sort(opts) {
|
|
81
|
+
this._db.data().sort((l, r) => {
|
|
82
|
+
for (const item of opts) {
|
|
83
|
+
const idx = item.property;
|
|
84
|
+
if ((l[idx] === undefined && r[idx] !== undefined) || l[idx] < r[idx]) return item.descending ? 1 : -1;
|
|
85
|
+
if ((l[idx] !== undefined && r[idx] === undefined) || l[idx] > r[idx]) return item.descending ? -1 : 1;
|
|
86
|
+
}
|
|
87
|
+
return 0;
|
|
88
|
+
});
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
}
|