-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathonEnterExitRetain.ts
36 lines (33 loc) · 1.31 KB
/
onEnterExitRetain.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
/** @publicapi @module ng1 */ /** */
import {
StateObject,
TransitionStateHookFn,
HookResult,
Transition,
services,
ResolveContext,
extend,
} from '@uirouter/core';
import { getLocals } from '../services';
import { Ng1StateDeclaration } from '../interface';
/**
* This is a [[StateBuilder.builder]] function for angular1 `onEnter`, `onExit`,
* `onRetain` callback hooks on a [[Ng1StateDeclaration]].
*
* When the [[StateBuilder]] builds a [[StateObject]] object from a raw [[StateDeclaration]], this builder
* ensures that those hooks are injectable for @uirouter/angularjs (ng1).
*
* @internalapi
*/
export const getStateHookBuilder = (hookName: 'onEnter' | 'onExit' | 'onRetain') =>
function stateHookBuilder(stateObject: StateObject): TransitionStateHookFn {
const hook = stateObject[hookName];
const pathname = hookName === 'onExit' ? 'from' : 'to';
function decoratedNg1Hook(trans: Transition, state: Ng1StateDeclaration): HookResult {
const resolveContext = new ResolveContext(trans.treeChanges(pathname));
const subContext = resolveContext.subContext(state.$$state());
const locals = extend(getLocals(subContext), { $state$: state, $transition$: trans });
return services.$injector.invoke(hook, this, locals);
}
return hook ? decoratedNg1Hook : undefined;
};