-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
tab-nav-bar.ts
155 lines (135 loc) · 4.63 KB
/
tab-nav-bar.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {
AfterContentInit,
Component,
Directive,
ElementRef,
HostBinding,
Inject,
Input,
NgZone,
OnDestroy,
Optional,
ViewChild,
ViewEncapsulation
} from '@angular/core';
import {MdInkBar} from '../ink-bar';
import {CanDisable, mixinDisabled} from '../../core/common-behaviors/disabled';
import {MdRipple} from '../../core';
import {ViewportRuler} from '../../core/overlay/position/viewport-ruler';
import {Directionality, MD_RIPPLE_GLOBAL_OPTIONS, Platform, RippleGlobalOptions} from '../../core';
import {Observable} from 'rxjs/Observable';
import {Subject} from 'rxjs/Subject';
import 'rxjs/add/operator/auditTime';
import 'rxjs/add/operator/takeUntil';
import 'rxjs/add/observable/of';
import 'rxjs/add/observable/merge';
/**
* Navigation component matching the styles of the tab group header.
* Provides anchored navigation with animated ink bar.
*/
@Component({
moduleId: module.id,
selector: '[md-tab-nav-bar], [mat-tab-nav-bar]',
templateUrl: 'tab-nav-bar.html',
styleUrls: ['tab-nav-bar.css'],
host: {'class': 'mat-tab-nav-bar'},
encapsulation: ViewEncapsulation.None,
})
export class MdTabNav implements AfterContentInit, OnDestroy {
/** Subject that emits when the component has been destroyed. */
private _onDestroy = new Subject<void>();
_activeLinkChanged: boolean;
_activeLinkElement: ElementRef;
@ViewChild(MdInkBar) _inkBar: MdInkBar;
constructor(@Optional() private _dir: Directionality, private _ngZone: NgZone) { }
/** Notifies the component that the active link has been changed. */
updateActiveLink(element: ElementRef) {
this._activeLinkChanged = this._activeLinkElement != element;
this._activeLinkElement = element;
}
ngAfterContentInit(): void {
this._ngZone.runOutsideAngular(() => {
let dirChange = this._dir ? this._dir.change : Observable.of(null);
let resize = typeof window !== 'undefined' ?
Observable.fromEvent(window, 'resize').auditTime(10) :
Observable.of(null);
return Observable.merge(dirChange, resize)
.takeUntil(this._onDestroy)
.subscribe(() => this._alignInkBar());
});
}
/** Checks if the active link has been changed and, if so, will update the ink bar. */
ngAfterContentChecked(): void {
if (this._activeLinkChanged) {
this._alignInkBar();
this._activeLinkChanged = false;
}
}
ngOnDestroy() {
this._onDestroy.next();
}
/** Aligns the ink bar to the active link. */
_alignInkBar(): void {
if (this._activeLinkElement) {
this._inkBar.alignToElement(this._activeLinkElement.nativeElement);
}
}
}
// Boilerplate for applying mixins to MdTabLink.
export class MdTabLinkBase {}
export const _MdTabLinkMixinBase = mixinDisabled(MdTabLinkBase);
/**
* Link inside of a `md-tab-nav-bar`.
*/
@Directive({
selector: '[md-tab-link], [mat-tab-link], [mdTabLink], [matTabLink]',
inputs: ['disabled'],
host: {
'class': 'mat-tab-link',
'[attr.aria-disabled]': 'disabled.toString()',
'[class.mat-tab-disabled]': 'disabled'
}
})
export class MdTabLink extends _MdTabLinkMixinBase implements OnDestroy, CanDisable {
/** Whether the tab link is active or not. */
private _isActive: boolean = false;
/** Reference to the instance of the ripple for the tab link. */
private _tabLinkRipple: MdRipple;
/** Whether the link is active. */
@Input()
get active(): boolean { return this._isActive; }
set active(value: boolean) {
this._isActive = value;
if (value) {
this._mdTabNavBar.updateActiveLink(this._elementRef);
}
}
/** @docs-private */
@HostBinding('tabIndex')
get tabIndex(): number {
return this.disabled ? -1 : 0;
}
constructor(private _mdTabNavBar: MdTabNav,
private _elementRef: ElementRef,
ngZone: NgZone,
ruler: ViewportRuler,
platform: Platform,
@Optional() @Inject(MD_RIPPLE_GLOBAL_OPTIONS) globalOptions: RippleGlobalOptions) {
super();
// Manually create a ripple instance that uses the tab link element as trigger element.
// Notice that the lifecycle hooks for the ripple config won't be called anymore.
this._tabLinkRipple = new MdRipple(_elementRef, ngZone, ruler, platform, globalOptions);
}
ngOnDestroy() {
// Manually call the ngOnDestroy lifecycle hook of the ripple instance because it won't be
// called automatically since its instance is not created by Angular.
this._tabLinkRipple.ngOnDestroy();
}
}