-
Notifications
You must be signed in to change notification settings - Fork 925
Expand file tree
/
Copy pathOverlayView.jsx
More file actions
185 lines (163 loc) · 5.13 KB
/
OverlayView.jsx
File metadata and controls
185 lines (163 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* global google */
import _ from "lodash"
import invariant from "invariant"
import React from "react"
import ReactDOM from "react-dom"
import PropTypes from "prop-types"
import {
componentDidMount,
componentDidUpdate,
componentWillUnmount,
} from "../utils/MapChildHelper"
import { getOffsetOverride, getLayoutStyles } from "../utils/OverlayViewHelper"
import { MAP, ANCHOR, OVERLAY_VIEW } from "../constants"
export const __jscodeshiftPlaceholder__ = `{
"eventMapOverrides": {
},
"getInstanceFromComponent": "this.state[OVERLAY_VIEW]"
}`
/**
* A wrapper around `google.maps.OverlayView`
*
* @see https://developers.google.com/maps/documentation/javascript/3.exp/reference#OverlayView
*/
export class OverlayView extends React.PureComponent {
static FLOAT_PANE = `floatPane`
static MAP_PANE = `mapPane`
static MARKER_LAYER = `markerLayer`
static OVERLAY_LAYER = `overlayLayer`
static OVERLAY_MOUSE_TARGET = `overlayMouseTarget`
static propTypes = {
__jscodeshiftPlaceholder__: null,
/**
* @see https://developers.google.com/maps/documentation/javascript/3.exp/reference#OverlayView
*/
mapPaneName: PropTypes.string,
/**
* @see https://developers.google.com/maps/documentation/javascript/3.exp/reference#OverlayView
*/
position: PropTypes.object,
/**
* @see https://developers.google.com/maps/documentation/javascript/3.exp/reference#OverlayView
*/
bounds: PropTypes.object,
/**
* @see https://developers.google.com/maps/documentation/javascript/3.exp/reference#OverlayView
*/
children: PropTypes.node.isRequired,
/**
* @see https://developers.google.com/maps/documentation/javascript/3.exp/reference#OverlayView
*/
getPixelPositionOffset: PropTypes.func,
}
static contextTypes = {
[MAP]: PropTypes.object,
[ANCHOR]: PropTypes.object,
}
/*
* @see https://developers.google.com/maps/documentation/javascript/3.exp/reference#OverlayView
*/
constructor(props, context) {
super(props, context)
const overlayView = new google.maps.OverlayView()
// You must implement three methods: onAdd(), draw(), and onRemove().
overlayView.onAdd = _.bind(this.onAdd, this)
overlayView.draw = _.bind(this.draw, this)
overlayView.onRemove = _.bind(this.onRemove, this)
this.onPositionElement = _.bind(this.onPositionElement, this)
// You must call setMap() with a valid Map object to trigger the call to
// the onAdd() method and setMap(null) in order to trigger the onRemove() method.
overlayView.setMap(this.context[MAP])
this.state = {
[OVERLAY_VIEW]: overlayView,
}
}
onAdd() {
this.containerElement = document.createElement(`div`)
this.containerElement.style.position = `absolute`
}
draw() {
const { mapPaneName } = this.props
invariant(
!!mapPaneName,
`OverlayView requires either props.mapPaneName or props.defaultMapPaneName but got %s`,
mapPaneName
)
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#MapPanes
const mapPanes = this.state[OVERLAY_VIEW].getPanes()
mapPanes[mapPaneName].appendChild(this.containerElement)
if (React.version.match(/^16/)) {
this.onPositionElement()
this.forceUpdate()
} else {
ReactDOM.unstable_renderSubtreeIntoContainer(
this,
React.Children.only(this.props.children),
this.containerElement,
this.onPositionElement
)
}
}
onPositionElement() {
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#MapCanvasProjection
const mapCanvasProjection = this.state[OVERLAY_VIEW].getProjection()
const offset = {
x: 0,
y: 0,
...getOffsetOverride(this.containerElement, this.props),
}
const layoutStyles = getLayoutStyles(
mapCanvasProjection,
offset,
this.props
)
_.assign(this.containerElement.style, layoutStyles)
}
onRemove() {
if (this.containerElement) {
this.containerElement.parentNode.removeChild(this.containerElement)
}
if (!React.version.match(/^16/)) {
ReactDOM.unmountComponentAtNode(this.containerElement)
}
this.containerElement = null
}
componentDidMount() {
componentDidMount(this, this.state[OVERLAY_VIEW], eventMap)
}
componentDidUpdate(prevProps) {
componentDidUpdate(
this,
this.state[OVERLAY_VIEW],
eventMap,
updaterMap,
prevProps
)
if (!React.version.match(/^16/)) {
_.delay(this.state[OVERLAY_VIEW].draw)
}
}
componentWillUnmount() {
componentWillUnmount(this)
const overlayView = this.state[OVERLAY_VIEW]
if (overlayView) {
overlayView.setMap(null)
// You must implement three methods: onAdd(), draw(), and onRemove().
overlayView.onAdd = null
overlayView.draw = null
overlayView.onRemove = null
}
}
render() {
if (React.version.match(/^16/) && this.containerElement) {
return ReactDOM.createPortal(
React.Children.only(this.props.children),
this.containerElement
)
}
return false
}
}
export default OverlayView
const eventMap = {}
const updaterMap = {}