|
| 1 | +import {Directive, Output, EventEmitter} from "angular2/core"; |
| 2 | +import {StateService} from "../state/stateService"; |
| 3 | +import {UiSref} from "./uiSref"; |
| 4 | +import {UIRouter} from "../router"; |
| 5 | +import {Node} from "../path/node"; |
| 6 | +import {TransitionService} from "../transition/transitionService"; |
| 7 | +import {Transition} from "../transition/transition"; |
| 8 | +import {TargetState} from "../state/targetState"; |
| 9 | +import {TreeChanges} from "../transition/interface"; |
| 10 | +import {State} from "../state/stateObject"; |
| 11 | +import {anyTrueR, tail} from "../common/common"; |
| 12 | +import {UIRouterGlobals} from "../globals"; |
| 13 | + |
| 14 | +export interface SrefStatus { |
| 15 | + active: boolean; |
| 16 | + exact: boolean; |
| 17 | + entering: boolean; |
| 18 | + exiting: boolean; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Emits events when the uiSref status changes |
| 23 | + * |
| 24 | + * This API is subject to change. |
| 25 | + */ |
| 26 | +@Directive({ selector: '[uiSrefStatus],[uiSrefActive],[uiSrefActiveEq]' }) |
| 27 | +export class UiSrefStatus { |
| 28 | + private _deregisterHook; |
| 29 | + |
| 30 | + // current statuses of the state/params the uiSref directive is linking to |
| 31 | + @Output("uiSrefStatus") uiSrefStatus = new EventEmitter<SrefStatus>(); |
| 32 | + |
| 33 | + status: SrefStatus = { |
| 34 | + active: false, |
| 35 | + exact: false, |
| 36 | + entering: false, |
| 37 | + exiting: false |
| 38 | + }; |
| 39 | + |
| 40 | + constructor(transitionService: TransitionService, |
| 41 | + private _globals: UIRouterGlobals, |
| 42 | + private _stateService: StateService, |
| 43 | + public sref: UiSref) { |
| 44 | + this._deregisterHook = transitionService.onStart({}, ($transition$) => this._transition($transition$)); |
| 45 | + } |
| 46 | + |
| 47 | + ngOnInit() { |
| 48 | + let lastTrans = this._globals.transitionHistory.peekTail(); |
| 49 | + if (lastTrans != null) { |
| 50 | + this._transition(lastTrans); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + ngOnDestroy() { |
| 55 | + this._deregisterHook() |
| 56 | + } |
| 57 | + |
| 58 | + private _setStatus(status: SrefStatus) { |
| 59 | + this.status = status; |
| 60 | + this.uiSrefStatus.emit(status); |
| 61 | + } |
| 62 | + |
| 63 | + private _transition($transition$: Transition) { |
| 64 | + let sref = this.sref; |
| 65 | + |
| 66 | + let status: SrefStatus = <any> { |
| 67 | + active: false, |
| 68 | + exact: false, |
| 69 | + entering: false, |
| 70 | + exiting: false |
| 71 | + }; |
| 72 | + |
| 73 | + let srefTarget: TargetState = this._stateService.target(sref.state, sref.params, sref.options); |
| 74 | + if (!srefTarget.exists()) { |
| 75 | + return this._setStatus(status); |
| 76 | + } |
| 77 | + |
| 78 | + let tc: TreeChanges = $transition$.treeChanges(); |
| 79 | + let state: State = srefTarget.$state(); |
| 80 | + const isTarget = (node: Node) => node.state === state; |
| 81 | + |
| 82 | + status.active = tc.from.map(isTarget).reduce(anyTrueR, false); |
| 83 | + status.exact = tail(tc.from.map(isTarget)) === true; |
| 84 | + status.entering = tc.entering.map(isTarget).reduce(anyTrueR, false); |
| 85 | + status.exiting = tc.exiting.map(isTarget).reduce(anyTrueR, false); |
| 86 | + |
| 87 | + if ($transition$.isActive()) { |
| 88 | + this._setStatus(status); |
| 89 | + } |
| 90 | + |
| 91 | + let update = (currentPath: Node[]) => () => { |
| 92 | + if (!$transition$.isActive()) return; // superseded |
| 93 | + status.active = currentPath.map(isTarget).reduce(anyTrueR, false); |
| 94 | + status.exact = tail(currentPath.map(isTarget)) === true; |
| 95 | + status.entering = status.exiting = false; |
| 96 | + this._setStatus(status); |
| 97 | + }; |
| 98 | + |
| 99 | + $transition$.promise.then(update(tc.to), update(tc.from)); |
| 100 | + } |
| 101 | +} |
0 commit comments