react-simple-maps
Version:
An svg map component built with and for React
292 lines (251 loc) • 11.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
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 ZoomableGroup = function (_Component) {
_inherits(ZoomableGroup, _Component);
function ZoomableGroup(props) {
_classCallCheck(this, ZoomableGroup);
var _this = _possibleConstructorReturn(this, (ZoomableGroup.__proto__ || Object.getPrototypeOf(ZoomableGroup)).call(this, props));
var backdrop = (0, _utils.computeBackdrop)(props.projection, props.backdrop);
_this.state = {
mouseX: (0, _utils.calculateMousePosition)("x", props.projection, props, props.zoom, 1),
mouseY: (0, _utils.calculateMousePosition)("y", props.projection, props, props.zoom, 1),
mouseXStart: 0,
mouseYStart: 0,
isPressed: false,
resizeFactorX: 1,
resizeFactorY: 1,
backdrop: {
width: Math.round(backdrop.width),
height: Math.round(backdrop.height),
x: Math.round(backdrop.x),
y: Math.round(backdrop.y)
}
};
_this.handleMouseMove = _this.handleMouseMove.bind(_this);
_this.handleMouseUp = _this.handleMouseUp.bind(_this);
_this.handleMouseDown = _this.handleMouseDown.bind(_this);
_this.handleTouchStart = _this.handleTouchStart.bind(_this);
_this.handleTouchMove = _this.handleTouchMove.bind(_this);
_this.handleResize = _this.handleResize.bind(_this);
return _this;
}
_createClass(ZoomableGroup, [{
key: "handleMouseMove",
value: function handleMouseMove(_ref) {
var pageX = _ref.pageX,
pageY = _ref.pageY;
if (this.props.disablePanning) return;
if (!this.state.isPressed) return;
this.setState({
mouseX: pageX - this.state.mouseXStart,
mouseY: pageY - this.state.mouseYStart
});
}
}, {
key: "handleTouchMove",
value: function handleTouchMove(_ref2) {
var touches = _ref2.touches;
this.handleMouseMove(touches[0]);
}
}, {
key: "handleMouseUp",
value: function handleMouseUp() {
if (this.props.disablePanning) return;
if (!this.state.isPressed) return;
this.setState({
isPressed: false
});
if (!this.props.onMoveEnd) return;
var _state = this.state,
mouseX = _state.mouseX,
mouseY = _state.mouseY,
resizeFactorX = _state.resizeFactorX,
resizeFactorY = _state.resizeFactorY;
var _props = this.props,
zoom = _props.zoom,
width = _props.width,
height = _props.height,
projection = _props.projection,
onMoveEnd = _props.onMoveEnd;
var x = width / 2 - mouseX * resizeFactorX / zoom;
var y = height / 2 - mouseY * resizeFactorY / zoom;
var newCenter = projection.invert([x, y]);
onMoveEnd(newCenter);
}
}, {
key: "handleMouseDown",
value: function handleMouseDown(_ref3) {
var pageX = _ref3.pageX,
pageY = _ref3.pageY;
if (this.props.disablePanning) return;
var _state2 = this.state,
mouseX = _state2.mouseX,
mouseY = _state2.mouseY,
resizeFactorX = _state2.resizeFactorX,
resizeFactorY = _state2.resizeFactorY;
var _props2 = this.props,
zoom = _props2.zoom,
width = _props2.width,
height = _props2.height,
projection = _props2.projection,
onMoveStart = _props2.onMoveStart;
this.setState({
isPressed: true,
mouseXStart: pageX - mouseX,
mouseYStart: pageY - mouseY
});
if (!onMoveStart) return;
var x = width / 2 - mouseX * resizeFactorX / zoom;
var y = height / 2 - mouseY * resizeFactorY / zoom;
var currentCenter = projection.invert([x, y]);
onMoveStart(currentCenter);
}
}, {
key: "handleTouchStart",
value: function handleTouchStart(_ref4) {
var touches = _ref4.touches;
if (touches.length > 1) {
this.handleMouseDown(touches[0]);
} else {
this.handleMouseUp();
}
}
}, {
key: "preventTouchScroll",
value: function preventTouchScroll(evt) {
if (evt.touches.length > 1) {
evt.preventDefault();
}
}
}, {
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(nextProps) {
var _state3 = this.state,
mouseX = _state3.mouseX,
mouseY = _state3.mouseY,
resizeFactorX = _state3.resizeFactorX,
resizeFactorY = _state3.resizeFactorY;
var _props3 = this.props,
projection = _props3.projection,
center = _props3.center,
zoom = _props3.zoom;
var zoomFactor = nextProps.zoom / zoom;
var centerChanged = JSON.stringify(nextProps.center) !== JSON.stringify(center);
this.setState({
zoom: nextProps.zoom,
mouseX: centerChanged ? (0, _utils.calculateMousePosition)("x", nextProps.projection, nextProps, nextProps.zoom, resizeFactorX) : mouseX * zoomFactor,
mouseY: centerChanged ? (0, _utils.calculateMousePosition)("y", nextProps.projection, nextProps, nextProps.zoom, resizeFactorY) : mouseY * zoomFactor
});
}
}, {
key: "handleResize",
value: function handleResize() {
var _props4 = this.props,
width = _props4.width,
height = _props4.height,
projection = _props4.projection,
zoom = _props4.zoom;
var resizeFactorX = (0, _utils.calculateResizeFactor)(this.zoomableGroupNode.parentNode.getBoundingClientRect().width, width);
var resizeFactorY = (0, _utils.calculateResizeFactor)(this.zoomableGroupNode.parentNode.getBoundingClientRect().height, height);
var xPercentageChange = 1 / resizeFactorX * this.state.resizeFactorX;
var yPercentageChange = 1 / resizeFactorY * this.state.resizeFactorY;
this.setState({
resizeFactorX: resizeFactorX,
resizeFactorY: resizeFactorY,
mouseX: this.state.mouseX * xPercentageChange,
mouseY: this.state.mouseY * yPercentageChange
});
}
}, {
key: "componentDidMount",
value: function componentDidMount() {
var _props5 = this.props,
width = _props5.width,
height = _props5.height,
projection = _props5.projection,
zoom = _props5.zoom;
var resizeFactorX = (0, _utils.calculateResizeFactor)(this.zoomableGroupNode.parentNode.getBoundingClientRect().width, width);
var resizeFactorY = (0, _utils.calculateResizeFactor)(this.zoomableGroupNode.parentNode.getBoundingClientRect().height, height);
this.setState({
resizeFactorX: resizeFactorX,
resizeFactorY: resizeFactorY,
mouseX: (0, _utils.calculateMousePosition)("x", projection, this.props, zoom, resizeFactorX),
mouseY: (0, _utils.calculateMousePosition)("y", projection, this.props, zoom, resizeFactorY)
});
window.addEventListener("resize", this.handleResize);
window.addEventListener("mouseup", this.handleMouseUp);
this.zoomableGroupNode.addEventListener("touchmove", this.preventTouchScroll);
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
window.removeEventListener("resize", this.handleResize);
window.removeEventListener("mouseup", this.handleMouseUp);
this.zoomableGroupNode.removeEventListener("touchmove", this.preventTouchScroll);
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var _props6 = this.props,
width = _props6.width,
height = _props6.height,
zoom = _props6.zoom,
style = _props6.style,
projection = _props6.projection,
children = _props6.children;
var _state4 = this.state,
mouseX = _state4.mouseX,
mouseY = _state4.mouseY,
resizeFactorX = _state4.resizeFactorX,
resizeFactorY = _state4.resizeFactorY;
return _react2.default.createElement(
"g",
{ className: "rsm-zoomable-group",
ref: function ref(zoomableGroupNode) {
return _this2.zoomableGroupNode = zoomableGroupNode;
},
transform: "\n translate(\n " + Math.round((width / 2 + resizeFactorX * mouseX) * 100) / 100 + "\n " + Math.round((height / 2 + resizeFactorY * mouseY) * 100) / 100 + "\n )\n scale(" + zoom + ")\n translate(" + -width / 2 + " " + -height / 2 + ")\n ",
onMouseMove: this.handleMouseMove,
onMouseUp: this.handleMouseUp,
onMouseDown: this.handleMouseDown,
onTouchStart: this.handleTouchStart,
onTouchMove: this.handleTouchMove,
onTouchEnd: this.handleMouseUp,
style: style
},
_react2.default.createElement("rect", {
x: this.state.backdrop.x,
y: this.state.backdrop.y,
width: this.state.backdrop.width,
height: this.state.backdrop.height,
fill: "transparent",
style: { strokeWidth: 0 }
}),
(0, _utils.createNewChildren)(children, this.props)
);
}
}]);
return ZoomableGroup;
}(_react.Component);
ZoomableGroup.defaultProps = {
center: [0, 0],
backdrop: {
x: [-179.9, 179.9],
y: [89.9, -89.9]
},
zoom: 1,
disablePanning: false
};
exports.default = ZoomableGroup;