UNPKG

react-simple-maps

Version:
236 lines (197 loc) 8.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = require("react"); var _react2 = _interopRequireDefault(_react); var _d3Geo = require("d3-geo"); var _utils = require("./utils"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var pathCache = {}; var renderPath = function renderPath(cacheId, geography, projection, round, precision) { if (pathCache[cacheId]) return pathCache[cacheId]; var pathString = cacheId ? pathCache[cacheId] ? pathCache[cacheId] : round ? (0, _utils.roundPath)((0, _d3Geo.geoPath)().projection(projection)(geography), precision) : (0, _d3Geo.geoPath)().projection(projection)(geography) : round ? (0, _utils.roundPath)((0, _d3Geo.geoPath)().projection(projection)(geography), precision) : (0, _d3Geo.geoPath)().projection(projection)(geography); if (cacheId) pathCache[cacheId] = pathString; return pathString; }; var Geography = function (_Component) { _inherits(Geography, _Component); function Geography() { _classCallCheck(this, Geography); var _this = _possibleConstructorReturn(this, (Geography.__proto__ || Object.getPrototypeOf(Geography)).call(this)); _this.state = { hover: false, pressed: false }; _this.handleMouseEnter = _this.handleMouseEnter.bind(_this); _this.handleMouseMove = _this.handleMouseMove.bind(_this); _this.handleMouseLeave = _this.handleMouseLeave.bind(_this); _this.handleMouseDown = _this.handleMouseDown.bind(_this); _this.handleMouseUp = _this.handleMouseUp.bind(_this); _this.handleMouseClick = _this.handleMouseClick.bind(_this); _this.handleFocus = _this.handleFocus.bind(_this); _this.handleBlur = _this.handleBlur.bind(_this); return _this; } _createClass(Geography, [{ key: "handleMouseClick", value: function handleMouseClick(evt) { evt.persist(); var _props = this.props, onClick = _props.onClick, geography = _props.geography; return onClick && onClick(geography, evt); } }, { key: "handleMouseEnter", value: function handleMouseEnter(evt) { evt.persist(); var _props2 = this.props, onMouseEnter = _props2.onMouseEnter, geography = _props2.geography; this.setState({ hover: true }, function () { return onMouseEnter && onMouseEnter(geography, evt); }); } }, { key: "handleMouseMove", value: function handleMouseMove(evt) { evt.persist(); if (this.state.pressed) return; var _props3 = this.props, onMouseMove = _props3.onMouseMove, geography = _props3.geography; if (!this.state.hover) { this.setState({ hover: true }, function () { return onMouseMove && onMouseMove(geography, evt); }); } else if (onMouseMove) onMouseMove(geography, evt);else return; } }, { key: "handleMouseLeave", value: function handleMouseLeave(evt) { evt.persist(); var _props4 = this.props, onMouseLeave = _props4.onMouseLeave, geography = _props4.geography; this.setState({ hover: false, pressed: false }, function () { return onMouseLeave && onMouseLeave(geography, evt); }); } }, { key: "handleMouseDown", value: function handleMouseDown(evt) { evt.persist(); var _props5 = this.props, onMouseDown = _props5.onMouseDown, geography = _props5.geography; this.setState({ pressed: true }, function () { return onMouseDown && onMouseDown(geography, evt); }); } }, { key: "handleMouseUp", value: function handleMouseUp(evt) { evt.persist(); var _props6 = this.props, onMouseUp = _props6.onMouseUp, geography = _props6.geography; this.setState({ pressed: false }, function () { return onMouseUp && onMouseUp(geography, evt); }); } }, { key: "handleFocus", value: function handleFocus(evt) { evt.persist(); var _props7 = this.props, onFocus = _props7.onFocus, geography = _props7.geography; this.setState({ hover: true }, function () { return onFocus && onFocus(geography, evt); }); } }, { key: "handleBlur", value: function handleBlur(evt) { evt.persist(); var _props8 = this.props, onBlur = _props8.onBlur, geography = _props8.geography; this.setState({ hover: false }, function () { return onBlur && onBlur(geography, evt); }); } }, { key: "render", value: function render() { var _this2 = this; var _props9 = this.props, geography = _props9.geography, projection = _props9.projection, round = _props9.round, cacheId = _props9.cacheId, precision = _props9.precision, tabable = _props9.tabable, style = _props9.style; var _state = this.state, hover = _state.hover, pressed = _state.pressed; var pathString = renderPath(cacheId, geography, projection, round, precision); var excludeProps = ["geography", "projection", "round", "cacheId", "precision", "tabable", "style", "onClick", "onMouseEnter", "onMouseMove", "onMouseLeave", "onMouseDown", "onMouseUp", "onFocus", "onBlur"]; var restProps = Object.keys(this.props).filter(function (key) { return excludeProps.indexOf(key) === -1; }).reduce(function (obj, key) { obj[key] = _this2.props[key]; return obj; }, {}); return _react2.default.createElement("path", _extends({ d: pathString, className: "rsm-geography" + (pressed ? " rsm-geography--pressed" : "") + (hover ? " rsm-geography--hover" : ""), style: style[pressed || hover ? pressed ? "pressed" : "hover" : "default"], onClick: this.handleMouseClick, onMouseEnter: this.handleMouseEnter, onMouseMove: this.handleMouseMove, onMouseLeave: this.handleMouseLeave, onMouseDown: this.handleMouseDown, onMouseUp: this.handleMouseUp, onFocus: this.handleFocus, onBlur: this.handleBlur, tabIndex: tabable ? 0 : -1 }, restProps)); } }]); return Geography; }(_react.Component); Geography.defaultProps = { precision: 0.1, cacheId: null, round: false, tabable: true, style: { default: {}, hover: {}, pressed: {} } }; exports.default = Geography;