1
1
import {
2
2
AfterContentInit ,
3
3
Component ,
4
- ContentChildren ,
5
4
Directive ,
6
5
ElementRef ,
7
6
HostBinding ,
8
7
Inject ,
9
8
Optional ,
10
- QueryList ,
11
- ViewChild ,
12
9
ViewEncapsulation ,
13
- input
10
+ input ,
11
+ contentChildren ,
12
+ viewChild
14
13
} from '@angular/core' ;
15
14
import { FocusableOption , FocusKeyManager } from '@angular/cdk/a11y' ;
16
15
import { LEFT_ARROW , RIGHT_ARROW , TAB } from '@angular/cdk/keycodes' ;
@@ -43,8 +42,8 @@ export class CarouselItem implements FocusableOption {
43
42
} )
44
43
export class Carousel implements AfterContentInit {
45
44
readonly ariaLabel = input < string | undefined > ( undefined , { alias : 'aria-label' } ) ;
46
- @ ContentChildren ( CarouselItem ) items ! : QueryList < CarouselItem > ;
47
- @ ViewChild ( 'list' ) list ! : ElementRef < HTMLElement > ;
45
+ readonly items = contentChildren ( CarouselItem ) ;
46
+ readonly list = viewChild . required < ElementRef < HTMLElement > > ( 'list' ) ;
48
47
@HostBinding ( 'class.animations-disabled' ) readonly animationsDisabled : boolean ;
49
48
position = 0 ;
50
49
showPrevArrow = false ;
@@ -79,12 +78,12 @@ export class Carousel implements AfterContentInit {
79
78
}
80
79
81
80
ngAfterContentInit ( ) : void {
82
- this . _keyManager = new FocusKeyManager < CarouselItem > ( this . items ) ;
81
+ this . _keyManager = new FocusKeyManager < CarouselItem > ( this . items ( ) ) ;
83
82
}
84
83
85
84
/** Goes to the next set of items */
86
85
next ( ) {
87
- for ( let i = this . index ; i < this . items . length ; i ++ ) {
86
+ for ( let i = this . index ; i < this . items ( ) . length ; i ++ ) {
88
87
if ( this . _isOutOfView ( i ) ) {
89
88
this . index = i ;
90
89
this . _scrollToActiveItem ( ) ;
@@ -106,7 +105,7 @@ export class Carousel implements AfterContentInit {
106
105
107
106
/** Updates the `tabindex` of each of the items based on their active state. */
108
107
private _updateItemTabIndices ( ) {
109
- this . items . forEach ( ( item : CarouselItem ) => {
108
+ this . items ( ) . forEach ( ( item : CarouselItem ) => {
110
109
if ( this . _keyManager != null ) {
111
110
item . tabindex = item === this . _keyManager . activeItem ? '0' : '-1' ;
112
111
}
@@ -119,7 +118,7 @@ export class Carousel implements AfterContentInit {
119
118
return ;
120
119
}
121
120
122
- const itemsArray = this . items . toArray ( ) ;
121
+ const itemsArray = this . items ( ) ;
123
122
let targetItemIndex = this . index ;
124
123
125
124
// Only shift the carousel by one if we're going forwards. This
@@ -129,7 +128,7 @@ export class Carousel implements AfterContentInit {
129
128
}
130
129
131
130
this . position = itemsArray [ targetItemIndex ] . element . nativeElement . offsetLeft ;
132
- this . list . nativeElement . style . transform = `translateX(-${ this . position } px)` ;
131
+ this . list ( ) . nativeElement . style . transform = `translateX(-${ this . position } px)` ;
133
132
this . showPrevArrow = this . index > 0 ;
134
133
this . showNextArrow = false ;
135
134
@@ -143,15 +142,15 @@ export class Carousel implements AfterContentInit {
143
142
144
143
/** Checks whether an item at a specific index is outside of the viewport. */
145
144
private _isOutOfView ( index : number , side ?: 'start' | 'end' ) {
146
- const { offsetWidth, offsetLeft} = this . items . toArray ( ) [ index ] . element . nativeElement ;
145
+ const { offsetWidth, offsetLeft} = this . items ( ) [ index ] . element . nativeElement ;
147
146
148
147
if ( ( ! side || side === 'start' ) && offsetLeft - this . position < 0 ) {
149
148
return true ;
150
149
}
151
150
152
151
return (
153
152
( ! side || side === 'end' ) &&
154
- offsetWidth + offsetLeft - this . position > this . list . nativeElement . clientWidth
153
+ offsetWidth + offsetLeft - this . position > this . list ( ) . nativeElement . clientWidth
155
154
) ;
156
155
}
157
156
}
0 commit comments