-
Notifications
You must be signed in to change notification settings - Fork 7
/
rows.js
369 lines (324 loc) · 8.83 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* 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'
import { gw_executeExpression } from '../expression'
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()
}
/**
* Ensures the row index is visible by vertically scrolling the grid
*
* @param {String} id the grid's id
* @param {String|Number} index the row index or key
* @param {String} position {'top', 'middle', 'bottom', undefined/null}
*/
export function gw_ensureIndexVisible(id, index, position) {
const api = gw_getGrid(id).options.api
const node = api.getRowNode(index)
if (node) {
api.ensureNodeVisible(node, position)
} else {
api.ensureIndexVisible(Number(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 = 'ArrowUp'
const KEY_DOWN = 'ArrowDown'
const KEY_LEFT = 'ArrowLeft'
const KEY_RIGHT = 'ArrowRight'
switch (params.key) {
case KEY_DOWN:
// set selected cell on current cell + 1
if (suggestedNextCell && !suggestedNextCell.rowPinned) {
if (previousCell.rowPinned) {
const row = options.api.getDisplayedRowAtIndex(
options.api.getFirstDisplayedRow()
)
if (row) {
row.setSelected(true)
}
} else {
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
if (suggestedNextCell && !suggestedNextCell.rowPinned) {
if (previousCell.rowPinned) {
const row = options.api.getDisplayedRowAtIndex(
options.api.getLastDisplayedRow()
)
if (row) {
row.setSelected(true)
}
} else {
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')
}
}
/**
* Returns a row based on `getRowNodeId` config defined in the grid context
*
* @param {String} id the grid's id
* @param {Object} grid the grid object
*/
export function gw_getRowNodeId(_id, grid) {
const rowNodeId = grid.api.gridOptionsWrapper.gridOptions.context.getRowNodeId
return grid.data[rowNodeId]
}
/**
* Update the grid's data set
*
* @param {String} id the grid's id
* @param {Object} json json object which contains the new dataset to update the grid
*/
export function gw_setRowData(id, json) {
const options = gw_getGrid(id).options
options.api.setRowData(json)
options.rowData = json
}
/**
* Update the grid with a transaction object
*
* @param {String} id the grid id
* @param {Object} transaction
*/
export function gw_updateRowData(id, transaction, batchUpdate) {
const options = gw_getGrid(id).options
if (transaction.remove.length) {
let items = []
transaction.remove.forEach(index => {
items.push(options.api.getRowNode(index).data)
})
transaction.remove = items
}
if (transaction.add.length) {
transaction.add = transaction.add.reverse()
}
if (!batchUpdate) {
options.api.applyTransaction(transaction)
} else {
options.api.applyTransactionAsync(transaction)
}
}
/**
* 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
const api = options.api
api.forEachNode(row => {
row.setRowHeight(height)
})
api.onRowHeightChanged()
}
/**
* Set the given row height
*
* @param {String} id the grid id
* @param {Number|String} index the row index or key
* @param {Number} height the new height
*/
export function gw_setRowHeight(id, index, height) {
const options = gw_getGrid(id).options
const api = options.api
const row = api.getRowNode(index) || api.getDisplayedRowAtIndex(index)
if (row) {
row.setRowHeight(height)
options.api.onRowHeightChanged()
} else {
console.warn(`Failed to set height for row ${index}. Row cannot be found`)
}
}
/**
* Reset the row height
*
* @param {String} id the grid id
*/
export function gw_resetRowHeights(id) {
gw_getGrid(id).options.api.resetRowHeights()
}
/**
* Select row or more based on the row id or index
*
* @param {String} id the grid's id
* @param {Array} rows an array of row keys and indexes to select
*/
export function gw_setSelectedRows(id, rows) {
const options = gw_getGrid(id).options
const api = options.api
api.forEachNodeAfterFilterAndSort(node => {
if (
rows.indexOf(String(node.rowIndex)) > -1 ||
rows.indexOf(String(node.id)) > -1
) {
node.setSelected(true)
node.expanded = true
}
})
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 the row by its id or index
*
* @param {String} id grid's id
* @param {String|Number} index
*
* @return {String} row as JSON
*/
export function gw_getRow(id, index) {
const options = gw_getGrid(id).options
return JSON.stringify(
gw_parseNode(options.api.getRowNode(index), options.context)
)
}
/**
* Get rows from the grid
*
* @param {String} id The grid's id
* @param {String} phase The rows phase
* @param {String} filterExpression A filter expression
*
* @return {String} Json stringified array of rows
*/
export function gw_getRows(id, phase, filterExpression) {
const options = gw_getGrid(id).options
const parsed = []
options.api[phase](node => {
if (!node.group) {
if (filterExpression) {
const canProcess = gw_executeExpression(filterExpression, {
value: node.data,
context: options.context,
oldValue: null,
newValue: null,
node: node,
data: node.data,
colDef: null,
rowIndex: node.rowIndex,
api: node.gridApi,
columnApi: node.columnApi,
getValue: () => node.data,
column: null,
columnGroup:
// eslint-disable-next-line no-prototype-builtins
node.hasOwnProperty('parent') && node.parent.hasOwnProperty('key')
? node.parent.key
: '',
})
if (canProcess) {
parsed.push(gw_parseNode(node, options.context))
}
} else {
parsed.push(gw_parseNode(node, options.context))
}
}
})
return JSON.stringify(parsed)
}
/**
* Gets the grid to remove a row from the DOM and recreate it again from scratch.
*
* @param {String} id grid's id
*/
export function gw_redrawRows(id) {
gw_getGrid(id).options.api.redrawRows()
}
/**
* Pin an array of tow to the top of the grid
*
* @param {String} id the grid's id
* @param {Array} data array of rows
*/
export function gw_setPinnedTopRowData(id, data) {
gw_getGrid(id).options.api.setPinnedTopRowData(data)
}
/**
* Pin an array of tow to the bottom of the grid
*
* @param {String} id the grid's id
* @param {Array} data array of rows
*/
export function gw_setPinnedBottomRowData(id, data) {
gw_getGrid(id).options.api.setPinnedBottomRowData(data)
}