|
| 1 | +import { EventEmitter, Output } from '@angular/core'; |
| 2 | +import { Component, OnInit, Input, Renderer2, ViewChild, ElementRef, ChangeDetectorRef } from '@angular/core'; |
| 3 | +import Hammer from 'hammerjs'; |
| 4 | +import { fromEvent } from 'rxjs'; |
| 5 | +import { throttleTime, reduce } from 'rxjs/operators'; |
| 6 | +import { Subscription } from 'rxjs'; |
| 7 | +import { trigger, transition, style, animate, state } from '@angular/animations'; |
| 8 | + |
| 9 | +@Component({ |
| 10 | + selector: 'fiv-dialog', |
| 11 | + templateUrl: './dialog.component.html', |
| 12 | + styleUrls: ['./dialog.component.scss'], |
| 13 | + animations: [ |
| 14 | + trigger('dialogAnim', [ |
| 15 | + transition('hidden => slideIn-top', [ |
| 16 | + style({ |
| 17 | + transform: 'translateY(-100%)' |
| 18 | + }), |
| 19 | + animate('275ms 100ms cubic-bezier(0.32,1,0.23,1)') |
| 20 | + ]), |
| 21 | + transition('hidden => slideIn-bottom', [ |
| 22 | + style({ |
| 23 | + transform: 'translateY(100%)' |
| 24 | + }), |
| 25 | + animate('275ms 100ms cubic-bezier(0.32,1,0.23,1)') |
| 26 | + ]), |
| 27 | + transition('hidden => fadeIn-bottom', [ |
| 28 | + style({ |
| 29 | + opacity: 0 |
| 30 | + }), |
| 31 | + animate('360ms ease-out') |
| 32 | + ]), |
| 33 | + transition('hidden => fadeIn-top', [ |
| 34 | + style({ |
| 35 | + opacity: 0 |
| 36 | + }), |
| 37 | + animate('360ms ease-out') |
| 38 | + ]), |
| 39 | + transition('hidden => fadeIn-center', [ |
| 40 | + style({ |
| 41 | + opacity: 0, |
| 42 | + top: '50%', |
| 43 | + transform: 'translateY(-50%)', |
| 44 | + }), |
| 45 | + animate('360ms ease-out') |
| 46 | + ]), |
| 47 | + transition('hidden => slideIn-center', [ |
| 48 | + style({ |
| 49 | + top: '50%', |
| 50 | + transform: 'translateY(-25%)', |
| 51 | + opacity: 0 |
| 52 | + }), |
| 53 | + animate('275ms 100ms cubic-bezier(0.32,1,0.23,1)') |
| 54 | + ]), |
| 55 | + transition('slideIn-center => hidden', [ |
| 56 | + animate('120ms ease-out', style({ |
| 57 | + // top: '50%', |
| 58 | + // transform: 'translateY(+50%)', |
| 59 | + opacity: 0 |
| 60 | + })) |
| 61 | + ]), |
| 62 | + transition('* => hidden', [ |
| 63 | + animate('240ms ease-out') |
| 64 | + ]), |
| 65 | + state('hidden', style({ |
| 66 | + display: 'none', |
| 67 | + opacity: '0', |
| 68 | + }) |
| 69 | + ), |
| 70 | + state('slideIn-top', style({ |
| 71 | + top: 0, |
| 72 | + }) |
| 73 | + ), |
| 74 | + state('slideIn-bottom', style({ |
| 75 | + bottom: 0 |
| 76 | + }) |
| 77 | + ), |
| 78 | + state('fadeIn-top', style({ |
| 79 | + top: 0 |
| 80 | + }) |
| 81 | + ), |
| 82 | + state('fadeIn-bottom', style({ |
| 83 | + bottom: 0 |
| 84 | + }) |
| 85 | + ), |
| 86 | + state('fadeIn-center', style({ |
| 87 | + top: '50%', |
| 88 | + transform: 'translateY(-50%)', |
| 89 | + 'margin-top': 0 |
| 90 | + }) |
| 91 | + ), |
| 92 | + state('slideIn-center', style({ |
| 93 | + top: '50%', |
| 94 | + transform: 'translateY(-50%)', |
| 95 | + 'margin-top': 0 |
| 96 | + }) |
| 97 | + ), |
| 98 | + ]), trigger('backdropAnim', [ |
| 99 | + transition('void => *', [ |
| 100 | + style({ |
| 101 | + opacity: 0 |
| 102 | + }), |
| 103 | + animate('275ms 100ms cubic-bezier(0.32,1,0.23,1)') |
| 104 | + ]), |
| 105 | + transition('* => void', [ |
| 106 | + animate('120ms 100ms cubic-bezier(0.32,1,0.23,1)'), style({ |
| 107 | + opacity: 0 |
| 108 | + }), |
| 109 | + ]) |
| 110 | + ])] |
| 111 | +}) |
| 112 | +export class DialogComponent implements OnInit { |
| 113 | + |
| 114 | + animationState = 'hidden'; |
| 115 | + |
| 116 | + @Input() animation: 'slideIn' | 'fadeIn' = 'slideIn'; |
| 117 | + @Input() verticalAlign: 'top' | 'bottom' | 'center' = 'bottom'; |
| 118 | + @Input() backdrop = false; |
| 119 | + @Input() backdropClose = true; |
| 120 | + @Input() swipeEnabled = true; |
| 121 | + |
| 122 | + @Output() fivClose: EventEmitter<DialogComponent> = new EventEmitter(); |
| 123 | + @Output() fivOpen: EventEmitter<DialogComponent> = new EventEmitter(); |
| 124 | + |
| 125 | + @ViewChild('card') card: ElementRef; |
| 126 | + @ViewChild('backdrop') backdropElem: ElementRef; |
| 127 | + |
| 128 | + private panSubs: Subscription[] = []; |
| 129 | + |
| 130 | + constructor(private renderer: Renderer2, public change: ChangeDetectorRef) { |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + setupPan(elem: ElementRef, threshold: number) { |
| 135 | + |
| 136 | + if (!this.swipeEnabled) { |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + const hammer = new Hammer(elem); |
| 141 | + hammer.get('pan').set({ direction: Hammer.DIRECTION_ALL }); |
| 142 | + |
| 143 | + const panup = fromEvent(hammer, 'panup') |
| 144 | + .pipe( |
| 145 | + throttleTime(2) |
| 146 | + ) |
| 147 | + .subscribe(res => { |
| 148 | + console.log('panup', res); |
| 149 | + this.onSwipeUp(res, threshold); |
| 150 | + }); |
| 151 | + |
| 152 | + const pandown = fromEvent(hammer, 'pandown') |
| 153 | + .pipe( |
| 154 | + throttleTime(2) |
| 155 | + ) |
| 156 | + .subscribe(res => { |
| 157 | + console.log('pandown sub', res); |
| 158 | + this.onSwipeDown(res, threshold); |
| 159 | + }); |
| 160 | + |
| 161 | + const panend = fromEvent(hammer, 'panend pancancel') |
| 162 | + .subscribe((res: any) => { |
| 163 | + if (res.distance < threshold) { |
| 164 | + this.renderer.setStyle(this.card.nativeElement, 'transform', `translateY(0px)`); |
| 165 | + } |
| 166 | + }); |
| 167 | + |
| 168 | + this.panSubs.push(pandown, panup, panend); |
| 169 | + |
| 170 | + |
| 171 | + } |
| 172 | + |
| 173 | + ngOnInit() { |
| 174 | + console.log(this.card); |
| 175 | + } |
| 176 | + |
| 177 | + showDialog() { |
| 178 | + this.animationState = `${this.animation}-${this.verticalAlign}`; |
| 179 | + |
| 180 | + } |
| 181 | + |
| 182 | + hideDialog() { |
| 183 | + this.animationState = 'hidden'; |
| 184 | + this.fivClose.emit(this); |
| 185 | + this.panSubs.forEach(sub => { |
| 186 | + sub.unsubscribe(); |
| 187 | + }); |
| 188 | + } |
| 189 | + |
| 190 | + onSwipeUp(event, threshold) { |
| 191 | + console.log(Math.exp(event.distance / 50)); |
| 192 | + const velocity = 1 + Math.exp(event.distance / 50) / 50; |
| 193 | + |
| 194 | + if (this.verticalAlign === 'top') { |
| 195 | + this.renderer.setStyle(this.card.nativeElement, 'transform', `translateY(-${event.distance * velocity}px)`); |
| 196 | + if (event.distance > threshold) { |
| 197 | + console.log('swipe down happened'); |
| 198 | + this.hideDialog(); |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + onSwipeDown(event, threshold) { |
| 204 | + const velocity = 1 + Math.exp(event.distance / 50) / 50; |
| 205 | + console.log(velocity); |
| 206 | + if (this.verticalAlign === 'bottom' || this.verticalAlign === 'center') { |
| 207 | + this.renderer.setStyle(this.card.nativeElement, 'transform', `translateY(${event.distance * velocity}px)`); |
| 208 | + if (event.distance > threshold) { |
| 209 | + console.log('swipe down happened'); |
| 210 | + this.hideDialog(); |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + } |
| 215 | + |
| 216 | + onDialogAnimationDone(event) { |
| 217 | + if (event.toState === 'hidden') { |
| 218 | + this.renderer.setStyle(this.card.nativeElement, 'transform', `translateY(0px)`); |
| 219 | + } |
| 220 | + if (event.fromState === 'hidden') { |
| 221 | + this.setupPan(this.card.nativeElement, 58); |
| 222 | + this.fivOpen.emit(this); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + backdropAnimDone(event) { |
| 227 | + console.log(this.backdropElem, event); |
| 228 | + if (event.fromState === 'void') { |
| 229 | + // this.change.detectChanges(); |
| 230 | + console.log(this.backdropElem.nativeElement); |
| 231 | + this.setupPan(this.backdropElem.nativeElement, 112); |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | +} |
0 commit comments