Skip to content

Commit

Permalink
refactor(Polyline): 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
<Polyline
  onClick={_.noop}
  onRightclick={_.noop}
  onDragstart={_.noop}
/>
```

After:

```js
<Polyline
  onClick={_.noop}
  onRightClick={_.noop}
  onDragStart={_.noop}
/>
```
  • Loading branch information
tomchentw committed Oct 4, 2016
1 parent ea39062 commit 5603319
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 144 deletions.
162 changes: 108 additions & 54 deletions src/lib/Polyline.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,133 @@
/* global google */
import _ from "lodash";

import {
default as React,
Component,
PropTypes,
} from "react";

import {
default as canUseDOM,
} from "can-use-dom";
MAP,
POLYLINE,
} from "./constants";

import {
default as PolylineCreator,
polylineDefaultPropTypes,
polylineControlledPropTypes,
polylineEventPropTypes,
} from "./creators/PolylineCreator";

import { default as GoogleMapHolder } from "./creators/GoogleMapHolder";

export default class Polyline extends Component {
static propTypes = {
// Uncontrolled default[props] - used only in componentDidMount
...polylineDefaultPropTypes,
// Controlled [props] - used in componentDidMount/componentDidUpdate
...polylineControlledPropTypes,
// Event [onEventName]
...polylineEventPropTypes,
}

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#Polyline
draggable: PropTypes.bool,
editable: PropTypes.bool,
options: PropTypes.object,
path: PropTypes.any,
visible: PropTypes.bool,
};

const defaultUncontrolledPropTypes = addDefaultPrefixToPropTypes(controlledPropTypes);

const eventMap = {
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#Polyline
// [].map.call($0.querySelectorAll("tr>td>code"), function(it){ return it.textContent; })
onClick: `click`,

onDblClick: `dblclick`,

onDrag: `drag`,

onDragEnd: `dragend`,

onDragStart: `dragstart`,

onMouseDown: `mousedown`,

onMouseMove: `mousemove`,

onMouseOut: `mouseout`,

onMouseOver: `mouseover`,

onMouseUp: `mouseup`,

onRightClick: `rightclick`,
};

const publicMethodMap = {
// Public APIs
//
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#Polyline
//
// [].map.call($0.querySelectorAll("tr>td>code"), function(it){ return it.textContent; })
// .filter(function(it){ return it.match(/^get/) && !it.match(/^getMap/); })
getDraggable() { return this.state.polyline.getDraggable(); }
// .filter(function(it){ return it.match(/^get/) && !it.match(/Map$/); })
getDraggable(polyline) { return polyline.getDraggable(); },

getEditable() { return this.state.polyline.getEditable(); }
getEditable(polyline) { return polyline.getEditable(); },

getPath() { return this.state.polyline.getPath(); }
getPath(polyline) { return polyline.getPath(); },

getVisible() { return this.state.polyline.getVisible(); }
getVisible(polyline) { return polyline.getVisible(); },
// END - Public APIs
//
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#Polyline
};

state = {
}
const controlledPropUpdaterMap = {
draggable(polyline, draggable) { polyline.setDraggable(draggable); },
editable(polyline, editable) { polyline.setEditable(editable); },
options(polyline, options) { polyline.setOptions(options); },
path(polyline, path) { polyline.setPath(path); },
visible(polyline, visible) { polyline.setVisible(visible); },
};

componentWillMount() {
const { mapHolderRef } = this.context;
function getInstanceFromComponent(component) {
return component.state[POLYLINE];
}

if (!canUseDOM) {
return;
}
const polyline = PolylineCreator._createPolyline({
...this.props,
mapHolderRef,
export default _.flowRight(
React.createClass,
enhanceElement(getInstanceFromComponent, publicMethodMap, eventMap, controlledPropUpdaterMap),
)({
displayName: `Polyline`,

propTypes: {
...controlledPropTypes,
...defaultUncontrolledPropTypes,
},

contextTypes: {
[MAP]: PropTypes.object,
},

getInitialState() {
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#Polyline
const polyline = new google.maps.Polyline({
map: this.context[MAP],
...collectUncontrolledAndControlledProps(
defaultUncontrolledPropTypes,
controlledPropTypes,
this.props
),
});
return {
[POLYLINE]: polyline,
};
},

this.setState({ polyline });
}
componentWillUnmount() {
const polyline = getInstanceFromComponent(this);
if (polyline) {
polyline.setMap(null);
}
},

render() {
if (this.state.polyline) {
return (
<PolylineCreator polyline={this.state.polyline} {...this.props}>
{this.props.children}
</PolylineCreator>
);
} 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 @@ -5,3 +5,5 @@ export const MAP = `__SECRET_MAP_DO_NOT_USE_OR_YOU_WILL_BE_FIRED`;
export const MARKER = `__SECRET_MARKER_DO_NOT_USE_OR_YOU_WILL_BE_FIRED`;

export const RECTANGLE = `__SECRET_RECTANGLE_DO_NOT_USE_OR_YOU_WILL_BE_FIRED`;

export const POLYLINE = `__SECRET_POLYLINE_DO_NOT_USE_OR_YOU_WILL_BE_FIRED`;
75 changes: 0 additions & 75 deletions src/lib/creators/PolylineCreator.js

This file was deleted.

15 changes: 0 additions & 15 deletions src/lib/eventLists/PolylineEventList.js

This file was deleted.

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

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

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

0 comments on commit 5603319

Please sign in to comment.