|
| 1 | +/* |
| 2 | + * Copyright 2018, 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 | +const React = require('react'); |
| 9 | +const PropTypes = require('prop-types'); |
| 10 | +const { isString } = require('lodash'); |
| 11 | + |
| 12 | +/** |
| 13 | + * Base map component that renders a map. |
| 14 | + * It is implementation independent. |
| 15 | + * The implementation of the layer is provided by the `plugins` property |
| 16 | + * @prop {string} id the id of the map div |
| 17 | + * @prop {object} options. Options to pass to the map component (generically constant) |
| 18 | + * @prop {object} map the map properties (projection...) This is generically the dynamic part of the map options. |
| 19 | + * @prop {object[]} layers the layers to add to the map |
| 20 | + * @prop {object} plugins specific implementation of the components to render. |
| 21 | + * Must contain implementations for: |
| 22 | + * - Map React component for Map |
| 23 | + * - Layer React component for Layer |
| 24 | + * - Feature (optional) React component for vector Feature |
| 25 | + * - tools (optional) any support tool you want to use |
| 26 | + * @prop {array} tools. A list of tools (string name or object with `name` and other options as attribute) to add to the map. |
| 27 | + * @prop {object} eventHandlers handlers for map events |
| 28 | + * Each tool must be implemented in plugins.tools |
| 29 | + * |
| 30 | + */ |
| 31 | +class BaseMap extends React.Component { |
| 32 | + static propTypes = { |
| 33 | + id: PropTypes.string, |
| 34 | + options: PropTypes.object, |
| 35 | + map: PropTypes.object, |
| 36 | + mapStateSource: PropTypes.string, |
| 37 | + eventHandlers: PropTypes.object, |
| 38 | + layers: PropTypes.array, |
| 39 | + plugins: PropTypes.any, |
| 40 | + tools: PropTypes.array, |
| 41 | + getLayerProps: PropTypes.func |
| 42 | + }; |
| 43 | + |
| 44 | + static defaultProps = { |
| 45 | + id: '__base_map__', |
| 46 | + options: {}, |
| 47 | + map: {}, |
| 48 | + tools: [], |
| 49 | + eventHandlers: { |
| 50 | + onMapViewChanges: () => {}, |
| 51 | + onClick: () => {}, |
| 52 | + onMouseMove: () => {}, |
| 53 | + onLayerLoading: () => {}, |
| 54 | + onLayerError: () => {} |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + getTool = (tool) => { |
| 59 | + const { plugins } = this.props; |
| 60 | + if (isString(tool)) { |
| 61 | + return { |
| 62 | + name: tool, |
| 63 | + impl: plugins.tools[tool] |
| 64 | + }; |
| 65 | + } |
| 66 | + return { |
| 67 | + name: tool.name, |
| 68 | + impl: plugins.tools[tool.name], |
| 69 | + ...tool |
| 70 | + }; |
| 71 | + }; |
| 72 | + |
| 73 | + renderLayers = () => { |
| 74 | + const projection = this.props.map.projection || 'EPSG:3857'; |
| 75 | + const { plugins } = this.props; |
| 76 | + const { Layer } = plugins; |
| 77 | + return this.props.layers.map((layer, index) => { |
| 78 | + return ( |
| 79 | + <Layer |
| 80 | + type={layer.type} |
| 81 | + srs={projection} |
| 82 | + position={index} |
| 83 | + key={layer.id || layer.name} |
| 84 | + options={layer} |
| 85 | + > |
| 86 | + {this.renderLayerContent(layer, projection)} |
| 87 | + </Layer> |
| 88 | + ); |
| 89 | + }); |
| 90 | + }; |
| 91 | + |
| 92 | + renderLayerContent = (layer, projection) => { |
| 93 | + if (layer.features && layer.type === "vector") { |
| 94 | + const { plugins } = this.props; |
| 95 | + const { Feature } = plugins; |
| 96 | + return layer.features.map((feature) => { |
| 97 | + return ( |
| 98 | + <Feature |
| 99 | + key={feature.id} |
| 100 | + msId={feature.id} |
| 101 | + type={feature.type} |
| 102 | + crs={projection} |
| 103 | + geometry={feature.geometry} |
| 104 | + msId={feature.id} |
| 105 | + featuresCrs={layer.featuresCrs || 'EPSG:4326'} |
| 106 | + // FEATURE STYLE OVERWRITE LAYER STYLE |
| 107 | + layerStyle={layer.style} |
| 108 | + style={feature.style || layer.style || null} |
| 109 | + properties={feature.properties} /> |
| 110 | + ); |
| 111 | + }); |
| 112 | + } |
| 113 | + return null; |
| 114 | + }; |
| 115 | + |
| 116 | + renderTools = () => { |
| 117 | + return this.props.tools.map((tool) => { |
| 118 | + const {impl: Tool, name, ...options} = this.getTool(tool); |
| 119 | + return <Tool key={name} {...options} />; |
| 120 | + }); |
| 121 | + }; |
| 122 | + |
| 123 | + render() { |
| 124 | + const {plugins} = this.props; |
| 125 | + const {Map} = plugins; |
| 126 | + if (this.props.map) { |
| 127 | + return ( |
| 128 | + <Map |
| 129 | + id={this.props.id} |
| 130 | + zoomControl={false} |
| 131 | + center={{ x: 0, y: 0 }} |
| 132 | + zoom={1} |
| 133 | + mapStateSource={this.props.mapStateSource || this.props.id} |
| 134 | + {...this.props.options} |
| 135 | + {...this.props.map} |
| 136 | + {...this.props.eventHandlers} |
| 137 | + > |
| 138 | + {this.renderLayers()} |
| 139 | + {this.renderTools()} |
| 140 | + </Map> |
| 141 | + ); |
| 142 | + } |
| 143 | + return null; |
| 144 | + } |
| 145 | +} |
| 146 | +module.exports = BaseMap; |
0 commit comments