UNPKG

12.6 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 MapboxGl = __importStar(require("mapbox-gl"));
35var React = __importStar(require("react"));
36var map_events_1 = require("./map-events");
37var context_1 = require("./context");
38var react_dom_1 = require("react-dom");
39var isEqual = require('deep-equal');
40var defaultZoom = [11];
41var defaultMovingMethod = 'flyTo';
42var defaultCenter = [-0.2416815, 51.5285582];
43var ReactMapboxFactory = function (_a) {
44 var accessToken = _a.accessToken, apiUrl = _a.apiUrl, _b = _a.minZoom, minZoom = _b === void 0 ? 0 : _b, _c = _a.maxZoom, maxZoom = _c === void 0 ? 20 : _c, _d = _a.hash, hash = _d === void 0 ? false : _d, _e = _a.preserveDrawingBuffer, preserveDrawingBuffer = _e === void 0 ? false : _e, _f = _a.scrollZoom, scrollZoom = _f === void 0 ? true : _f, _g = _a.interactive, interactive = _g === void 0 ? true : _g, _h = _a.dragRotate, dragRotate = _h === void 0 ? true : _h, _j = _a.pitchWithRotate, pitchWithRotate = _j === void 0 ? true : _j, _k = _a.attributionControl, attributionControl = _k === void 0 ? true : _k, customAttribution = _a.customAttribution, _l = _a.logoPosition, logoPosition = _l === void 0 ? 'bottom-left' : _l, _m = _a.renderWorldCopies, renderWorldCopies = _m === void 0 ? true : _m, _o = _a.trackResize, trackResize = _o === void 0 ? true : _o, _p = _a.touchZoomRotate, touchZoomRotate = _p === void 0 ? true : _p, _q = _a.doubleClickZoom, doubleClickZoom = _q === void 0 ? true : _q, _r = _a.keyboard, keyboard = _r === void 0 ? true : _r, _s = _a.dragPan, dragPan = _s === void 0 ? true : _s, _t = _a.boxZoom, boxZoom = _t === void 0 ? true : _t, _u = _a.refreshExpiredTiles, refreshExpiredTiles = _u === void 0 ? true : _u, _v = _a.failIfMajorPerformanceCaveat, failIfMajorPerformanceCaveat = _v === void 0 ? false : _v, _w = _a.bearingSnap, bearingSnap = _w === void 0 ? 7 : _w, _x = _a.antialias, antialias = _x === void 0 ? false : _x, mapInstance = _a.mapInstance, transformRequest = _a.transformRequest;
45 var _y;
46 return _y = (function (_super) {
47 __extends(ReactMapboxGl, _super);
48 function ReactMapboxGl() {
49 var _this = _super !== null && _super.apply(this, arguments) || this;
50 _this.state = {
51 map: mapInstance,
52 ready: false
53 };
54 _this.listeners = {};
55 _this._isMounted = true;
56 _this.calcCenter = function (bounds) { return [
57 (bounds[0][0] + bounds[1][0]) / 2,
58 (bounds[0][1] + bounds[1][1]) / 2
59 ]; };
60 _this.setRef = function (x) {
61 _this.container = x;
62 };
63 return _this;
64 }
65 ReactMapboxGl.prototype.componentDidMount = function () {
66 var _this = this;
67 var _a = this.props, style = _a.style, onStyleLoad = _a.onStyleLoad, center = _a.center, pitch = _a.pitch, zoom = _a.zoom, fitBounds = _a.fitBounds, fitBoundsOptions = _a.fitBoundsOptions, bearing = _a.bearing, maxBounds = _a.maxBounds;
68 MapboxGl.accessToken = accessToken;
69 if (apiUrl) {
70 MapboxGl.config.API_URL = apiUrl;
71 }
72 if (!Array.isArray(zoom)) {
73 throw new Error('zoom need to be an array type of length 1 for reliable update');
74 }
75 var opts = {
76 preserveDrawingBuffer: preserveDrawingBuffer,
77 hash: hash,
78 zoom: zoom[0],
79 minZoom: minZoom,
80 maxZoom: maxZoom,
81 maxBounds: maxBounds,
82 container: this.container,
83 center: fitBounds && center === defaultCenter
84 ? this.calcCenter(fitBounds)
85 : center,
86 style: style,
87 scrollZoom: scrollZoom,
88 attributionControl: attributionControl,
89 customAttribution: customAttribution,
90 interactive: interactive,
91 dragRotate: dragRotate,
92 pitchWithRotate: pitchWithRotate,
93 renderWorldCopies: renderWorldCopies,
94 trackResize: trackResize,
95 touchZoomRotate: touchZoomRotate,
96 doubleClickZoom: doubleClickZoom,
97 keyboard: keyboard,
98 dragPan: dragPan,
99 boxZoom: boxZoom,
100 refreshExpiredTiles: refreshExpiredTiles,
101 logoPosition: logoPosition,
102 bearingSnap: bearingSnap,
103 failIfMajorPerformanceCaveat: failIfMajorPerformanceCaveat,
104 antialias: antialias,
105 transformRequest: transformRequest
106 };
107 if (bearing) {
108 if (!Array.isArray(bearing)) {
109 throw new Error('bearing need to be an array type of length 1 for reliable update');
110 }
111 opts.bearing = bearing[0];
112 }
113 if (pitch) {
114 if (!Array.isArray(pitch)) {
115 throw new Error('pitch need to be an array type of length 1 for reliable update');
116 }
117 opts.pitch = pitch[0];
118 }
119 var map = this.state.map;
120 if (!map) {
121 map = new MapboxGl.Map(opts);
122 this.setState({ map: map });
123 }
124 if (fitBounds) {
125 map.fitBounds(fitBounds, fitBoundsOptions, { fitboundUpdate: true });
126 }
127 map.on('load', function (evt) {
128 if (_this._isMounted) {
129 _this.setState({ ready: true });
130 }
131 if (onStyleLoad) {
132 onStyleLoad(map, evt);
133 }
134 });
135 this.listeners = map_events_1.listenEvents(map_events_1.events, this.props, map);
136 };
137 ReactMapboxGl.prototype.componentWillUnmount = function () {
138 var map = this.state.map;
139 this._isMounted = false;
140 if (map) {
141 map.remove();
142 }
143 };
144 ReactMapboxGl.prototype.componentDidUpdate = function (prevProps) {
145 var _this = this;
146 var map = this.state.map;
147 if (!map) {
148 return null;
149 }
150 this.listeners = map_events_1.updateEvents(this.listeners, this.props, map);
151 var center = map.getCenter();
152 var zoom = map.getZoom();
153 var bearing = map.getBearing();
154 var pitch = map.getPitch();
155 var didZoomUpdate = prevProps.zoom !== this.props.zoom &&
156 (this.props.zoom && this.props.zoom[0]) !== zoom;
157 var didCenterUpdate = prevProps.center !== this.props.center &&
158 ((this.props.center && this.props.center[0]) !== center.lng ||
159 (this.props.center && this.props.center[1]) !== center.lat);
160 var didBearingUpdate = prevProps.bearing !== this.props.bearing &&
161 (this.props.bearing && this.props.bearing[0]) !== bearing;
162 var didPitchUpdate = prevProps.pitch !== this.props.pitch &&
163 (this.props.pitch && this.props.pitch[0]) !== pitch;
164 if (this.props.maxBounds) {
165 var didMaxBoundsUpdate = prevProps.maxBounds !== this.props.maxBounds;
166 if (didMaxBoundsUpdate) {
167 map.setMaxBounds(this.props.maxBounds);
168 }
169 }
170 if (this.props.fitBounds) {
171 var fitBounds = prevProps.fitBounds;
172 var didFitBoundsUpdate = fitBounds !== this.props.fitBounds ||
173 this.props.fitBounds.length !== (fitBounds && fitBounds.length) ||
174 !!fitBounds.filter(function (c, i) {
175 var nc = _this.props.fitBounds && _this.props.fitBounds[i];
176 return c[0] !== (nc && nc[0]) || c[1] !== (nc && nc[1]);
177 })[0];
178 if (didFitBoundsUpdate ||
179 !isEqual(prevProps.fitBoundsOptions, this.props.fitBoundsOptions)) {
180 map.fitBounds(this.props.fitBounds, this.props.fitBoundsOptions, {
181 fitboundUpdate: true
182 });
183 }
184 }
185 if (didZoomUpdate ||
186 didCenterUpdate ||
187 didBearingUpdate ||
188 didPitchUpdate) {
189 var mm = this.props.movingMethod || defaultMovingMethod;
190 var _a = this.props, flyToOptions = _a.flyToOptions, animationOptions = _a.animationOptions;
191 map[mm](__assign({}, animationOptions, flyToOptions, { zoom: didZoomUpdate && this.props.zoom ? this.props.zoom[0] : zoom, center: didCenterUpdate ? this.props.center : center, bearing: didBearingUpdate ? this.props.bearing : bearing, pitch: didPitchUpdate ? this.props.pitch : pitch }));
192 }
193 if (!isEqual(prevProps.style, this.props.style)) {
194 map.setStyle(this.props.style);
195 }
196 return null;
197 };
198 ReactMapboxGl.prototype.render = function () {
199 var _a = this.props, containerStyle = _a.containerStyle, className = _a.className, children = _a.children, renderChildrenInPortal = _a.renderChildrenInPortal;
200 var _b = this.state, ready = _b.ready, map = _b.map;
201 if (renderChildrenInPortal) {
202 var container = ready && map && typeof map.getCanvasContainer === 'function'
203 ? map.getCanvasContainer()
204 : undefined;
205 return (React.createElement(context_1.MapContext.Provider, { value: map },
206 React.createElement("div", { ref: this.setRef, className: className, style: __assign({}, containerStyle) }, ready && container && react_dom_1.createPortal(children, container))));
207 }
208 return (React.createElement(context_1.MapContext.Provider, { value: map },
209 React.createElement("div", { ref: this.setRef, className: className, style: __assign({}, containerStyle) }, ready && children)));
210 };
211 return ReactMapboxGl;
212 }(React.Component)),
213 _y.defaultProps = {
214 onStyleLoad: function (map, evt) { return null; },
215 center: defaultCenter,
216 zoom: defaultZoom,
217 bearing: 0,
218 movingMethod: defaultMovingMethod,
219 pitch: 0,
220 containerStyle: {
221 textAlign: 'left'
222 }
223 },
224 _y;
225};
226exports.default = ReactMapboxFactory;
227//# sourceMappingURL=map.js.map
\No newline at end of file