1
|
|
|
import { fromJS, List } from 'immutable'; |
2
|
|
|
import { generateLastUpdate } from './../../util/lastUpdate'; |
3
|
|
|
|
4
|
|
|
import { DataSource } from './../../records'; |
5
|
|
|
|
6
|
|
|
import { getTreePathFromId } from './../../util/getTreePathFromId'; |
7
|
|
|
import { moveTreeNode } from './../../util/moveTreeNode'; |
8
|
|
|
import { setTreeValue } from './../../util/setTreeValue'; |
9
|
|
|
import { treeToFlatList } from './../../util/treeToFlatList'; |
10
|
|
|
import { setKeysInData } from './../../util/getData'; |
11
|
|
|
|
12
|
|
|
export const setData = (state, { |
13
|
|
|
currentRecords, data, gridType, stateKey, treeData, total |
14
|
|
|
}) => { |
15
|
|
|
|
16
|
|
|
const keyedData = setKeysInData(data); |
17
|
|
|
let keyedCurr; |
18
|
|
|
|
19
|
|
|
if (currentRecords) { |
20
|
|
|
keyedCurr = setKeysInData(currentRecords); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
return state.setIn([stateKey], new DataSource({ |
24
|
|
|
data: keyedData, |
25
|
|
|
proxy: keyedData, |
26
|
|
|
total: total || keyedData.count(), |
27
|
|
|
treeData: fromJS(treeData), |
28
|
|
|
gridType: gridType || 'grid', |
29
|
|
|
currentRecords: keyedCurr |
30
|
|
|
? keyedCurr |
31
|
|
View Code Duplication |
: keyedData, |
|
|
|
|
32
|
|
|
lastUpdate: generateLastUpdate() |
33
|
|
|
})); |
34
|
|
|
}; |
35
|
|
|
|
36
|
|
|
export const setPartialTreeData = (state, { |
37
|
|
|
data, parentId, showTreeRootNode, stateKey |
38
|
|
|
}) => { |
39
|
|
|
|
40
|
|
|
const tree = state.getIn([stateKey, 'treeData']); |
41
|
|
|
const flat = state.getIn([stateKey, 'data']); |
42
|
|
|
const pathToNode = [ |
43
|
|
|
-1, ...getTreePathFromId(flat, parentId) |
44
|
|
|
]; |
45
|
|
|
const updatedTree = setTreeValue( |
46
|
|
|
tree, pathToNode, { children: data } |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
let updatedFlat = treeToFlatList(updatedTree); |
50
|
|
|
|
51
|
|
|
if (!showTreeRootNode) { |
52
|
|
|
updatedFlat = updatedFlat.shift(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
const record = state.get(stateKey).merge({ |
56
|
|
|
data: updatedFlat, |
57
|
|
|
currentRecords: updatedFlat, |
58
|
|
|
treeData: updatedTree, |
59
|
|
|
proxy: updatedFlat, |
60
|
|
|
total: updatedFlat.count(), |
61
|
|
|
lastUpdate: generateLastUpdate() |
62
|
|
|
}); |
63
|
|
|
|
64
|
|
|
return state.setIn([stateKey], record); |
65
|
|
|
}; |
66
|
|
|
|
67
|
|
|
export const dismissEditor = (state, { stateKey }) => { |
68
|
|
|
const previousData = state.getIn([stateKey, 'data']); |
69
|
|
|
const previousProxy = state.getIn([stateKey, 'proxy']); |
70
|
|
|
let previousTotal = state.getIn([stateKey, 'total']); |
71
|
|
|
|
72
|
|
|
// upon dismiss, if a new row was in edit |
73
|
|
|
// but isn't save, update the total to reflect that |
74
|
|
|
if (previousData |
75
|
|
|
&& previousProxy |
76
|
|
|
&& previousData.size > previousProxy.size) { |
77
|
|
|
previousTotal = previousTotal - 1; |
78
|
|
|
} |
79
|
|
|
const record = state.get(stateKey); |
80
|
|
|
|
81
|
|
|
if (record) { |
82
|
|
|
const updated = record.merge({ |
83
|
|
|
data: previousProxy, |
84
|
|
|
proxy: previousProxy, |
85
|
|
|
currentRecords: previousProxy, |
86
|
|
|
isEditing: false, |
87
|
|
|
total: previousTotal, |
88
|
|
|
lastUpdate: generateLastUpdate() |
89
|
|
|
}); |
90
|
|
|
|
91
|
|
|
return state.setIn([stateKey], updated); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return state; |
95
|
|
|
}; |
96
|
|
|
|
97
|
|
|
export const removeRow = (state, { stateKey, rowIndex }) => { |
98
|
|
|
const existingState = state.get(stateKey); |
99
|
|
|
const currentTotal = existingState.get('total'); |
100
|
|
|
|
101
|
|
|
const remainingRows = state |
102
|
|
|
.getIn([stateKey, 'data']) |
103
|
|
|
.remove(rowIndex || 0, 1); |
104
|
|
|
|
105
|
|
|
const record = state.get(stateKey); |
106
|
|
|
|
107
|
|
|
const updatedTotal = existingState |
108
|
|
|
&& currentTotal |
109
|
|
|
&& currentTotal > 0 |
110
|
|
|
? currentTotal - 1 |
111
|
|
|
: 0; |
112
|
|
|
|
113
|
|
|
const updated = record.merge({ |
114
|
|
|
data: remainingRows, |
115
|
|
|
proxy: remainingRows, |
116
|
|
|
currentRecords: remainingRows, |
117
|
|
|
total: updatedTotal, |
118
|
|
|
lastUpdate: generateLastUpdate() |
119
|
|
|
}); |
120
|
|
|
|
121
|
|
|
return state.setIn([stateKey], updated); |
122
|
|
|
}; |
123
|
|
|
|
124
|
|
|
export const updateRow = (state, { rowIndex, stateKey, values }) => { |
125
|
|
|
|
126
|
|
|
const data = state.getIn([stateKey, 'data']); |
127
|
|
|
const row = data |
128
|
|
|
? data.get(rowIndex) |
129
|
|
|
: null; |
130
|
|
|
|
131
|
|
|
if (!row) { |
132
|
|
|
return state; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
const updatedRow = row.merge(values); |
136
|
|
|
const updatedData = state.getIn([stateKey, 'data']) |
137
|
|
|
.set(rowIndex, updatedRow); |
138
|
|
|
|
139
|
|
|
const record = state.get(stateKey); |
140
|
|
|
|
141
|
|
|
const updated = record.merge({ |
142
|
|
|
data: updatedData, |
143
|
|
|
proxy: updatedData, |
144
|
|
|
currentRecords: updatedData, |
145
|
|
|
lastUpdate: generateLastUpdate() |
146
|
|
|
}); |
147
|
|
|
|
148
|
|
|
return state.setIn([stateKey], updated); |
149
|
|
|
}; |
150
|
|
|
|
151
|
|
|
export const addNewRow = (state, { rowId, stateKey, rowIndex }) => { |
152
|
|
|
const existingState = state.get(stateKey); |
153
|
|
|
const isEditing = existingState && existingState.get('isEditing'); |
154
|
|
|
let data = existingState && existingState.get('data'); |
155
|
|
|
|
156
|
|
|
if (existingState && isEditing) { |
157
|
|
|
return state; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
let newRow = data |
161
|
|
|
&& data.size > 0 |
162
|
|
|
&& data.get(0) |
163
|
|
|
? data.get(0).reduce((p, i, c) => { return p.set(c, ''); }, fromJS({})) |
164
|
|
|
: fromJS({}); |
165
|
|
|
|
166
|
|
|
newRow = newRow.set('_key', rowId); |
167
|
|
|
|
168
|
|
|
if (!data) { |
169
|
|
|
data = new List(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
const updatedTotal = existingState |
173
|
|
|
&& existingState.get('total') |
174
|
|
|
? existingState.get('total') |
175
|
|
|
: 0; |
176
|
|
|
|
177
|
|
|
rowIndex = rowIndex === undefined |
178
|
|
|
? 0 |
179
|
|
|
: rowIndex; |
180
|
|
|
|
181
|
|
|
const newData = data.insert(rowIndex, newRow); |
182
|
|
|
|
183
|
|
|
const updated = existingState.merge({ |
184
|
|
|
data: newData, |
185
|
|
|
proxy: data, |
186
|
|
|
isEditing: true, |
187
|
|
|
total: updatedTotal + 1, |
188
|
|
|
lastUpdate: generateLastUpdate() |
189
|
|
|
}); |
190
|
|
|
|
191
|
|
|
return state.setIn([stateKey], updated); |
192
|
|
|
}; |
193
|
|
|
|
194
|
|
|
export const moveNode = (state, { |
195
|
|
|
current, next, showTreeRootNode, stateKey |
196
|
|
|
}) => { |
197
|
|
|
const nextPath = List(next.path); |
198
|
|
|
const tree = state.getIn([stateKey, 'treeData']); |
199
|
|
|
const currentPath = List(current.path); |
200
|
|
|
|
201
|
|
|
const newTreeMove = moveTreeNode( |
202
|
|
|
tree, |
203
|
|
|
current.index, |
204
|
|
View Code Duplication |
currentPath, |
|
|
|
|
205
|
|
|
next.index, |
206
|
|
|
nextPath |
207
|
|
|
); |
208
|
|
|
|
209
|
|
|
let flatMove = treeToFlatList(newTreeMove); |
210
|
|
|
|
211
|
|
|
// remove root-node |
212
|
|
|
if (!showTreeRootNode) { |
213
|
|
|
flatMove = flatMove.shift(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
const record = state.get(stateKey); |
217
|
|
|
const updated = record.merge({ |
218
|
|
|
data: flatMove, |
219
|
|
|
currentRecords: flatMove, |
220
|
|
|
treeData: newTreeMove, |
221
|
|
|
proxy: flatMove, |
222
|
|
|
lastUpdate: generateLastUpdate() |
223
|
|
|
}); |
224
|
|
|
|
225
|
|
|
return state.setIn([stateKey], updated); |
226
|
|
|
}; |
227
|
|
|
|
228
|
|
|
export const setTreeNodeVisibility = (state, { |
229
|
|
|
id, showTreeRootNode, stateKey |
230
|
|
|
}) => { |
231
|
|
|
|
232
|
|
|
const flat = state.getIn([stateKey, 'data']); |
233
|
|
|
const tree = state.getIn([stateKey, 'treeData']); |
234
|
|
|
|
235
|
|
|
const currentVisibility = !!flat |
236
|
|
|
.find(node => node.get('_id') === id).get('_hideChildren'); |
237
|
|
|
|
238
|
|
|
const path = [-1, ...getTreePathFromId(flat, id)]; |
239
|
|
|
|
240
|
|
|
const updatedTree = setTreeValue( |
241
|
|
|
tree, path, { _hideChildren: !currentVisibility } |
242
|
|
|
); |
243
|
|
|
|
244
|
|
|
let updatedList = treeToFlatList(updatedTree); |
245
|
|
|
|
246
|
|
|
// remove root-node |
247
|
|
|
if (!showTreeRootNode) { |
248
|
|
|
updatedList = updatedList.shift(); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
const record = state.get(stateKey); |
252
|
|
|
const updated = record.merge({ |
253
|
|
|
data: updatedList, |
254
|
|
|
currentRecords: updatedList, |
255
|
|
|
treeData: updatedTree, |
256
|
|
|
proxy: updatedList, |
257
|
|
|
lastUpdate: generateLastUpdate() |
258
|
|
|
}); |
259
|
|
|
|
260
|
|
|
return state.setIn([stateKey], updated); |
261
|
|
|
}; |
262
|
|
|
|
263
|
|
|
export const saveRow = (state, { rowIndex, stateKey, values }) => { |
264
|
|
|
const data = state |
265
|
|
|
.getIn([stateKey, 'data']) |
266
|
|
|
.set(rowIndex, fromJS(values)); |
267
|
|
|
|
268
|
|
|
const record = state.get(stateKey); |
269
|
|
|
const updated = record.merge({ |
270
|
|
|
data: data, |
271
|
|
|
proxy: data, |
272
|
|
|
currentRecords: data, |
273
|
|
|
lastUpdate: generateLastUpdate() |
274
|
|
|
}); |
275
|
|
|
|
276
|
|
|
return state.setIn([stateKey], updated); |
277
|
|
|
}; |
278
|
|
|
|
279
|
|
|
export const sortData = (state, { data, stateKey }) => { |
280
|
|
|
|
281
|
|
|
const record = state.get(stateKey); |
282
|
|
|
const updated = record.merge({ |
283
|
|
|
data: data, |
284
|
|
|
lastUpdate: generateLastUpdate() |
285
|
|
|
}); |
286
|
|
|
|
287
|
|
|
return state.setIn([stateKey], updated); |
288
|
|
|
}; |
289
|
|
|
|
290
|
|
|
export const clearFilter = (state, { stateKey }) => { |
291
|
|
|
const proxy = state.getIn([stateKey, 'proxy']); |
292
|
|
|
const prevData = state.getIn([stateKey, 'data']); |
293
|
|
|
const recs = proxy || prevData; |
294
|
|
|
|
295
|
|
|
const record = state.get(stateKey); |
296
|
|
|
const updated = record.merge({ |
297
|
|
|
data: recs, |
298
|
|
|
proxy: recs, |
299
|
|
|
currentRecords: recs, |
300
|
|
|
lastUpdate: generateLastUpdate() |
301
|
|
|
}); |
302
|
|
|
|
303
|
|
|
return state.setIn([stateKey], updated); |
304
|
|
|
}; |
305
|
|
|
|
306
|
|
|
export const filterData = (state, { data, stateKey }) => { |
307
|
|
|
const record = state.get(stateKey); |
308
|
|
|
const updated = record.merge({ |
309
|
|
|
data: data, |
310
|
|
|
lastUpdate: generateLastUpdate() |
311
|
|
|
}); |
312
|
|
|
|
313
|
|
|
return state.setIn([stateKey], updated); |
314
|
|
|
}; |
315
|
|
|
|