UNPKG

10.3 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 }
9 return function (d, b) {
10 extendStatics(d, b);
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13 };
14})();
15var __assign = (this && this.__assign) || function () {
16 __assign = Object.assign || function(t) {
17 for (var s, i = 1, n = arguments.length; i < n; i++) {
18 s = arguments[i];
19 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
20 t[p] = s[p];
21 }
22 return t;
23 };
24 return __assign.apply(this, arguments);
25};
26var __importStar = (this && this.__importStar) || function (mod) {
27 if (mod && mod.__esModule) return mod;
28 var result = {};
29 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
30 result["default"] = mod;
31 return result;
32};
33var __importDefault = (this && this.__importDefault) || function (mod) {
34 return (mod && mod.__esModule) ? mod : { "default": mod };
35};
36Object.defineProperty(exports, "__esModule", { value: true });
37var React = __importStar(require("react"));
38var isEqual = require('deep-equal');
39var diff_1 = __importDefault(require("./util/diff"));
40var eventToHandler = {
41 touchstart: 'onTouchStart',
42 touchend: 'onTouchEnd',
43 touchcancel: 'onTouchCancel',
44 mousemove: 'onMouseMove',
45 mouseenter: 'onMouseEnter',
46 mouseleave: 'onMouseLeave',
47 mousedown: 'onMouseDown',
48 mouseup: 'onMouseUp',
49 click: 'onClick'
50};
51var Layer = (function (_super) {
52 __extends(Layer, _super);
53 function Layer() {
54 var _this = _super !== null && _super.apply(this, arguments) || this;
55 _this.source = __assign({ type: 'geojson' }, _this.props.geoJSONSourceOptions, { data: {
56 type: 'FeatureCollection',
57 features: []
58 } });
59 _this.geometry = function (coordinates) {
60 switch (_this.props.type) {
61 case 'symbol':
62 case 'circle':
63 return {
64 type: 'Point',
65 coordinates: coordinates
66 };
67 case 'fill':
68 if (Array.isArray(coordinates[0][0][0])) {
69 return {
70 type: 'MultiPolygon',
71 coordinates: coordinates
72 };
73 }
74 return {
75 type: 'Polygon',
76 coordinates: coordinates
77 };
78 case 'line':
79 return {
80 type: 'LineString',
81 coordinates: coordinates
82 };
83 default:
84 return {
85 type: 'Point',
86 coordinates: coordinates
87 };
88 }
89 };
90 _this.makeFeature = function (props, id) { return ({
91 type: 'Feature',
92 geometry: _this.geometry(props.coordinates),
93 properties: __assign({}, props.properties, { id: id })
94 }); };
95 _this.initialize = function () {
96 var _a = _this.props, type = _a.type, layout = _a.layout, paint = _a.paint, sourceId = _a.sourceId, before = _a.before, images = _a.images, id = _a.id, metadata = _a.metadata, sourceLayer = _a.sourceLayer, minZoom = _a.minZoom, maxZoom = _a.maxZoom, filter = _a.filter;
97 var map = _this.props.map;
98 var layer = {
99 id: id,
100 source: sourceId || id,
101 type: type,
102 layout: layout,
103 paint: paint,
104 metadata: metadata
105 };
106 if (sourceLayer) {
107 layer['source-layer'] = sourceLayer;
108 }
109 if (minZoom) {
110 layer.minzoom = minZoom;
111 }
112 if (maxZoom) {
113 layer.maxzoom = maxZoom;
114 }
115 if (filter) {
116 layer.filter = filter;
117 }
118 if (images) {
119 var normalizedImages = !Array.isArray(images[0]) ? [images] : images;
120 normalizedImages
121 .filter(function (image) { return !map.hasImage(image[0]); })
122 .forEach(function (image) {
123 map.addImage(image[0], image[1], image[2]);
124 });
125 }
126 if (!sourceId && !map.getSource(id)) {
127 map.addSource(id, _this.source);
128 }
129 if (!map.getLayer(id)) {
130 map.addLayer(layer, before);
131 }
132 Object.entries(eventToHandler).forEach(function (_a) {
133 var event = _a[0], propName = _a[1];
134 var handler = _this.props[propName];
135 if (handler) {
136 map.on(event, id, handler);
137 }
138 });
139 };
140 _this.onStyleDataChange = function () {
141 if (!_this.props.map.getLayer(_this.props.id)) {
142 _this.initialize();
143 _this.forceUpdate();
144 }
145 };
146 _this.getChildren = function () {
147 var children = _this.props.children;
148 if (!children) {
149 return [];
150 }
151 if (Array.isArray(children)) {
152 return children.reduce(function (arr, next) { return arr.concat(next); }, []);
153 }
154 return [children];
155 };
156 return _this;
157 }
158 Layer.prototype.componentDidMount = function () {
159 var map = this.props.map;
160 this.initialize();
161 map.on('styledata', this.onStyleDataChange);
162 };
163 Layer.prototype.componentWillUnmount = function () {
164 var _this = this;
165 var map = this.props.map;
166 var _a = this.props, images = _a.images, id = _a.id;
167 if (!map || !map.getStyle()) {
168 return;
169 }
170 map.off('styledata', this.onStyleDataChange);
171 Object.entries(eventToHandler).forEach(function (_a) {
172 var event = _a[0], propName = _a[1];
173 var handler = _this.props[propName];
174 if (handler) {
175 map.off(event, id, handler);
176 }
177 });
178 if (map.getLayer(id)) {
179 map.removeLayer(id);
180 }
181 if (!this.props.sourceId) {
182 map.removeSource(id);
183 }
184 if (images) {
185 var normalizedImages = !Array.isArray(images[0]) ? [images] : images;
186 normalizedImages
187 .map(function (_a) {
188 var key = _a[0], rest = _a.slice(1);
189 return key;
190 })
191 .forEach(map.removeImage.bind(map));
192 }
193 };
194 Layer.prototype.componentDidUpdate = function (prevProps) {
195 var _this = this;
196 var paint = prevProps.paint, layout = prevProps.layout, before = prevProps.before, filter = prevProps.filter, id = prevProps.id, minZoom = prevProps.minZoom, maxZoom = prevProps.maxZoom, map = prevProps.map;
197 if (!isEqual(this.props.paint, paint)) {
198 var paintDiff_1 = diff_1.default(paint, this.props.paint);
199 Object.keys(paintDiff_1).forEach(function (key) {
200 map.setPaintProperty(id, key, paintDiff_1[key]);
201 });
202 }
203 if (!isEqual(this.props.layout, layout)) {
204 var layoutDiff_1 = diff_1.default(layout, this.props.layout);
205 Object.keys(layoutDiff_1).forEach(function (key) {
206 map.setLayoutProperty(id, key, layoutDiff_1[key]);
207 });
208 }
209 if (!isEqual(this.props.filter, filter)) {
210 map.setFilter(id, this.props.filter);
211 }
212 if (before !== this.props.before) {
213 map.moveLayer(id, this.props.before);
214 }
215 if (minZoom !== this.props.minZoom || maxZoom !== this.props.maxZoom) {
216 map.setLayerZoomRange(id, this.props.minZoom, this.props.maxZoom);
217 }
218 Object.entries(eventToHandler).forEach(function (_a) {
219 var event = _a[0], propName = _a[1];
220 var oldHandler = prevProps[propName];
221 var newHandler = _this.props[propName];
222 if (oldHandler !== newHandler) {
223 if (oldHandler) {
224 map.off(event, id, oldHandler);
225 }
226 if (newHandler) {
227 map.on(event, id, newHandler);
228 }
229 }
230 });
231 };
232 Layer.prototype.render = function () {
233 var _this = this;
234 var map = this.props.map;
235 var _a = this.props, sourceId = _a.sourceId, draggedChildren = _a.draggedChildren;
236 var children = this.getChildren();
237 if (draggedChildren) {
238 var draggableChildrenIds_1 = draggedChildren.map(function (child) { return child.key; });
239 children = children.map(function (child) {
240 var indexChildren = draggableChildrenIds_1.indexOf(child.key);
241 if (indexChildren !== -1) {
242 return draggedChildren[indexChildren];
243 }
244 return child;
245 });
246 }
247 var features = children
248 .map(function (_a, id) {
249 var props = _a.props;
250 return _this.makeFeature(props, id);
251 })
252 .filter(Boolean);
253 var source = map.getSource(sourceId || this.props.id);
254 if (source && !sourceId && source.setData) {
255 source.setData({
256 type: 'FeatureCollection',
257 features: features
258 });
259 }
260 return null;
261 };
262 Layer.defaultProps = {
263 type: 'symbol',
264 layout: {},
265 paint: {}
266 };
267 return Layer;
268}(React.Component));
269exports.default = Layer;
270//# sourceMappingURL=layer.js.map
\No newline at end of file