-
Notifications
You must be signed in to change notification settings - Fork 889
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
146 additions
and
101 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
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 |
---|---|---|
@@ -1,61 +1,82 @@ | ||
import React, { PropTypes } from 'react'; | ||
import React, { Children, PropTypes } from 'react'; | ||
import { Map, popup } from 'leaflet'; | ||
|
||
import latlngType from './types/latlng'; | ||
import MapComponent from './MapComponent'; | ||
|
||
export default class Popup extends MapComponent { | ||
static propTypes = { | ||
children: PropTypes.oneOfType([ | ||
PropTypes.arrayOf(PropTypes.node), | ||
PropTypes.node, | ||
]), | ||
children: PropTypes.node, | ||
map: PropTypes.instanceOf(Map), | ||
popupContainer: PropTypes.node, | ||
popupContainer: PropTypes.object, | ||
position: latlngType, | ||
}; | ||
|
||
componentWillMount() { | ||
super.componentWillMount(); | ||
const { children, map, ...props } = this.props; | ||
|
||
this.leafletElement = popup(props); | ||
this.leafletElement.on('open', ::this.renderPopupContent); | ||
this.leafletElement.on('close', ::this.removePopupContent); | ||
} | ||
|
||
componentDidUpdate(prevProps) { | ||
const { children, map, popupContainer, position, ...props } = this.props; | ||
if (children !== prevProps.children) { | ||
const content = React.renderToStaticMarkup(children); | ||
if (popupContainer) { | ||
popupContainer.bindPopup(content, props); | ||
} | ||
else { | ||
const el = this.leafletElement; | ||
el.setContent(content); | ||
if (position !== prevProps.position) el.setLatLng(position); | ||
componentDidMount() { | ||
const { map, popupContainer, position } = this.props; | ||
const el = this.leafletElement; | ||
|
||
if (popupContainer) { | ||
// Attach to container component | ||
popupContainer.bindPopup(el); | ||
} | ||
else { | ||
// Attach to a Map | ||
if (position) { | ||
el.setLatLng(position); | ||
} | ||
el.openOn(map); | ||
} | ||
} | ||
|
||
componentDidUpdate(prevProps) { | ||
const { position } = this.props; | ||
|
||
if (position !== prevProps.position) { | ||
this.leafletElement.setLatLng(position); | ||
} | ||
|
||
if (this.leafletElement._isOpen) { | ||
this.renderPopupContent(); | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
super.componentWillUnmount(); | ||
this.removePopupContent(); | ||
this.props.map.removeLayer(this.leafletElement); | ||
} | ||
|
||
render() { | ||
const { children, map, popupContainer, position, ...props } = this.props; | ||
if (children) { | ||
const content = React.renderToStaticMarkup(children); | ||
// Attach to container component | ||
if (popupContainer) { | ||
popupContainer.bindPopup(content, props); | ||
return null; | ||
} | ||
// Attach to a Map | ||
const el = this.leafletElement; | ||
el.setContent(content); | ||
if (position) el.setLatLng(position); | ||
el.openOn(map); | ||
renderPopupContent() { | ||
if (this.props.children) { | ||
React.render( | ||
Children.only(this.props.children), | ||
this.leafletElement._contentNode | ||
); | ||
|
||
this.leafletElement._updateLayout(); | ||
this.leafletElement._updatePosition(); | ||
this.leafletElement._adjustPan(); | ||
} | ||
else { | ||
this.removePopupContent(); | ||
} | ||
} | ||
|
||
removePopupContent() { | ||
React.unmountComponentAtNode(this.leafletElement._contentNode); | ||
} | ||
|
||
render() { | ||
return null; | ||
} | ||
} |