Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bounds to overlay view #206

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions examples/gh-pages/scripts/components/basics/OverlayView.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export default class OverlayViewExample extends Component {
defaultCenter={{lat: -34.397, lng: 150.644}}>
<OverlayView
position={{lat: -34.397, lng: 150.644}}
/*
* An alternative to specifying position is specifying bounds.
* bounds can either be an instance of google.maps.LatLngBounds
* or an object in the following format:
* bounds={ {ne: { lat: 62.400471, lng: -150.005608 }, sw: { { lat: 62.281819, lng: -150.287132 } } }
*/
/*
* 1. Specify the pane the OverlayView will be rendered to. For
* mouse interactivity, use `OverlayView.OVERLAY_MOUSE_TARGET`.
Expand Down
59 changes: 46 additions & 13 deletions lib/creators/OverlayViewCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ var overlayViewControlledPropTypes = {
mapPaneName: _react.PropTypes.string,
getPixelPositionOffset: _react.PropTypes.func,
position: _react.PropTypes.object,
children: _react.PropTypes.node
children: _react.PropTypes.node,
bounds: _react.PropTypes.object
};

exports.overlayViewControlledPropTypes = overlayViewControlledPropTypes;
Expand Down Expand Up @@ -142,19 +143,37 @@ var OverlayViewCreator = (function (_Component) {
overlayView._positionContainerElement = function _positionContainerElement() {
var left = undefined;
var top = undefined;
var position = this._getPixelPosition();
if (position) {
var x = position.x;
var y = position.y;

var offset = this._getOffset();
if (offset) {
x += offset.x;
y += offset.y;
var offset = this._getOffset();
if (this.get("bounds")) {
var bounds = this._getPixelBounds();
if (bounds) {
var sw = bounds.sw;
var ne = bounds.ne;

if (offset) {
sw.x += offset.x;
ne.y += offset.y;
}
left = sw.x + "px";
top = ne.y + "px";
this._containerElement.style.width = ne.x - sw.x + "px";
this._containerElement.style.height = sw.y - ne.y + "px";
}
} else {
var position = this._getPixelPosition();
if (position) {
var x = position.x;
var y = position.y;

if (offset) {
x += offset.x;
y += offset.y;
}
left = x + "px";
top = y + "px";
}
left = x + "px";
top = y + "px";
}

this._containerElement.style.left = left;
this._containerElement.style.top = top;
};
Expand All @@ -163,7 +182,6 @@ var OverlayViewCreator = (function (_Component) {
var projection = this.getProjection();
var position = this.get("position");
(0, _invariant2["default"])(!!position, "OverlayView requires a position/defaultPosition in your props instead of %s", position);

if (projection && position) {
if (!(position instanceof google.maps.LatLng)) {
position = new google.maps.LatLng(position.lat, position.lng);
Expand All @@ -172,6 +190,21 @@ var OverlayViewCreator = (function (_Component) {
}
};

overlayView._getPixelBounds = function _getPixelBounds() {
var projection = this.getProjection();
var bounds = this.get("bounds");
(0, _invariant2["default"])(!!bounds, "OverlayView requires a bounds in your props instead of %s", bounds);
if (projection && bounds) {
if (!(bounds instanceof google.maps.LatLngBounds)) {
bounds = new google.maps.LatLngBounds(new google.maps.LatLng(bounds.ne.lat, bounds.ne.lng), new google.maps.LatLng(bounds.sw.lat, bounds.sw.lng));
}
return {
sw: projection.fromLatLngToDivPixel(this.bounds.getSouthWest()),
ne: projection.fromLatLngToDivPixel(this.bounds.getNorthEast())
};
}
};

overlayView._getOffset = function _getOffset() {
// Allows the component to control the visual position of the OverlayView
// relative to the LatLng pixel position.
Expand Down
6 changes: 3 additions & 3 deletions lib/utils/componentLifecycleDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function componentLifecycleDecorator(_ref) {

// Stash component's own lifecycle methods to be invoked later
var componentDidMount = Component.prototype.hasOwnProperty("componentDidMount") ? Component.prototype.componentDidMount : noop;
var componentDidUpdate = Component.prototype.hasOwnProperty("componentDidUpdate") ? Component.prototype.componentDidUpdate : noop;
var componentWillReceiveProps = Component.prototype.hasOwnProperty("componentWillReceiveProps") ? Component.prototype.componentWillReceiveProps : noop;
var componentWillUnmount = Component.prototype.hasOwnProperty("componentWillUnmount") ? Component.prototype.componentWillUnmount : noop;

Object.defineProperty(Component.prototype, "componentDidMount", {
Expand All @@ -40,7 +40,7 @@ function componentLifecycleDecorator(_ref) {
}
});

Object.defineProperty(Component.prototype, "componentDidUpdate", {
Object.defineProperty(Component.prototype, "componentWillReceiveProps", {
enumerable: false,
configurable: true,
writable: true,
Expand All @@ -54,7 +54,7 @@ function componentLifecycleDecorator(_ref) {
}

// Hook into client's implementation, if it has any
componentDidUpdate.call(this, prevProps);
componentWillReceiveProps.call(this, prevProps);

register.call(this);
}
Expand Down
54 changes: 44 additions & 10 deletions src/creators/OverlayViewCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const overlayViewControlledPropTypes = {
getPixelPositionOffset: PropTypes.func,
position: PropTypes.object,
children: PropTypes.node,
bounds: PropTypes.object,
// NOTICE!!!!!!
//
// Only expose those with getters & setters in the table as controlled props.
Expand Down Expand Up @@ -95,17 +96,33 @@ export default class OverlayViewCreator extends Component {
overlayView._positionContainerElement = function _positionContainerElement() {
let left;
let top;
const position = this._getPixelPosition();
if (position) {
let { x, y } = position;
const offset = this._getOffset();
if (offset) {
x += offset.x;
y += offset.y;
const offset = this._getOffset();
if (this.get(`bounds`)) {
const bounds = this._getPixelBounds();
if (bounds) {
const { sw, ne } = bounds;
if (offset) {
sw.x += offset.x;
ne.y += offset.y;
}
left = sw.x + `px`;
top = ne.y + `px`;
this._containerElement.style.width = (ne.x - sw.x) + `px`;
this._containerElement.style.height = (sw.y - ne.y) + `px`;
}
} else {
const position = this._getPixelPosition();
if (position) {
let { x, y } = position;
if (offset) {
x += offset.x;
y += offset.y;
}
left = x + `px`;
top = y + `px`;
}
left = x + `px`;
top = y + `px`;
}

this._containerElement.style.left = left;
this._containerElement.style.top = top;
};
Expand All @@ -114,7 +131,6 @@ export default class OverlayViewCreator extends Component {
const projection = this.getProjection();
let position = this.get(`position`);
invariant(!!position, `OverlayView requires a position/defaultPosition in your props instead of %s`, position);

if (projection && position) {
if (!(position instanceof google.maps.LatLng)) {
position = new google.maps.LatLng(position.lat, position.lng);
Expand All @@ -123,6 +139,24 @@ export default class OverlayViewCreator extends Component {
}
};

overlayView._getPixelBounds = function _getPixelBounds() {
const projection = this.getProjection();
let bounds = this.get(`bounds`);
invariant(!!bounds, `OverlayView requires a bounds in your props instead of %s`, bounds);
if (projection && bounds) {
if (!(bounds instanceof google.maps.LatLngBounds)) {
bounds = new google.maps.LatLngBounds(
new google.maps.LatLng(bounds.ne.lat, bounds.ne.lng),
new google.maps.LatLng(bounds.sw.lat, bounds.sw.lng)
);
}
return {
sw: projection.fromLatLngToDivPixel(this.bounds.getSouthWest()),
ne: projection.fromLatLngToDivPixel(this.bounds.getNorthEast()),
};
}
};

overlayView._getOffset = function _getOffset() {
// Allows the component to control the visual position of the OverlayView
// relative to the LatLng pixel position.
Expand Down
6 changes: 3 additions & 3 deletions src/utils/componentLifecycleDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function componentLifecycleDecorator({ registerEvents, instanceMe

// Stash component's own lifecycle methods to be invoked later
const componentDidMount = Component.prototype.hasOwnProperty(`componentDidMount`) ? Component.prototype.componentDidMount : noop;
const componentDidUpdate = Component.prototype.hasOwnProperty(`componentDidUpdate`) ? Component.prototype.componentDidUpdate : noop;
const componentWillReceiveProps = Component.prototype.hasOwnProperty(`componentWillReceiveProps`) ? Component.prototype.componentWillReceiveProps : noop;
const componentWillUnmount = Component.prototype.hasOwnProperty(`componentWillUnmount`) ? Component.prototype.componentWillUnmount : noop;

Object.defineProperty(Component.prototype, `componentDidMount`, {
Expand All @@ -33,7 +33,7 @@ export default function componentLifecycleDecorator({ registerEvents, instanceMe
},
});

Object.defineProperty(Component.prototype, `componentDidUpdate`, {
Object.defineProperty(Component.prototype, `componentWillReceiveProps`, {
enumerable: false,
configurable: true,
writable: true,
Expand All @@ -47,7 +47,7 @@ export default function componentLifecycleDecorator({ registerEvents, instanceMe
}

// Hook into client's implementation, if it has any
componentDidUpdate.call(this, prevProps);
componentWillReceiveProps.call(this, prevProps);

register.call(this);
},
Expand Down