|
| 1 | +/* |
| 2 | + * Copyright 2021, GeoSolutions Sas. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +import React from 'react'; |
| 10 | +import { createPlugin, getMonitoredState } from '@mapstore/framework/utils/PluginsUtils'; |
| 11 | +import { connect } from 'react-redux'; |
| 12 | +import { createSelector } from 'reselect'; |
| 13 | +import { getConfigProp } from '@mapstore/framework/utils/ConfigUtils'; |
| 14 | +import DetailsPanel from '@js/components/DetailsPanel'; |
| 15 | +import { enableMapThumbnailViewer } from '@js/actions/gnresource'; |
| 16 | +import FaIcon from '@js/components/FaIcon/FaIcon'; |
| 17 | +import controls from '@mapstore/framework/reducers/controls'; |
| 18 | +import { setControlProperty } from '@mapstore/framework/actions/controls'; |
| 19 | +import gnresource from '@js/reducers/gnresource'; |
| 20 | +import { getSelectedLayerDataset } from '@js/selectors/resource'; |
| 21 | +import GNButton from '@js/components/Button'; |
| 22 | +import useDetectClickOut from '@js/hooks/useDetectClickOut'; |
| 23 | +import OverlayContainer from '@js/components/OverlayContainer'; |
| 24 | +import { withRouter } from 'react-router'; |
| 25 | +import { hashLocationToHref } from '@js/utils/SearchUtils'; |
| 26 | +import { mapSelector } from '@mapstore/framework/selectors/map'; |
| 27 | +import { parsePluginConfigExpressions } from '@js/utils/MenuUtils'; |
| 28 | +import tooltip from '@mapstore/framework/components/misc/enhancers/tooltip'; |
| 29 | +import tabComponents from '@js/plugins/detailviewer/tabComponents'; |
| 30 | + |
| 31 | +const Button = tooltip(GNButton); |
| 32 | + |
| 33 | +const ConnectedDetailsPanel = connect( |
| 34 | + createSelector([ |
| 35 | + state => state?.gnresource?.selectedLayerDataset || null, |
| 36 | + state => state?.gnresource?.loading || false, |
| 37 | + mapSelector, |
| 38 | + state => state?.gnresource?.showMapThumbnail || false |
| 39 | + ], (resource, loading, mapData, showMapThumbnail) => ({ |
| 40 | + resource, |
| 41 | + loading, |
| 42 | + initialBbox: mapData?.bbox, |
| 43 | + enableMapViewer: showMapThumbnail, |
| 44 | + resourceId: resource?.pk, |
| 45 | + tabComponents |
| 46 | + })), |
| 47 | + { |
| 48 | + closePanel: setControlProperty.bind(null, 'rightOverlay', 'enabled', false), |
| 49 | + onClose: enableMapThumbnailViewer |
| 50 | + } |
| 51 | +)(DetailsPanel); |
| 52 | + |
| 53 | +const ButtonViewer = ({ onClick, layer, size, status }) => { |
| 54 | + const layerResourceId = layer?.pk; |
| 55 | + const handleClickButton = () => { |
| 56 | + onClick(); |
| 57 | + }; |
| 58 | + return layerResourceId && status === 'LAYER' ? ( |
| 59 | + <Button |
| 60 | + variant="primary" |
| 61 | + size={size} |
| 62 | + onClick={handleClickButton} |
| 63 | + > |
| 64 | + <FaIcon name={'info-circle'} /> |
| 65 | + </Button> |
| 66 | + ) : null; |
| 67 | +}; |
| 68 | + |
| 69 | +const ConnectedButton = connect( |
| 70 | + createSelector([ |
| 71 | + getSelectedLayerDataset |
| 72 | + ], (layer) => ({ |
| 73 | + layer |
| 74 | + })), |
| 75 | + { |
| 76 | + onClick: setControlProperty.bind( |
| 77 | + null, |
| 78 | + 'rightOverlay', |
| 79 | + 'enabled', |
| 80 | + 'LayerDetailViewer' |
| 81 | + ) |
| 82 | + } |
| 83 | +)((ButtonViewer)); |
| 84 | + |
| 85 | +function LayerDetailViewer({ |
| 86 | + location, |
| 87 | + enabled, |
| 88 | + onClose, |
| 89 | + monitoredState, |
| 90 | + queryPathname = '/', |
| 91 | + tabs = [] |
| 92 | +}) { |
| 93 | + const parsedConfig = parsePluginConfigExpressions(monitoredState, { tabs }); |
| 94 | + |
| 95 | + const node = useDetectClickOut({ |
| 96 | + disabled: !enabled, |
| 97 | + onClickOut: () => { |
| 98 | + onClose(); |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + const handleFormatHref = (options) => { |
| 103 | + return hashLocationToHref({ |
| 104 | + location, |
| 105 | + ...options |
| 106 | + }); |
| 107 | + }; |
| 108 | + |
| 109 | + return ( |
| 110 | + <OverlayContainer |
| 111 | + enabled={enabled} |
| 112 | + ref={node} |
| 113 | + className="gn-overlay-wrapper" |
| 114 | + > |
| 115 | + <ConnectedDetailsPanel |
| 116 | + editTitle={null} |
| 117 | + editAbstract={null} |
| 118 | + editThumbnail={() => {}} |
| 119 | + activeEditMode={false} |
| 120 | + enableFavorite={false} |
| 121 | + formatHref={handleFormatHref} |
| 122 | + tabs={parsedConfig.tabs} |
| 123 | + pathname={queryPathname} |
| 124 | + /> |
| 125 | + </OverlayContainer> |
| 126 | + ); |
| 127 | +} |
| 128 | + |
| 129 | +const LayerDetailViewerPlugin = connect( |
| 130 | + createSelector( |
| 131 | + [ |
| 132 | + (state) => |
| 133 | + state?.controls?.rightOverlay?.enabled === 'LayerDetailViewer', |
| 134 | + getSelectedLayerDataset, |
| 135 | + state => getMonitoredState(state, getConfigProp('monitorState')) |
| 136 | + ], |
| 137 | + (enabled, layer, monitoredState) => ({ |
| 138 | + enabled, |
| 139 | + layer, |
| 140 | + monitoredState |
| 141 | + }) |
| 142 | + ), |
| 143 | + { |
| 144 | + onClose: setControlProperty.bind(null, 'rightOverlay', 'enabled', false) |
| 145 | + } |
| 146 | +)(withRouter(LayerDetailViewer)); |
| 147 | + |
| 148 | +export default createPlugin('LayerDetailViewer', { |
| 149 | + component: LayerDetailViewerPlugin, |
| 150 | + containers: { |
| 151 | + TOC: { |
| 152 | + target: 'toolbar', |
| 153 | + name: 'LayerDetailViewerButton', |
| 154 | + Component: ConnectedButton |
| 155 | + } |
| 156 | + }, |
| 157 | + reducers: { |
| 158 | + gnresource, |
| 159 | + controls |
| 160 | + } |
| 161 | +}); |
0 commit comments