@@ -24,18 +24,7 @@ import {
24
24
TrackByFunction ,
25
25
ViewChild
26
26
} from '@angular/core' ;
27
- import {
28
- EMPTY ,
29
- from ,
30
- fromEvent ,
31
- interval ,
32
- merge ,
33
- Observable ,
34
- of ,
35
- Subject ,
36
- Subscription ,
37
- timer
38
- } from 'rxjs' ;
27
+ import { EMPTY , fromEvent , interval , merge , Observable , of , Subject , timer } from 'rxjs' ;
39
28
import { debounceTime , filter , map , startWith , switchMap , takeUntil } from 'rxjs/operators' ;
40
29
41
30
import {
@@ -53,6 +42,7 @@ import {
53
42
NguCarouselStore
54
43
} from './ngu-carousel' ;
55
44
import { NguWindowScrollListener } from './ngu-window-scroll-listener' ;
45
+ import { NguCarouselHammerManager } from './ngu-carousel-hammer-manager' ;
56
46
57
47
type DirectionSymbol = '' | '-' ;
58
48
@@ -68,7 +58,8 @@ const NG_DEV_MODE = typeof ngDevMode === 'undefined' || ngDevMode;
68
58
selector : 'ngu-carousel' ,
69
59
templateUrl : 'ngu-carousel.component.html' ,
70
60
styleUrls : [ 'ngu-carousel.component.scss' ] ,
71
- changeDetection : ChangeDetectionStrategy . OnPush
61
+ changeDetection : ChangeDetectionStrategy . OnPush ,
62
+ providers : [ NguCarouselHammerManager ]
72
63
} )
73
64
// eslint-disable-next-line @angular-eslint/component-class-suffix
74
65
export class NguCarousel < T >
@@ -146,7 +137,7 @@ export class NguCarousel<T>
146
137
147
138
private _intervalController$ = new Subject < number > ( ) ;
148
139
149
- private _hammertime : HammerManager | null = null ;
140
+ private _hammer : HammerManager | null = null ;
150
141
151
142
private _withAnimation = true ;
152
143
@@ -191,7 +182,8 @@ export class NguCarousel<T>
191
182
@Inject ( IS_BROWSER ) private _isBrowser : boolean ,
192
183
private _cdr : ChangeDetectorRef ,
193
184
private _ngZone : NgZone ,
194
- private _nguWindowScrollListener : NguWindowScrollListener
185
+ private _nguWindowScrollListener : NguWindowScrollListener ,
186
+ private _nguCarouselHammerManager : NguCarouselHammerManager
195
187
) {
196
188
super ( ) ;
197
189
this . _setupButtonListeners ( ) ;
@@ -347,45 +339,48 @@ export class NguCarousel<T>
347
339
}
348
340
349
341
ngOnDestroy ( ) {
350
- this . _hammertime ?. destroy ( ) ;
342
+ this . _hammer ?. destroy ( ) ;
351
343
this . _destroy$ . next ( ) ;
352
344
}
353
345
354
346
/** Get Touch input */
355
347
private _setupHammer ( ) : void {
356
- from ( import ( 'hammerjs' ) )
357
- // Note: the dynamic import is always a microtask which may run after the view is destroyed.
358
- // `takeUntil` is used to prevent setting Hammer up if the view had been destroyed before
359
- // the HammerJS is loaded.
360
- . pipe ( takeUntil ( this . _destroy$ ) )
361
- . subscribe ( ( ) => {
362
- const hammertime = ( this . _hammertime = new Hammer ( this . _touchContainer . nativeElement ) ) ;
363
- hammertime . get ( 'pan' ) . set ( { direction : Hammer . DIRECTION_HORIZONTAL } ) ;
348
+ // Note: doesn't need to unsubscribe because streams are piped with `takeUntil` already.
349
+ this . _nguCarouselHammerManager
350
+ . createHammer ( this . _touchContainer . nativeElement )
351
+ . subscribe ( hammer => {
352
+ this . _hammer = hammer ;
364
353
365
- hammertime . on ( 'panstart' , ( ) => {
354
+ hammer . get ( 'pan' ) . set ( { direction : Hammer . DIRECTION_HORIZONTAL } ) ;
355
+
356
+ this . _nguCarouselHammerManager . on ( hammer , 'panstart' ) . subscribe ( ( ) => {
366
357
this . carouselWidth = this . _nguItemsContainer . nativeElement . offsetWidth ;
367
358
this . touchTransform = this . transform [ this . deviceType ! ] ! ;
368
359
this . dexVal = 0 ;
369
360
this . _setStyle ( this . _nguItemsContainer . nativeElement , 'transition' , '' ) ;
370
361
} ) ;
362
+
371
363
if ( this . vertical . enabled ) {
372
- hammertime . on ( 'panup' , ( ev : any ) => {
364
+ this . _nguCarouselHammerManager . on ( hammer , 'panup' ) . subscribe ( ( ev : any ) => {
373
365
this . _touchHandling ( 'panleft' , ev ) ;
374
366
} ) ;
375
- hammertime . on ( 'pandown' , ( ev : any ) => {
367
+
368
+ this . _nguCarouselHammerManager . on ( hammer , 'pandown' ) . subscribe ( ( ev : any ) => {
376
369
this . _touchHandling ( 'panright' , ev ) ;
377
370
} ) ;
378
371
} else {
379
- hammertime . on ( 'panleft' , ( ev : any ) => {
372
+ this . _nguCarouselHammerManager . on ( hammer , 'panleft' ) . subscribe ( ( ev : any ) => {
380
373
this . _touchHandling ( 'panleft' , ev ) ;
381
374
} ) ;
382
- hammertime . on ( 'panright' , ( ev : any ) => {
375
+
376
+ this . _nguCarouselHammerManager . on ( hammer , 'panright' ) . subscribe ( ( ev : any ) => {
383
377
this . _touchHandling ( 'panright' , ev ) ;
384
378
} ) ;
385
379
}
386
- hammertime . on ( 'panend pancancel' , ( ev : any ) => {
387
- if ( Math . abs ( ev . velocity ) >= this . velocity ) {
388
- this . touch . velocity = ev . velocity ;
380
+
381
+ this . _nguCarouselHammerManager . on ( hammer , 'panend pancancel' ) . subscribe ( ( { velocity } ) => {
382
+ if ( Math . abs ( velocity ) >= this . velocity ) {
383
+ this . touch . velocity = velocity ;
389
384
let direc = 0 ;
390
385
if ( ! this . RTL ) {
391
386
direc = this . touch . swipe === 'panright' ? 0 : 1 ;
@@ -403,10 +398,11 @@ export class NguCarousel<T>
403
398
this . _setStyle ( this . _nguItemsContainer . nativeElement , 'transform' , '' ) ;
404
399
}
405
400
} ) ;
406
- hammertime . on ( 'hammer.input' , ev => {
401
+
402
+ this . _nguCarouselHammerManager . on ( hammer , 'hammer.input' ) . subscribe ( ( { srcEvent } ) => {
407
403
// allow nested touch events to no propagate, this may have other side affects but works for now.
408
404
// TODO: It is probably better to check the source element of the event and only apply the handle to the correct carousel
409
- ev . srcEvent . stopPropagation ( ) ;
405
+ srcEvent . stopPropagation ( ) ;
410
406
} ) ;
411
407
} ) ;
412
408
}
0 commit comments