-
Notifications
You must be signed in to change notification settings - Fork 416
/
Copy pathmap.jsx
189 lines (180 loc) · 7.35 KB
/
map.jsx
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
/*
* 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 { find, isEqual, omit } from 'lodash';
import React from 'react';
import {connect} from 'react-redux';
import {branch, compose, createEventHandler, mapPropsStream, withHandlers, withProps, withPropsOnChange, withStateHandlers} from 'recompose';
import {createSelector} from 'reselect';
import uuid from "uuid";
import {getCurrentFocusedContentEl, isFocusOnContentSelector, resourcesSelector} from '../../../../selectors/geostory';
import {createMapObject} from '../../../../utils/GeoStoryUtils';
import Message from '../../../I18N/Message';
import ToolbarButton from '../../../misc/toolbar/ToolbarButton';
import withConfirm from '../../../misc/withConfirm';
import withNodeSelection from '../../../widgets/builder/wizard/map/enhancers/handleNodeSelection';
const ConfirmButton = withConfirm(ToolbarButton);
/**
* create a map property merging a resource map and a content map obj
* resourceId a and map should be present in props
*/
export default compose(
connect(
createSelector(
resourcesSelector,
isFocusOnContentSelector,
(resources, isContentFocused) => ({
resources,
isContentFocused
}))),
withProps(
({ resources, resourceId, map = {}}) => {
const cleanedMap = {...map, layers: (map.layers || []).map(l => l ? l : undefined)};
const resource = find(resources, { id: resourceId }) || {};
return { map: createMapObject(omit(resource.data, ['context']), cleanedMap)};
}
));
/**
* Adds resourceId and content map props needed to connect the mapEditor to the map.
* Adds disableReset prop when map hasn't been modified
*/
export const withFocusedContentMap = compose(
connect(createSelector(getCurrentFocusedContentEl, (focusedEl = {}) => ({focusedEl }))), // Map connection and update withFocusedMap
withProps(
({ focusedEl: {resourceId, map} = {}}) => {
return { map: map || {}, resourceId, disableReset: !map };
}
));
/**
* It Adjusts the path to update content map config obj
*/
export const handleMapUpdate = withHandlers({
onChangeMap: ({update, focusedContent = {}}) =>
(path, value) => {
update(`${focusedContent.path}.map.${path}`, value, "merge");
},
onChange: ({update, focusedContent = {}}) =>
(path, value) => {
update(focusedContent.path + `.${path}`, value, "merge");
}
});
/**
* Handle edit map toggle, map rest and open AdvancedMapEditor.
* Map reset restores the original resource map configuration by removing all content map configs
*/
export const handleToolbar = withHandlers({
toggleEditing: ({editMap, update, focusedContent}) => () =>
update(focusedContent.path + ".editMap", !editMap),
onReset: ({update, focusedContent: {path = ""} = {}}) => () => {
update(path + `.map`, undefined);
}
});
/**
* It adds toolbar button and handling of layer selection
*/
const ResetButton = (props) => (<ConfirmButton
glyph="repeat"
bsStyle= "primary"
className="square-button-md no-border"
tooltipId="geostory.contentToolbar.resetMap"
confirmTitle={<Message msgId="geostory.contentToolbar.resetMapConfirm" />}
confirmContent={<Message msgId="geostory.contentToolbar.resetConfirmContent" />}
{...props}
/>);
export const withToolbar = compose(
withProps(({disableReset, onReset, buttonItems = [], map}) => ({
buttons: [{
renderButton: <ResetButton disabled={disableReset} onClick={onReset}/>
},
...buttonItems.map(({Component}) => ({renderButton: <Component map={map} />}))]
})),
withNodeSelection, // Node selection
withStateHandlers(() => ({'editNode': undefined}), { // Node enable editing
setEditNode: () => node => ({'editNode': node}),
closeNodeEditor: () => () => ({'editNode': undefined})
}),
branch( // Setting node buttons
({editNode}) => !!editNode,
withProps(({ selectedNodes = [], closeNodeEditor = () => {}}) => ({
buttons: [{
visible: selectedNodes.length === 1,
tooltipId: "close",
glyph: "1-close",
onClick: closeNodeEditor
}]
})),
withProps(({ selectedNodes = [], setEditNode = () => { }, buttons = [] }) => ({
buttons: [{
visible: selectedNodes.length === 1,
glyph: "wrench",
tooltipId: "toc.toolLayerSettingsTooltip",
onClick: () => {setEditNode(selectedNodes[0]);}
}, ...buttons]
}))));
/**
* Add save changes logic.
*/
export const withSaveChanges = compose(
withPropsOnChange(["focusedContent"], ({map, focusedEl: {map: contentMap} = {} } ) => ({contentMap, lastSavedMap: map})),
withPropsOnChange(["map"], ({map, lastSavedMap}) => {
return {pendingChanges: !isEqual(lastSavedMap, map)};
})
);
/**
* Add close confirm if there are some pending changes
*/
export const withConfirmClose = compose(
withProps(({toggleEditing}) => ({
CloseBtn: (props) => (
<ToolbarButton onClick={toggleEditing} {...props} />)
}))
);
/**
* Add local map state management to map component
*/
export const withLocalMapState = mapPropsStream(props$ => {
const { stream: onMapViewChanges$, handler: onMapViewLocalChanges} = createEventHandler();
return props$
.pluck('map')
.distinctUntilChanged((a, b ) => isEqual(a, b))
.switchMap((map) => {
return onMapViewChanges$.map((localMapState) => {
return { map: {...map, ...localMapState}};
}).startWith({map});
})
.combineLatest(props$, ({map} = {}, props = {}) => {
return {
...props,
onMapViewLocalChanges,
map
};
});
});
// current implementation will update the map only if the movement
// between 12 decimals in the reference system to avoid rounded value
// changes due to float mathematic operations.
const isNearlyEqual = function(a, b) {
if (a === undefined || b === undefined) {
return false;
}
return a.toFixed(12) - b.toFixed(12) === 0;
};
/**
* Handle editing, when mapEditing is true, map changes updates the geostory state, otherwise local map state is updated
*/
export const withMapEditingAndLocalMapState = withHandlers( {
onMapViewChanges: ({update, editMap = false, onMapViewLocalChanges, map: {center: oCenter = {}, zoom: oZoom, mapStateSource: oMapStateSource, resolution: oResolution, resetPanAndZoom} = {}} = {}) => ({center = {}, zoom, mapStateSource, resolution}) => {
const equalCenter = isNearlyEqual(oCenter.x, center.x) && isNearlyEqual(oCenter.y, center.y);
if ((editMap && !(equalCenter && oZoom === zoom))) {
update("map", {center, zoom, mapStateSource: uuid(), resolution, resetPanAndZoom: false}, 'merge');
} else if (resetPanAndZoom) {
update("map", {center: oCenter, zoom: oZoom, mapStateSource: oMapStateSource, resolution: oResolution, resetPanAndZoom: false}, 'merge');
} else {
onMapViewLocalChanges({center, zoom, mapStateSource, resolution});
}
}
});