-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathCommon.ts
More file actions
178 lines (161 loc) · 6.66 KB
/
Common.ts
File metadata and controls
178 lines (161 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
167
168
169
170
171
172
173
174
175
176
177
import { HTMLWidget, Selection } from "@hpcc-js/common";
import { Grid, PagingGrid } from "./dgrid-shim.ts";
import { DBStore } from "./DBStore.ts";
import { type ColumnType } from "./RowFormatter.ts";
import "../src/Common.css";
export class Common extends HTMLWidget {
protected _columns: ColumnType[] = [];
protected _store = new DBStore(this._db);
protected _dgridDiv: Selection<HTMLDivElement, unknown, HTMLElement, unknown>;
protected _dgrid: typeof PagingGrid | typeof Grid;
protected _prevPaging: boolean;
private _prevSortBy: string;
private _prevSortByDescending: boolean;
private _prevMultiSelect: boolean;
constructor() {
super();
this._tag = "div";
}
protected formatSortBy(): [{ property: string, descending: boolean }] | undefined {
const idx = this.columns().indexOf(this.sortBy());
return idx >= 0 ? [{ property: idx.toString(), descending: this.sortByDescending() }] : undefined;
}
protected _supressEvents;
selection(): any[];
selection(_: any[]): this;
selection(_?: any[]): any[] | this {
if (!arguments.length) {
const retVal = [];
for (const id in this._dgrid.selection) {
if (this._dgrid.selection[id]) {
const storeItem = this._store.get(+id);
retVal.push(this.rowToObj(storeItem));
}
}
return retVal;
}
this._supressEvents = true;
this._dgrid?.clearSelection();
let first = true;
this.data().forEach((row, idx) => {
if (_.indexOf(row) >= 0) {
const row = this._dgrid?.row(idx);
if (row.element && first) {
first = false;
row.element.scrollIntoView();
}
this._dgrid?.select(idx);
}
});
this._supressEvents = false;
}
enter(domNode, element) {
super.enter(domNode, element);
this._dgridDiv = element.append("div")
.attr("class", "flat")
;
}
update(domNode, element) {
super.update(domNode, element);
this._store.renderHtml(this.renderHtml());
if (!this._dgrid || this._prevPaging !== this.pagination() ||
this._prevSortBy !== this.sortBy() ||
this._prevSortByDescending !== this.sortByDescending() ||
this._prevMultiSelect !== this.multiSelect()) {
this._prevPaging = this.pagination();
this._prevSortBy = this.sortBy();
this._prevSortByDescending = this.sortByDescending();
this._prevMultiSelect = this.multiSelect();
if (this._dgrid) {
this._dgrid.destroy();
this._dgridDiv = element.append("div")
.attr("class", "flat")
;
}
this._dgrid = new (this._prevPaging ? PagingGrid : Grid)({
columns: this._columns,
collection: this._store,
sort: this.formatSortBy(),
selectionMode: this.multiSelect() ? "extended" : "single",
deselectOnRefresh: true,
cellNavigation: false,
pagingLinks: 1,
pagingTextBox: true,
previousNextArrows: true,
firstLastArrows: true,
rowsPerPage: this.pageSize(),
pageSizeOptions: [1, 10, 25, 50, 100, 1000]
}, this._dgridDiv.node());
this._dgrid.on("dgrid-select", (evt) => {
if (this._supressEvents) return;
if (evt.rows && evt.rows.length && evt.rows[0].data) {
this.click(this.rowToObj(evt.rows[0].data.__origRow), "", true, { selection: this.selection() });
}
});
this._dgrid.on("dgrid-deselect", (evt) => {
if (this._supressEvents) return;
if (evt.rows && evt.rows.length && evt.rows[0].data) {
this.click(this.rowToObj(evt.rows[0].data.__origRow), "", false, { selection: this.selection() });
}
});
this._dgrid.on("dgrid-column-autofit", (evt) => {
if (this._supressEvents) return;
if (evt.detail?.label) {
const column = this._columns.find(c => c.label === evt.detail.label);
if (!column) return;
this.dblclickColResize(column.label, column);
}
});
this._dgrid.refresh({});
}
this._dgrid.noDataMessage = `<span class='dojoxGridNoData'>${this.noDataMessage()}</span>`;
this._dgrid.loadingMessage = `<span class='dojoxGridNoData'>${this.loadingMessage()}</span>`;
this._dgridDiv
.style("width", this.width() + "px")
.style("height", this.height() - 2 + "px")
;
this._dgrid.resize();
}
exit(domNode, element) {
delete this._prevPaging;
if (this._dgrid) {
this._dgrid.destroy();
delete this._dgrid;
}
super.exit(domNode, element);
}
click(row, col, sel, more) {
}
dblclickColResize(column, dgridColumn) {
}
}
Common.prototype._class += " dgrid_Common";
export interface Common {
noDataMessage(): string;
noDataMessage(_: string): this;
loadingMessage(): string;
loadingMessage(_: string): this;
pagination(): boolean;
pagination(_: boolean): this;
pageSize(): number;
pageSize(_: number): this;
sortable(): boolean;
sortable(_: boolean): this;
sortBy(): string;
sortBy(_: string): this;
sortByDescending(): boolean;
sortByDescending(_: boolean): this;
multiSelect(): boolean;
multiSelect(_: boolean): this;
renderHtml(): boolean;
renderHtml(_: boolean): this;
}
Common.prototype.publish("noDataMessage", "...empty...", "string", "No Data Message");
Common.prototype.publish("loadingMessage", "loading...", "string", "Loading Message");
Common.prototype.publish("pagination", false, "boolean", "Enable paging");
Common.prototype.publish("pageSize", 25, "number", "Page size");
Common.prototype.publish("sortable", false, "boolean", "Enable sorting by column");
Common.prototype.publish("sortBy", null, "set", "Default 'sort by' Column ID", function (this: Common) { return this.columns(); }, { optional: true });
Common.prototype.publish("sortByDescending", false, "boolean", "Default 'sort by' descending", undefined, { disable: self => !self.sortBy() });
Common.prototype.publish("multiSelect", false, "boolean", "Multiple Selection");
Common.prototype.publish("renderHtml", true, "boolean", "Render HTML");