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