google-map-react
Version:
isomorphic google map react component, allows render react components on the google map
339 lines (269 loc) • 11.5 kB
JavaScript
;
exports.__esModule = 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 _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _omit = require('./utils/omit');
var _omit2 = _interopRequireDefault(_omit);
var _shallowEqual = require('./utils/shallowEqual');
var _shallowEqual2 = _interopRequireDefault(_shallowEqual);
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; }
// utils
var mainStyle = {
width: '100%',
height: '100%',
left: 0,
top: 0,
margin: 0,
padding: 0,
position: 'absolute'
};
var style = {
width: 0,
height: 0,
left: 0,
top: 0,
backgroundColor: 'transparent',
position: 'absolute'
};
var GoogleMapMarkers = function (_Component) {
_inherits(GoogleMapMarkers, _Component);
/* eslint-disable react/forbid-prop-types */
function GoogleMapMarkers(props) {
_classCallCheck(this, GoogleMapMarkers);
var _this = _possibleConstructorReturn(this, _Component.call(this, props));
_this._getState = function () {
return {
children: _this.props.dispatcher.getChildren(),
updateCounter: _this.props.dispatcher.getUpdateCounter()
};
};
_this._onChangeHandler = function () {
if (!_this.dimesionsCache_) {
return;
}
var prevChildCount = (_this.state.children || []).length;
var state = _this._getState();
_this.setState(state, function () {
return (state.children || []).length !== prevChildCount && _this._onMouseChangeHandler();
});
};
_this._onChildClick = function () {
if (_this.props.onChildClick) {
if (_this.hoverChildProps_) {
var hoverKey = _this.hoverKey_;
var childProps = _this.hoverChildProps_;
// click works only on hovered item
_this.props.onChildClick(hoverKey, childProps);
}
}
};
_this._onChildMouseDown = function () {
if (_this.props.onChildMouseDown) {
if (_this.hoverChildProps_) {
var hoverKey = _this.hoverKey_;
var childProps = _this.hoverChildProps_;
// works only on hovered item
_this.props.onChildMouseDown(hoverKey, childProps);
}
}
};
_this._onChildMouseEnter = function (hoverKey, childProps) {
if (!_this.dimesionsCache_) {
return;
}
if (_this.props.onChildMouseEnter) {
_this.props.onChildMouseEnter(hoverKey, childProps);
}
_this.hoverChildProps_ = childProps;
_this.hoverKey_ = hoverKey;
_this.setState({ hoverKey: hoverKey });
};
_this._onChildMouseLeave = function () {
if (!_this.dimesionsCache_) {
return;
}
var hoverKey = _this.hoverKey_;
var childProps = _this.hoverChildProps_;
if (hoverKey !== undefined && hoverKey !== null) {
if (_this.props.onChildMouseLeave) {
_this.props.onChildMouseLeave(hoverKey, childProps);
}
_this.hoverKey_ = null;
_this.hoverChildProps_ = null;
_this.setState({ hoverKey: null });
}
};
_this._onMouseAllow = function (value) {
if (!value) {
_this._onChildMouseLeave();
}
_this.allowMouse_ = value;
};
_this._onMouseChangeHandler = function () {
if (_this.allowMouse_) {
_this._onMouseChangeHandlerRaf();
}
};
_this._onMouseChangeHandlerRaf = function () {
if (!_this.dimesionsCache_) {
return;
}
var mp = _this.props.dispatcher.getMousePosition();
if (mp) {
var distances = [];
var hoverDistance = _this.props.getHoverDistance();
_react2.default.Children.forEach(_this.state.children, function (child, childIndex) {
if (!child) return;
// layers
if (child.props.latLng === undefined && child.props.lat === undefined && child.props.lng === undefined) {
return;
}
var childKey = child.key !== undefined && child.key !== null ? child.key : childIndex;
var dist = _this.props.distanceToMouse(_this.dimesionsCache_[childKey], mp, child.props);
if (dist < hoverDistance) {
distances.push({
key: childKey,
dist: dist,
props: child.props
});
}
});
if (distances.length) {
distances.sort(function (a, b) {
return a.dist - b.dist;
});
var hoverKey = distances[0].key;
var childProps = distances[0].props;
if (_this.hoverKey_ !== hoverKey) {
_this._onChildMouseLeave();
_this._onChildMouseEnter(hoverKey, childProps);
}
} else {
_this._onChildMouseLeave();
}
} else {
_this._onChildMouseLeave();
}
};
_this._getDimensions = function (key) {
var childKey = key;
return _this.dimesionsCache_[childKey];
};
_this.props.dispatcher.on('kON_CHANGE', _this._onChangeHandler);
_this.props.dispatcher.on('kON_MOUSE_POSITION_CHANGE', _this._onMouseChangeHandler);
_this.props.dispatcher.on('kON_CLICK', _this._onChildClick);
_this.props.dispatcher.on('kON_MDOWN', _this._onChildMouseDown);
_this.dimesionsCache_ = {};
_this.hoverKey_ = null;
_this.hoverChildProps_ = null;
_this.allowMouse_ = true;
_this.state = _extends({}, _this._getState(), { hoverKey: null });
return _this;
}
/* eslint-enable react/forbid-prop-types */
GoogleMapMarkers.prototype.shouldComponentUpdate = function shouldComponentUpdate(nextProps, nextState) {
if (this.props.experimental === true) {
return !(0, _shallowEqual2.default)(this.props, nextProps) || !(0, _shallowEqual2.default)((0, _omit2.default)(this.state, ['hoverKey']), (0, _omit2.default)(nextState, ['hoverKey']));
}
return !(0, _shallowEqual2.default)(this.props, nextProps) || !(0, _shallowEqual2.default)(this.state, nextState);
};
GoogleMapMarkers.prototype.componentWillUnmount = function componentWillUnmount() {
this.props.dispatcher.removeListener('kON_CHANGE', this._onChangeHandler);
this.props.dispatcher.removeListener('kON_MOUSE_POSITION_CHANGE', this._onMouseChangeHandler);
this.props.dispatcher.removeListener('kON_CLICK', this._onChildClick);
this.props.dispatcher.removeListener('kON_MDOWN', this._onChildMouseDown);
this.dimesionsCache_ = null;
};
GoogleMapMarkers.prototype.render = function render() {
var _this2 = this;
var mainElementStyle = this.props.style || mainStyle;
this.dimesionsCache_ = {};
var markers = _react2.default.Children.map(this.state.children, function (child, childIndex) {
if (!child) return undefined;
if (child.props.latLng === undefined && child.props.lat === undefined && child.props.lng === undefined) {
return _react2.default.cloneElement(child, {
$geoService: _this2.props.geoService,
$onMouseAllow: _this2._onMouseAllow,
$prerender: _this2.props.prerender
});
}
var latLng = child.props.latLng !== undefined ? child.props.latLng : { lat: child.props.lat, lng: child.props.lng };
var pt = _this2.props.projectFromLeftTop ? _this2.props.geoService.fromLatLngToContainerPixel(latLng) : _this2.props.geoService.project(latLng);
var stylePtPos = {
left: pt.x,
top: pt.y
};
// If the component has a southeast corner defined (either as a LatLng, or a separate
// lat and lng pair), set the width and height based on the distance between the northwest
// and the southeast corner to lock the overlay to the correct geographic bounds.
if (child.props.seLatLng !== undefined || child.props.seLat !== undefined && child.props.seLng !== undefined) {
var seLatLng = child.props.seLatLng !== undefined ? child.props.seLatLng : { lat: child.props.seLat, lng: child.props.seLng };
var sePt = _this2.props.projectFromLeftTop ? _this2.props.geoService.fromLatLngToContainerPixel(seLatLng) : _this2.props.geoService.project(seLatLng);
stylePtPos.width = sePt.x - pt.x;
stylePtPos.height = sePt.y - pt.y;
}
var dx = 0;
var dy = 0;
if (!_this2.props.projectFromLeftTop) {
// center projection
if (_this2.props.geoService.hasSize()) {
dx = _this2.props.geoService.getWidth() / 2;
dy = _this2.props.geoService.getHeight() / 2;
}
}
// to prevent rerender on child element i need to pass
// const params $getDimensions and $dimensionKey instead of dimension object
var childKey = child.key !== undefined && child.key !== null ? child.key : childIndex;
_this2.dimesionsCache_[childKey] = _extends({
x: pt.x + dx,
y: pt.y + dy
}, latLng);
return _react2.default.createElement(
'div',
{
key: childKey,
style: _extends({}, style, stylePtPos),
className: child.props.$markerHolderClassName
},
_react2.default.cloneElement(child, {
$hover: childKey === _this2.state.hoverKey,
$getDimensions: _this2._getDimensions,
$dimensionKey: childKey,
$geoService: _this2.props.geoService,
$onMouseAllow: _this2._onMouseAllow,
$prerender: _this2.props.prerender
})
);
});
return _react2.default.createElement(
'div',
{ style: mainElementStyle },
markers
);
};
return GoogleMapMarkers;
}(_react.Component);
GoogleMapMarkers.propTypes = {
geoService: _propTypes2.default.any,
style: _propTypes2.default.any,
distanceToMouse: _propTypes2.default.func,
dispatcher: _propTypes2.default.any,
onChildClick: _propTypes2.default.func,
onChildMouseDown: _propTypes2.default.func,
onChildMouseLeave: _propTypes2.default.func,
onChildMouseEnter: _propTypes2.default.func,
getHoverDistance: _propTypes2.default.func,
projectFromLeftTop: _propTypes2.default.bool,
prerender: _propTypes2.default.bool
};
GoogleMapMarkers.defaultProps = {
projectFromLeftTop: false,
prerender: false
};
exports.default = GoogleMapMarkers;