|
| 1 | +import { AppComponent } from './app.component'; |
| 2 | +import { WelcomeComponent } from './welcome.component'; |
| 3 | +import { HomeComponent } from './home.component'; |
| 4 | +import { LoginComponent } from './login.component'; |
| 5 | +import { Transition } from 'ui-router-core'; |
| 6 | + |
| 7 | +/** |
| 8 | + * This is the parent state for the entire application. |
| 9 | + * |
| 10 | + * This state's primary purposes are: |
| 11 | + * 1) Shows the outermost chrome (including the navigation and logout for authenticated users) |
| 12 | + * 2) Provide a viewport (ui-view) for a substate to plug into |
| 13 | + */ |
| 14 | +export const appState = { |
| 15 | + name: 'app', |
| 16 | + redirectTo: 'welcome', |
| 17 | + component: AppComponent, |
| 18 | +}; |
| 19 | + |
| 20 | +/** |
| 21 | + * This is the 'welcome' state. It is the default state (as defined by app.js) if no other state |
| 22 | + * can be matched to the URL. |
| 23 | + */ |
| 24 | +export const welcomeState = { |
| 25 | + parent: 'app', |
| 26 | + name: 'welcome', |
| 27 | + url: '/welcome', |
| 28 | + component: WelcomeComponent, |
| 29 | +}; |
| 30 | + |
| 31 | +/** |
| 32 | + * This is a home screen for authenticated users. |
| 33 | + * |
| 34 | + * It shows giant buttons which activate their respective submodules: Messages, Contacts, Preferences |
| 35 | + */ |
| 36 | +export const homeState = { |
| 37 | + parent: 'app', |
| 38 | + name: 'home', |
| 39 | + url: '/home', |
| 40 | + component: HomeComponent, |
| 41 | +}; |
| 42 | + |
| 43 | + |
| 44 | +/** |
| 45 | + * This is the login state. It is activated when the user navigates to /login, or if a unauthenticated |
| 46 | + * user attempts to access a protected state (or substate) which requires authentication. (see routerhooks/requiresAuth.js) |
| 47 | + * |
| 48 | + * It shows a fake login dialog and prompts the user to authenticate. Once the user authenticates, it then |
| 49 | + * reactivates the state that the user originally came from. |
| 50 | + */ |
| 51 | +export const loginState = { |
| 52 | + parent: 'app', |
| 53 | + name: 'login', |
| 54 | + url: '/login', |
| 55 | + component: LoginComponent, |
| 56 | + resolve: { returnTo: returnTo } |
| 57 | +}; |
| 58 | + |
| 59 | +/** |
| 60 | + * A resolve function for 'login' state which figures out what state to return to, after a successful login. |
| 61 | + * |
| 62 | + * If the user was initially redirected to login state (due to the requiresAuth redirect), then return the toState/params |
| 63 | + * they were redirected from. Otherwise, if they transitioned directly, return the fromState/params. Otherwise |
| 64 | + * return the main "home" state. |
| 65 | + */ |
| 66 | +export function returnTo ($transition$: Transition): any { |
| 67 | + if ($transition$.redirectedFrom() != null) { |
| 68 | + // The user was redirected to the login state (e.g., via the requiresAuth hook when trying to activate contacts) |
| 69 | + // Return to the original attempted target state (e.g., contacts) |
| 70 | + return $transition$.redirectedFrom().targetState(); |
| 71 | + } |
| 72 | + |
| 73 | + const $state = $transition$.router.stateService; |
| 74 | + |
| 75 | + // The user was not redirected to the login state; they directly activated the login state somehow. |
| 76 | + // Return them to the state they came from. |
| 77 | + if ($transition$.from().name !== '') { |
| 78 | + return $state.target($transition$.from(), $transition$.params("from")); |
| 79 | + } |
| 80 | + |
| 81 | + // If the fromState's name is empty, then this was the initial transition. Just return them to the home state |
| 82 | + return $state.target('home'); |
| 83 | +} |
| 84 | + |
| 85 | +export const APP_STATES = [ appState, welcomeState, homeState, loginState ]; |
0 commit comments