UNPKG

@mapbox/react-map-gl

Version:

A React wrapper for MapboxGL-js and overlay API.

166 lines (140 loc) 6.2 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf3 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _react = require("react"); var _propTypes = _interopRequireDefault(require("prop-types")); var _mapContext = _interopRequireDefault(require("./map-context")); // 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. var propTypes = { /** Event handling */ captureScroll: _propTypes["default"].bool, // Stop map pan & rotate captureDrag: _propTypes["default"].bool, // Stop map click captureClick: _propTypes["default"].bool, // Stop map double click captureDoubleClick: _propTypes["default"].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) { (0, _inherits2["default"])(BaseControl, _PureComponent); function BaseControl() { var _getPrototypeOf2; var _this; (0, _classCallCheck2["default"])(this, BaseControl); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = (0, _possibleConstructorReturn2["default"])(this, (_getPrototypeOf2 = (0, _getPrototypeOf3["default"])(BaseControl)).call.apply(_getPrototypeOf2, [this].concat(args))); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "_context", {}); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "_events", null); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "_containerRef", (0, _react.createRef)()); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "_onScroll", function (evt) { if (_this.props.captureScroll) { evt.stopPropagation(); } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "_onDragStart", function (evt) { if (_this.props.captureDrag) { evt.stopPropagation(); } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "_onDblClick", function (evt) { if (_this.props.captureDoubleClick) { evt.stopPropagation(); } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "_onClick", function (evt) { if (_this.props.captureClick) { evt.stopPropagation(); } }); return _this; } (0, _createClass2["default"])(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 (0, _react.createElement)(_mapContext["default"].Consumer, null, function (context) { _this2._context = context; return _this2._render(); }); } }]); return BaseControl; }(_react.PureComponent); exports["default"] = BaseControl; (0, _defineProperty2["default"])(BaseControl, "propTypes", propTypes); (0, _defineProperty2["default"])(BaseControl, "defaultProps", defaultProps); //# sourceMappingURL=base-control.js.map