@@ -24,18 +24,7 @@ import {
2424 TrackByFunction ,
2525 ViewChild
2626} 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' ;
3928import { debounceTime , filter , map , startWith , switchMap , takeUntil } from 'rxjs/operators' ;
4029
4130import {
@@ -53,6 +42,7 @@ import {
5342 NguCarouselStore
5443} from './ngu-carousel' ;
5544import { NguWindowScrollListener } from './ngu-window-scroll-listener' ;
45+ import { NguCarouselHammerManager } from './ngu-carousel-hammer-manager' ;
5646
5747type DirectionSymbol = '' | '-' ;
5848
@@ -68,7 +58,8 @@ const NG_DEV_MODE = typeof ngDevMode === 'undefined' || ngDevMode;
6858 selector : 'ngu-carousel' ,
6959 templateUrl : 'ngu-carousel.component.html' ,
7060 styleUrls : [ 'ngu-carousel.component.scss' ] ,
71- changeDetection : ChangeDetectionStrategy . OnPush
61+ changeDetection : ChangeDetectionStrategy . OnPush ,
62+ providers : [ NguCarouselHammerManager ]
7263} )
7364// eslint-disable-next-line @angular-eslint/component-class-suffix
7465export class NguCarousel < T >
@@ -146,7 +137,7 @@ export class NguCarousel<T>
146137
147138 private _intervalController$ = new Subject < number > ( ) ;
148139
149- private _hammertime : HammerManager | null = null ;
140+ private _hammer : HammerManager | null = null ;
150141
151142 private _withAnimation = true ;
152143
@@ -191,7 +182,8 @@ export class NguCarousel<T>
191182 @Inject ( IS_BROWSER ) private _isBrowser : boolean ,
192183 private _cdr : ChangeDetectorRef ,
193184 private _ngZone : NgZone ,
194- private _nguWindowScrollListener : NguWindowScrollListener
185+ private _nguWindowScrollListener : NguWindowScrollListener ,
186+ private _nguCarouselHammerManager : NguCarouselHammerManager
195187 ) {
196188 super ( ) ;
197189 this . _setupButtonListeners ( ) ;
@@ -347,45 +339,48 @@ export class NguCarousel<T>
347339 }
348340
349341 ngOnDestroy ( ) {
350- this . _hammertime ?. destroy ( ) ;
342+ this . _hammer ?. destroy ( ) ;
351343 this . _destroy$ . next ( ) ;
352344 }
353345
354346 /** Get Touch input */
355347 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 ;
364353
365- hammertime . on ( 'panstart' , ( ) => {
354+ hammer . get ( 'pan' ) . set ( { direction : Hammer . DIRECTION_HORIZONTAL } ) ;
355+
356+ this . _nguCarouselHammerManager . on ( hammer , 'panstart' ) . subscribe ( ( ) => {
366357 this . carouselWidth = this . _nguItemsContainer . nativeElement . offsetWidth ;
367358 this . touchTransform = this . transform [ this . deviceType ! ] ! ;
368359 this . dexVal = 0 ;
369360 this . _setStyle ( this . _nguItemsContainer . nativeElement , 'transition' , '' ) ;
370361 } ) ;
362+
371363 if ( this . vertical . enabled ) {
372- hammertime . on ( 'panup' , ( ev : any ) => {
364+ this . _nguCarouselHammerManager . on ( hammer , 'panup' ) . subscribe ( ( ev : any ) => {
373365 this . _touchHandling ( 'panleft' , ev ) ;
374366 } ) ;
375- hammertime . on ( 'pandown' , ( ev : any ) => {
367+
368+ this . _nguCarouselHammerManager . on ( hammer , 'pandown' ) . subscribe ( ( ev : any ) => {
376369 this . _touchHandling ( 'panright' , ev ) ;
377370 } ) ;
378371 } else {
379- hammertime . on ( 'panleft' , ( ev : any ) => {
372+ this . _nguCarouselHammerManager . on ( hammer , 'panleft' ) . subscribe ( ( ev : any ) => {
380373 this . _touchHandling ( 'panleft' , ev ) ;
381374 } ) ;
382- hammertime . on ( 'panright' , ( ev : any ) => {
375+
376+ this . _nguCarouselHammerManager . on ( hammer , 'panright' ) . subscribe ( ( ev : any ) => {
383377 this . _touchHandling ( 'panright' , ev ) ;
384378 } ) ;
385379 }
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 ;
389384 let direc = 0 ;
390385 if ( ! this . RTL ) {
391386 direc = this . touch . swipe === 'panright' ? 0 : 1 ;
@@ -403,10 +398,11 @@ export class NguCarousel<T>
403398 this . _setStyle ( this . _nguItemsContainer . nativeElement , 'transform' , '' ) ;
404399 }
405400 } ) ;
406- hammertime . on ( 'hammer.input' , ev => {
401+
402+ this . _nguCarouselHammerManager . on ( hammer , 'hammer.input' ) . subscribe ( ( { srcEvent } ) => {
407403 // allow nested touch events to no propagate, this may have other side affects but works for now.
408404 // 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 ( ) ;
410406 } ) ;
411407 } ) ;
412408 }
0 commit comments