UNPKG

@mapbox/react-map-gl

Version:

A React wrapper for MapboxGL-js and overlay API.

158 lines (134 loc) 5.36 kB
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; import _createClass from "@babel/runtime/helpers/esm/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf"; import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/esm/inherits"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; // Copyright (c) 2015 Uber Technologies, Inc. // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. import { PureComponent, createElement, createRef } from 'react'; import PropTypes from 'prop-types'; import MapContext from './map-context'; var propTypes = { /** Event handling */ captureScroll: PropTypes.bool, // Stop map pan & rotate captureDrag: PropTypes.bool, // Stop map click captureClick: PropTypes.bool, // Stop map double click captureDoubleClick: PropTypes.bool }; var defaultProps = { captureScroll: false, captureDrag: true, captureClick: true, captureDoubleClick: true }; /* * PureComponent doesn't update when context changes. * The only way is to implement our own shouldComponentUpdate here. Considering * the parent component (StaticMap or InteractiveMap) is pure, and map re-render * is almost always triggered by a viewport change, we almost definitely need to * recalculate the marker's position when the parent re-renders. */ var BaseControl = /*#__PURE__*/ function (_PureComponent) { _inherits(BaseControl, _PureComponent); function BaseControl() { var _getPrototypeOf2; var _this; _classCallCheck(this, BaseControl); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(BaseControl)).call.apply(_getPrototypeOf2, [this].concat(args))); _defineProperty(_assertThisInitialized(_this), "_context", {}); _defineProperty(_assertThisInitialized(_this), "_events", null); _defineProperty(_assertThisInitialized(_this), "_containerRef", createRef()); _defineProperty(_assertThisInitialized(_this), "_onScroll", function (evt) { if (_this.props.captureScroll) { evt.stopPropagation(); } }); _defineProperty(_assertThisInitialized(_this), "_onDragStart", function (evt) { if (_this.props.captureDrag) { evt.stopPropagation(); } }); _defineProperty(_assertThisInitialized(_this), "_onDblClick", function (evt) { if (_this.props.captureDoubleClick) { evt.stopPropagation(); } }); _defineProperty(_assertThisInitialized(_this), "_onClick", function (evt) { if (_this.props.captureClick) { evt.stopPropagation(); } }); return _this; } _createClass(BaseControl, [{ key: "componentDidMount", value: function componentDidMount() { var ref = this._containerRef.current; if (!ref) { return; } var eventManager = this._context.eventManager; // Return early if no eventManager is found if (eventManager) { this._events = { wheel: this._onScroll, panstart: this._onDragStart, anyclick: this._onClick, click: this._onClick, dblclick: this._onDblClick }; eventManager.on(this._events, ref); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { var eventManager = this._context.eventManager; if (eventManager && this._events) { eventManager.off(this._events); } } }, { key: "_render", value: function _render() { throw new Error('_render() not implemented'); } }, { key: "render", value: function render() { var _this2 = this; return createElement(MapContext.Consumer, null, function (context) { _this2._context = context; return _this2._render(); }); } }]); return BaseControl; }(PureComponent); _defineProperty(BaseControl, "propTypes", propTypes); _defineProperty(BaseControl, "defaultProps", defaultProps); export { BaseControl as default }; //# sourceMappingURL=base-control.js.map