/
app.states.ts
117 lines (104 loc) · 3.75 KB
/
app.states.ts
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
import { AppComponent } from './app.component';
import { WelcomeComponent } from './welcome.component';
import { HomeComponent } from './home.component';
import { LoginComponent } from './login.component';
import { Transition } from '@uirouter/core';
/**
* This is the parent state for the entire application.
*
* This state's primary purposes are:
* 1) Shows the outermost chrome (including the navigation and logout for authenticated users)
* 2) Provide a viewport (ui-view) for a substate to plug into
*/
export const appState = {
name: 'app',
redirectTo: 'welcome',
component: AppComponent,
};
/**
* This is the 'welcome' state. It is the default state (as defined by app.js) if no other state
* can be matched to the URL.
*/
export const welcomeState = {
parent: 'app',
name: 'welcome',
url: '/welcome',
component: WelcomeComponent,
};
/**
* This is a home screen for authenticated users.
*
* It shows giant buttons which activate their respective submodules: Messages, Contacts, Preferences
*/
export const homeState = {
parent: 'app',
name: 'home',
url: '/home',
component: HomeComponent,
};
/**
* This is the login state. It is activated when the user navigates to /login, or if a unauthenticated
* user attempts to access a protected state (or substate) which requires authentication. (see routerhooks/requiresAuth.js)
*
* It shows a fake login dialog and prompts the user to authenticate. Once the user authenticates, it then
* reactivates the state that the user originally came from.
*/
export const loginState = {
parent: 'app',
name: 'login',
url: '/login',
component: LoginComponent,
resolve: [
{ token: 'returnTo', deps: [Transition], resolveFn: returnTo },
]
};
/**
* A resolve function for 'login' state which figures out what state to return to, after a successful login.
*
* If the user was initially redirected to login state (due to the requiresAuth redirect), then return the toState/params
* they were redirected from. Otherwise, if they transitioned directly, return the fromState/params. Otherwise
* return the main "home" state.
*/
export function returnTo ($transition$: Transition): any {
if ($transition$.redirectedFrom() != null) {
// The user was redirected to the login state (e.g., via the requiresAuth hook when trying to activate contacts)
// Return to the original attempted target state (e.g., contacts)
return $transition$.redirectedFrom().targetState();
}
const $state = $transition$.router.stateService;
// The user was not redirected to the login state; they directly activated the login state somehow.
// Return them to the state they came from.
if ($transition$.from().name !== '') {
return $state.target($transition$.from(), $transition$.params('from'));
}
// If the fromState's name is empty, then this was the initial transition. Just return them to the home state
return $state.target('home');
}
// This future state is a placeholder for all the lazy loaded Contacts states
// The Contacts NgModule isn't loaded until a Contacts link is activated
export const contactsFutureState = {
name: 'contacts.**',
url: '/contacts',
loadChildren: './contacts/contacts.module#ContactsModule'
};
// This future state is a placeholder for the lazy loaded Prefs states
export const prefsFutureState = {
name: 'prefs.**',
url: '/prefs',
loadChildren: './prefs/prefs.module#PrefsModule'
};
// This future state is a placeholder for the lazy loaded My Messages feature module
export const mymessagesFutureState = {
name: 'mymessages.**',
url: '/mymessages',
loadChildren: './mymessages/mymessages.module#MymessagesModule'
};
export const APP_STATES = [
appState,
welcomeState,
homeState,
loginState,
contactsFutureState,
prefsFutureState,
mymessagesFutureState,
];