-
Notifications
You must be signed in to change notification settings - Fork 939
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(OverlayView): add OverlayView component
- Loading branch information
Pete Browne
committed
May 30, 2015
1 parent
3433bff
commit e566632
Showing
5 changed files
with
224 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
examples/gh-pages/scripts/components/basics/OverlayView.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import React from "react/addons"; | ||
import {GoogleMaps, OverlayView} from "react-google-maps"; | ||
|
||
const STYLES = { | ||
mapContainer: { | ||
height: "100%" | ||
}, | ||
overlayView: { | ||
background: "white", | ||
border: "1px solid #ccc", | ||
padding: 15 | ||
} | ||
}; | ||
|
||
class OverlayViewExample extends React.Component { | ||
constructor (...args) { | ||
super(...args); | ||
this.state = {count: 0}; | ||
} | ||
|
||
render () { | ||
const {props, state} = this, | ||
{googleMapsApi, ...otherProps} = props, | ||
{count} = state; | ||
return ( | ||
<GoogleMaps | ||
containerProps={{...otherProps, style: STYLES.mapContainer}} | ||
googleMapsApi={google.maps} | ||
zoom={8} | ||
center={{lat: -34.397, lng: 150.644}}> | ||
<OverlayView | ||
position={{lat: -34.397, lng: 150.644}} | ||
/* | ||
* 1. Specify the pane the OverlayView will be rendered to. For | ||
* mouse interactivity, use `OverlayView.OVERLAY_MOUSE_TARGET`. | ||
* Defaults to `OverlayView.OVERLAY_LAYER`. | ||
*/ | ||
mapPane={OverlayView.OVERLAY_MOUSE_TARGET} | ||
/* | ||
* 2. Tweak the OverlayView's pixel position. In this case, we're | ||
* centering the content. | ||
*/ | ||
getPixelPositionOffset={this.getPixelPositionOffset} | ||
/* | ||
* 3. Create OverlayView content using standard React components. | ||
*/> | ||
<div style={STYLES.overlayView}> | ||
<h1>OverlayView</h1> | ||
<button onClick={this.onClick.bind(this)}> | ||
I have been clicked {count} time{count === 1 ? '' : 's'} | ||
</button> | ||
</div> | ||
</OverlayView> | ||
</GoogleMaps> | ||
); | ||
} | ||
|
||
onClick () { | ||
this.setState({count: this.state.count + 1}); | ||
} | ||
|
||
getPixelPositionOffset (width, height) { | ||
return {x: -(width / 2), y: -(height / 2)}; | ||
} | ||
} | ||
|
||
export default OverlayViewExample; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React from "react"; | ||
|
||
import SimpleChildComponent from "./internals/SimpleChildComponent"; | ||
import createOverlayViewProxy from "./internals/createOverlayViewProxy"; | ||
import createRegisterEvents from "./internals/createRegisterEvents"; | ||
import exposeGetters from "./internals/exposeGetters"; | ||
|
||
class OverlayView extends SimpleChildComponent { | ||
_createOrUpdateInstance () { | ||
const {props} = this; | ||
if (!props.googleMapsApi || !props.map) { | ||
return; | ||
} | ||
var {instance} = this.state; | ||
|
||
if (instance) { | ||
instance = super._createOrUpdateInstance(); | ||
instance.draw(); | ||
} else { | ||
instance = createOverlayViewProxy(this.props); | ||
exposeGetters(this, instance.constructor.prototype, instance); | ||
this.setState({instance}); | ||
} | ||
return instance; | ||
} | ||
} | ||
|
||
OverlayView.FLOAT_PANE = "floatPane"; | ||
OverlayView.MAP_PANE = "mapPane"; | ||
OverlayView.MARKER_LAYER = "markerLayer"; | ||
OverlayView.OVERLAY_LAYER = "overlayLayer"; | ||
OverlayView.OVERLAY_MOUSE_TARGET = "overlayMouseTarget"; | ||
|
||
OverlayView.propTypes = { | ||
...SimpleChildComponent.propTypes, | ||
mapPane: React.PropTypes.oneOf([ | ||
OverlayView.FLOAT_PANE, | ||
OverlayView.MAP_PANE, | ||
OverlayView.MARKER_LAYER, | ||
OverlayView.OVERLAY_LAYER, | ||
OverlayView.OVERLAY_MOUSE_TARGET | ||
]), | ||
getPixelPositionOffset: React.PropTypes.func | ||
}; | ||
|
||
OverlayView.defaultProps = { | ||
mapPane: OverlayView.OVERLAY_LAYER | ||
}; | ||
|
||
OverlayView._registerEvents = createRegisterEvents( | ||
"projection_changed position_changed" | ||
); | ||
|
||
export default OverlayView; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import React from "react"; | ||
|
||
var OverlayViewProxy; | ||
|
||
// Since the Google Maps API can be asynchronously loaded, we define the | ||
// OverlayView subclass in a factory method that's called when it's available. | ||
export default function createOverlayViewProxy (props) { | ||
const {googleMapsApi, ...googleMapsConfig} = props; | ||
if (!googleMapsApi) { | ||
return; | ||
} | ||
if (!OverlayViewProxy) { | ||
OverlayViewProxy = defineOverlayViewProxyClass(googleMapsApi); | ||
} | ||
return new OverlayViewProxy(googleMapsConfig); | ||
} | ||
|
||
function defineOverlayViewProxyClass (googleMapsApi) { | ||
return class OverlayViewProxy extends googleMapsApi.OverlayView { | ||
onAdd () { | ||
let mapPane = this.get("mapPane"); | ||
this._containerElement = document.createElement("div"); | ||
this._containerElement.style.position = "absolute"; | ||
this.getPanes()[mapPane].appendChild(this._containerElement); | ||
} | ||
|
||
draw () { | ||
if (!this._containerElement) { | ||
return; | ||
} | ||
this._renderContent(); | ||
this._positionContainerElement(); | ||
} | ||
|
||
onRemove () { | ||
React.unmountComponentAtNode(this._containerElement); | ||
this._containerElement.parentNode.removeChild(this._containerElement); | ||
this._containerElement = null; | ||
} | ||
|
||
_renderContent () { | ||
React.render( | ||
<div>{this.get("children")}</div>, | ||
this._containerElement | ||
); | ||
} | ||
|
||
_positionContainerElement () { | ||
let left, top; | ||
let position = this._getPixelPosition(); | ||
if (position) { | ||
let {x, y} = position; | ||
let offset = this._getOffset(); | ||
if (offset) { | ||
x += offset.x; | ||
y += offset.y; | ||
} | ||
left = x + "px"; | ||
top = y + "px"; | ||
} | ||
this._containerElement.style.left = left; | ||
this._containerElement.style.top = top; | ||
} | ||
|
||
_getPixelPosition () { | ||
let projection = this.getProjection(); | ||
let position = this.get("position"); | ||
if (projection && position) { | ||
if (!(position instanceof googleMapsApi.LatLng)) { | ||
position = new googleMapsApi.LatLng(position.lat, position.lng); | ||
} | ||
return projection.fromLatLngToDivPixel(position); | ||
} | ||
} | ||
|
||
_getOffset () { | ||
// Allows the component to control the visual position of the OverlayView | ||
// relative to the LatLng pixel position. | ||
let getPixelPositionOffset = this.get("getPixelPositionOffset"); | ||
if (getPixelPositionOffset) { | ||
return getPixelPositionOffset( | ||
this._containerElement.offsetWidth, | ||
this._containerElement.offsetHeight | ||
); | ||
} | ||
} | ||
} | ||
} |