@@ -38,7 +38,8 @@ import {
38
38
Observable ,
39
39
of ,
40
40
Subject ,
41
- Subscription
41
+ Subscription ,
42
+ timer
42
43
} from 'rxjs' ;
43
44
import { debounceTime , filter , map , startWith , switchMap , takeUntil } from 'rxjs/operators' ;
44
45
import {
@@ -48,20 +49,20 @@ import {
48
49
NguCarouselPrevDirective
49
50
} from './../ngu-carousel.directive' ;
50
51
import {
52
+ Transfrom ,
51
53
Breakpoints ,
52
54
NguCarouselConfig ,
53
55
NguCarouselOutletContext ,
54
56
NguCarouselStore
55
57
} from './ngu-carousel' ;
58
+ import { NguWindowScrollListener } from './ngu-window-scroll-listener' ;
56
59
57
- // @dynamic
58
60
@Component ( {
59
61
selector : 'ngu-carousel' ,
60
62
templateUrl : 'ngu-carousel.component.html' ,
61
63
styleUrls : [ 'ngu-carousel.component.scss' ] ,
62
64
changeDetection : ChangeDetectionStrategy . OnPush
63
65
} )
64
- // @dynamic
65
66
// eslint-disable-next-line @angular-eslint/component-class-suffix
66
67
export class NguCarousel < T >
67
68
extends NguCarouselStore
@@ -88,7 +89,6 @@ export class NguCarousel<T>
88
89
listener1 : ( ) => void ;
89
90
listener2 : ( ) => void ;
90
91
listener3 : ( ) => void ;
91
- listener4 : ( ) => void ;
92
92
93
93
@Input ( 'dataSource' )
94
94
get dataSource ( ) : any {
@@ -147,8 +147,6 @@ export class NguCarousel<T>
147
147
148
148
private carousel : any ;
149
149
150
- private onScrolling : any ;
151
-
152
150
private _hammertime : HammerManager | null = null ;
153
151
154
152
private _destroy$ = new Subject < void > ( ) ;
@@ -179,7 +177,8 @@ export class NguCarousel<T>
179
177
private _differs : IterableDiffers ,
180
178
@Inject ( PLATFORM_ID ) private platformId : object ,
181
179
private _cdr : ChangeDetectorRef ,
182
- private _ngZone : NgZone
180
+ private _ngZone : NgZone ,
181
+ private _nguWindowScrollListener : NguWindowScrollListener
183
182
) {
184
183
super ( ) ;
185
184
}
@@ -230,17 +229,21 @@ export class NguCarousel<T>
230
229
if ( ! this . arrayChanges ) return ;
231
230
232
231
this . arrayChanges . forEachOperation (
233
- ( item : IterableChangeRecord < any > , adjustedPreviousIndex : number , currentIndex : number ) => {
234
- const node = this . _getNodeDef ( data [ currentIndex ] , currentIndex ) ;
232
+ (
233
+ item : IterableChangeRecord < any > ,
234
+ adjustedPreviousIndex : number | null ,
235
+ currentIndex : number | null
236
+ ) => {
237
+ const node = this . _getNodeDef ( data [ currentIndex ! ] , currentIndex ! ) ;
235
238
236
239
if ( item . previousIndex == null ) {
237
- const context = new NguCarouselOutletContext < any > ( data [ currentIndex ] ) ;
238
- context . index = currentIndex ;
239
- viewContainer . createEmbeddedView ( node . template , context , currentIndex ) ;
240
+ const context = new NguCarouselOutletContext < any > ( data [ currentIndex ! ] ) ;
241
+ context . index = currentIndex ! ;
242
+ viewContainer . createEmbeddedView ( node . template , context , currentIndex ! ) ;
240
243
} else if ( currentIndex == null ) {
241
- viewContainer . remove ( adjustedPreviousIndex ) ;
244
+ viewContainer . remove ( adjustedPreviousIndex ! ) ;
242
245
} else {
243
- const view = viewContainer . get ( adjustedPreviousIndex ) ;
246
+ const view = viewContainer . get ( adjustedPreviousIndex ! ) ;
244
247
viewContainer . move ( view ! , currentIndex ) ;
245
248
}
246
249
}
@@ -341,9 +344,9 @@ export class NguCarousel<T>
341
344
this . onMove . complete ( ) ;
342
345
343
346
/** remove listeners */
344
- clearTimeout ( this . onScrolling ) ;
345
- for ( let i = 1 ; i <= 4 ; i ++ ) {
346
- const str = `listener${ i } ` ;
347
+ for ( let i = 1 ; i <= 3 ; i ++ ) {
348
+ // TODO: revisit later.
349
+ const str = `listener${ i } ` as 'listener1' | 'listener2' | 'listener3' ;
347
350
this [ str ] && this [ str ] ( ) ;
348
351
}
349
352
}
@@ -644,7 +647,6 @@ export class NguCarousel<T>
644
647
) ;
645
648
}
646
649
647
- // tslint:disable-next-line:no-unused-expression
648
650
this . RTL && ! this . vertical . enabled && this . _renderer . addClass ( this . carousel , 'ngurtl' ) ;
649
651
this . _createStyleElem ( `${ dism } ${ itemStyle } ` ) ;
650
652
this . _storeCarouselData ( ) ;
@@ -653,7 +655,6 @@ export class NguCarousel<T>
653
655
/** logic to scroll the carousel step 1 */
654
656
private _carouselScrollOne ( Btn : number ) : void {
655
657
let itemSpeed = this . speed ;
656
- let translateXval = 0 ;
657
658
let currentSlide = 0 ;
658
659
let touchMove = Math . ceil ( this . dexVal / this . itemWidth ) ;
659
660
touchMove = isFinite ( touchMove ) ? touchMove : 0 ;
@@ -670,7 +671,7 @@ export class NguCarousel<T>
670
671
itemSpeed = 400 ;
671
672
this . _btnBoolean ( 0 , 1 ) ;
672
673
} else if ( this . slideItems >= MoveSlide ) {
673
- currentSlide = translateXval = 0 ;
674
+ currentSlide = 0 ;
674
675
this . _btnBoolean ( 1 , 0 ) ;
675
676
} else {
676
677
this . _btnBoolean ( 0 , 0 ) ;
@@ -690,7 +691,7 @@ export class NguCarousel<T>
690
691
currentSlide = this . dataSource . length - this . items ;
691
692
this . _btnBoolean ( 0 , 1 ) ;
692
693
} else if ( this . isLast ) {
693
- currentSlide = translateXval = 0 ;
694
+ currentSlide = 0 ;
694
695
itemSpeed = 400 ;
695
696
this . _btnBoolean ( 1 , 0 ) ;
696
697
} else {
@@ -708,8 +709,6 @@ export class NguCarousel<T>
708
709
709
710
/** logic to scroll the carousel step 2 */
710
711
private _carouselScrollTwo ( Btn : number , currentSlide : number , itemSpeed : number ) : void {
711
- // tslint:disable-next-line:no-unused-expression
712
-
713
712
if ( this . dexVal !== 0 ) {
714
713
const val = Math . abs ( this . touch . velocity ) ;
715
714
let somt = Math . floor ( ( this . dexVal / val / this . dexVal ) * ( this . deviceWidth - this . dexVal ) ) ;
@@ -750,15 +749,15 @@ export class NguCarousel<T>
750
749
this . isLast = ! ! last ;
751
750
}
752
751
753
- private _transformString ( grid : string , slide : number ) : string {
752
+ private _transformString ( grid : keyof Transfrom , slide : number ) : string {
754
753
let collect = '' ;
755
754
collect += `${ this . styleid } { transform: translate3d(` ;
756
755
757
756
if ( this . vertical . enabled ) {
758
- this . transform [ grid ] = ( this . vertical . height / this . inputs . grid [ grid ] ) * slide ;
757
+ this . transform [ grid ] = ( this . vertical . height / this . inputs . grid [ grid ] ! ) * slide ;
759
758
collect += `0, -${ this . transform [ grid ] } px, 0` ;
760
759
} else {
761
- this . transform [ grid ] = ( 100 / this . inputs . grid [ grid ] ) * slide ;
760
+ this . transform [ grid ] = ( 100 / this . inputs . grid [ grid ] ! ) * slide ;
762
761
collect += `${ this . directionSym } ${ this . transform [ grid ] } %, 0, 0` ;
763
762
}
764
763
collect += `); }` ;
@@ -808,12 +807,18 @@ export class NguCarousel<T>
808
807
private _carouselInterval ( ) : void {
809
808
const container = this . carouselMain1 . nativeElement ;
810
809
if ( this . interval && this . loop ) {
811
- this . listener4 = this . _renderer . listen ( 'window' , 'scroll' , ( ) => {
812
- clearTimeout ( this . onScrolling ) ;
813
- this . onScrolling = setTimeout ( ( ) => {
814
- this . _onWindowScrolling ( ) ;
815
- } , 600 ) ;
816
- } ) ;
810
+ this . _nguWindowScrollListener
811
+ . pipe (
812
+ // Note: do not use `debounceTime` since it may flush queued actions within the Angular zone.
813
+ switchMap ( ( ) => timer ( 600 ) ) ,
814
+ takeUntil ( this . _destroy$ )
815
+ )
816
+ . subscribe ( ( ) => {
817
+ // Note: we don't run change detection on each `scroll` event, but we re-enter the
818
+ // Angular zone once the DOM timer fires to be backwards compatible.
819
+ // TODO: revisit later since we may not run change detection at all on this task.
820
+ this . _ngZone . run ( ( ) => this . _onWindowScrolling ( ) ) ;
821
+ } ) ;
817
822
818
823
const mapToZero = map ( ( ) => 0 ) ;
819
824
const mapToOne = map ( ( ) => 1 ) ;
0 commit comments