Skip to content

Commit

Permalink
refactor(KmlLayer): rewrite with enhanceElement and cleaner interfaces
Browse files Browse the repository at this point in the history
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
tomchentw committed Oct 4, 2016
1 parent da0ebc2 commit 2479b88
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 155 deletions.
147 changes: 92 additions & 55 deletions src/lib/KmlLayer.js
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;
},
});
2 changes: 2 additions & 0 deletions src/lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ export const POLYLINE = `__SECRET_POLYLINE_DO_NOT_USE_OR_YOU_WILL_BE_FIRED`;
export const POLYGON = `__SECRET_POLYGON_DO_NOT_USE_OR_YOU_WILL_BE_FIRED`;

export const CIRCLE = `__SECRET_CIRCLE_DO_NOT_USE_OR_YOU_WILL_BE_FIRED`;

export const KML_LAYER = `__SECRET_KML_LAYER_DO_NOT_USE_OR_YOU_WILL_BE_FIRED`;
93 changes: 0 additions & 93 deletions src/lib/creators/KmlLayerCreator.js

This file was deleted.

7 changes: 0 additions & 7 deletions src/lib/eventLists/KmlLayerEventList.js

This file was deleted.

4 changes: 4 additions & 0 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ export {
export {
default as Circle,
} from "./Circle";

export {
default as KmlLayer,
} from "./KmlLayer";

0 comments on commit 2479b88

Please sign in to comment.