|
| 1 | +/* global google */ |
| 2 | +import _ from "lodash"; |
| 3 | + |
| 4 | +import invariant from "invariant"; |
| 5 | + |
1 | 6 | import {
|
2 | 7 | default as React,
|
3 |
| - Component, |
4 | 8 | PropTypes,
|
| 9 | + Children, |
5 | 10 | } from "react";
|
6 | 11 |
|
7 | 12 | import {
|
8 |
| - default as canUseDOM, |
9 |
| -} from "can-use-dom"; |
| 13 | + render, |
| 14 | + unmountComponentAtNode, |
| 15 | +} from "react-dom"; |
10 | 16 |
|
11 | 17 | import {
|
12 |
| - default as InfoWindowCreator, |
13 |
| - infoWindowDefaultPropTypes, |
14 |
| - infoWindowControlledPropTypes, |
15 |
| - infoWindowEventPropTypes, |
16 |
| -} from "./creators/InfoWindowCreator"; |
17 |
| - |
18 |
| -import { default as GoogleMapHolder } from "./creators/GoogleMapHolder"; |
19 |
| - |
20 |
| -export default class InfoWindow extends Component { |
21 |
| - static propTypes = { |
22 |
| - // Uncontrolled default[props] - used only in componentDidMount |
23 |
| - ...infoWindowDefaultPropTypes, |
24 |
| - // Controlled [props] - used in componentDidMount/componentDidUpdate |
25 |
| - ...infoWindowControlledPropTypes, |
26 |
| - // Event [onEventName] |
27 |
| - ...infoWindowEventPropTypes, |
28 |
| - } |
| 18 | + MAP, |
| 19 | + ANCHOR, |
| 20 | + INFO_WINDOW, |
| 21 | +} from "./constants"; |
29 | 22 |
|
30 |
| - static contextTypes = { |
31 |
| - mapHolderRef: PropTypes.instanceOf(GoogleMapHolder), |
32 |
| - } |
| 23 | +import { |
| 24 | + addDefaultPrefixToPropTypes, |
| 25 | + collectUncontrolledAndControlledProps, |
| 26 | + default as enhanceElement, |
| 27 | +} from "./enhanceElement"; |
33 | 28 |
|
34 |
| - // Public APIs |
| 29 | +const controlledPropTypes = { |
| 30 | + // NOTICE!!!!!! |
35 | 31 | //
|
36 |
| - // https://developers.google.com/maps/documentation/javascript/3.exp/reference#InfoWindow |
| 32 | + // Only expose those with getters & setters in the table as controlled props. |
37 | 33 | //
|
| 34 | + // [].map.call($0.querySelectorAll("tr>td>code", function(it){ return it.textContent; }) |
| 35 | + // .filter(function(it){ return it.match(/^set/) && !it.match(/^setMap/); }) |
| 36 | + // |
| 37 | + // https://developers.google.com/maps/documentation/javascript/3.exp/reference#InfoWindow |
| 38 | + children: PropTypes.element, |
| 39 | + options: PropTypes.object, |
| 40 | + position: PropTypes.any, |
| 41 | + zIndex: PropTypes.number, |
| 42 | +}; |
| 43 | + |
| 44 | +const defaultUncontrolledPropTypes = addDefaultPrefixToPropTypes(controlledPropTypes); |
| 45 | + |
| 46 | +const eventMap = { |
| 47 | + // https://developers.google.com/maps/documentation/javascript/3.exp/reference#InfoWindow |
38 | 48 | // [].map.call($0.querySelectorAll("tr>td>code"), function(it){ return it.textContent; })
|
39 |
| - // .filter(function(it){ return it.match(/^get/) && !it.match(/^getMap/); }) |
40 |
| - getContent() { /* TODO: children */ } |
| 49 | + onCloseClick: `closeclick`, |
| 50 | + |
| 51 | + onContentChanged: `content_changed`, |
41 | 52 |
|
42 |
| - getPosition() { return this.state.infoWindow.getPosition(); } |
| 53 | + onDomReady: `domready`, |
43 | 54 |
|
44 |
| - getZIndex() { return this.state.infoWindow.getZIndex(); } |
45 |
| - // END - Public APIs |
| 55 | + onPositionChanged: `position_changed`, |
| 56 | + |
| 57 | + onZIndexChanged: `zindex_changed`, |
| 58 | +}; |
| 59 | + |
| 60 | +const publicMethodMap = { |
| 61 | + // Public APIs |
46 | 62 | //
|
47 | 63 | // https://developers.google.com/maps/documentation/javascript/3.exp/reference#InfoWindow
|
| 64 | + // |
| 65 | + // [].map.call($0.querySelectorAll("tr>td>code"), function(it){ return it.textContent; }) |
| 66 | + // .filter(function(it){ return it.match(/^get/) && !it.match(/Map$/); }) |
| 67 | + getPosition(infoWindow) { return infoWindow.getPosition(); }, |
48 | 68 |
|
49 |
| - state = { |
| 69 | + getZIndex(infoWindow) { return infoWindow.getZIndex(); }, |
| 70 | + // END - Public APIs |
| 71 | +}; |
| 72 | + |
| 73 | +const controlledPropUpdaterMap = { |
| 74 | + children(infoWindow, children) { |
| 75 | + render(Children.only(children), infoWindow.getContent()); |
| 76 | + }, |
| 77 | + options(infoWindow, options) { infoWindow.setOptions(options); }, |
| 78 | + position(infoWindow, position) { infoWindow.setPosition(position); }, |
| 79 | + zIndex(infoWindow, zIndex) { infoWindow.setZIndex(zIndex); }, |
| 80 | +}; |
| 81 | + |
| 82 | +function getInstanceFromComponent(component) { |
| 83 | + return component.state[INFO_WINDOW]; |
| 84 | +} |
| 85 | + |
| 86 | +function openInfoWindow(context, infoWindow) { |
| 87 | + const map = context[MAP]; |
| 88 | + const anchor = context[ANCHOR]; |
| 89 | + if (anchor) { |
| 90 | + infoWindow.open(map, anchor); |
| 91 | + } else if (infoWindow.getPosition()) { |
| 92 | + infoWindow.open(map); |
| 93 | + } else { |
| 94 | + invariant(false, |
| 95 | +`You must provide either an anchor (typically a <Marker>) or a position for <InfoWindow>.` |
| 96 | + ); |
50 | 97 | }
|
| 98 | +} |
51 | 99 |
|
52 |
| - componentWillMount() { |
53 |
| - const { mapHolderRef } = this.context; |
| 100 | +export default _.flowRight( |
| 101 | + React.createClass, |
| 102 | + enhanceElement(getInstanceFromComponent, publicMethodMap, eventMap, controlledPropUpdaterMap), |
| 103 | +)({ |
| 104 | + displayName: `InfoWindow`, |
54 | 105 |
|
55 |
| - if (!canUseDOM) { |
56 |
| - return; |
57 |
| - } |
58 |
| - const infoWindow = InfoWindowCreator._createInfoWindow({ |
59 |
| - ...this.props, |
60 |
| - mapHolderRef, |
| 106 | + propTypes: { |
| 107 | + ...controlledPropTypes, |
| 108 | + ...defaultUncontrolledPropTypes, |
| 109 | + }, |
| 110 | + |
| 111 | + contextTypes: { |
| 112 | + [MAP]: PropTypes.object, |
| 113 | + [ANCHOR]: PropTypes.object, |
| 114 | + }, |
| 115 | + |
| 116 | + getInitialState() { |
| 117 | + const map = this.context[MAP]; |
| 118 | + // https://developers.google.com/maps/documentation/javascript/3.exp/reference#InfoWindow |
| 119 | + const infoWindow = new google.maps.InfoWindow({ |
| 120 | + map, |
| 121 | + ...collectUncontrolledAndControlledProps( |
| 122 | + defaultUncontrolledPropTypes, |
| 123 | + controlledPropTypes, |
| 124 | + this.props |
| 125 | + ), |
| 126 | + // Override props of ReactElement type |
| 127 | + content: document.createElement(`div`), |
| 128 | + children: undefined, |
61 | 129 | });
|
| 130 | + openInfoWindow(this.context, infoWindow); |
| 131 | + return { |
| 132 | + [INFO_WINDOW]: infoWindow, |
| 133 | + }; |
| 134 | + }, |
62 | 135 |
|
63 |
| - this.setState({ infoWindow }); |
64 |
| - } |
| 136 | + componentDidMount() { |
| 137 | + const infoWindow = getInstanceFromComponent(this); |
| 138 | + controlledPropUpdaterMap.children(infoWindow, this.props.children); |
| 139 | + }, |
65 | 140 |
|
66 |
| - render() { |
67 |
| - if (this.state.infoWindow) { |
68 |
| - return ( |
69 |
| - <InfoWindowCreator infoWindow={this.state.infoWindow} {...this.props}> |
70 |
| - {this.props.children} |
71 |
| - </InfoWindowCreator> |
72 |
| - ); |
73 |
| - } else { |
74 |
| - return (<noscript />); |
| 141 | + componentWillReceiveProps(nextProps, nextContext) { |
| 142 | + const anchorChanged = this.context[ANCHOR] !== nextContext[ANCHOR]; |
| 143 | + if (anchorChanged) { |
| 144 | + const infoWindow = getInstanceFromComponent(this); |
| 145 | + openInfoWindow(nextContext, infoWindow); |
75 | 146 | }
|
76 |
| - } |
77 |
| -} |
| 147 | + }, |
| 148 | + |
| 149 | + componentWillUnmount() { |
| 150 | + const infoWindow = getInstanceFromComponent(this); |
| 151 | + if (infoWindow) { |
| 152 | + unmountComponentAtNode(infoWindow.getContent()); |
| 153 | + infoWindow.setMap(null); |
| 154 | + } |
| 155 | + }, |
| 156 | + |
| 157 | + render() { |
| 158 | + return false; |
| 159 | + }, |
| 160 | +}); |
0 commit comments