-
Notifications
You must be signed in to change notification settings - Fork 383
/
config.js
140 lines (132 loc) · 5.99 KB
/
config.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
/**
* Copyright 2015, 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 {
MAP_CONFIG_LOADED,
MAP_INFO_LOAD_START,
MAP_INFO_LOADED,
MAP_INFO_LOAD_ERROR,
MAP_CONFIG_LOAD_ERROR,
MAP_SAVE_ERROR,
MAP_SAVED,
RESET_MAP_SAVE_ERROR
} from '../actions/config';
import { MAP_CREATED, DETAILS_LOADED } from '../actions/maps';
import assign from 'object-assign';
import ConfigUtils from '../utils/ConfigUtils';
import { set, unset } from '../utils/ImmutableUtils';
import { transformLineToArcs } from '../utils/CoordinatesUtils';
import { findIndex, castArray } from 'lodash';
function mapConfig(state = null, action) {
let map;
switch (action.type) {
case MAP_CONFIG_LOADED:
let size = state && state.map && state.map.present && state.map.present.size || state && state.map && state.map.size;
let hasVersion = action.config && action.config.version >= 2;
// we get from the configuration what will be used as the initial state
let mapState = action.legacy && !hasVersion ? ConfigUtils.convertFromLegacy(action.config) : ConfigUtils.normalizeConfig(action.config.map);
// regenerate geodesic lines as property since that info has not been saved
let annotationsLayerIndex = findIndex(mapState.layers, layer => layer.id === "annotations");
if (annotationsLayerIndex !== -1) {
let featuresLayer = mapState.layers[annotationsLayerIndex].features.map(feature => {
if (feature.type === "FeatureCollection") {
return {
...feature,
features: feature.features.map(f => {
if (f.properties.useGeodesicLines) {
return set("properties.geometryGeodesic", {type: "LineString", coordinates: transformLineToArcs(f.geometry.coordinates)}, f);
}
return f;
})
};
}
if (feature.properties.geometryGeodesic) {
return set("properties.geometryGeodesic", {type: "LineString", coordinates: transformLineToArcs(feature.geometry.coordinates)}, feature);
}
return state;
});
mapState.layers[annotationsLayerIndex] = set("features", featuresLayer, mapState.layers[annotationsLayerIndex]);
}
let newMapState = {
...mapState,
layers: mapState.layers.map( l => {
if (l.group === "background" && (l.type === "ol" || l.type === "OpenLayers.Layer")) {
l.type = "empty";
}
return l;
}),
mapConfigRawData: { ...action.config }
};
newMapState.map = assign({}, newMapState.map, {mapId: action.mapId, size, version: hasVersion ? action.config.version : 1});
// we store the map initial state for future usage
return assign({}, newMapState, {mapInitialConfig: {...newMapState.map, mapId: action.mapId}});
case MAP_CONFIG_LOAD_ERROR:
return {
loadingError: {...action.error, mapId: action.mapId}
};
case MAP_INFO_LOAD_START:
map = state && state.map && state.map.present ? state.map.present : state && state.map;
if (map && map.mapId === action.mapId) {
map = assign({}, map, {loadingInfo: true});
return assign({}, state, {map: map});
}
return state;
case MAP_INFO_LOAD_ERROR: {
map = state && state.map && state.map.present ? state.map.present : state && state.map;
if (map && map.mapId === action.mapId) {
map = assign({}, map, {loadingInfoError: action.error, loadingInfo: false});
return assign({}, state, {map: map});
}
return state;
}
case MAP_INFO_LOADED:
map = state && state.map && state.map.present ? state.map.present : state && state.map;
if (map && (`${map.mapId}` === `${action.mapId}` || !map.mapId && !action.mapId)) {
map = assign({}, map, {info: action.info, loadingInfo: false});
return assign({}, state, {map: map});
}
return state;
case DETAILS_LOADED:
map = state && state.map && state.map.present ? state.map.present : state && state.map;
if (map && map.mapId.toString() === action.mapId.toString()) {
map = assign({}, map, {
info:
assign({}, map.info, {
details: action.detailsUri,
detailsSettings: action.detailsSettings
})
});
return assign({}, state, {map: map});
}
return state;
case MAP_CREATED: {
map = state && state.map && state.map.present ? state.map.present : state && state.map;
if (map) {
const {name, description, canDelete = false, canCopy = false, canEdit = false} = action.metadata || {};
// version needed to avoid automapupdate to start
map = assign({}, map, {mapId: action.resourceId, info: {...map.info, name, description, canEdit, canDelete, canCopy}, version: 2});
return assign({}, state, {map: map});
}
return state;
}
case MAP_SAVE_ERROR:
map = state && state.map && state.map.present ? state.map.present : state && state.map;
map = set('mapSaveErrors', castArray(action.error), map);
return assign({}, state, {map: map});
case MAP_SAVED:
map = state && state.map && state.map.present ? state.map.present : state && state.map;
map = unset('mapSaveErrors', map);
return assign({}, state, {map: map});
case RESET_MAP_SAVE_ERROR:
map = state?.map?.present || state?.map;
map = unset('mapSaveErrors', map);
return {...state, map};
default:
return state;
}
}
export default mapConfig;