This repository has been archived by the owner on Dec 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
124 lines (99 loc) · 2.86 KB
/
index.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
119
120
121
122
123
124
import { React, Inferno, Component } from './infact'
import PropTypes from 'prop-types'
import pin from './img/pin.png'
import pinRetina from './img/pin@2x.png'
import pinHover from './img/pin-hover.png'
import pinHoverRetina from './img/pin-hover@2x.png'
const imageOffset = {
left: 15,
top: 31
}
export default class Marker extends Component {
static propTypes = process.env.BABEL_ENV === 'inferno' ? {} : {
// input, passed to events
anchor: PropTypes.array.isRequired,
payload: PropTypes.any,
// optional modifiers
hover: PropTypes.bool,
// callbacks
onClick: PropTypes.func,
onContextMenu: PropTypes.func,
onMouseOver: PropTypes.func,
onMouseOut: PropTypes.func,
// pigeon variables
left: PropTypes.number,
top: PropTypes.number,
// pigeon functions
latLngToPixel: PropTypes.func,
pixelToLatLng: PropTypes.func
}
constructor (props) {
super(props)
this.state = {
hover: false
}
}
// what do you expect to get back with the event
eventParameters = (event) => ({
event,
anchor: this.props.anchor,
payload: this.props.payload
})
// controls
isRetina () {
return typeof window !== 'undefined' && window.devicePixelRatio >= 2
}
// modifiers
isHover () {
return typeof this.props.hover === 'boolean' ? this.props.hover : this.state.hover
}
image () {
return this.isRetina() ? (this.isHover() ? pinHoverRetina : pinRetina) : (this.isHover() ? pinHover : pin)
}
// lifecycle
componentDidMount () {
let images = this.isRetina() ? [
pinRetina, pinHoverRetina
] : [
pin, pinHover
]
images.forEach(image => {
let img = new window.Image()
img.src = image
})
}
// delegators
handleClick = (event) => {
this.props.onClick && this.props.onClick(this.eventParameters(event))
}
handleContextMenu = (event) => {
this.props.onContextMenu && this.props.onContextMenu(this.eventParameters(event))
}
handleMouseOver = (event) => {
this.props.onMouseOver && this.props.onMouseOver(this.eventParameters(event))
this.setState({ hover: true })
}
handleMouseOut = (event) => {
this.props.onMouseOut && this.props.onMouseOut(this.eventParameters(event))
this.setState({ hover: false })
}
// render
render () {
const { left, top, onClick } = this.props
const style = {
position: 'absolute',
transform: `translate(${left - imageOffset.left}px, ${top - imageOffset.top}px)`,
cursor: onClick ? 'pointer' : 'default'
}
return (
<div style={style}
className='pigeon-click-block'
onClick={this.handleClick}
onContextMenu={this.handleContextMenu}
onMouseOver={this.handleMouseOver}
onMouseOut={this.handleMouseOut}>
<img src={this.image()} width={29} height={34} alt='' />
</div>
)
}
}