/
zoom-control.js
118 lines (104 loc) · 2.85 KB
/
zoom-control.js
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
import React, { Component, PropTypes } from 'react';
const containerStyle = {
position: 'absolute',
zIndex: 2,
display: 'flex',
flexDirection: 'column',
boxShadow: '0px 1px 4px rgba(0, 0, 0, .3)',
border: '1px solid rgba(0, 0, 0, 0.1)',
};
const positions = {
topRight: { top: 10, right: 10, bottom: 'auto', left: 'auto' },
topLeft: { top: 10, left: 10, bottom: 'auto', right: 'auto' },
bottomRight: { bottom: 10, right: 10, top: 'auto', left: 'auto' },
bottomLeft: { bottom: 10, left: 10, top: 'auto', right: 'auto' },
};
const buttonStyle = {
backgroundColor: '#f9f9f9',
opacity: 0.95,
transition: 'background-color 0.16s ease-out',
cursor: 'pointer',
border: 0,
height: 26,
width: 26,
backgroundImage: "url('https://api.mapbox.com/mapbox.js/v2.4.0/images/icons-000000@2x.png')",
backgroundPosition: '0px 0px',
backgroundSize: '26px 260px',
outline: 0,
};
const buttonStyleHovered = {
backgroundColor: '#fff',
opacity: 1,
};
const buttonStylePlus = {
borderBottom: '1px solid rgba(0, 0, 0, 0.1)',
borderTopLeftRadius: 2,
borderTopRightRadius: 2,
};
const buttonStyleMinus = {
backgroundPosition: '0px -26px',
borderBottomLeftRadius: 2,
borderBottomRightRadius: 2,
};
const [PLUS, MINUS] = [0, 1];
const POSITIONS = Object.keys(positions);
export default class ZoomControl extends Component {
static propTypes = {
zoomDiff: PropTypes.number,
onControlClick: PropTypes.func,
position: PropTypes.oneOf(POSITIONS),
style: PropTypes.object,
};
static defaultProps = {
position: POSITIONS[0],
zoomDiff: 0.5,
onControlClick: (map, zoomDiff) => {
map.setZoom(map.getZoom() + zoomDiff);
},
};
state = {
hover: undefined,
};
static contextTypes = {
map: PropTypes.object,
};
onMouseAction = (hover) => {
if (hover !== this.state.hover) {
this.setState({ hover });
}
};
render() {
const { onControlClick, zoomDiff, position, style } = this.props;
const { hover } = this.state;
const { map } = this.context;
return (
<div
style={{
...containerStyle,
...positions[position],
...style,
}}>
<button
style={{
...buttonStyle,
...buttonStylePlus,
...(hover === PLUS && buttonStyleHovered),
}}
onMouseOver={this.onMouseAction.bind(this, PLUS)}
onMouseOut={this.onMouseAction}
onClick={onControlClick.bind(this, map, zoomDiff)}>
</button>
<button
style={{
...buttonStyle,
...buttonStyleMinus,
...(hover === MINUS && buttonStyleHovered),
}}
onMouseOver={this.onMouseAction.bind(this, MINUS)}
onMouseOut={this.onMouseAction}
onClick={onControlClick.bind(this, map, -zoomDiff)}>
</button>
</div>
);
}
}