-
Notifications
You must be signed in to change notification settings - Fork 7
/
rows.js
302 lines (253 loc) · 7.19 KB
/
rows.js
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* This file is part of the BBjGridExWidget plugin.
* (c) Basis Europe <eu@Basis.AgGridComponents.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { gw_getGrid } from "./utilities";
import { gw_parseNode } from "events/utilities";
const { deepParseJson } = require("deep-parse-json");
export function gw_setQuickFilter(id, filter) {
gw_getGrid(id)
.options
.api
.setQuickFilter(filter);
}
export function gw_expandAll(id) {
gw_getGrid(id)
.options
.api
.expandAll();
}
export function gw_collapseAll(id) {
gw_getGrid(id)
.options
.api
.collapseAll();
}
export function gw_setVisibleRow(id, index, position) {
gw_getGrid(id)
.options
.api
.ensureIndexVisible(index, position);
}
export function gw_navigateToNextRow(id, params) {
const options = gw_getGrid(id).options;
let previousCell = params.previousCellPosition;
let suggestedNextCell = params.nextCellPosition;
const KEY_UP = 38;
const KEY_DOWN = 40;
const KEY_LEFT = 37;
const KEY_RIGHT = 39;
switch (params.key) {
case KEY_DOWN:
// set selected cell on current cell + 1
options.api.forEachNode((node) => {
if (previousCell.rowIndex + 1 === node.rowIndex) {
node.setSelected(true);
}
});
return suggestedNextCell;
case KEY_UP:
// set selected cell on current cell - 1
options.api.forEachNode((node) => {
if (previousCell.rowIndex - 1 === node.rowIndex) {
node.setSelected(true);
}
});
return suggestedNextCell;
case KEY_LEFT:
case KEY_RIGHT:
return suggestedNextCell;
default:
throw new Error("You have super strange keyboard");
}
}
export function gw_getRowNodeId(id, data) {
return data[gw_getGrid(id).options.context.getRowNodeId];
}
export function gw_setRowsData(id, json) {
const options = gw_getGrid(id).options;
options.api.setRowData(json);
options.rowData = json;
options.api.refreshClientSideRowModel('group');
}
export function gw_setRowData(id, row) {
const options = gw_getGrid(id).options;
options.api.updateRowData({ update: [row] });
options.api.refreshClientSideRowModel('group');
}
export function gw_removeRows(id, indexes) {
const options = gw_getGrid(id).options;
let items = [];
indexes.forEach(index => {
items.push(options.api.getRowNode(index).data);
});
options.api.updateRowData({ remove: items });
options.api.refreshClientSideRowModel('group');
}
export function gw_addRows(id, index, rows) {
const options = gw_getGrid(id).options;
options.api.updateRowData({ add: rows, addIndex: index });
options.api.refreshClientSideRowModel('group');
}
/**
* Set the height of all rows
*
* @param {String} id the grid id
* @param {Number} height the row height
*/
export function gw_setRowsHeight(id, height) {
const options = gw_getGrid(id).options;
options.api.forEachNode(row => {
row.setRowHeight(height);
});
options.api.onRowHeightChanged();
}
/**
* Set the given row height
*
* @param {String} id the grid id
* @param {Number} index the row index
* @param {Number} height the new height
*/
export function gw_setRowHeight(id, index, height) {
const options = gw_getGrid(id).options;
const row = options.api.getDisplayedRowAtIndex(index);
if (row) {
row.setRowHeight(height);
options.api.onRowHeightChanged();
} else {
console.warn(`Failed to set height for row ${index}. Row can not be found`);
}
}
export function gw_setSelectedRows(id, rows) {
const options = gw_getGrid(id).options;
options.api.forEachNodeAfterFilterAndSort(node => {
if (rows.indexOf(node.rowIndex) > -1) {
node.setSelected(true);
node.expanded = true;
}
});
options.api.onGroupExpandedOrCollapsed();
}
export function gw_selectAll(id, filtered) {
const options = gw_getGrid(id).options;
if (1 === filtered) {
options.api.selectAllFiltered();
} else {
options.api.selectAll();
}
}
export function gw_deselectAll(id, filtered) {
const options = gw_getGrid(id).options;
if (1 === filtered) {
options.api.deselectAllFiltered();
} else {
options.api.deselectAll();
}
}
/**
* Get the current selected rows
*
* @param {Number} id grid's id
*
* @returns {String} selected rows as JSON
*/
export function gw_getSelectedRows(id) {
const options = gw_getGrid(id).options;
const nodes = options.api.getSelectedNodes();
let parsed = [];
nodes.forEach(node => {
parsed.push(gw_parseNode(node, options.context));
});
return JSON.stringify(parsed);
}
/**
* Get the last selected row
*
* @param {Number} id grid's id
*
* @returns {String} selected row as JSON
*/
export function gw_getSelectedRow(id) {
const rows = JSON.parse(gw_getSelectedRows(id));
if (rows.length) {
return JSON.stringify(rows[rows.length - 1]);
}
return '';
}
/**
* Get Range Selections
*
* The method will create a range json model for each range which can be parsed in BBj side.
*
* @param {Number} id grid's id
*
* @returns {String} selected ranges as JSON
*/
export function gw_getRangeSelections(id) {
const options = gw_getGrid(id).options;
const context = options.context;
const api = options.api;
const ranges = api.getCellRanges();
const model = api.getModel();
let result = [];
ranges.forEach(range => {
// const start = gw_parseNode(model.getRow(range.startRow.rowIndex), options.context);
// const end = gw_parseNode(model.getRow(range.endRow.rowIndex), options.context);
//if (start !== false && end !== false) {
const columns = range.columns.reduce((accumulator, current) => {
if ("ag-Grid-AutoColumn" !== current.colId) {
accumulator.push(current.colId);
}
return accumulator;
}, []).filter(Boolean);
const starIndex = Math.min(range.startRow.rowIndex, range.endRow.rowIndex);
const endIndex = Math.max(range.startRow.rowIndex, range.endRow.rowIndex);
const rows = [];
for (let rowIndex = starIndex; rowIndex <= endIndex; rowIndex++) {
const node = model.getRow(rowIndex);
if (node) {
const parsedNode = gw_parseNode(node, context);
if(parsedNode) rows.push(parsedNode);
}
}
if (rows.length && columns.length) {
result.push({ rows, columns });
}
//}
});
return JSON.stringify(result);
}
/**
* Add new cell range
*
* @param {Number} id grid's id
* @param {Object} range bounded or unbounded range model
*/
export function gw_addCellRange(id, range) {
const options = gw_getGrid(id).options;
const pr = deepParseJson(JSON.stringify(range));
const start = !(pr.start || null) ?
0 : (Number.isInteger(+pr.start) ? +pr.start : options.api.getRowNode(pr.start).rowIndex);
const end = !(pr.end || null) ?
(options.rowData.length - 1) : (Number.isInteger(+pr.end) ? +pr.end : options.api.getRowNode(pr.end).rowIndex);
options.api.addCellRange({
rowStartIndex: Math.abs(start),
rowEndIndex: Math.abs(end),
columns: pr.columns
});
}
/**
* Clears the selected range.
*
* @param {Number} id grid's id
*/
export function gw_clearRangeSelection(id) {
gw_getGrid(id)
.options
.api
.clearRangeSelection();
}