UNPKG

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