-
Notifications
You must be signed in to change notification settings - Fork 927
Expand file tree
/
Copy pathMapChildHelper.js
More file actions
94 lines (85 loc) · 2.16 KB
/
Copy pathMapChildHelper.js
File metadata and controls
94 lines (85 loc) · 2.16 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
/* global google */
/* eslint-disable no-param-reassign */
import _ from "lodash"
function rdcUncontrolledAndControlledProps(acc, value, key) {
if (_.has(acc.prevProps, key)) {
const match = key.match(/^default(\S+)/)
if (match) {
const unprefixedKey = _.lowerFirst(match[1])
if (!_.has(acc.nextProps, unprefixedKey)) {
acc.nextProps[unprefixedKey] = acc.prevProps[key]
}
} else {
acc.nextProps[key] = acc.prevProps[key]
}
}
return acc
}
function applyUpdaterToNextProps(updaterMap, prevProps, nextProps, instance) {
_.forEach(updaterMap, (fn, key) => {
const nextValue = nextProps[key]
if (nextValue !== prevProps[key]) {
fn(instance, nextValue)
}
})
}
export function construct(propTypes, updaterMap, prevProps, instance) {
const { nextProps } = _.reduce(propTypes, rdcUncontrolledAndControlledProps, {
nextProps: {},
prevProps,
})
applyUpdaterToNextProps(
updaterMap,
{
/* empty prevProps for construct */
},
nextProps,
instance
)
}
export function componentDidMount(component, instance, eventMap) {
registerEvents(component, instance, eventMap)
}
export function componentDidUpdate(
component,
instance,
eventMap,
updaterMap,
prevProps
) {
component.unregisterAllEvents()
applyUpdaterToNextProps(updaterMap, prevProps, component.props, instance)
registerEvents(component, instance, eventMap)
}
export function componentWillUnmount(component) {
component.unregisterAllEvents()
}
function registerEvents(component, instance, eventMap) {
const registeredList = _.reduce(
eventMap,
(acc, googleEventName, onEventName) => {
if (_.isFunction(component.props[onEventName])) {
acc.push(
google.maps.event.addListener(
instance,
googleEventName,
function(mouseEvent){
component.props[onEventName](mouseEvent)
}
)
)
}
return acc
},
[]
)
component.unregisterAllEvents = _.bind(
_.forEach,
null,
registeredList,
unregisterEvent
)
}
function unregisterEvent(registered) {
google.maps.event.removeListener(registered)
}