-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ng2.uiSrefActive): Implement uiSrefStatus, uiSrefActive, uiSrefA…
…ctiveEq
- Loading branch information
1 parent
0eb7406
commit fcb15c5
Showing
7 changed files
with
135 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
|
||
import {UiSref} from "../ng2/uiSref"; | ||
import {UiSrefClass} from "../ng2/uiSrefActive"; | ||
import {UiSrefActive} from "../ng2/uiSrefActive"; | ||
import {UiView} from "../ng2/uiView"; | ||
import {UiSrefStatus} from "./uiSrefStatus"; | ||
|
||
export * from "./uiView"; | ||
export * from "./uiSref"; | ||
export * from "./uiSrefStatus"; | ||
export * from "./uiSrefActive"; | ||
export * from "./uiView"; | ||
|
||
export let UIROUTER_DIRECTIVES = [UiSref, UiSrefClass, UiView]; | ||
export let UIROUTER_DIRECTIVES = [UiSref, UiView, UiSrefActive, UiSrefStatus]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,20 @@ | ||
/** @module ng2 */ /** */ | ||
import {UIRouter} from "../router"; | ||
import {Directive} from "angular2/core"; | ||
import {UiSref} from "./uiSref"; | ||
|
||
@Directive({ | ||
selector: '[uiSrefClass]', | ||
inputs: ['uiSrefClass'] | ||
}) | ||
export class UiSrefClass { | ||
// current statuses of the bound uiSref directive | ||
active = false; | ||
exact = false; | ||
entering = false; | ||
exiting = false; | ||
inactive = true; | ||
import {Directive, Input, ElementRef, Host, Renderer} from "angular2/core"; | ||
import {UiSrefStatus, SrefStatus} from "./uiSrefStatus"; | ||
|
||
patterns: any; | ||
classes: string; | ||
sref: UiSref; | ||
@Directive({ selector: '[uiSrefActive],[uiSrefActiveEq]' }) | ||
export class UiSrefActive { | ||
private _classes: string[] = []; | ||
@Input('uiSrefActive') set active(val) { this._classes = val.split("\s+")}; | ||
|
||
//constructor($transitions: TransitionService, public router: UIRouter) { | ||
constructor(public router: UIRouter) { | ||
this.ngOnDestroy = <any> router.transitionService.onSuccess({}, this._update.bind(this)); | ||
} | ||
|
||
ngOnDestroy() {} | ||
|
||
/** | ||
* e.g. | ||
* { | ||
* active: 'active && !exiting', | ||
* loading: 'entering', | ||
* active: matches('admin.*') | ||
* } | ||
*/ | ||
set uiSrefClass(val) { | ||
console.log(val); // [uiSrefClass]="{active: isActive}" logs as "{active: undefined}" | ||
this.patterns = val; | ||
} | ||
|
||
public provideUiSref(sref: UiSref) { | ||
this.sref = sref; | ||
this._update(); | ||
} | ||
private _classesEq: string[] = []; | ||
@Input('uiSrefActiveEq') set activeEq(val) { this._classesEq = val.split("\s+")}; | ||
|
||
private _update() { | ||
if (!this.sref) return; | ||
// update classes | ||
constructor(uiSrefStatus: UiSrefStatus, rnd: Renderer, @Host() host: ElementRef) { | ||
uiSrefStatus.uiSrefStatus.subscribe((next: SrefStatus) => { | ||
this._classes.forEach(cls => rnd.setElementClass(host.nativeElement, cls, next.active)); | ||
this._classesEq.forEach(cls => rnd.setElementClass(host.nativeElement, cls, next.exact)); | ||
}); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import {Directive, Output, EventEmitter} from "angular2/core"; | ||
import {StateService} from "../state/stateService"; | ||
import {UiSref} from "./uiSref"; | ||
import {UIRouter} from "../router"; | ||
import {Node} from "../path/node"; | ||
import {TransitionService} from "../transition/transitionService"; | ||
import {Transition} from "../transition/transition"; | ||
import {TargetState} from "../state/targetState"; | ||
import {TreeChanges} from "../transition/interface"; | ||
import {State} from "../state/stateObject"; | ||
import {anyTrueR, tail} from "../common/common"; | ||
import {UIRouterGlobals} from "../globals"; | ||
|
||
export interface SrefStatus { | ||
active: boolean; | ||
exact: boolean; | ||
entering: boolean; | ||
exiting: boolean; | ||
} | ||
|
||
/** | ||
* Emits events when the uiSref status changes | ||
* | ||
* This API is subject to change. | ||
*/ | ||
@Directive({ selector: '[uiSrefStatus],[uiSrefActive],[uiSrefActiveEq]' }) | ||
export class UiSrefStatus { | ||
private _deregisterHook; | ||
|
||
// current statuses of the state/params the uiSref directive is linking to | ||
@Output("uiSrefStatus") uiSrefStatus = new EventEmitter<SrefStatus>(); | ||
|
||
status: SrefStatus = { | ||
active: false, | ||
exact: false, | ||
entering: false, | ||
exiting: false | ||
}; | ||
|
||
constructor(transitionService: TransitionService, | ||
private _globals: UIRouterGlobals, | ||
private _stateService: StateService, | ||
public sref: UiSref) { | ||
this._deregisterHook = transitionService.onStart({}, ($transition$) => this._transition($transition$)); | ||
} | ||
|
||
ngOnInit() { | ||
let lastTrans = this._globals.transitionHistory.peekTail(); | ||
if (lastTrans != null) { | ||
this._transition(lastTrans); | ||
} | ||
} | ||
|
||
ngOnDestroy() { | ||
this._deregisterHook() | ||
} | ||
|
||
private _setStatus(status: SrefStatus) { | ||
this.status = status; | ||
this.uiSrefStatus.emit(status); | ||
} | ||
|
||
private _transition($transition$: Transition) { | ||
let sref = this.sref; | ||
|
||
let status: SrefStatus = <any> { | ||
active: false, | ||
exact: false, | ||
entering: false, | ||
exiting: false | ||
}; | ||
|
||
let srefTarget: TargetState = this._stateService.target(sref.state, sref.params, sref.options); | ||
if (!srefTarget.exists()) { | ||
return this._setStatus(status); | ||
} | ||
|
||
let tc: TreeChanges = $transition$.treeChanges(); | ||
let state: State = srefTarget.$state(); | ||
const isTarget = (node: Node) => node.state === state; | ||
|
||
status.active = tc.from.map(isTarget).reduce(anyTrueR, false); | ||
status.exact = tail(tc.from.map(isTarget)) === true; | ||
status.entering = tc.entering.map(isTarget).reduce(anyTrueR, false); | ||
status.exiting = tc.exiting.map(isTarget).reduce(anyTrueR, false); | ||
|
||
if ($transition$.isActive()) { | ||
this._setStatus(status); | ||
} | ||
|
||
let update = (currentPath: Node[]) => () => { | ||
if (!$transition$.isActive()) return; // superseded | ||
status.active = currentPath.map(isTarget).reduce(anyTrueR, false); | ||
status.exact = tail(currentPath.map(isTarget)) === true; | ||
status.entering = status.exiting = false; | ||
this._setStatus(status); | ||
}; | ||
|
||
$transition$.promise.then(update(tc.to), update(tc.from)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters