-
Notifications
You must be signed in to change notification settings - Fork 416
/
Copy pathWidgets.jsx
355 lines (343 loc) · 13.6 KB
/
Widgets.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
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
/*
* 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 React from 'react';
import PropTypes from 'prop-types';
import {connect} from 'react-redux';
import {createSelector} from 'reselect';
import {compose, defaultProps, withHandlers, withProps, withPropsOnChange, withState} from 'recompose';
import {createPlugin} from '../utils/PluginsUtils';
import {mapIdSelector} from '../selectors/map';
import {
dependenciesSelector,
getFloatingWidgets,
getFloatingWidgetsLayout,
getMaximizedState,
getVisibleFloatingWidgets,
isTrayEnabled,
getTblWidgetZoomLoader
} from '../selectors/widgets';
import {
changeLayout,
deleteWidget,
editWidget,
exportCSV,
exportImage,
toggleCollapse,
toggleCollapseAll,
toggleMaximize,
updateWidgetProperty,
init
} from '../actions/widgets';
import editOptions from './widgets/editOptions';
import autoDisableWidgets from './widgets/autoDisableWidgets';
const RIGHT_MARGIN = 55;
import { widthProvider, heightProvider } from '../components/layout/enhancers/gridLayout';
import WidgetsViewBase from '../components/widgets/view/WidgetsView';
import {mapLayoutValuesSelector} from "../selectors/maplayout";
const WidgetsView =
compose(
connect(
createSelector(
mapIdSelector,
getVisibleFloatingWidgets,
getFloatingWidgetsLayout,
getMaximizedState,
dependenciesSelector,
(state) => mapLayoutValuesSelector(state, { right: true}),
state => state.browser && state.browser.mobile,
getFloatingWidgets,
getTblWidgetZoomLoader,
(id, widgets, layouts, maximized, dependencies, mapLayout, isMobileAgent, dropdownWidgets, recordZoomLoading) => ({
id,
widgets,
layouts,
maximized,
dependencies,
mapLayout,
isMobileAgent,
dropdownWidgets,
recordZoomLoading
})
), {
editWidget,
updateWidgetProperty,
exportCSV,
toggleCollapse,
toggleCollapseAll,
toggleMaximize,
exportImage,
deleteWidget,
onLayoutChange: changeLayout
}
),
// functionalities concerning auto-resize, layout and style.
compose(
heightProvider({ debounceTime: 20, closest: true, querySelector: '.fill' }),
widthProvider({ overrideWidthProvider: false }),
withProps(({ isMobileAgent, width, mapLayout, singleWidgetLayoutBreakpoint = 1024 }) => {
const rightOffset = mapLayout?.right ?? 0;
const isSingleWidgetLayout = isMobileAgent || width <= singleWidgetLayoutBreakpoint;
const leftOffset = isSingleWidgetLayout ? 0 : 500;
const viewWidth = width - (rightOffset + RIGHT_MARGIN + leftOffset);
const backgroundSelectorOffset = isSingleWidgetLayout ? (isMobileAgent ? 40 : 60) : 0;
return {
backgroundSelectorOffset,
isSingleWidgetLayout,
viewWidth,
rightOffset,
leftOffset,
singleWidgetLayoutBreakpoint
};
}),
withProps(({
width,
rowHeight,
cols,
height,
maximized,
leftOffset,
viewWidth,
isSingleWidgetLayout,
singleWidgetLayoutMaxHeight = 300,
singleWidgetLayoutMinHeight = 200,
backgroundSelectorOffset
} = {}) => {
const divHeight = isSingleWidgetLayout
? (height - backgroundSelectorOffset - 120) / 2
: height - backgroundSelectorOffset - 120;
const nRows = isSingleWidgetLayout ? 1 : 4;
const rowHeightRecalculated = !isSingleWidgetLayout
? Math.floor(divHeight / nRows - 20)
: divHeight > singleWidgetLayoutMaxHeight
? singleWidgetLayoutMaxHeight
: divHeight < singleWidgetLayoutMinHeight
? singleWidgetLayoutMinHeight
: singleWidgetLayoutMaxHeight;
const maximizedStyle = maximized?.widget ? {
width: '100%',
height: '100%',
marginTop: 0,
bottom: 'auto',
top: 0,
left: 0,
zIndex: 1330
} : {};
const maximizedProps = maximized?.widget ? {
width,
useDefaultWidthProvider: false,
rowHeight: height - 50,
breakpoints: { xxs: 0 },
cols: { xxs: 1 }
} : {};
const widthOptions = width ? {width: viewWidth - 1} : {};
const baseHeight = isSingleWidgetLayout
? rowHeightRecalculated
: Math.floor((height - 100) / (rowHeightRecalculated + 10)) * (rowHeightRecalculated + 10);
return ({
rowHeight: isSingleWidgetLayout ? rowHeightRecalculated : rowHeight || rowHeightRecalculated,
className: "on-map",
breakpoints: isSingleWidgetLayout ? { xxs: 0 } : { md: 0 },
cols: cols || { md: 6, xxs: 1 },
...widthOptions,
useDefaultWidthProvider: false,
style: {
left: leftOffset + 'px',
bottom: 65 + backgroundSelectorOffset,
height: baseHeight,
width: viewWidth + 'px',
position: 'absolute',
zIndex: 50,
...maximizedStyle
},
...maximizedProps
});
})
),
/* toolsOptions configurations support
* Provide functionalities to manage widgets tools visibility.
*/
compose(
defaultProps({
toolsOptions: {
showPin: "user.role===ADMIN", // users can lock widgets to disable editing, move and collapse
seeHidden: "user.role===ADMIN", // users that can see the hidden widgets (hidden with hide tool, visible only to the users that has showHide = true)
showHide: false, // show the hide tool in menu
showCollapse: true,
showMaximize: true
}
}),
// allow to customize toolsOptions object, with rules. see accessRuleParser
editOptions("toolsOptions", { asObject: true }),
// do not show collapse if tray is not present
compose(
connect(createSelector(isTrayEnabled, tray => ({ tray }))),
withPropsOnChange(
["toolsOptions", "tray"],
({toolsOptions, tray}) => ({
toolsOptions: toolsOptions ? {
...toolsOptions,
showCollapse: toolsOptions.showCollapse && tray
} : toolsOptions
})
)
),
// hide hidden widgets to user has not access to
withPropsOnChange(
["widgets", "toolsOptions"],
({ widgets = [], toolsOptions = {}}) => ({
widgets: widgets.filter(({ hide }) => hide ? toolsOptions.seeHidden : true)
})
),
// making only one widget displayed at a time for mobile view
compose(
// add state to store currently selected widget
withState('activeWidget', 'setActiveWidget', false),
withHandlers({
toggleCollapse: props => (w) => {
const showWidget = props.widgets?.find(el => el.id === props.activeWidget?.id);
if (props.isSingleWidgetLayout && showWidget) {
props.toggleCollapseAll();
} else {
props.toggleCollapse(w);
}
}
}),
// adjust dropdown options according to the widgets visibility for the user
withPropsOnChange(
["dropdownWidgets", "toolsOptions"],
({ dropdownWidgets = [], toolsOptions = {}}) => ({
dropdownWidgets: dropdownWidgets.filter(({ hide }) => hide ? toolsOptions.seeHidden : true)
})
),
// set default active widget whenever set of widgets has changed and singleWidgetLayout is used
withPropsOnChange(
["widgets", "isSingleWidgetLayout", "id"],
({widgets, isSingleWidgetLayout, activeWidget, setActiveWidget}) => {
if (widgets.length && isSingleWidgetLayout && !activeWidget) {
setActiveWidget(widgets[0]);
}
}
),
withPropsOnChange(
['activeWidget', 'isSingleWidgetLayout', 'widgets'],
({
activeWidget,
dropdownWidgets,
isSingleWidgetLayout,
widgets,
toolsOptions,
layouts,
setActiveWidget
}) => {
if (activeWidget && isSingleWidgetLayout && widgets.length) {
const widget = {
...activeWidget,
options: {
activeWidget,
dropdownWidgets,
...(activeWidget.options ?? {}),
singleWidget: true,
setActiveWidget
}
};
return {
canEdit: false,
toolsOptions: {
...toolsOptions,
showPin: false
},
layouts: {
...layouts,
xxs: [
...(layouts.xxs.map(el => ({...el, h: 1, w: 1, x: 0, y: 0})))
]
},
widgets: [widget]
};
}
return false;
}
)
)
)
)(WidgetsViewBase);
class Widgets extends React.Component {
static propTypes = {
enabled: PropTypes.bool,
enableZoomInTblWidget: PropTypes.bool
};
static defaultProps = {
enabled: true,
enableZoomInTblWidget: true
};
componentDidMount() {
this.props.onMount(this.props.pluginCfg?.defaults);
}
render() {
return this.props.enabled ? <WidgetsView {...this.props /* pass options to the plugin */ } /> : null;
}
}
/**
* Renders widgets on the map.
* @memberof plugins
* @name Widgets
* @class
* @prop {object} [toolsOptions] options to show and manage widgets tools. Widget tools are buttons available in some widgets. Any entry of this object can be configured using accessRules.
* Access rules can be defined using the syntax (@see components.misc.enhancers.security.accessRuleParser).
* The accessible parts of the state are `{mapInfo: {canEdit, canDelete...}, user: {role: "USER"}}`. So you can define rules like this:
* ```
* {showPin: ["__OR__", "user.role===ADMIN", "mapInfo.canEdit"]}
* ```
* @prop {boolean|string|array} [toolsOptions.showPin] show lock tool. By default is visible only to the admin
* @prop {boolean|string|array} [toolsOptions.showHide] show the "hide tool" for the widget (the tool allows to hide the widget to users that have `seeHidden=false` ). By default is false, in the most common case it should be the same of `seeHidden`.
* @prop {boolean|string|array} [toolsOptions.seeHidden] hides the widgets under particular conditions
* @prop {number} cfg.rowHeight Rows have a static height
* @prop {object} cfg.cols Number of columns in this layout. default is { md: 6, xxs: 1 }
* @prop {object} cfg.defaults options that are used to initialize the plugin when mounted
* @prop {object} cfg.defaults.initialSize new widget's default sizes in grid units. It contains 2 integers, `w` and `h`, representing the initial size of the new widget. This is useful when customizing `rowHeight` and/or `cols`, to generate a widget with a proportionated size.
* @example
* ```
* {
* "name": "Widgets",
* "cfg": {
* "cols": { "md": 48, "xxs": 1 },
* "rowHeight": 10,
* "defaults": {
* "initialSize": {
* "h": 8,
* "w": 8
* }
* }
* }
* }
* ```
* @prop {object} [dateFormats] object containing custom formats for date/time attribute types ( in [ISO_8601](https://en.wikipedia.org/wiki/ISO_8601) format). Once specified, custom formats will be applied for specific attribute types in Table widget. Following keys are supported: `date-time`, `date`, `time`. Example:
* ```
* "dateFormats": {
* "date-time": "MM DD YYYY - HH:mm:ss",
* "date": "MM DD YYYY",
* "time": "HH:mm:ss"
* }
* ```
*
*/
const WidgetsPlugin = connect(null, {onMount: init}
)(autoDisableWidgets(Widgets));
export default createPlugin("WidgetsPlugin", {
component: WidgetsPlugin,
containers: {
TOC: {
doNotHide: true,
name: "Widgets"
}
},
reducers: {
widgets: require('../reducers/widgets').default
},
epics: require('../epics/widgets').default
});