-
Notifications
You must be signed in to change notification settings - Fork 807
Expand file tree
/
Copy pathInfoWindow.js
More file actions
122 lines (98 loc) · 2.38 KB
/
InfoWindow.js
File metadata and controls
122 lines (98 loc) · 2.38 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
import React from 'react'
import PropTypes from 'prop-types'
import ReactDOM from 'react-dom'
import ReactDOMServer from 'react-dom/server'
export class InfoWindow extends React.Component {
componentDidMount() {
this.renderInfoWindow();
}
componentDidUpdate(prevProps) {
const {google, map} = this.props;
if (!google || !map) {
return;
}
if (map !== prevProps.map) {
this.renderInfoWindow();
}
if (this.props.position !== prevProps.position) {
this.updatePosition();
}
if (this.props.children !== prevProps.children) {
this.updateContent();
}
if ((this.props.visible !== prevProps.visible ||
this.props.marker !== prevProps.marker ||
this.props.position !== prevProps.position)) {
this.props.visible ?
this.openWindow() :
this.closeWindow();
}
}
renderInfoWindow() {
const {
map,
google,
mapCenter,
...props
} = this.props;
if (!google || !google.maps) {
return;
}
const iw = this.infowindow = new google.maps.InfoWindow({
content: '',
...props
});
google.maps.event
.addListener(iw, 'closeclick', this.onClose.bind(this))
google.maps.event
.addListener(iw, 'domready', this.onOpen.bind(this));
}
onOpen() {
if (this.props.onOpen) {
this.props.onOpen(this.props);
}
}
onClose() {
if (this.props.onClose) {
this.props.onClose(this.props);
}
}
openWindow() {
this.infowindow.open(this.props.map, this.props.marker);
}
updatePosition() {
let pos = this.props.position;
if (!(pos instanceof google.maps.LatLng)) {
pos = pos && new google.maps.LatLng(pos.lat, pos.lng);
}
this.infowindow.setPosition(pos);
}
updateContent() {
const content = this.renderChildren();
this.infowindow.setContent(content);
}
closeWindow() {
this.infowindow.close();
}
renderChildren() {
const {children} = this.props;
return ReactDOMServer.renderToString(children);
}
render() {
return null;
}
}
InfoWindow.propTypes = {
children: PropTypes.element.isRequired,
map: PropTypes.object,
marker: PropTypes.object,
position: PropTypes.object,
visible: PropTypes.bool,
// callbacks
onClose: PropTypes.func,
onOpen: PropTypes.func
}
InfoWindow.defaultProps = {
visible: false
}
export default InfoWindow