-
Notifications
You must be signed in to change notification settings - Fork 936
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(KmlLayer): rewrite with enhanceElement and cleaner interfaces
BREAKING CHANGE: Naming convention for event handlers has tweaked to follow React's convention. Before: ```js <KmlLayer onClick={_.noop} onDefaultviewportChanged={_.noop} onStatusChanged={_.noop} /> ``` After: ```js <KmlLayer onClick={_.noop} onDefaultViewportChanged={_.noop} onStatusChanged={_.noop} /> ```
- Loading branch information
Showing
5 changed files
with
98 additions
and
155 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,119 @@ | ||
/* global google */ | ||
import _ from "lodash"; | ||
|
||
import { | ||
default as React, | ||
Component, | ||
PropTypes, | ||
} from "react"; | ||
|
||
import { | ||
default as canUseDOM, | ||
} from "can-use-dom"; | ||
MAP, | ||
KML_LAYER, | ||
} from "./constants"; | ||
|
||
import { | ||
default as KmlLayerCreator, | ||
kmlLayerDefaultPropTypes, | ||
kmlLayerControlledPropTypes, | ||
kmlLayerEventPropTypes, | ||
} from "./creators/KmlLayerCreator"; | ||
|
||
import { default as GoogleMapHolder } from "./creators/GoogleMapHolder"; | ||
|
||
export default class KmlLayer extends Component { | ||
static propTypes = { | ||
// Uncontrolled default[props] - used only in componentDidMount | ||
...kmlLayerDefaultPropTypes, | ||
// Controlled [props] - used in componentDidMount/componentDidUpdate | ||
...kmlLayerControlledPropTypes, | ||
// Event [onEventName] | ||
...kmlLayerEventPropTypes, | ||
} | ||
|
||
static contextTypes = { | ||
mapHolderRef: PropTypes.instanceOf(GoogleMapHolder), | ||
} | ||
addDefaultPrefixToPropTypes, | ||
collectUncontrolledAndControlledProps, | ||
default as enhanceElement, | ||
} from "./enhanceElement"; | ||
|
||
const controlledPropTypes = { | ||
// NOTICE!!!!!! | ||
// | ||
// Only expose those with getters & setters in the table as controlled props. | ||
// | ||
// [].map.call($0.querySelectorAll("tr>td>code", function(it){ return it.textContent; }) | ||
// .filter(function(it){ return it.match(/^set/) && !it.match(/^setMap/); }) | ||
// | ||
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#KmlLayer | ||
defaultViewport: PropTypes.any, | ||
metadata: PropTypes.any, | ||
status: PropTypes.any, | ||
url: PropTypes.string, | ||
zIndex: PropTypes.number, | ||
}; | ||
|
||
const defaultUncontrolledPropTypes = addDefaultPrefixToPropTypes(controlledPropTypes); | ||
|
||
const eventMap = { | ||
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#KmlLayer | ||
// [].map.call($0.querySelectorAll("tr>td>code"), function(it){ return it.textContent; }) | ||
onClick: `click`, | ||
|
||
onDefaultViewportChanged: `defaultviewport_changed`, | ||
|
||
onStatusChanged: `status_changed`, | ||
}; | ||
|
||
const publicMethodMap = { | ||
// Public APIs | ||
// | ||
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#KmlLayer | ||
// | ||
// [].map.call($0.querySelectorAll("tr>td>code"), function(it){ return it.textContent; }) | ||
// .filter(function(it){ return it.match(/^get/) && !it.match(/Map$/); }) | ||
getDefaultViewport() { return this.state.kmlLayer.getDefaultViewport(); } | ||
getDefaultViewport(kmlLayer) { return kmlLayer.getDefaultViewport(); }, | ||
|
||
getMetadata() { return this.state.kmlLayer.getMetadata(); } | ||
getMetadata(kmlLayer) { return kmlLayer.getMetadata(); }, | ||
|
||
getStatus() { return this.state.kmlLayer.getStatus(); } | ||
getStatus(kmlLayer) { return kmlLayer.getStatus(); }, | ||
|
||
getUrl() { return this.state.kmlLayer.getUrl(); } | ||
getUrl(kmlLayer) { return kmlLayer.getUrl(); }, | ||
|
||
getZIndex() { return this.state.marker.getZIndex(); } | ||
getZIndex(kmlLayer) { return kmlLayer.getZIndex(); }, | ||
// END - Public APIs | ||
// | ||
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#KmlLayer | ||
}; | ||
|
||
state = { | ||
} | ||
const controlledPropUpdaterMap = { | ||
defaultViewport(kmlLayer, defaultViewport) { kmlLayer.setDefaultViewport(defaultViewport); }, | ||
metadata(kmlLayer, metadata) { kmlLayer.setMetadata(metadata); }, | ||
status(kmlLayer, status) { kmlLayer.setStatus(status); }, | ||
url(kmlLayer, url) { kmlLayer.setUrl(url); }, | ||
zIndex(kmlLayer, zIndex) { kmlLayer.setZIndex(zIndex); }, | ||
}; | ||
|
||
componentWillMount() { | ||
const { mapHolderRef } = this.context; | ||
function getInstanceFromComponent(component) { | ||
return component.state[KML_LAYER]; | ||
} | ||
|
||
if (!canUseDOM) { | ||
return; | ||
} | ||
const kmlLayer = KmlLayerCreator._createKmlLayer({ | ||
...this.props, | ||
mapHolderRef, | ||
export default _.flowRight( | ||
React.createClass, | ||
enhanceElement(getInstanceFromComponent, publicMethodMap, eventMap, controlledPropUpdaterMap), | ||
)({ | ||
displayName: `KmlLayer`, | ||
|
||
propTypes: { | ||
...controlledPropTypes, | ||
...defaultUncontrolledPropTypes, | ||
}, | ||
|
||
contextTypes: { | ||
[MAP]: PropTypes.object, | ||
}, | ||
|
||
getInitialState() { | ||
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#KmlLayer | ||
const kmlLayer = new google.maps.KmlLayer({ | ||
map: this.context[MAP], | ||
...collectUncontrolledAndControlledProps( | ||
defaultUncontrolledPropTypes, | ||
controlledPropTypes, | ||
this.props | ||
), | ||
}); | ||
return { | ||
[KML_LAYER]: kmlLayer, | ||
}; | ||
}, | ||
|
||
this.setState({ kmlLayer }); | ||
} | ||
componentWillUnmount() { | ||
const kmlLayer = getInstanceFromComponent(this); | ||
if (kmlLayer) { | ||
kmlLayer.setMap(null); | ||
} | ||
}, | ||
|
||
render() { | ||
const { mapHolderRef } = this.context; | ||
if (this.state.kmlLayer) { | ||
return ( | ||
<KmlLayerCreator mapHolderRef={mapHolderRef} kmlLayer={this.state.kmlLayer} {...this.props}> | ||
{this.props.children} | ||
</KmlLayerCreator> | ||
); | ||
} else { | ||
return (<noscript />); | ||
} | ||
} | ||
} | ||
return false; | ||
}, | ||
}); |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -25,3 +25,7 @@ export { | |
export { | ||
default as Circle, | ||
} from "./Circle"; | ||
|
||
export { | ||
default as KmlLayer, | ||
} from "./KmlLayer"; |