1 | var __extends = (this && this.__extends) || (function () {
|
2 | var extendStatics = function (d, b) {
|
3 | extendStatics = Object.setPrototypeOf ||
|
4 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
5 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
6 | return extendStatics(d, b);
|
7 | }
|
8 | return function (d, b) {
|
9 | extendStatics(d, b);
|
10 | function __() { this.constructor = d; }
|
11 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
12 | };
|
13 | })();
|
14 | var __assign = (this && this.__assign) || function () {
|
15 | __assign = Object.assign || function(t) {
|
16 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
17 | s = arguments[i];
|
18 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
19 | t[p] = s[p];
|
20 | }
|
21 | return t;
|
22 | };
|
23 | return __assign.apply(this, arguments);
|
24 | };
|
25 | import * as React from 'react';
|
26 | var isEqual = require('deep-equal');
|
27 | import diff from './util/diff';
|
28 | import { generateID } from './util/uid';
|
29 | import { withMap } from './context';
|
30 | var types = ['symbol', 'line', 'fill', 'fill-extrusion', 'circle'];
|
31 | var toCamelCase = function (str) {
|
32 | return str
|
33 | .replace(/(?:^\w|[A-Z]|\b\w)/g, function (letter, index) {
|
34 | return index === 0 ? letter.toLowerCase() : letter.toUpperCase();
|
35 | })
|
36 | .replace(/[\s+]|-/g, '');
|
37 | };
|
38 | var eventToHandler = {
|
39 | mousemove: 'OnMouseMove',
|
40 | mouseenter: 'OnMouseEnter',
|
41 | mouseleave: 'OnMouseLeave',
|
42 | mousedown: 'OnMouseDown',
|
43 | mouseup: 'OnMouseUp',
|
44 | click: 'OnClick'
|
45 | };
|
46 | var GeoJSONLayer = (function (_super) {
|
47 | __extends(GeoJSONLayer, _super);
|
48 | function GeoJSONLayer() {
|
49 | var _this = _super !== null && _super.apply(this, arguments) || this;
|
50 | _this.id = _this.props.id || "geojson-" + generateID();
|
51 | _this.source = __assign({ type: 'geojson' }, _this.props.sourceOptions, { data: _this.props.data });
|
52 | _this.layerIds = [];
|
53 | _this.buildLayerId = function (type) {
|
54 | return _this.id + "-" + type;
|
55 | };
|
56 | _this.createLayer = function (type) {
|
57 | var _a = _this.props, before = _a.before, layerOptions = _a.layerOptions, map = _a.map;
|
58 | var layerId = _this.buildLayerId(type);
|
59 | _this.layerIds.push(layerId);
|
60 | var paint = _this.props[toCamelCase(type) + "Paint"] || {};
|
61 | var visibility = Object.keys(paint).length ? 'visible' : 'none';
|
62 | var layout = _this.props[toCamelCase(type) + "Layout"] || {
|
63 | visibility: visibility
|
64 | };
|
65 | var layer = __assign({ id: layerId, source: _this.id, type: type, paint: paint, layout: layout }, layerOptions);
|
66 | map.addLayer(layer, before);
|
67 | _this.mapLayerMouseHandlers(type);
|
68 | };
|
69 | _this.mapLayerMouseHandlers = function (type) {
|
70 | var map = _this.props.map;
|
71 | var layerId = _this.buildLayerId(type);
|
72 | var events = Object.keys(eventToHandler);
|
73 | events.forEach(function (event) {
|
74 | var handler = _this.props["" + toCamelCase(type) + eventToHandler[event]] || null;
|
75 | if (handler) {
|
76 | map.on(event, layerId, handler);
|
77 | }
|
78 | });
|
79 | };
|
80 | _this.onStyleDataChange = function () {
|
81 | if (!_this.props.map.getSource(_this.id)) {
|
82 | _this.unbind();
|
83 | _this.initialize();
|
84 | _this.forceUpdate();
|
85 | }
|
86 | };
|
87 | _this.isGeoJSONSource = function (source) {
|
88 | return !!source &&
|
89 | typeof source.setData === 'function';
|
90 | };
|
91 | return _this;
|
92 | }
|
93 | GeoJSONLayer.prototype.initialize = function () {
|
94 | var map = this.props.map;
|
95 | map.addSource(this.id, this.source);
|
96 | this.createLayer('symbol');
|
97 | this.createLayer('line');
|
98 | this.createLayer('fill');
|
99 | this.createLayer('fill-extrusion');
|
100 | this.createLayer('circle');
|
101 | };
|
102 | GeoJSONLayer.prototype.unbind = function () {
|
103 | var _this = this;
|
104 | var map = this.props.map;
|
105 | if (map.getSource(this.id)) {
|
106 | var layers = map.getStyle().layers;
|
107 | if (layers) {
|
108 | layers
|
109 | .filter(function (layer) { return layer.source === _this.id; })
|
110 | .forEach(function (layer) { return map.removeLayer(layer.id); });
|
111 | }
|
112 | map.removeSource(this.id);
|
113 | }
|
114 | types.forEach(function (type) {
|
115 | var events = Object.keys(eventToHandler);
|
116 | events.forEach(function (event) {
|
117 | var prop = toCamelCase(type) + eventToHandler[event];
|
118 | if (_this.props[prop]) {
|
119 | map.off(event, _this.buildLayerId(type), _this.props[prop]);
|
120 | }
|
121 | });
|
122 | });
|
123 | this.layerIds.forEach(function (lId) {
|
124 | if (map.getLayer(lId)) {
|
125 | map.removeLayer(lId);
|
126 | }
|
127 | });
|
128 | };
|
129 | GeoJSONLayer.prototype.componentDidMount = function () {
|
130 | var map = this.props.map;
|
131 | this.initialize();
|
132 | map.on('styledata', this.onStyleDataChange);
|
133 | };
|
134 | GeoJSONLayer.prototype.componentWillUnmount = function () {
|
135 | var map = this.props.map;
|
136 | if (!map || !map.getStyle()) {
|
137 | return;
|
138 | }
|
139 | map.off('styledata', this.onStyleDataChange);
|
140 | this.unbind();
|
141 | };
|
142 | GeoJSONLayer.prototype.componentDidUpdate = function (prevProps) {
|
143 | var _this = this;
|
144 | var data = prevProps.data, before = prevProps.before, layerOptions = prevProps.layerOptions, map = prevProps.map;
|
145 | var source = map.getSource(this.id);
|
146 | if (!this.isGeoJSONSource(source)) {
|
147 | return;
|
148 | }
|
149 | if (this.props.data !== data) {
|
150 | source.setData(this.props.data);
|
151 | this.source = __assign({ type: 'geojson' }, this.props.sourceOptions, { data: this.props.data });
|
152 | }
|
153 | var layerFilterChanged = this.props.layerOptions &&
|
154 | layerOptions &&
|
155 | !isEqual(this.props.layerOptions.filter, layerOptions.filter);
|
156 | types.forEach(function (type) {
|
157 | var layerId = _this.buildLayerId(type);
|
158 | if (_this.props.layerOptions && layerFilterChanged) {
|
159 | map.setFilter(layerId, _this.props.layerOptions.filter || []);
|
160 | }
|
161 | var paintProp = toCamelCase(type) + 'Paint';
|
162 | if (!isEqual(prevProps[paintProp], _this.props[paintProp])) {
|
163 | var paintDiff_1 = diff(prevProps[paintProp], _this.props[paintProp]);
|
164 | Object.keys(paintDiff_1).forEach(function (key) {
|
165 | map.setPaintProperty(layerId, key, paintDiff_1[key]);
|
166 | });
|
167 | }
|
168 | var layoutProp = toCamelCase(type) + 'Layout';
|
169 | if (!isEqual(prevProps[layoutProp], _this.props[layoutProp])) {
|
170 | var layoutDiff_1 = diff(prevProps[layoutProp], _this.props[layoutProp]);
|
171 | Object.keys(layoutDiff_1).forEach(function (key) {
|
172 | map.setLayoutProperty(layerId, key, layoutDiff_1[key]);
|
173 | });
|
174 | }
|
175 | var events = Object.keys(eventToHandler);
|
176 | events.forEach(function (event) {
|
177 | var prop = toCamelCase(type) + eventToHandler[event];
|
178 | if (prevProps[prop] !== _this.props[prop]) {
|
179 | if (prevProps[prop]) {
|
180 | map.off(event, layerId, prevProps[prop]);
|
181 | }
|
182 | if (_this.props[prop]) {
|
183 | map.on(event, layerId, _this.props[prop]);
|
184 | }
|
185 | }
|
186 | });
|
187 | if (before !== _this.props.before) {
|
188 | map.moveLayer(layerId, _this.props.before);
|
189 | }
|
190 | });
|
191 | };
|
192 | GeoJSONLayer.prototype.render = function () {
|
193 | return null;
|
194 | };
|
195 | return GeoJSONLayer;
|
196 | }(React.Component));
|
197 | export { GeoJSONLayer };
|
198 | export default withMap(GeoJSONLayer);
|
199 |
|
\ | No newline at end of file |