-
Notifications
You must be signed in to change notification settings - Fork 383
/
featuregrid.js
399 lines (391 loc) · 13.5 KB
/
featuregrid.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
* Copyright 2017, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
import assign from 'object-assign';
import { head, get } from 'lodash';
import {
SELECT_FEATURES,
DESELECT_FEATURES,
TOGGLE_FEATURES_SELECTION,
CLEAR_SELECTION,
SET_FEATURES,
FEATURES_MODIFIED,
CREATE_NEW_FEATURE,
SAVING,
SAVE_SUCCESS,
SAVE_ERROR,
CLEAR_CHANGES,
CHANGE_PAGE,
DOCK_SIZE_FEATURES,
SET_LAYER,
TOGGLE_TOOL,
CUSTOMIZE_ATTRIBUTE,
SET_SELECTION_OPTIONS,
TOGGLE_MODE,
MODES,
GEOMETRY_CHANGED,
HIDE_SYNC_POPOVER,
TOGGLE_SHOW_AGAIN_FLAG,
DELETE_GEOMETRY_FEATURE,
START_DRAWING_FEATURE,
SET_PERMISSION,
DISABLE_TOOLBAR,
OPEN_FEATURE_GRID,
CLOSE_FEATURE_GRID,
UPDATE_FILTER,
INIT_PLUGIN,
SIZE_CHANGE,
STORE_ADVANCED_SEARCH_FILTER,
GRID_QUERY_RESULT,
LOAD_MORE_FEATURES,
SET_UP,
SET_TIME_SYNC
} from '../actions/featuregrid';
import { FEATURE_TYPE_LOADED, QUERY_CREATE } from '../actions/wfsquery';
import { CHANGE_DRAWING_STATUS } from '../actions/draw';
import uuid from 'uuid';
const emptyResultsState = {
advancedFilters: {},
filters: {},
editingAllowedRoles: ["ADMIN"],
enableColumnFilters: true,
showFilteredObject: false,
timeSync: false,
showTimeSync: false,
open: false,
canEdit: false,
focusOnEdit: true,
showAgain: false,
showPopoverSync: localStorage && localStorage.getItem("showPopoverSync") !== null ? localStorage.getItem("showPopoverSync") === "true" : true,
mode: MODES.VIEW,
changes: [],
pagination: {
page: 0,
size: 20
},
select: [],
multiselect: false,
drawing: false,
newFeatures: [],
features: [],
dockSize: 0.35
};
const isSameFeature = (f1, f2) => f2 === f1 || (f1.id !== undefined && f1.id !== null && f1.id === f2.id);
const isPresent = (f1, features = []) => features.filter( f2 => isSameFeature(f1, f2)).length > 0;
const applyUpdate = (f, updates, updatesGeom) => {
if (updatesGeom) {
return {...f,
properties: {
...f.properties,
...updates
},
geometry: updatesGeom.geometry
};
}
return {...f,
properties: {
...f.properties,
...updates
}};
};
const applyNewChanges = (features, changedFeatures, updates, updatesGeom) =>
features.map(f => isPresent(f, changedFeatures) ? applyUpdate(f, updates, updatesGeom) : f);
/**
* Manages the state of the featuregrid
* The properties represent the shape of the state
* @prop {string[]} editingAllowedRoles array of user roles allowed to enter in edit mode
* @prop {boolean} canEdit flag used to enable editing on the feature grid
* @prop {object} filters filters for quick search. `{attribute: "name", value: "filter_value", opeartor: "=", rawValue: "the fitler raw value"}`
* @prop {boolean} enableColumnFilters enables column filter. [configurable]
* @prop {boolean} open feature grid open or close
* @prop {string} mode `VIEW` or `EDIT`
* @prop {array} changes list of current changes in editing
* @prop {object} pagination object containing current page and page size of the feature grid.
* @prop {array} select A list of the selected features ids
* @prop {boolean} multiselect enables multiselect (edit mode by default allows multiselect)
* @prop {boolean} drawing flag set during drawing
* @prop {array} newFeatures array of new features created in editing
* @prop {array} features list of the features currently loaded in the feature grid
* @prop {boolean} timeSync false by default. If true, enables the time sync on the timeline
* @prop {boolean} showTimeSync false by default. If true, shows the time sync button. Its disabled by default because WFS do not support officially time.
* @example
* {
* editingAllowedRoles: ["ADMIN"],
* filters: {},
* enableColumnFilters: true,
* open: false,
* canEdit: false,
* focusOnEdit: true,
* mode: MODES.VIEW,
* changes: [],
* pagination: {
* page: 0,
* size: 20
* },
* select: [],
* multiselect: false,
* drawing: false,
* newFeatures: [],
* features: [],
* dockSize: 0.35
* }
*
* @memberof reducers
*/
function featuregrid(state = emptyResultsState, action) {
switch (action.type) {
case INIT_PLUGIN: {
return assign({}, state, {
showPopoverSync: localStorage && localStorage.getItem("showPopoverSync") !== null ? localStorage.getItem("showPopoverSync") === "true" : true,
editingAllowedRoles: action.options.editingAllowedRoles || state.editingAllowedRoles || ["ADMIN"],
virtualScroll: !!action.options.virtualScroll,
maxStoredPages: action.options.maxStoredPages || 5
});
}
case LOAD_MORE_FEATURES:
case CHANGE_PAGE: {
return assign({}, state, {
pagination: {
page: action.page !== undefined ? action.page : state.pagination.page,
size: action.size !== undefined ? action.size : state.pagination.size
}
});
}
case SELECT_FEATURES: {
const features = action.features.filter(f => f.id !== 'empty_row');
if (state.multiselect && action.append) {
return assign({}, state, {select: action.append ? [...state.select, ...features] : features});
}
if (features && state.select && state.select[0] && features[0] && state.select.length === 1 && isSameFeature(features[0], state.select[0])) {
return state;
}
return assign({}, state, {select: (features || []).splice(0, 1)});
}
case TOGGLE_FEATURES_SELECTION:
let keepValues = state.select.filter( f => !isPresent(f, action.features));
// let removeValues = state.select.filter( f => isPresent(f, action.features));
let newValues = action.features.filter( f => !isPresent(f, state.select));
let res = keepValues.concat( (newValues || []));
return assign({}, state, {select: res});
case DESELECT_FEATURES:
return assign({}, state, {
select: state.select.filter(f1 => !isPresent(f1, action.features))
});
case SET_SELECTION_OPTIONS: {
return assign({}, state, {multiselect: action.multiselect});
}
case SET_UP: {
return assign({}, state, action.options || {});
}
case CLEAR_SELECTION:
return assign({}, state, {select: [], changes: []});
case SET_FEATURES:
return assign({}, state, {features: action.features});
case DOCK_SIZE_FEATURES:
return assign({}, state, {dockSize: action.dockSize});
case SET_LAYER:
return assign({}, state, {selectedLayer: action.id});
case TOGGLE_TOOL:
return assign({}, state, {
tools: {
...state.tools,
[action.tool]: action.value === undefined ? !(state.tools && state.tools[action.tool]) : action.value
}
});
case CUSTOMIZE_ATTRIBUTE:
return assign({}, state, {
attributes: {
...state.attributes,
[action.name]: {
...(state.attributes && state.attributes[action.name] || {}),
[action.key]: action.value || (state.attributes && state.attributes[action.name] && !state.attributes[action.name][action.key])
}
}
});
case TOGGLE_MODE: {
return assign({}, state, {
showPopoverSync: localStorage && localStorage.getItem("showPopoverSync") !== null ? localStorage.getItem("showPopoverSync") === "true" : action.mode !== MODES.EDIT,
tools: action.mode === MODES.EDIT ? {} : state.tools,
mode: action.mode,
multiselect: action.mode === MODES.EDIT,
drawing: false
});
}
case FEATURES_MODIFIED: {
const newFeaturesChanges = action.features.filter(f => f._new) || [];
return assign({}, state, {
newFeatures: newFeaturesChanges.length > 0 ? applyNewChanges(state.newFeatures, newFeaturesChanges, action.updated, null) : state.newFeatures,
changes: [...(state && state.changes || []), ...(action.features.filter(f => !f._new).map(f => ({
id: f.id,
updated: action.updated
})))]
});
}
case SAVING: {
return assign({}, state, {
saving: true,
loading: true
});
}
case SAVE_SUCCESS: {
return assign({}, state, {
deleteConfirm: false,
saved: true,
saving: false,
drawing: false,
loading: false
});
}
case CLEAR_CHANGES: {
return assign({}, state, {
saved: false,
deleteConfirm: false,
drawing: false,
newFeatures: [],
changes: [],
select: []
});
}
case CREATE_NEW_FEATURE: {
let id = uuid.v1();
return assign({}, state, {
newFeatures: action.features.map(f => ({...f, _new: true, id: id, type: "Feature",
geometry: null
})),
select: action.features.map(f => ({...f, _new: true, id: id, type: "Feature",
geometry: null
}))
});
}
case SAVE_ERROR: {
return assign({}, state, {
deleteConfirm: false,
saving: false,
loading: false,
drawing: false
});
}
case GEOMETRY_CHANGED: {
const newFeaturesChanges = action.features.filter(f => f._new) || [];
return assign({}, state, {
newFeatures: newFeaturesChanges.length > 0 ? applyNewChanges(state.newFeatures, newFeaturesChanges, null, {geometry: {...head(newFeaturesChanges).geometry} }) : state.newFeatures,
changes: [...(state && state.changes || []), ...(action.features.filter(f => !f._new).map(f => ({
id: f.id,
updated: {geometry: head(action.features).geometry}
})))],
drawing: false
});
}
case DELETE_GEOMETRY_FEATURE: {
const newFeaturesChanges = action.features.filter(f => f._new) || [];
return assign({}, state, {
newFeatures: newFeaturesChanges.length > 0 ? applyNewChanges(state.newFeatures, newFeaturesChanges, null, {geometry: null}) : state.newFeatures,
changes: [...(state && state.changes || []), ...(action.features.filter(f => !f._new).map(f => ({
id: f.id,
updated: {geometry: null}
})))]
});
}
case FEATURE_TYPE_LOADED: {
return assign({}, state, {
localType: get(action, "featureType.original.featureTypes[0].properties[1].localType")
});
}
case START_DRAWING_FEATURE: {
return assign({}, state, {
drawing: !state.drawing
});
}
case OPEN_FEATURE_GRID: {
return assign({}, state, {
open: true
});
}
case CLOSE_FEATURE_GRID: {
return assign({}, state, {
open: false,
pagination: {
page: 0,
size: state.pagination.size
},
mode: MODES.VIEW,
tools: {},
saved: false,
deleteConfirm: false,
drawing: false,
newFeatures: [],
changes: [],
select: []
});
}
case DISABLE_TOOLBAR: {
return assign({}, state, {
disableToolbar: action.disabled
});
}
case SET_PERMISSION: {
return assign({}, state, {
canEdit: action.permission.canEdit
});
}
case CHANGE_DRAWING_STATUS: {
if (action.status === "clean") {
return assign({}, state, {
drawing: false
});
}
return state;
}
case UPDATE_FILTER : {
const {attribute} = (action.update || {});
if (attribute) {
return assign({}, state, {
filters: {
...state.filters,
[attribute]: action.update
}
});
}
return state;
}
case QUERY_CREATE : {
return assign({}, state, {
filters: {
}
});
}
case SIZE_CHANGE : {
const maxDockSize = action.dockProps && action.dockProps.maxDockSize;
const minDockSize = action.dockProps && action.dockProps.minDockSize;
const size = maxDockSize && minDockSize && minDockSize <= action.size && maxDockSize >= action.size && action.size
|| maxDockSize && maxDockSize < action.size && maxDockSize
|| minDockSize && minDockSize > action.size && minDockSize
|| action.size;
return assign({}, state, {
dockSize: size
});
}
case STORE_ADVANCED_SEARCH_FILTER : {
return assign({}, state, {advancedFilters: assign({}, state.advancedFilters, {[state.selectedLayer]: action.filterObj})});
}
case GRID_QUERY_RESULT: {
return assign({}, state, {features: action.features || [], pages: action.pages || []});
}
case HIDE_SYNC_POPOVER: {
return assign({}, state, {showPopoverSync: false});
}
case TOGGLE_SHOW_AGAIN_FLAG: {
return assign({}, state, {showAgain: !state.showAgain});
}
case SET_TIME_SYNC: {
return assign({}, state, {timeSync: action.value});
}
default:
return state;
}
}
export default featuregrid;