-
Notifications
You must be signed in to change notification settings - Fork 119
/
RNAppTour.js
105 lines (84 loc) · 2.41 KB
/
RNAppTour.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
import { findNodeHandle, NativeModules } from 'react-native'
const { RNAppTour } = NativeModules
class AppTour {
static ShowSequence(sequence) {
let appTourTargets = sequence.getAll()
let viewIds = new Map(),
sortedViewIds = [],
props = {}
appTourTargets &&
appTourTargets.forEach((appTourTarget, key, appTourTargets) => {
if (
appTourTarget.props.order === undefined ||
appTourTarget.props.order === null
)
throw new Error(
'Each tour target should have a order mandatory props.'
)
viewIds.set(appTourTarget.props.order, appTourTarget.view)
props[appTourTarget.view] = appTourTarget.props
})
let viewOrder = Array.from(viewIds.keys())
viewOrder = viewOrder.sort((a, b) => a - b)
viewOrder.forEach(vOrder => {
sortedViewIds.push(viewIds.get(vOrder))
})
RNAppTour.ShowSequence(sortedViewIds, props)
}
static ShowFor(appTourTarget) {
RNAppTour.ShowFor(appTourTarget.view, appTourTarget.props)
}
}
class AppTourSequence {
constructor() {
this.appTourTargets = new Map()
}
add(appTourTarget) {
this.appTourTargets.set(appTourTarget.key, appTourTarget)
}
remove(appTourTarget) {
this.appTourTargets.delete(appTourTarget.key)
}
removeAll() {
this.appTourTargets = new Map()
}
get(appTourTarget) {
return this.appTourTargets.get(appTourTarget)
}
getAll() {
return this.appTourTargets
}
}
class AppTourView {
static for(view, props) {
if (view === undefined || view === null)
throw new Error(
'Provided tour view reference is undefined or null, please add a preliminary validation before adding for tour.'
)
let useNativeTag = false;
let keyValue = 0;
if (view._reactInternalFiber === undefined ||
view._reactInternalFiber === null) {
useNativeTag = true;
}
if (!useNativeTag) {
if (view._reactInternalFiber.key === undefined ||
view._reactInternalFiber.key === null) {
throw new Error(
'Each tour view should have a key prop. Please check the tour component props.'
)
}
}
if (useNativeTag) {
keyValue = view._nativeTag;
} else {
keyValue = view._reactInternalFiber.key;
}
return {
key: keyValue,
view: findNodeHandle(view),
props: props
}
}
}
export { AppTourView, AppTourSequence, AppTour }