@@ -47,22 +47,19 @@ export abstract class MatListItemBase implements AfterContentInit, OnDestroy, Ri
47
47
// TODO(mmalerba): Add @Input for disabling ripple.
48
48
rippleDisabled : boolean ;
49
49
50
- _element : HTMLElement ;
51
-
52
50
private _subscriptions = new Subscription ( ) ;
53
51
54
52
private _rippleRenderer : RippleRenderer ;
55
53
56
- protected constructor ( private _elementRef : ElementRef < HTMLElement > , protected _ngZone : NgZone ,
54
+ protected constructor ( public _elementRef : ElementRef < HTMLElement > , protected _ngZone : NgZone ,
57
55
listBase : MatListBase , platform : Platform ) {
58
- this . _element = this . _elementRef . nativeElement ;
59
56
this . rippleDisabled = listBase . _isNonInteractive ;
60
57
if ( ! listBase . _isNonInteractive ) {
61
- this . _element . classList . add ( 'mat-mdc-list-item-interactive' ) ;
58
+ this . _elementRef . nativeElement . classList . add ( 'mat-mdc-list-item-interactive' ) ;
62
59
}
63
60
this . _rippleRenderer =
64
- new RippleRenderer ( this , this . _ngZone , this . _element , platform ) ;
65
- this . _rippleRenderer . setupTriggerEvents ( this . _element ) ;
61
+ new RippleRenderer ( this , this . _ngZone , this . _elementRef . nativeElement , platform ) ;
62
+ this . _rippleRenderer . setupTriggerEvents ( this . _elementRef . nativeElement ) ;
66
63
}
67
64
68
65
ngAfterContentInit ( ) {
@@ -74,8 +71,15 @@ export abstract class MatListItemBase implements AfterContentInit, OnDestroy, Ri
74
71
this . _rippleRenderer . _removeTriggerEvents ( ) ;
75
72
}
76
73
74
+ /**
75
+ * Changes the tabindex of all button and anchor children of this item.
76
+ *
77
+ * This method is used by the `MatInteractiveBaseList` to implement the
78
+ * `setTabIndexForListItemChildren` method on the `MDCListAdapter`
79
+ */
77
80
_setTabIndexForChildren ( value : number ) {
78
- this . _element . querySelectorAll ( 'a, button' ) . forEach ( el => ( el as HTMLElement ) . tabIndex = value ) ;
81
+ this . _elementRef . nativeElement . querySelectorAll < HTMLElement > ( 'a, button' )
82
+ . forEach ( el => el . tabIndex = value ) ;
79
83
}
80
84
81
85
/**
@@ -86,7 +90,7 @@ export abstract class MatListItemBase implements AfterContentInit, OnDestroy, Ri
86
90
this . _ngZone . runOutsideAngular ( ( ) => {
87
91
this . _subscriptions . add ( this . lines . changes . pipe ( startWith ( this . lines ) )
88
92
. subscribe ( ( lines : QueryList < ElementRef < Element > > ) => {
89
- this . _element . classList
93
+ this . _elementRef . nativeElement . classList
90
94
. toggle ( 'mat-mdc-list-item-single-line' , lines . length <= 1 ) ;
91
95
lines . forEach ( ( line : ElementRef < Element > , index : number ) => {
92
96
toggleClass ( line . nativeElement ,
@@ -113,7 +117,7 @@ export abstract class MatInteractiveListBase extends MatListBase
113
117
_handleKeydown ( event : KeyboardEvent ) {
114
118
const index = this . _indexForElement ( event . target as HTMLElement ) ;
115
119
this . _foundation . handleKeydown (
116
- event , this . _itemAtIndex ( index ) . _element === event . target , index ) ;
120
+ event , this . _elementAtIndex ( index ) === event . target , index ) ;
117
121
}
118
122
119
123
@HostListener ( 'click' , [ '$event' ] )
@@ -136,21 +140,20 @@ export abstract class MatInteractiveListBase extends MatListBase
136
140
protected _adapter : MDCListAdapter = {
137
141
getListItemCount : ( ) => this . _items . length ,
138
142
listItemAtIndexHasClass :
139
- ( index , className ) => this . _itemAtIndex ( index ) . _element . classList . contains ( className ) ,
143
+ ( index , className ) => this . _elementAtIndex ( index ) . classList . contains ( className ) ,
140
144
addClassForElementIndex :
141
- ( index , className ) => this . _itemAtIndex ( index ) . _element . classList . add ( className ) ,
145
+ ( index , className ) => this . _elementAtIndex ( index ) . classList . add ( className ) ,
142
146
removeClassForElementIndex :
143
- ( index , className ) => this . _itemAtIndex ( index ) . _element . classList . remove ( className ) ,
144
- getAttributeForElementIndex :
145
- ( index , attr ) => this . _itemAtIndex ( index ) . _element . getAttribute ( attr ) ,
147
+ ( index , className ) => this . _elementAtIndex ( index ) . classList . remove ( className ) ,
148
+ getAttributeForElementIndex : ( index , attr ) => this . _elementAtIndex ( index ) . getAttribute ( attr ) ,
146
149
setAttributeForElementIndex :
147
- ( index , attr , value ) => this . _itemAtIndex ( index ) . _element . setAttribute ( attr , value ) ,
150
+ ( index , attr , value ) => this . _elementAtIndex ( index ) . setAttribute ( attr , value ) ,
148
151
setTabIndexForListItemChildren :
149
152
( index , value ) => this . _itemAtIndex ( index ) . _setTabIndexForChildren ( Number ( value ) ) ,
150
- getFocusedElementIndex : ( ) => this . _indexForElement ( this . _document ?. activeELement ) ,
153
+ getFocusedElementIndex : ( ) => this . _indexForElement ( this . _document ?. activeElement ) ,
151
154
isFocusInsideList : ( ) => this . _element . nativeElement . contains ( this . _document ?. activeElement ) ,
152
155
isRootFocused : ( ) => this . _element . nativeElement === this . _document ?. activeElement ,
153
- focusItemAtIndex : index => this . _itemAtIndex ( index ) . _element . focus ( ) ,
156
+ focusItemAtIndex : index => this . _elementAtIndex ( index ) . focus ( ) ,
154
157
155
158
// The following methods have a dummy implementation in the base class because they are only
156
159
// applicable to certain types of lists. They should be implemented for the concrete classes
@@ -167,32 +170,46 @@ export abstract class MatInteractiveListBase extends MatListBase
167
170
168
171
protected _foundation : MDCListFoundation ;
169
172
170
- constructor ( protected _element : ElementRef < HTMLElement > ,
171
- @Inject ( DOCUMENT ) protected _document : any ) {
173
+ protected _document : Document ;
174
+
175
+ private _itemsArr : MatListItemBase [ ] = [ ] ;
176
+
177
+ private _subscriptions = new Subscription ( ) ;
178
+
179
+ constructor ( protected _element : ElementRef < HTMLElement > , @Inject ( DOCUMENT ) document : any ) {
172
180
super ( ) ;
181
+ this . _document = document ;
173
182
this . _isNonInteractive = false ;
174
183
this . _foundation = new MDCListFoundation ( this . _adapter ) ;
175
184
}
176
185
177
186
ngAfterViewInit ( ) {
178
187
this . _foundation . init ( ) ;
179
- const first = this . _items . toArray ( ) [ 0 ] ?. _element ;
188
+ const first = this . _itemAtIndex ( 0 ) ;
180
189
if ( first ) {
181
- first . tabIndex = 0 ;
190
+ first . _elementRef . nativeElement . tabIndex = 0 ;
182
191
}
183
192
this . _foundation . layout ( ) ;
193
+ this . _subscriptions . add (
194
+ this . _items . changes . subscribe ( ( ) => this . _itemsArr = this . _items . toArray ( ) ) ) ;
184
195
}
185
196
186
197
ngOnDestroy ( ) {
187
198
this . _foundation . destroy ( ) ;
199
+ this . _subscriptions . unsubscribe ( ) ;
188
200
}
189
201
190
202
private _itemAtIndex ( index : number ) : MatListItemBase {
191
- return this . _items . toArray ( ) [ index ] ;
203
+ return this . _itemsArr [ index ] ;
204
+ }
205
+
206
+ private _elementAtIndex ( index : number ) : HTMLElement {
207
+ return this . _itemAtIndex ( index ) . _elementRef . nativeElement ;
192
208
}
193
209
194
- private _indexForElement ( element : HTMLElement | null ) {
195
- return element ? this . _items . toArray ( ) . findIndex ( i => i . _element . contains ( element ) ) : - 1 ;
210
+ private _indexForElement ( element : Element | null ) {
211
+ return element ?
212
+ this . _itemsArr . findIndex ( i => i . _elementRef . nativeElement . contains ( element ) ) : - 1 ;
196
213
}
197
214
}
198
215
0 commit comments