|
| 1 | +/* global google */ |
| 2 | +import _ from "lodash"; |
| 3 | + |
1 | 4 | import { |
2 | 5 | default as React, |
3 | | - Component, |
4 | 6 | PropTypes, |
5 | 7 | } from "react"; |
6 | 8 |
|
7 | 9 | import { |
8 | | - default as canUseDOM, |
9 | | -} from "can-use-dom"; |
| 10 | + MAP, |
| 11 | + RECTANGLE, |
| 12 | +} from "./constants"; |
10 | 13 |
|
11 | 14 | import { |
12 | | - default as RectangleCreator, |
13 | | - rectangleDefaultPropTypes, |
14 | | - rectangleControlledPropTypes, |
15 | | - rectangleEventPropTypes, |
16 | | -} from "./creators/RectangleCreator"; |
17 | | - |
18 | | -import { default as GoogleMapHolder } from "./creators/GoogleMapHolder"; |
19 | | - |
20 | | -/* |
21 | | - * Original author: @alistairjcbrown |
22 | | - * Original PR: https://github.com/tomchentw/react-google-maps/pull/80 |
23 | | - */ |
24 | | -export default class Rectangle extends Component { |
25 | | - static propTypes = { |
26 | | - // Uncontrolled default[props] - used only in componentDidMount |
27 | | - ...rectangleDefaultPropTypes, |
28 | | - // Controlled [props] - used in componentDidMount/componentDidUpdate |
29 | | - ...rectangleControlledPropTypes, |
30 | | - // Event [onEventName] |
31 | | - ...rectangleEventPropTypes, |
32 | | - } |
33 | | - |
34 | | - static contextTypes = { |
35 | | - mapHolderRef: PropTypes.instanceOf(GoogleMapHolder), |
36 | | - } |
| 15 | + addDefaultPrefixToPropTypes, |
| 16 | + collectUncontrolledAndControlledProps, |
| 17 | + default as enhanceElement, |
| 18 | +} from "./enhanceElement"; |
| 19 | + |
| 20 | +const controlledPropTypes = { |
| 21 | + // NOTICE!!!!!! |
| 22 | + // |
| 23 | + // Only expose those with getters & setters in the table as controlled props. |
| 24 | + // |
| 25 | + // [].map.call($0.querySelectorAll("tr>td>code", function(it){ return it.textContent; }) |
| 26 | + // .filter(function(it){ return it.match(/^set/) && !it.match(/^setMap/); }) |
| 27 | + // |
| 28 | + // https://developers.google.com/maps/documentation/javascript/3.exp/reference#Rectangle |
| 29 | + bounds: PropTypes.any, |
| 30 | + draggable: PropTypes.bool, |
| 31 | + editable: PropTypes.bool, |
| 32 | + options: PropTypes.object, |
| 33 | + visible: PropTypes.bool, |
| 34 | +}; |
| 35 | + |
| 36 | +const defaultUncontrolledPropTypes = addDefaultPrefixToPropTypes(controlledPropTypes); |
| 37 | + |
| 38 | +const eventMap = { |
| 39 | + // https://developers.google.com/maps/documentation/javascript/3.exp/reference#Rectangle |
| 40 | + // [].map.call($0.querySelectorAll("tr>td>code"), function(it){ return it.textContent; }) |
| 41 | + onBoundsChanged: `bounds_changed`, |
| 42 | + |
| 43 | + onClick: `click`, |
| 44 | + |
| 45 | + onDblClick: `dblclick`, |
| 46 | + |
| 47 | + onDrag: `drag`, |
| 48 | + |
| 49 | + onDragEnd: `dragend`, |
| 50 | + |
| 51 | + onDragStart: `dragstart`, |
| 52 | + |
| 53 | + onMouseDown: `mousedown`, |
37 | 54 |
|
| 55 | + onMouseMove: `mousemove`, |
| 56 | + |
| 57 | + onMouseOut: `mouseout`, |
| 58 | + |
| 59 | + onMouseOver: `mouseover`, |
| 60 | + |
| 61 | + onMouseUp: `mouseup`, |
| 62 | + |
| 63 | + onRightClick: `rightclick`, |
| 64 | +}; |
| 65 | + |
| 66 | +const publicMethodMap = { |
38 | 67 | // Public APIs |
39 | 68 | // |
40 | 69 | // https://developers.google.com/maps/documentation/javascript/3.exp/reference#Rectangle |
41 | 70 | // |
42 | 71 | // [].map.call($0.querySelectorAll("tr>td>code"), function(it){ return it.textContent; }) |
43 | | - // .filter(function(it){ return it.match(/^get/) && !it.match(/^getMap/); }) |
44 | | - getBounds() { return this.state.rectangle.getBounds(); } |
| 72 | + // .filter(function(it){ return it.match(/^get/) && !it.match(/Map$/); }) |
| 73 | + getBounds(rectangle) { return rectangle.getBounds(); }, |
45 | 74 |
|
46 | | - getDraggable() { return this.state.rectangle.getDraggable(); } |
| 75 | + getDraggable(rectangle) { return rectangle.getDraggable(); }, |
47 | 76 |
|
48 | | - getEditable() { return this.state.rectangle.getEditable(); } |
| 77 | + getEditable(rectangle) { return rectangle.getEditable(); }, |
49 | 78 |
|
50 | | - getVisible() { return this.state.rectangle.getVisible(); } |
| 79 | + getVisible(rectangle) { return rectangle.getVisible(); }, |
51 | 80 | // END - Public APIs |
52 | | - // |
53 | | - // https://developers.google.com/maps/documentation/javascript/3.exp/reference#Rectangle |
| 81 | +}; |
54 | 82 |
|
55 | | - state = { |
56 | | - } |
| 83 | +const controlledPropUpdaterMap = { |
| 84 | + bounds(rectangle, bounds) { rectangle.setBounds(bounds); }, |
| 85 | + draggable(rectangle, draggable) { rectangle.setDraggable(draggable); }, |
| 86 | + editable(rectangle, editable) { rectangle.setEditable(editable); }, |
| 87 | + options(rectangle, options) { rectangle.setOptions(options); }, |
| 88 | + visible(rectangle, visible) { rectangle.setVisible(visible); }, |
| 89 | +}; |
57 | 90 |
|
58 | | - componentWillMount() { |
59 | | - const { mapHolderRef } = this.context; |
| 91 | +function getInstanceFromComponent(component) { |
| 92 | + return component.state[RECTANGLE]; |
| 93 | +} |
60 | 94 |
|
61 | | - if (!canUseDOM) { |
62 | | - return; |
63 | | - } |
64 | | - const rectangle = RectangleCreator._createRectangle({ |
65 | | - ...this.props, |
66 | | - mapHolderRef, |
| 95 | +export default _.flowRight( |
| 96 | + React.createClass, |
| 97 | + enhanceElement(getInstanceFromComponent, publicMethodMap, eventMap, controlledPropUpdaterMap), |
| 98 | +)({ |
| 99 | + displayName: `Rectangle`, |
| 100 | + |
| 101 | + propTypes: { |
| 102 | + ...controlledPropTypes, |
| 103 | + ...defaultUncontrolledPropTypes, |
| 104 | + }, |
| 105 | + |
| 106 | + contextTypes: { |
| 107 | + [MAP]: PropTypes.object, |
| 108 | + }, |
| 109 | + |
| 110 | + getInitialState() { |
| 111 | + // https://developers.google.com/maps/documentation/javascript/3.exp/reference#Rectangle |
| 112 | + const rectangle = new google.maps.Rectangle({ |
| 113 | + map: this.context[MAP], |
| 114 | + ...collectUncontrolledAndControlledProps( |
| 115 | + defaultUncontrolledPropTypes, |
| 116 | + controlledPropTypes, |
| 117 | + this.props |
| 118 | + ), |
67 | 119 | }); |
| 120 | + return { |
| 121 | + [RECTANGLE]: rectangle, |
| 122 | + }; |
| 123 | + }, |
68 | 124 |
|
69 | | - this.setState({ rectangle }); |
70 | | - } |
| 125 | + componentWillUnmount() { |
| 126 | + const rectangle = getInstanceFromComponent(this); |
| 127 | + if (rectangle) { |
| 128 | + rectangle.setMap(null); |
| 129 | + } |
| 130 | + }, |
71 | 131 |
|
72 | 132 | render() { |
73 | | - if (this.state.rectangle) { |
74 | | - return ( |
75 | | - <RectangleCreator rectangle={this.state.rectangle} {...this.props}> |
76 | | - {this.props.children} |
77 | | - </RectangleCreator> |
78 | | - ); |
79 | | - } else { |
80 | | - return (<noscript />); |
81 | | - } |
82 | | - } |
83 | | -} |
| 133 | + return false; |
| 134 | + }, |
| 135 | +}); |
0 commit comments