@@ -14,12 +14,10 @@ import { DIRECTION_HORIZONTAL, DIRECTION_VERTICAL } from 'ionic-angular/gestures
1414import { ElementRef , Renderer , Component , OnInit , OnDestroy , AfterViewInit , NgZone , ViewChild } from '@angular/core' ;
1515import { DomSanitizer , SafeUrl } from '@angular/platform-browser' ;
1616
17- import { ImageViewerGesture } from './image-viewer-gesture' ;
17+ import { ImageViewerTransitionGesture } from './image-viewer-transition-gesture' ;
18+ import { ImageViewerZoomGesture } from './image-viewer-zoom-gesture' ;
1819import { ImageViewerEnter , ImageViewerLeave } from './image-viewer-transitions' ;
1920
20- const DOUBLE_TAP_INTERVAL = 300 ;
21- const MAX_SCALE = 2 ;
22-
2321@Component ( {
2422 selector : 'image-viewer' ,
2523 template : `
@@ -32,27 +30,19 @@ const MAX_SCALE = 2;
3230
3331 <div class="image-wrapper">
3432 <div class="image" #imageContainer>
35- <img [src]="imageUrl" (click)="onImageClick()" tappable />
33+ <img [src]="imageUrl" tappable />
3634 </div>
3735 </div>
3836 `
3937} )
4038export class ImageViewerComponent extends Ion implements OnInit , OnDestroy , AfterViewInit {
4139 public imageUrl : SafeUrl ;
4240
43- private dragGesture : PanGesture ;
41+ public dragGesture : ImageViewerTransitionGesture ;
4442
4543 @ViewChild ( 'imageContainer' ) imageContainer ;
46- private pinchGesture : Gesture ;
47- private adjustScale = 1 ;
48- private adjustDeltaX = 0 ;
49- private adjustDeltaY = 0 ;
50-
51- private currentScale : number = 1 ;
52- private currentDeltaX = null ;
53- private currentDeltaY = null ;
44+ private pinchGesture : ImageViewerZoomGesture ;
5445
55- private dblClickInAction : boolean ;
5646 public isZoomed : boolean ;
5747
5848 constructor (
@@ -75,86 +65,16 @@ export class ImageViewerComponent extends Ion implements OnInit, OnDestroy, Afte
7565
7666 ngOnInit ( ) {
7767 let gestureCallBack = ( ) => this . _nav . pop ( ) ;
78- this . _zone . runOutsideAngular ( ( ) => this . dragGesture = new ImageViewerGesture ( this . platform , this , this . domCtrl , gestureCallBack ) ) ;
68+ this . _zone . runOutsideAngular ( ( ) => this . dragGesture = new ImageViewerTransitionGesture ( this . platform , this , this . domCtrl , this . renderer , gestureCallBack ) ) ;
7969 }
8070
8171 ngAfterViewInit ( ) {
8272 // imageContainer is set after the view has been initialized
83- this . _zone . runOutsideAngular ( ( ) => {
84- this . pinchGesture = new Gesture ( this . imageContainer . nativeElement , { direction : DIRECTION_HORIZONTAL | DIRECTION_VERTICAL } ) ;
85- this . pinchGesture . listen ( ) ;
86- this . pinchGesture . on ( 'pinch' , ( e ) => this . onPinch ( e ) ) ;
87- this . pinchGesture . on ( 'pinchend' , ( e ) => this . onPinchEnd ( e ) ) ;
88- this . pinchGesture . on ( 'pan' , ( e ) => this . onPan ( e ) ) ;
89- } ) ;
73+ this . _zone . runOutsideAngular ( ( ) => this . pinchGesture = new ImageViewerZoomGesture ( this , this . imageContainer , this . platform , this . renderer ) ) ;
9074 }
9175
9276 ngOnDestroy ( ) {
9377 this . dragGesture && this . dragGesture . destroy ( ) ;
9478 this . pinchGesture && this . pinchGesture . destroy ( ) ;
9579 }
96-
97- onImageClick ( ) {
98- if ( this . dblClickInAction ) {
99- this . onImageDblClick ( ) ;
100- } else {
101- this . dblClickInAction = true ;
102- setTimeout ( ( ) => this . dblClickInAction = false , DOUBLE_TAP_INTERVAL ) ;
103- }
104- }
105-
106- onImageDblClick ( ) {
107- // Clear eventual transforms caused by a previous pinch
108- this . imageContainer . nativeElement . style . transform = '' ;
109-
110- this . isZoomed = ! this . isZoomed ;
111-
112- if ( this . isZoomed ) {
113- this . currentScale === 2 ;
114- }
115-
116- this . renderer . setElementClass ( this . imageContainer . nativeElement , 'zoom' , this . isZoomed ) ;
117- }
118-
119- onPinch ( event ) {
120- this . dragGesture . abort ( event ) ;
121-
122- this . currentScale = Math . max ( 1 , Math . min ( MAX_SCALE , this . adjustScale * event . scale ) ) ;
123-
124- if ( this . currentScale === 1 ) {
125- this . currentDeltaX = 0 ;
126- this . currentDeltaY = 0 ;
127- } else {
128- this . currentDeltaX = this . adjustDeltaX + ( event . deltaX / this . currentScale ) ;
129- this . currentDeltaY = this . adjustDeltaY + ( event . deltaY / this . currentScale ) ;
130- }
131-
132- this . setImageContainerTransform ( ) ;
133- }
134-
135- onPan ( event ) {
136- if ( this . isZoomed ) {
137- this . currentDeltaX = this . adjustDeltaX + event . deltaX ;
138- this . currentDeltaY = this . adjustDeltaY + event . deltaY ;
139-
140- this . setImageContainerTransform ( ) ;
141- }
142- }
143-
144- onPinchEnd ( event ) {
145- this . isZoomed = ( this . currentScale !== 1 ) ;
146-
147- // Saving the final transforms for adjustment next time the user interacts.
148- this . adjustScale = this . currentScale ;
149- this . adjustDeltaX = this . currentDeltaX ;
150- this . adjustDeltaY = this . currentDeltaY ;
151- }
152-
153- setImageContainerTransform ( ) {
154- const transforms = [ ] ;
155- transforms . push ( `scale(${ this . currentScale } )` ) ;
156- transforms . push ( `translate(${ this . currentDeltaX } px, ${ this . currentDeltaY } px)` ) ;
157-
158- this . renderer . setElementStyle ( this . imageContainer . nativeElement , 'transform' , transforms . join ( ' ' ) ) ;
159- }
16080}
0 commit comments