-
Notifications
You must be signed in to change notification settings - Fork 409
/
Copy pathhandleMapSelect.js
71 lines (66 loc) · 3.38 KB
/
handleMapSelect.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
/*
* Copyright 2019, 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 { compose, withState, mapPropsStream, withHandlers } from 'recompose';
import axios from '../../../../../../libs/ajax';
import ConfigUtils from '../../../../../../utils/ConfigUtils';
import { excludeGoogleBackground, extractTileMatrixFromSources } from '../../../../../../utils/LayersUtils';
import { EMPTY_MAP } from "../../../../../../utils/MapUtils";
import { is3DVisualizationMode } from "../../../../../../utils/MapTypeUtils";
import { getResource } from '../../../../../../api/persistence';
import '../../../../../../libs/bindings/rxjsRecompose';
import uuidv1 from 'uuid/v1';
import castArray from 'lodash/castArray';
const handleMapSelect = compose(
withState('selected', "setSelected", null),
withState('mapLoading', "setMapLoading", false),
withHandlers({
onMapChoice: ({ onMapSelected = () => { }, selectedSource = {}, includeMapId = false } = {}) => (maps = []) => {
return axios.all(
castArray(maps).map(map =>
(typeof map.id === 'string'
? axios.get(map.id).then(response => response.data)
: getResource(map.id, {baseURL: selectedSource.baseURL, includeAttributes: false})
.toPromise()
.then(({data}) => data)
).then(config => {
let mapState = (!config.version && typeof map.id !== 'string') ? ConfigUtils.convertFromLegacy(config) : ConfigUtils.normalizeConfig(config.map);
return {
mapId: uuidv1(),
name: typeof map.id === 'string' ? EMPTY_MAP : map.name,
...(mapState && mapState.map || {}),
...(includeMapId ? {id: map.id} : {}),
groups: mapState && mapState.groups || [],
layers: excludeGoogleBackground(mapState.layers.map(l => {
if (l.group === "background" && (l.type === "ol" || l.type === "OpenLayers.Layer")) {
l.type = "empty";
}
return l;
}))
};
}).then(res => {
// Extract tileMatrix from source and update layers
res.layers = res.sources ? res.layers.map(l => {
const tileMatrix = extractTileMatrixFromSources(res.sources, l);
return {...l, ...tileMatrix};
}) : res.layers;
// enable identify tool on map widgets only for 2D maps
return {...res, mapInfoControl: !is3DVisualizationMode(res) };
}))
).then((results)=> onMapSelected({ maps: results }));
}
}),
mapPropsStream(props$ =>
props$.distinctUntilKeyChanged('selected').filter(({ selected } = {}) => selected).startWith({})
.combineLatest(props$, ({ canProceed } = {}, props) => ({
canProceed,
...props
})
)
)
);
export default handleMapSelect;