-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathuiSref.ts
100 lines (90 loc) · 3.18 KB
/
uiSref.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
/** @module ng2_directives */ /** */
import {UIRouter} from "../router";
import {Directive, Inject, Input} from "angular2/core";
import {Optional} from "angular2/core";
import {ElementRef} from "angular2/core";
import {Renderer} from "angular2/core";
import {UiView, ParentUiViewInject} from "./uiView";
import {extend} from "../common/common";
/** @hidden */
@Directive({ selector: 'a[uiSref]' })
export class AnchorUiSref {
constructor(public _el: ElementRef, public _renderer: Renderer) { }
update(href) {
this._renderer.setElementProperty(this._el.nativeElement, 'href', href);
}
}
/**
* A directive when clicked, initiates a [[Transition]] to a [[TargetState]].
*
* ### Purpose
*
* This directive is applied to anchor tags (`<a>`) or any other clickable element. It is a state reference (or sref --
* similar to an href). When clicked, the directive will transition to that state by calling [[StateService.go]],
* and optionally supply state parameter values and transition options.
*
* When this directive is on an anchor tag, it will also add an `href` attribute to the anchor.
*
* ### Selector
*
* - `[uiSref]`: The directive is created as an attribute on an element, e.g., `<a uiSref></a>`
*
* ### Inputs
*
* - `uiSref`: the target state's name, e.g., `uiSref="foostate"`. If a component template uses a relative `uiSref`,
* e.g., `uiSref=".child"`, the reference is relative to that component's state.
*
* - `uiParams`: any target state parameter values, as an object, e.g., `[uiParams]="{ fooId: bar.fooId }"`
*
* - `uiOptions`: [[TransitionOptions]], e.g., `[uiOptions]="{ inherit: false }"`
*
* @example
* ```html
*
* <!-- Targets bar state' -->
* <a uiSref="bar">Bar</a>
*
* <!-- Assume this component's state is "foo".
* Relatively targets "foo.child" -->
* <a uiSref=".child">Foo Child</a>
*
* <!-- Targets "bar" state and supplies parameter value -->
* <a uiSref="bar" [uiParams]="{ barId: foo.barId }">Bar {{foo.barId}}</a>
*
* <!-- Targets "bar" state and parameter, doesn't inherit existing parameters-->
* <a uiSref="bar" [uiParams]="{ barId: foo.barId }" [uiOptions]="{ inherit: false }">Bar {{foo.barId}}</a>
* ```
*/
@Directive({
selector: '[uiSref]',
host: { '(click)': 'go()' }
})
export class UiSref {
@Input('uiSref') state: string;
@Input('uiParams') params: any;
@Input('uiOptions') options: any;
constructor(
private _router: UIRouter,
@Inject(UiView.PARENT_INJECT) public parent: ParentUiViewInject,
@Optional() private _anchorUiSref: AnchorUiSref
) { }
set "uiSref"(val) { this.state = val; this.update(); }
set "uiParams"(val) { this.params = val; this.update(); }
set "uiOptions"(val) { this.options = val; this.update(); }
ngOnInit() {
this.update();
}
update() {
if (this._anchorUiSref) {
this._anchorUiSref.update(this._router.stateService.href(this.state, this.params, this.getOptions()));
}
}
getOptions() {
let defOpts = { relative: this.parent && this.parent.context && this.parent.context.name, inherit: true };
return extend(defOpts, this.options || {});
}
go() {
this._router.stateService.go(this.state, this.params, this.getOptions());
return false;
}
}