This repository has been archived by the owner on Feb 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathExNavigationContext.js
208 lines (181 loc) · 5.29 KB
/
ExNavigationContext.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* @flow
*/
import invariant from 'invariant';
import Actions from './ExNavigationActions';
import createNavigationStore, { batchNavigationActions } from './ExNavigationStore';
// import ExNavigationVisibilityManager from 'ExNavigationVisibilityManager';
import type ExNavigatorContext from './ExNavigatorContext';
import type {
ExNavigationRoute,
ExNavigationRouter,
} from './ExNavigationRouter';
import type {
ExNavigationStore,
ExNavigationState,
} from './ExNavigationStore';
/**
* Represents
*/
export default class NavigationContext {
_store: ExNavigationStore;
_router: ExNavigationRouter<*>;
_navigatorContexts: {
[navigatorUID: string]: ExNavigatorContext<*>
};
// _visiblityManager: ExNavigationVisibilityManager;
registerNavigatorContext: (navigatorUID: string, navigatorContext: ExNavigatorContext<*>) => void;
constructor({ store, router }: { store?: ExNavigationStore, router: ExNavigationRouter<*> }) {
if (store == null) {
store = createNavigationStore();
}
this._store = store;
this._router = router;
this._navigatorContexts = {};
}
getNavigator(navigatorId: string): ExNavigatorContext<*> {
let navigatorContext;
const contextKeys = Object.keys(this._navigatorContexts);
contextKeys.forEach(contextKey => {
let c = this._navigatorContexts[contextKey];
if (c.navigatorId === navigatorId) {
if (!navigatorContext) {
navigatorContext = c;
} else {
throw new Error(`More than one navigator exists with id '${navigatorId}'. Please access the navigator context using 'getNavigatorByUID'.`);
}
}
});
invariant(navigatorContext, 'Navigator does not exist.');
return navigatorContext;
}
/**
* Returns the NavigatorContext for the specified navigator.
*/
getNavigatorByUID(navigatorUID: string): ExNavigatorContext<*> {
// return the NavigatorContext for navigatorId
invariant(this._navigatorContexts[navigatorUID], 'Navigator does not exist.');
return this._navigatorContexts[navigatorUID];
}
getCurrentNavigatorUID(): ?string {
const state = this.navigationState;
if (state) {
return state.currentNavigatorUID;
}
return null;
}
getFocusedRoute(): ?ExNavigationRoute {
if (!this.navigationState) {
return null;
}
const currentNavigator = this.navigationState.navigators[this.navigationState.currentNavigatorUID];
if (!currentNavigator) {
return null;
}
return currentNavigator.routes[currentNavigator.index];
}
registerNavigatorContext = (navigatorUID: string, navigatorContext: ExNavigatorContext<*>) => {
this._navigatorContexts[navigatorUID] = navigatorContext;
}
unregisterNavigatorContext = (navigatorUID: string) => {
delete this._navigatorContexts[navigatorUID];
}
get store(): ExNavigationStore {
return this._store;
}
get navigationStateKey(): string {
return this._store.__exNavigationStateKey;
}
get dispatch(): (action: mixed) => any {
if (!this._store) {
throw new Error('Store is not set on navigation context.');
}
return this._store.dispatch;
}
get router(): ExNavigationRouter<*> {
return this._router;
}
get navigationState(): ?ExNavigationState {
const state = this._store.getState();
if (state == null) {
return null;
}
return state[this._store.__exNavigationStateKey];
}
performAction(actionFn: Function) {
let actions = [];
const stateUtils = {
drawer: (uid) => ({
jumpToItem: (itemId) => {
actions.push(
Actions.jumpToItem(uid, {
key: itemId,
})
);
},
toggleDrawer: () => {
actions.push(
Actions.toggleDrawer(uid)
);
},
}),
tabs: (uid) => ({
jumpToTab: (tabId) => {
actions.push(
Actions.jumpToTab(uid, {
key: tabId,
})
);
},
}),
stacks: (uid) => ({
push: (route) => {
actions.push(
Actions.push(uid, route)
);
},
pop: () => {
actions.push(
Actions.pop(uid)
);
},
popToTop: () => {
actions.push(
Actions.popToTop(uid)
);
},
replace: (route) => {
actions.push(
Actions.replace(uid, route)
);
},
immediatelyResetStack: (routes, index) => {
const mappedChildren = routes.map((route, i) => {
invariant(route !== null && route.key, `Route at index ${i} is null or malformed.`);
return route;
});
actions.push(
Actions.immediatelyResetStack(uid, mappedChildren, index)
);
},
updateCurrentRouteParams: (newParams) => {
actions.push(
Actions.updateCurrentRouteParams(uid, newParams)
);
},
showLocalAlert: (message, options) => {
actions.push(
Actions.showLocalAlert(uid, message, options)
);
},
hideLocalAlert: () => {
actions.push(
Actions.hideLocalAlert(uid)
);
},
}),
};
actionFn(stateUtils);
this.store.dispatch(batchNavigationActions(actions));
}
}