UNPKG

6.22 kBJavaScriptView Raw
1var __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})();
14var __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};
25import * as React from 'react';
26import { withMap } from './context';
27var Source = (function (_super) {
28 __extends(Source, _super);
29 function Source() {
30 var _this = _super !== null && _super.apply(this, arguments) || this;
31 _this.id = _this.props.id;
32 _this.onStyleDataChange = function () {
33 if (!_this.props.map.getLayer(_this.id)) {
34 _this.initialize();
35 _this.forceUpdate();
36 }
37 };
38 _this.initialize = function () {
39 var map = _this.props.map;
40 var _a = _this.props, geoJsonSource = _a.geoJsonSource, tileJsonSource = _a.tileJsonSource, onSourceAdded = _a.onSourceAdded;
41 if (!map.getSource(_this.id) && (geoJsonSource || tileJsonSource)) {
42 if (geoJsonSource) {
43 map.addSource(_this.id, geoJsonSource);
44 }
45 else if (tileJsonSource) {
46 map.addSource(_this.id, tileJsonSource);
47 }
48 map.on('sourcedata', _this.onData);
49 if (onSourceAdded) {
50 onSourceAdded(map.getSource(_this.id));
51 }
52 }
53 };
54 _this.onData = function () {
55 var map = _this.props.map;
56 var source = map.getSource(_this.props.id);
57 if (!source || !map.isSourceLoaded(_this.props.id)) {
58 return;
59 }
60 var onSourceLoaded = _this.props.onSourceLoaded;
61 if (source && onSourceLoaded) {
62 onSourceLoaded(source);
63 }
64 if (source && _this.props.geoJsonSource && _this.props.geoJsonSource.data) {
65 source.setData(_this.props.geoJsonSource.data);
66 }
67 map.off('sourcedata', _this.onData);
68 };
69 return _this;
70 }
71 Source.prototype.componentDidMount = function () {
72 var map = this.props.map;
73 map.on('styledata', this.onStyleDataChange);
74 this.initialize();
75 };
76 Source.prototype.removeSource = function () {
77 var _this = this;
78 var map = this.props.map;
79 if (map.getSource(this.id)) {
80 var _a = map.getStyle().layers, layers_1 = _a === void 0 ? [] : _a;
81 layers_1 = layers_1
82 .map(function (layer, idx) {
83 var before = (layers_1[idx + 1] || { id: undefined }).id;
84 return __assign({}, layer, { before: before });
85 })
86 .filter(function (layer) { return layer.source === _this.id; });
87 layers_1.forEach(function (layer) { return map.removeLayer(layer.id); });
88 map.removeSource(this.id);
89 return layers_1.reverse();
90 }
91 return [];
92 };
93 Source.prototype.componentWillUnmount = function () {
94 var map = this.props.map;
95 if (!map || !map.getStyle()) {
96 return;
97 }
98 map.off('styledata', this.onStyleDataChange);
99 this.removeSource();
100 };
101 Source.prototype.componentDidUpdate = function (prevProps) {
102 var geoJsonSource = prevProps.geoJsonSource, tileJsonSource = prevProps.tileJsonSource, map = prevProps.map;
103 var source = map.getSource(this.id);
104 if (tileJsonSource && this.props.tileJsonSource) {
105 var urlUpdated = false;
106 var tilesUpdated = false;
107 if (source && source.type === 'vector') {
108 var hasNewSourceUrl = tileJsonSource.url !== this.props.tileJsonSource.url;
109 if (hasNewSourceUrl && this.props.tileJsonSource.url !== undefined) {
110 source.setUrl(this.props.tileJsonSource.url);
111 urlUpdated = true;
112 }
113 var hasNewSourceTiles = tileJsonSource.tiles !== this.props.tileJsonSource.tiles;
114 if (hasNewSourceTiles &&
115 this.props.tileJsonSource.tiles !== undefined) {
116 source.setTiles(this.props.tileJsonSource.tiles);
117 tilesUpdated = true;
118 }
119 }
120 var hasNewTilesSource = (!urlUpdated && tileJsonSource.url !== this.props.tileJsonSource.url) ||
121 (!tilesUpdated &&
122 tileJsonSource.tiles !== this.props.tileJsonSource.tiles) ||
123 tileJsonSource.minzoom !== this.props.tileJsonSource.minzoom ||
124 tileJsonSource.maxzoom !== this.props.tileJsonSource.maxzoom;
125 if (hasNewTilesSource) {
126 var layers = this.removeSource();
127 map.addSource(this.id, this.props.tileJsonSource);
128 layers.forEach(function (layer) { return map.addLayer(layer, layer.before); });
129 }
130 }
131 if (geoJsonSource &&
132 this.props.geoJsonSource &&
133 this.props.geoJsonSource.data !== geoJsonSource.data &&
134 this.props.geoJsonSource.data &&
135 source &&
136 source.type === 'geojson') {
137 source.setData(this.props.geoJsonSource.data);
138 }
139 };
140 Source.prototype.render = function () {
141 return null;
142 };
143 return Source;
144}(React.Component));
145export { Source };
146export default withMap(Source);
147//# sourceMappingURL=source.js.map
\No newline at end of file