-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsceneLifecircleMixin.js
135 lines (98 loc) · 3.85 KB
/
sceneLifecircleMixin.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
125
126
127
128
129
130
131
132
133
134
135
'use strict';
/*
* SceneFocusMixin
* Scene life Circle
*
* Cases:
* onEnterance - Fired at "former to current"
* onBackIn - Fired at "latter to current"
* onWillFocus - Fired at "other to current",two cases above covered.
* onDidFocus - Fired at current scene active(after animation).
* onbackwardblur - Fired at "current will jump to latter"
* onForwardBlur - Fired at "current will jump to former"
* onWillBlur - Fired at "current will blur",two cases above covered.
* onDidBlur - Fired at "current to others" animation done.Before this.componentDidUnmounte
*/
var SceneFocusMixin = {
componentWillMount:function(){
var { route, navigator, routeIndex } = this.props;
var { navigationContext } = navigator;
var { index } = route;
if( !route || !navigator ) return;
// if( navigator.didFocusAdded ) return;
this._sceneDidfocusListener = (
navigationContext.addListener('didfocus',( event ) => {
var routeStack = navigator.getCurrentRoutes();
var targetRoute = event.data.route;
var targetIndex = route.index;
var routeStackLength = routeStack.length;
/*
Based on route & routeStack relationship.
this.onEnterance : enter from former one.
this.onBackIn : enter from latter one.
this.onDidFocus : fired when active.
*/
if( targetRoute == route && routeStackLength - 1 == targetIndex ){
// Focus from front view
navigationContext.emit( 'didblur',{ route:routeStack[ routeStackLength - 2 ] } )
this.onEnterance && this.onEnterance( event, navigator, route, targetIndex );
this.onDidFocus && this.onDidFocus( event, navigator, route, targetIndex )
}
if( targetRoute == route && routeStackLength - 2 == targetIndex ){
// Focus from after view
this.onBackIn && this.onBackIn( event, navigator, route, targetIndex );
this.onDidFocus && this.onDidFocus( event, navigator, route, targetIndex )
}
})
)
this._sceneWillfocusListener = (
navigationContext.addListener('willfocus', ( event ) => {
var routeStack = navigator.getCurrentRoutes();
var targetRoute = event.data.route;
var targetIndex = route.index;
// var { targetIndex } = routeStack.filter( ( route, index ) => { if( route == targetRoute ){ return route.targetIndex = index } })
var routeStackLength = routeStack.length;
if( route == targetRoute ){
// Will focus this view
this.onWillFocus && this.onWillFocus();
}
/*
Based on route & routeStack relationship.
this.onBackwardBlur : Jump to other scene.Before animation.
this.onForwardBlur : Back to other scene.Before animation.
this.onWillBlur : Fired while leave current scene.
*/
if( route == routeStack[ routeStackLength - 1 ] && route != targetRoute ){
if( targetRoute == routeStack[ routeStackLength -2 ]){
// Will unmount
this.onBackwardBlur && this.onBackwardBlur( event, navigator, route, targetIndex )
} else {
// Will blur this view
this.onForwardBlur && this.onForwardBlur( event, navigator, route, targetIndex )
}
this.onWillBlur && this.onWillBlur();
}
})
)
this._sceneDidBlurListener = (
navigationContext.addListener('didblur',( event ) => {
var routeStack = navigator.getCurrentRoutes();
var targetRoute = event.data.route;
var targetIndex = route.index;
if( route == targetRoute ){
// will blur this view
this.onDidBlur && this.onDidBlur( event, navigator, route, targetIndex );
}
})
)
},
componentWillUnmount:function(){
this._removeSceneListeners()
},
_removeSceneListeners:function(){
this._sceneDidfocusListener && this._sceneDidfocusListener.remove();
this._sceneWillfocusListener && this._sceneWillfocusListener.remove();
this._sceneDidBlurListener && this._sceneDidBlurListener.remove();
}
}
module.exports = SceneFocusMixin;