/
Layer.jsx
197 lines (190 loc) · 7.3 KB
/
Layer.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
/**
* Copyright 2015, 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.
*/
const React = require('react');
const PropTypes = require('prop-types');
const assign = require('object-assign');
const isEmpty = require('lodash.isempty');
const isEqual = require('lodash.isequal');
const omit = require('lodash.omit');
const CoordinatesUtils = require('../../../utils/CoordinatesUtils');
const LayerRegistry = require('./plugins/index');
class OpenlayersLayer extends React.Component {
static propTypes = {
map: PropTypes.object,
mapId: PropTypes.string,
type: PropTypes.string,
srs: PropTypes.string,
zIndex: PropTypes.number,
options: PropTypes.object,
setLayerLoading: PropTypes.func,
swipe: PropTypes.number
}
static defaultProps = {
swipe: null
}
state = {
layer: null
}
componentDidMount() {
this.valid = true;
this.tilestoload = 0;
this.createLayer(this.props.type, this.props.options, this.props.zIndex);
}
componentWillReceiveProps(newProps) {
if (this.props.options) {
this.updateLayer(newProps, this.props);
}
if(!this.state.layer) {
return;
}
const newVisibility = newProps.options && newProps.options.visibility !== false;
this.setLayerVisibility(newVisibility);
const newOpacity = (newProps.options && newProps.options.opacity !== undefined) ? newProps.options.opacity : 255.;
this.state.layer.setOpacity(newOpacity / 255.);
if (newProps.zIndex !== this.props.zIndex && this.state.layer.setZIndex) {
this.state.layer.setZIndex(newProps.zIndex);
}
if(newProps.swipe != this.props.swipe) {
newProps.map.render();
}
}
componentWillUnmount() {
if (this.state.layer && this.props.map) {
if (this.state.layer.detached) {
this.state.layer.remove();
} else {
this.props.map.removeLayer(this.state.layer);
}
}
}
render() {
if (this.props.children) {
const layer = this.state.layer;
if(!layer) {
return null;
}
return (
<noscript>
{React.Children.map(this.props.children, child => {
return child ? React.cloneElement(child, {container: layer}) : null;
})}
</noscript>
);
}
let layerCreator = LayerRegistry[this.props.type];
if (layerCreator && layerCreator.render) {
return layerCreator.render(this.props.options, this.props.map, this.props.mapId, this.state.layer);
}
return null;
}
setLayerVisibility = (visibility) => {
var oldVisibility = this.props.options && this.props.options.visibility !== false;
if (visibility !== oldVisibility && this.state.layer && this.isValid(this.state.layer)) {
this.state.layer.setVisible(visibility);
}
}
generateOpts = (options, zIndex, srs) => {
return assign({}, options, {zIndex: zIndex, srs});
}
createLayer = (type, options, zIndex) => {
let layerCreator = LayerRegistry[type];
if (layerCreator) {
const layerOptions = this.generateOpts(options, zIndex, CoordinatesUtils.normalizeSRS(this.props.srs));
let layer = layerCreator.create(layerOptions, this.props.map, this.props.mapId);
if (layer && !layer.detached) {
this.addLayer(layer, options);
}
this.setState({layer: layer});
}
}
updateLayer = (newProps, oldProps) => {
// optimization to avoid to update the layer if not necessary
if (newProps.zIndex === oldProps.zIndex && newProps.srs === oldProps.srs) {
// check if options are the same, except loading
if (newProps.options === oldProps.options) return;
if (isEqual(omit(newProps.options, ["loading"]), omit(oldProps.options, ["loading"]) ) ) {
return;
}
}
let layerCreator = LayerRegistry[this.props.type];
if (layerCreator && layerCreator.update) {
layerCreator.update(
this.state.layer,
this.generateOpts(newProps.options, newProps.zIndex, CoordinatesUtils.normalizeSRS(newProps.srs)),
this.generateOpts(oldProps.options, oldProps.zIndex, CoordinatesUtils.normalizeSRS(oldProps.srs)),
this.props.map,
this.props.mapId);
}
}
addLayer = (layer, options) => {
if (this.isValid(layer)) {
this.props.map.addLayer(layer);
layer.on('precompose', (event) => {
let ctx = event.context;
ctx.save();
ctx.beginPath();
if(this.props.swipe) {
let width = ctx.canvas.width * (this.props.swipe / 100.);
ctx.rect(0, 0, width, ctx.canvas.height);
ctx.clip();
}
});
layer.on('postcompose', (event) => {
event.context.restore();
});
if(options.zoomToExtent) {
let map = this.props.map;
let source = layer.getSource();
source.once('change',(e) => {
if(source.getState() === 'ready') {
if(source.getFeatures().length > 0) {
map.getView().fit(source.getExtent(), map.getSize());
}
}
});
}
if (!options.tiled) {
layer.getSource().on('imageloadstart', () => {
this.props.setLayerLoading(options.uuid, true);
});
layer.getSource().on('imageloadend', () => {
this.props.setLayerLoading(options.uuid, false);
});
layer.getSource().on('imageloaderror', (event) => {
this.props.setLayerLoading(options.uuid, false);
});
}
else {
layer.getSource().on('tileloadstart', () => {
if (this.tilestoload === 0) {
this.props.setLayerLoading(options.uuid, true);
}
this.tilestoload++;
});
layer.getSource().on('tileloadend', () => {
this.tilestoload--;
if (this.tilestoload === 0) {
this.props.setLayerLoading(options.uuid, false);
}
});
layer.getSource().on('tileloaderror', (event) => {
this.tilestoload--;
if (this.tilestoload === 0) {
this.props.setLayerLoading(options.uuid, false);
}
});
}
}
}
isValid = (layer) => {
var layerCreator = LayerRegistry[this.props.type];
this.valid = layerCreator && layerCreator.isValid ? layerCreator.isValid(layer) : true;
return this.valid;
}
};
module.exports = OpenlayersLayer;