1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/frontend/app/gallery/lightbox/lightbox.gallery.component.ts

425 lines
12 KiB
TypeScript
Raw Normal View History

2018-03-31 03:30:30 +08:00
import {ChangeDetectorRef, Component, ElementRef, EventEmitter, HostListener, OnDestroy, Output, QueryList, ViewChild} from '@angular/core';
import {PhotoDTO} from '../../../../common/entities/PhotoDTO';
import {GalleryPhotoComponent} from '../grid/photo/photo.grid.gallery.component';
import {Dimension} from '../../model/IRenderable';
import {FullScreenService} from '../fullscreen.service';
import {OverlayService} from '../overlay.service';
import {Subscription} from 'rxjs';
import {animate, AnimationBuilder, AnimationPlayer, style} from '@angular/animations';
import {GalleryLightboxPhotoComponent} from './photo/photo.lightbox.gallery.component';
import {Observable} from 'rxjs/Observable';
@Component({
selector: 'gallery-lightbox',
styleUrls: ['./lightbox.gallery.component.css'],
2017-07-11 04:00:22 +08:00
templateUrl: './lightbox.gallery.component.html'
})
export class GalleryLightboxComponent implements OnDestroy {
2017-07-18 00:30:16 +08:00
@Output('onLastElement') onLastElement = new EventEmitter();
2018-03-31 03:30:30 +08:00
@ViewChild('photo') photoElement: GalleryLightboxPhotoComponent;
@ViewChild('lightbox') lightboxElement: ElementRef;
public navigation = {hasPrev: true, hasNext: true};
public blackCanvasOpacity: any = 0;
2017-07-20 04:40:27 +08:00
private activePhotoId: number = null;
public activePhoto: GalleryPhotoComponent;
private gridPhotoQL: QueryList<GalleryPhotoComponent>;
public visible = false;
private changeSubscription: Subscription = null;
private timer: Observable<number>;
private timerSub: Subscription;
public playBackState: number = 0;
public controllersDimmed = true;
public controllersVisible = true;
public infoPanelVisible = false;
public infoPanelWidth = 0;
2017-07-17 02:49:12 +08:00
public animating = false;
2016-05-01 03:36:24 +08:00
2016-07-06 18:53:49 +08:00
2017-07-11 04:00:22 +08:00
constructor(public fullScreenService: FullScreenService,
private changeDetector: ChangeDetectorRef, private overlayService: OverlayService,
private _builder: AnimationBuilder) {
}
2016-05-01 00:33:07 +08:00
ngOnInit(): void {
this.timer = Observable.timer(1000, 2000);
}
2016-05-01 03:36:24 +08:00
ngOnDestroy(): void {
this.pause();
if (this.changeSubscription != null) {
this.changeSubscription.unsubscribe();
}
2017-07-20 04:40:27 +08:00
if (this.visibilityTimer != null) {
clearTimeout(this.visibilityTimer);
}
if (this.iPvisibilityTimer != null) {
clearTimeout(this.iPvisibilityTimer);
}
}
//noinspection JSUnusedGlobalSymbols
@HostListener('window:resize', ['$event'])
onResize() {
if (this.activePhoto) {
2017-07-11 04:00:22 +08:00
this.animateLightbox();
this.updateActivePhoto(this.activePhotoId);
}
}
public nextImage() {
if (this.activePhotoId + 1 < this.gridPhotoQL.length) {
this.showPhoto(this.activePhotoId + 1);
if (this.activePhotoId + 3 >= this.gridPhotoQL.length) {
this.onLastElement.emit({}); //trigger to render more photos if there are
}
return;
2016-05-13 05:00:38 +08:00
}
}
2016-05-13 05:00:38 +08:00
public prevImage() {
this.pause();
if (this.activePhotoId > 0) {
this.showPhoto(this.activePhotoId - 1);
return;
2016-05-13 05:00:38 +08:00
}
}
2016-05-13 05:00:38 +08:00
2017-07-11 04:00:22 +08:00
private showPhoto(photoIndex: number, resize: boolean = true) {
this.activePhoto = null;
this.changeDetector.detectChanges();
2017-07-11 04:00:22 +08:00
this.updateActivePhoto(photoIndex, resize);
}
public show(photo: PhotoDTO) {
this.controllersVisible = true;
this.showControls();
this.visible = true;
let selectedPhoto = this.findPhotoComponent(photo);
if (selectedPhoto === null) {
2018-03-31 03:30:30 +08:00
throw new Error('Can\'t find Photo');
}
2017-07-11 04:00:22 +08:00
const lightboxDimension = selectedPhoto.getDimension();
lightboxDimension.top -= this.getBodyScrollTop();
2017-07-16 05:54:17 +08:00
this.animating = true;
this.animatePhoto(selectedPhoto.getDimension(), this.calcLightBoxPhotoDimension(selectedPhoto.gridPhoto.photo)).onDone(() => {
this.animating = false;
});
2017-07-11 04:00:22 +08:00
this.animateLightbox(
lightboxDimension,
<Dimension>{
top: 0,
left: 0,
2017-07-17 02:49:12 +08:00
width: this.getPhotoFrameWidth(),
height: this.getPhotoFrameHeight()
2017-07-11 04:00:22 +08:00
});
this.blackCanvasOpacity = 0;
this.startPhotoDimension = selectedPhoto.getDimension();
//disable scroll
this.overlayService.showOverlay();
this.blackCanvasOpacity = 1.0;
this.showPhoto(this.gridPhotoQL.toArray().indexOf(selectedPhoto), false);
}
2018-03-31 03:30:30 +08:00
startPhotoDimension: Dimension = <Dimension>{top: 0, left: 0, width: 0, height: 0};
private updateActivePhoto(photoIndex: number, resize: boolean = true) {
let pcList = this.gridPhotoQL.toArray();
if (photoIndex < 0 || photoIndex > this.gridPhotoQL.length) {
throw new Error('Can\'t find the photo');
}
this.activePhotoId = photoIndex;
this.activePhoto = pcList[photoIndex];
if (resize) {
this.animatePhoto(this.calcLightBoxPhotoDimension(this.activePhoto.gridPhoto.photo));
}
this.navigation.hasPrev = photoIndex > 0;
this.navigation.hasNext = photoIndex + 1 < pcList.length;
let to = this.activePhoto.getDimension();
//if target image out of screen -> scroll to there
if (this.getBodyScrollTop() > to.top || this.getBodyScrollTop() + this.getPhotoFrameHeight() < to.top) {
this.setBodyScrollTop(to.top);
}
}
public hide() {
this.controllersVisible = false;
this.fullScreenService.exitFullScreen();
2017-07-13 00:31:19 +08:00
this.pause();
2017-07-16 05:54:17 +08:00
this.animating = true;
2017-07-11 04:00:22 +08:00
const lightboxDimension = this.activePhoto.getDimension();
lightboxDimension.top -= this.getBodyScrollTop();
this.blackCanvasOpacity = 0;
2017-07-11 04:00:22 +08:00
this.animatePhoto(this.calcLightBoxPhotoDimension(this.activePhoto.gridPhoto.photo), this.activePhoto.getDimension());
this.animateLightbox(<Dimension>{
top: 0,
left: 0,
2017-07-17 02:49:12 +08:00
width: this.getPhotoFrameWidth(),
height: this.getPhotoFrameHeight()
2017-07-16 05:54:17 +08:00
}, lightboxDimension).onDone(() => {
2017-07-11 04:00:22 +08:00
this.visible = false;
this.activePhoto = null;
2017-07-20 04:40:27 +08:00
this.activePhotoId = null;
this.overlayService.hideOverlay();
2017-07-16 05:54:17 +08:00
});
2017-07-11 04:41:30 +08:00
this.hideInfoPanel(false);
}
2017-07-16 05:54:17 +08:00
animatePhoto(from: Dimension, to: Dimension = from): AnimationPlayer {
const elem = this._builder.build([
2017-07-11 04:00:22 +08:00
style(<any>Dimension.toString(from)),
animate(300,
style(<any>Dimension.toString(to)))
])
2017-07-16 05:54:17 +08:00
.create(this.photoElement.elementRef.nativeElement);
elem.play();
return elem;
2017-07-11 04:00:22 +08:00
}
animateLightbox(from: Dimension = <Dimension>{
top: 0,
left: 0,
2017-07-17 02:49:12 +08:00
width: this.getPhotoFrameWidth(),
height: this.getPhotoFrameHeight()
2017-07-16 05:54:17 +08:00
}, to: Dimension = from): AnimationPlayer {
const elem = this._builder.build([
2017-07-11 04:00:22 +08:00
style(<any>Dimension.toString(from)),
animate(300,
style(<any>Dimension.toString(to)))
])
2017-07-16 05:54:17 +08:00
.create(this.lightboxElement.nativeElement);
elem.play();
return elem;
2017-07-11 04:00:22 +08:00
}
setGridPhotoQL(value: QueryList<GalleryPhotoComponent>) {
if (this.changeSubscription != null) {
this.changeSubscription.unsubscribe();
}
this.gridPhotoQL = value;
this.changeSubscription = this.gridPhotoQL.changes.subscribe(() => {
if (this.activePhotoId != null && this.gridPhotoQL.length > this.activePhotoId) {
this.updateActivePhoto(this.activePhotoId);
}
});
}
2017-07-11 04:00:22 +08:00
private findPhotoComponent(photo: any): GalleryPhotoComponent {
let galleryPhotoComponents = this.gridPhotoQL.toArray();
for (let i = 0; i < galleryPhotoComponents.length; i++) {
if (galleryPhotoComponents[i].gridPhoto.photo == photo) {
return galleryPhotoComponents[i];
}
}
return null;
}
//noinspection JSUnusedGlobalSymbols
@HostListener('window:keydown', ['$event'])
onKeyPress(e: KeyboardEvent) {
if (this.visible != true) {
return;
}
let event: KeyboardEvent = window.event ? <any>window.event : e;
switch (event.keyCode) {
case 37:
if (this.activePhotoId > 0) {
this.prevImage();
2016-12-30 18:58:04 +08:00
}
break;
case 39:
if (this.activePhotoId < this.gridPhotoQL.length - 1) {
this.nextImage();
}
break;
case 27: //escape
this.hide();
break;
2016-07-05 04:10:30 +08:00
}
}
2016-07-05 04:10:30 +08:00
iPvisibilityTimer = null;
public toggleInfoPanel() {
if (this.infoPanelWidth != 400) {
this.showInfoPanel();
} else {
this.hideInfoPanel();
}
}
showInfoPanel() {
this.infoPanelVisible = true;
2017-07-11 04:00:22 +08:00
const starPhotoPos = this.calcLightBoxPhotoDimension(this.activePhoto.gridPhoto.photo);
this.infoPanelWidth = 400;
const endPhotoPos = this.calcLightBoxPhotoDimension(this.activePhoto.gridPhoto.photo);
this.animatePhoto(starPhotoPos, endPhotoPos);
this.animateLightbox(<Dimension>{
top: 0,
left: 0,
2017-07-17 02:49:12 +08:00
width: this.getPhotoFrameWidth() + 400,
height: this.getPhotoFrameHeight()
2017-07-11 04:00:22 +08:00
},
<Dimension>{
top: 0,
left: 0,
2017-07-17 02:49:12 +08:00
width: this.getPhotoFrameWidth(),
height: this.getPhotoFrameHeight()
2017-07-11 04:00:22 +08:00
});
if (this.iPvisibilityTimer != null) {
clearTimeout(this.iPvisibilityTimer);
}
}
2017-07-11 04:41:30 +08:00
hideInfoPanel(animate: boolean = true) {
this.iPvisibilityTimer = setTimeout(() => {
this.iPvisibilityTimer = null;
this.infoPanelVisible = false;
}, 1000);
2017-07-11 04:00:22 +08:00
const starPhotoPos = this.calcLightBoxPhotoDimension(this.activePhoto.gridPhoto.photo);
this.infoPanelWidth = 0;
const endPhotoPos = this.calcLightBoxPhotoDimension(this.activePhoto.gridPhoto.photo);
2017-07-11 04:41:30 +08:00
if (animate) {
this.animatePhoto(starPhotoPos, endPhotoPos);
}
if (animate) {
this.animateLightbox(<Dimension>{
top: 0,
left: 0,
2017-07-17 02:49:12 +08:00
width: this.getPhotoFrameWidth() - 400,
height: this.getPhotoFrameHeight()
2017-07-11 04:41:30 +08:00
},
<Dimension>{
top: 0,
left: 0,
2017-07-17 02:49:12 +08:00
width: this.getPhotoFrameWidth(),
height: this.getPhotoFrameHeight()
2017-07-11 04:41:30 +08:00
});
}
}
2016-05-01 03:36:24 +08:00
private getBodyScrollTop(): number {
return window.scrollY;
}
2016-06-16 22:24:47 +08:00
private setBodyScrollTop(value: number) {
window.scrollTo(window.scrollX, value);
}
2016-05-01 03:36:24 +08:00
public getPhotoFrameWidth() {
return Math.max(window.innerWidth - this.infoPanelWidth, 0);
}
2016-07-06 18:53:49 +08:00
public getPhotoFrameHeight() {
return window.innerHeight;
}
2016-12-30 18:58:04 +08:00
private calcLightBoxPhotoDimension(photo: PhotoDTO): Dimension {
let width = 0;
let height = 0;
2017-07-17 02:49:12 +08:00
const photoAspect = photo.metadata.size.width / photo.metadata.size.height;
const windowAspect = this.getPhotoFrameWidth() / this.getPhotoFrameHeight();
if (photoAspect < windowAspect) {
width = Math.round(photo.metadata.size.width * (this.getPhotoFrameHeight() / photo.metadata.size.height));
height = this.getPhotoFrameHeight();
} else {
2017-07-17 02:49:12 +08:00
width = this.getPhotoFrameWidth();
height = Math.round(photo.metadata.size.height * (this.getPhotoFrameWidth() / photo.metadata.size.width));
2016-05-01 03:36:24 +08:00
}
2017-07-17 02:49:12 +08:00
let top = (this.getPhotoFrameHeight() / 2 - height / 2);
let left = (this.getPhotoFrameWidth() / 2 - width / 2);
2016-05-01 03:36:24 +08:00
return <Dimension>{top: top, left: left, width: width, height: height};
}
visibilityTimer = null;
@HostListener('mousemove')
onMousemove() {
this.showControls();
}
private showControls() {
this.controllersDimmed = true;
if (this.visibilityTimer != null) {
clearTimeout(this.visibilityTimer);
}
this.visibilityTimer = setTimeout(this.hideControls, 2000);
}
private hideControls = () => {
this.controllersDimmed = false;
};
2017-07-17 02:49:12 +08:00
public pause() {
if (this.timerSub != null) {
this.timerSub.unsubscribe();
}
this.playBackState = 0;
}
public play() {
this.pause();
this.timerSub = this.timer.filter(t => t % 2 == 0).subscribe(() => {
2017-07-28 06:04:19 +08:00
if (this.photoElement.imageLoadFinished == false) {
return;
}
if (this.navigation.hasNext) {
this.nextImage();
} else {
this.showPhoto(0);
}
});
this.playBackState = 1;
}
public fastForward() {
this.pause();
this.timerSub = this.timer.subscribe(() => {
2017-07-28 06:04:19 +08:00
if (this.photoElement.imageLoadFinished == false) {
return;
}
if (this.navigation.hasNext) {
this.nextImage();
} else {
this.showPhoto(0);
}
});
this.playBackState = 2;
}
}