-
Notifications
You must be signed in to change notification settings - Fork 314
/
main_map_block.jsx
129 lines (111 loc) · 4.01 KB
/
main_map_block.jsx
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
import React, {PropTypes, Component} from 'react/addons';
import controllable from 'react-controllables';
import shouldPureComponentUpdate from 'react-pure-render/function';
import GoogleMap from 'google-map-react';
import MarkerExample, {K_SCALE_NORMAL} from './marker_example.jsx';
import {getScale, getRealFromTo} from '../helpers/calc_markers_visibility.js';
import markerDescriptions from '../constants/marker_descriptions.js';
import {customDistanceToMouse} from '../helpers/custom_distance.js';
import {List} from 'immutable';
const K_MARGIN_TOP = 30;
const K_MARGIN_RIGHT = 30;
const K_MARGIN_BOTTOM = 30;
const K_MARGIN_LEFT = 30;
const K_HOVER_DISTANCE = 30;
@controllable(['center', 'zoom', 'markers'])
export default class MainMapBlock extends Component {
static propTypes = {
onCenterChange: PropTypes.func, // @controllable generated fn
onZoomChange: PropTypes.func, // @controllable generated fn
onBoundsChange: PropTypes.func,
onMarkerHover: PropTypes.func,
onChildClick: PropTypes.func,
center: PropTypes.any,
zoom: PropTypes.number,
markers: PropTypes.any,
visibleRowFirst: PropTypes.number,
visibleRowLast: PropTypes.number,
maxVisibleRows: PropTypes.number,
hoveredRowIndex: PropTypes.number,
openBallonIndex: PropTypes.number
}
static defaultProps = {
center: new List([59.744465, 30.042834]),
zoom: 10,
visibleRowFirst: -1,
visibleRowLast: -1,
hoveredRowIndex: -1
}
shouldComponentUpdate = shouldPureComponentUpdate;
constructor(props) {
super(props);
}
_onBoundsChange = (center, zoom, bounds, marginBounds) => {
if (this.props.onBoundsChange) {
this.props.onBoundsChange({center, zoom, bounds, marginBounds});
} else {
this.props.onCenterChange(center);
this.props.onZoomChange(zoom);
}
}
_onChildClick = (key, childProps) => {
const markerId = childProps.marker.get('id');
const index = this.props.markers.findIndex(m => m.get('id') === markerId);
if (this.props.onChildClick) {
this.props.onChildClick(index);
}
}
_onChildMouseEnter = (key, childProps) => {
const markerId = childProps.marker.get('id');
const index = this.props.markers.findIndex(m => m.get('id') === markerId);
if (this.props.onMarkerHover) {
this.props.onMarkerHover(index);
}
}
_onChildMouseLeave = (/* key, childProps */) => {
if (this.props.onMarkerHover) {
this.props.onMarkerHover(-1);
}
}
_onBalloonCloseClick = () => {
if (this.props.onChildClick) {
this.props.onChildClick(-1);
}
}
_distanceToMouse = customDistanceToMouse;
render() {
const {rowFrom, rowTo} = getRealFromTo(this.props.visibleRowFirst, this.props.visibleRowLast, this.props.maxVisibleRows, this.props.markers.size);
const Markers = this.props.markers &&
this.props.markers.filter((m, index) => index >= rowFrom && index <= rowTo)
.map((marker, index) => (
<MarkerExample
// required props
key={marker.get('id')}
lat={marker.get('lat')}
lng={marker.get('lng')}
// any user props
showBallon={index + rowFrom === this.props.openBallonIndex}
onCloseClick={this._onBalloonCloseClick}
hoveredAtTable={index + rowFrom === this.props.hoveredRowIndex}
scale={getScale(index + rowFrom, this.props.visibleRowFirst, this.props.visibleRowLast, K_SCALE_NORMAL)}
{...markerDescriptions[marker.get('type')]}
marker={marker} />
));
return (
<GoogleMap
// apiKey={null}
center={this.props.center.toJS()}
zoom={this.props.zoom}
onBoundsChange={this._onBoundsChange}
onChildClick={this._onChildClick}
onChildMouseEnter={this._onChildMouseEnter}
onChildMouseLeave={this._onChildMouseLeave}
margin={[K_MARGIN_TOP, K_MARGIN_RIGHT, K_MARGIN_BOTTOM, K_MARGIN_LEFT]}
hoverDistance={K_HOVER_DISTANCE}
distanceToMouse={this._distanceToMouse}
>
{Markers}
</GoogleMap>
);
}
}