-
Notifications
You must be signed in to change notification settings - Fork 416
/
Copy pathmap.js
158 lines (141 loc) · 6.3 KB
/
map.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
/*
* Copyright 2016, 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 CoordinatesUtils from '../utils/CoordinatesUtils';
import { createSelector } from 'reselect';
import {get, memoize, round} from 'lodash';
import {detectIdentifyInMapPopUp} from "../utils/MapUtils";
import { isLoggedIn } from './security';
/**
* selects map state
* @name map
* @memberof selectors
* @static
*/
/**
* get the current map configuration from state
* @function
* @memberof selectors.map
* @param {object} state the state
* @return {object} the map configuration
*/
export const mapSelector = (state) => state.map && state.map.present || state.map || state.config && state.config.map || null;
export const projectionSelector = createSelector([mapSelector], (map) => map && map.projection);
export const stateMapIdSelector = (state) => get(mapSelector(state), "mapId") && parseInt(get(mapSelector(state), "mapId"), 10) || null;
export const mapIdSelector = (state) => get(state, "mapInitialConfig.mapId") && parseInt(get(state, "mapInitialConfig.mapId"), 10) || stateMapIdSelector(state);
export const mapInfoSelector = state => get(mapSelector(state), "info");
export const mapInfoLoadingSelector = state => get(mapSelector(state), "loadingInfo", false);
export const mapSaveErrorsSelector = state => get(mapSelector(state), "mapSaveErrors");
export const mapInfoDetailsUriFromIdSelector = state => get(mapInfoSelector(state), "attributes.details");
export const mapInfoDetailsSettingsFromIdSelector = state => get(mapInfoSelector(state), "attributes.detailsSettings");
export const mapIsEditableSelector = state => {
const mapInfoCanEdit = get(mapInfoSelector(state), 'canEdit');
if (mapInfoCanEdit === undefined) {
return get(state, 'context.resource.canEdit');
}
return mapInfoCanEdit;
};
export const mapInfoAttributesSelector = state => get(mapInfoSelector(state), 'attributes');
/**
* Show editable feature checkbox based on user permission on the map resource
* @memberof selectors.map
* @param {object} state the state
* @returns {boolean} flag to show/hide the option
*/
export const showEditableFeatureCheckboxSelector = state => {
const { id: mapId, canEdit: mapCanEdit } = mapInfoSelector(state) ?? {};
const { id: contextId } = get(state, 'context.resource', {});
if (isLoggedIn(state)) {
// in case of context without a map hide the option
if (contextId && !mapId) {
return false;
}
return mapId ? mapCanEdit : true;
}
return false;
};
// TODO: move these in selectors/localConfig.js or selectors/config.js
export const projectionDefsSelector = (state) => state.localConfig && state.localConfig.projectionDefs || [];
export const mapConstraintsSelector = state => state.localConfig && state.localConfig.mapConstraints || {};
export const configuredRestrictedExtentSelector = (state) => mapConstraintsSelector(state).restrictedExtent;
export const configuredExtentCrsSelector = (state) => mapConstraintsSelector(state).crs;
export const configuredMinZoomSelector = state => {
const constraints = mapConstraintsSelector(state);
const projection = projectionSelector(state);
return projection && get(constraints, `projectionsConstraints["${projection}"].minZoom`) || get(constraints, 'minZoom');
};
// END localConfig selectors
export const mapLimitsSelector = state => get(mapSelector(state), "limits");
export const mapBboxSelector = state => get(mapSelector(state), "bbox");
export const minZoomSelector = state => get(mapLimitsSelector(state), "minZoom");
export const resolutionsSelector = state => get(mapSelector(state), "resolutions");
export const currentZoomLevelSelector = state => get(mapSelector(state), "zoom");
export const currentResolutionSelector = createSelector(
resolutionsSelector,
currentZoomLevelSelector,
(resolutions = [], currentZoomLvl) => {
return resolutions[round(currentZoomLvl)];
}
);
/**
* Get the scales of the current map
* @function
* @memberof selectors.map
* @param {object} state the state
* @return {number[]} the scales of the map
*/
export const scalesSelector = createSelector(
[resolutionsSelector, projectionSelector],
(resolutions, projection) => {
if (resolutions && projection) {
const units = CoordinatesUtils.getUnits(projection);
const dpm = 96 * (100 / 2.54);
return resolutions.map((resolution) => resolution * dpm * (units === 'degrees' ? 111194.87428468118 : 1));
}
return [];
}
);
/**
* Get version of the map
* @function
* @memberof selectors.map
* @param {object} state the state
* @return {number} version of the map
*/
export const mapVersionSelector = (state) => state.map && state.map.present && state.map.present.version || 1;
/**
* Get name/title of the map
* @function
* @memberof selectors.map
* @param {object} state the state
* @return {string} name/title of the map
*/
export const mapNameSelector = (state) => {
const mapInfo = mapInfoSelector(state);
const attributes = mapInfoAttributesSelector(state);
return attributes?.title || mapInfo?.name || '';
};
export const mapSizeSelector = (state) => state?.map?.present?.size ?? 0;
export const mapSizeValuesSelector = memoize((attributes = {}) => createSelector(
mapSizeSelector,
(sizes) => {
return sizes && Object.keys(sizes).filter(key =>
attributes[key]).reduce((a, key) => {
return ({...a, [key]: sizes[key]});
},
{}) || {};
}
), (attributes) => JSON.stringify(attributes));
export const mouseMoveListenerSelector = (state) => get(mapSelector(state), 'eventListeners.mousemove', []);
export const isMouseMoveActiveSelector = (state) => !!mouseMoveListenerSelector(state).length;
export const isMouseMoveCoordinatesActiveSelector = (state) => mouseMoveListenerSelector(state).includes('mouseposition');
export const isMouseMoveIdentifyActiveSelector = (state) => {
return mouseMoveListenerSelector(state).includes('identifyFloatingTool');
};
export const identifyFloatingToolSelector = (state) => {
return mouseMoveListenerSelector(state).includes('identifyFloatingTool') || state.mode === "embedded" || (state.mapPopups?.popups && detectIdentifyInMapPopUp(state));
};