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

440 lines
14 KiB
TypeScript
Raw Normal View History

2019-07-15 01:31:34 +08:00
import {ChangeDetectorRef, Component, ElementRef, HostListener, OnDestroy, OnInit, QueryList, ViewChild} from '@angular/core';
2018-03-31 03:30:30 +08:00
import {GalleryPhotoComponent} from '../grid/photo/photo.grid.gallery.component';
import {Dimension} from '../../../model/IRenderable';
2018-03-31 03:30:30 +08:00
import {FullScreenService} from '../fullscreen.service';
import {OverlayService} from '../overlay.service';
import {animate, AnimationBuilder, AnimationPlayer, style} from '@angular/animations';
import {GalleryLightboxMediaComponent} from './media/media.lightbox.gallery.component';
2019-07-15 01:31:34 +08:00
import {Subscription} from 'rxjs';
import {ActivatedRoute, Params, Router} from '@angular/router';
import {PageHelper} from '../../../model/page.helper';
import {QueryService} from '../../../model/query.service';
import {MediaDTO} from '../../../../../common/entities/MediaDTO';
import {QueryParams} from '../../../../../common/QueryParams';
import {GalleryService} from '../gallery.service';
import {PhotoDTO} from '../../../../../common/entities/PhotoDTO';
2019-07-15 01:31:34 +08:00
import {ControlsLightboxComponent} from './controls/controls.lightbox.gallery.component';
export enum LightboxStates {
2018-05-29 02:03:12 +08:00
Open = 1,
Closing = 2,
Closed = 3
}
@Component({
2018-05-04 06:23:48 +08:00
selector: 'app-gallery-lightbox',
2019-07-15 01:31:34 +08:00
styleUrls: ['./lightbox.gallery.component.css'],
2017-07-11 04:00:22 +08:00
templateUrl: './lightbox.gallery.component.html'
})
2018-05-04 06:23:48 +08:00
export class GalleryLightboxComponent implements OnDestroy, OnInit {
2017-07-18 00:30:16 +08:00
2019-07-19 23:47:49 +08:00
@ViewChild('photo', {static: true}) mediaElement: GalleryLightboxMediaComponent;
2019-07-19 06:12:22 +08:00
@ViewChild('controls', {static: false}) controls: ControlsLightboxComponent;
@ViewChild('lightbox', {static: false}) lightboxElement: ElementRef;
@ViewChild('root', {static: false}) root: ElementRef;
public navigation = {hasPrev: true, hasNext: true};
2018-11-29 06:49:33 +08:00
public blackCanvasOpacity = 0;
public activePhoto: GalleryPhotoComponent;
public status: LightboxStates = LightboxStates.Closed;
2019-07-15 01:31:34 +08:00
public infoPanelVisible = false;
public infoPanelWidth = 0;
public animating = false;
public photoFrameDim = {width: 1, height: 1, aspect: 1};
2019-07-15 02:01:49 +08:00
private startPhotoDimension: Dimension = <Dimension>{top: 0, left: 0, width: 0, height: 0};
private iPvisibilityTimer: number = null;
private visibilityTimer: number = null;
private delayedMediaShow: string = null;
2019-07-15 01:31:34 +08:00
private activePhotoId: number = null;
private gridPhotoQL: QueryList<GalleryPhotoComponent>;
private subscription: {
photosChange: Subscription,
route: Subscription
} = {
photosChange: null,
route: null
};
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,
private router: Router,
private queryService: QueryService,
private galleryService: GalleryService,
private route: ActivatedRoute) {
}
2016-05-01 00:33:07 +08:00
2019-07-15 01:31:34 +08:00
get Title(): string {
if (!this.activePhoto) {
return null;
}
2019-07-20 06:29:16 +08:00
return (<PhotoDTO>this.activePhoto.gridMedia.media).metadata.caption;
2019-07-15 01:31:34 +08:00
}
public toggleFullscreen(): void {
if (this.fullScreenService.isFullScreenEnabled()) {
this.fullScreenService.exitFullScreen();
} else {
this.fullScreenService.showFullScreen(this.root.nativeElement);
}
}
ngOnInit(): void {
2019-07-15 01:31:34 +08:00
this.updatePhotoFrameDim();
this.subscription.route = this.route.queryParams.subscribe((params: Params) => {
2018-11-30 22:36:42 +08:00
if (params[QueryParams.gallery.photo] && params[QueryParams.gallery.photo] !== '') {
if (!this.gridPhotoQL) {
return this.delayedMediaShow = params[QueryParams.gallery.photo];
}
2018-11-30 22:36:42 +08:00
this.onNavigateTo(params[QueryParams.gallery.photo]);
} else if (this.status === LightboxStates.Open) {
this.delayedMediaShow = null;
2018-12-23 22:04:52 +08:00
this.hideLightbox();
}
});
}
2016-05-01 03:36:24 +08:00
ngOnDestroy(): void {
2019-07-15 01:31:34 +08:00
if (this.controls) {
this.controls.pause();
}
if (this.subscription.photosChange != null) {
this.subscription.photosChange.unsubscribe();
}
if (this.subscription.route != null) {
this.subscription.route.unsubscribe();
}
2017-07-20 04:40:27 +08:00
if (this.visibilityTimer != null) {
clearTimeout(this.visibilityTimer);
}
if (this.iPvisibilityTimer != null) {
clearTimeout(this.iPvisibilityTimer);
}
}
onNavigateTo(photoStringId: string) {
2019-07-20 06:29:16 +08:00
if (this.activePhoto && this.queryService.getMediaStringId(this.activePhoto.gridMedia.media) === photoStringId) {
return;
}
2018-12-18 07:06:25 +08:00
2019-07-15 01:31:34 +08:00
if (this.controls) {
this.controls.resetZoom();
}
2019-07-20 06:29:16 +08:00
const photo = this.gridPhotoQL.find(i => this.queryService.getMediaStringId(i.gridMedia.media) === photoStringId);
if (!photo) {
return this.delayedMediaShow = photoStringId;
}
if (this.status === LightboxStates.Closed) {
2019-07-20 06:29:16 +08:00
this.showLigthbox(photo.gridMedia.media);
} else {
this.showPhoto(this.gridPhotoQL.toArray().indexOf(photo));
}
this.delayedMediaShow = null;
}
setGridPhotoQL(value: QueryList<GalleryPhotoComponent>) {
if (this.subscription.photosChange != null) {
this.subscription.photosChange.unsubscribe();
}
this.gridPhotoQL = value;
this.subscription.photosChange = this.gridPhotoQL.changes.subscribe(() => {
if (this.activePhotoId != null && this.gridPhotoQL.length > this.activePhotoId) {
this.updateActivePhoto(this.activePhotoId);
}
if (this.delayedMediaShow) {
this.onNavigateTo(this.delayedMediaShow);
}
});
if (this.delayedMediaShow) {
this.onNavigateTo(this.delayedMediaShow);
}
}
//noinspection JSUnusedGlobalSymbols
@HostListener('window:resize', ['$event'])
onResize() {
2019-07-19 05:07:07 +08:00
this.updatePhotoFrameDim();
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.navigateToPhoto(this.activePhotoId + 1);
2019-07-15 01:31:34 +08:00
} else {
this.navigateToPhoto(0);
2016-05-13 05:00:38 +08:00
}
}
2016-05-13 05:00:38 +08:00
public prevImage() {
2019-07-15 01:31:34 +08:00
if (this.controls) {
this.controls.pause();
}
if (this.activePhotoId > 0) {
this.navigateToPhoto(this.activePhotoId - 1);
2016-05-13 05:00:38 +08:00
}
}
2016-05-13 05:00:38 +08:00
2018-11-05 02:28:32 +08:00
public showLigthbox(photo: MediaDTO) {
2019-07-15 01:31:34 +08:00
if (this.controls) {
this.controls.resetZoom();
}
this.status = LightboxStates.Open;
2018-05-04 06:23:48 +08:00
const 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 -= PageHelper.ScrollY;
2017-07-16 05:54:17 +08:00
this.animating = true;
2019-07-20 06:29:16 +08:00
this.animatePhoto(selectedPhoto.getDimension(), this.calcLightBoxPhotoDimension(selectedPhoto.gridMedia.media)).onDone(() => {
2017-07-16 05:54:17 +08:00
this.animating = false;
});
2017-07-11 04:00:22 +08:00
this.animateLightbox(
lightboxDimension,
<Dimension>{
top: 0,
left: 0,
2019-07-15 01:31:34 +08:00
width: this.photoFrameDim.width,
height: this.photoFrameDim.height
2017-07-11 04:00:22 +08:00
});
this.blackCanvasOpacity = 0;
this.startPhotoDimension = selectedPhoto.getDimension();
2018-05-04 06:23:48 +08:00
// disable scroll
2017-07-11 04:00:22 +08:00
this.overlayService.showOverlay();
this.blackCanvasOpacity = 1.0;
this.showPhoto(this.gridPhotoQL.toArray().indexOf(selectedPhoto), false);
}
public hide() {
this.router.navigate([],
{queryParams: this.queryService.getParams()}).catch(console.error);
}
2017-07-16 05:54:17 +08:00
animatePhoto(from: Dimension, to: Dimension = from): AnimationPlayer {
const elem = this._builder.build([
2018-12-23 22:04:52 +08:00
style(Dimension.toString(from)),
2017-07-11 04:00:22 +08:00
animate(300,
2018-12-23 22:04:52 +08:00
style(Dimension.toString(to)))
2017-07-11 04:00:22 +08:00
])
.create(this.mediaElement.elementRef.nativeElement);
2017-07-16 05:54:17 +08:00
elem.play();
2019-07-15 01:31:34 +08:00
2017-07-16 05:54:17 +08:00
return elem;
2017-07-11 04:00:22 +08:00
}
animateLightbox(from: Dimension = <Dimension>{
top: 0,
left: 0,
2019-07-15 01:31:34 +08:00
width: this.photoFrameDim.width,
height: this.photoFrameDim.height
2017-07-16 05:54:17 +08:00
}, to: Dimension = from): AnimationPlayer {
const elem = this._builder.build([
2018-12-23 22:04:52 +08:00
style(Dimension.toString(from)),
2017-07-11 04:00:22 +08:00
animate(300,
2018-12-23 22:04:52 +08:00
style(Dimension.toString(to)))
2017-07-11 04:00:22 +08:00
])
2017-07-16 05:54:17 +08:00
.create(this.lightboxElement.nativeElement);
elem.play();
return elem;
2017-07-11 04:00:22 +08:00
}
public toggleInfoPanel() {
2018-05-04 06:23:48 +08:00
if (this.infoPanelWidth !== 400) {
this.showInfoPanel();
} else {
this.hideInfoPanel();
}
}
2018-05-04 06:23:48 +08:00
hideInfoPanel(_animate: boolean = true) {
2018-11-29 06:49:33 +08:00
this.iPvisibilityTimer = window.setTimeout(() => {
2018-05-04 06:23:48 +08:00
this.iPvisibilityTimer = null;
this.infoPanelVisible = false;
}, 1000);
2019-07-20 06:29:16 +08:00
const starPhotoPos = this.calcLightBoxPhotoDimension(this.activePhoto.gridMedia.media);
2018-05-04 06:23:48 +08:00
this.infoPanelWidth = 0;
2019-07-15 01:31:34 +08:00
this.updatePhotoFrameDim();
2019-07-20 06:29:16 +08:00
const endPhotoPos = this.calcLightBoxPhotoDimension(this.activePhoto.gridMedia.media);
2018-05-04 06:23:48 +08:00
if (_animate) {
this.animatePhoto(starPhotoPos, endPhotoPos);
}
if (_animate) {
this.animateLightbox(<Dimension>{
top: 0,
left: 0,
2019-07-15 01:31:34 +08:00
width: Math.max(this.photoFrameDim.width - 400, 0),
height: this.photoFrameDim.height
2018-05-04 06:23:48 +08:00
},
<Dimension>{
top: 0,
left: 0,
2019-07-15 01:31:34 +08:00
width: this.photoFrameDim.width,
height: this.photoFrameDim.height
2018-05-04 06:23:48 +08:00
});
}
}
isInfoPanelAnimating(): boolean {
return this.iPvisibilityTimer != null;
}
showInfoPanel() {
this.infoPanelVisible = true;
2017-07-11 04:00:22 +08:00
2019-07-20 06:29:16 +08:00
const starPhotoPos = this.calcLightBoxPhotoDimension(this.activePhoto.gridMedia.media);
2017-07-11 04:00:22 +08:00
this.infoPanelWidth = 400;
2019-07-15 01:31:34 +08:00
this.updatePhotoFrameDim();
2019-07-20 06:29:16 +08:00
const endPhotoPos = this.calcLightBoxPhotoDimension(this.activePhoto.gridMedia.media);
2017-07-11 04:00:22 +08:00
this.animatePhoto(starPhotoPos, endPhotoPos);
this.animateLightbox(<Dimension>{
top: 0,
left: 0,
2019-07-15 01:31:34 +08:00
width: this.photoFrameDim.width + 400,
height: this.photoFrameDim.height
2017-07-11 04:00:22 +08:00
},
<Dimension>{
top: 0,
left: 0,
2019-07-15 01:31:34 +08:00
width: this.photoFrameDim.width,
height: this.photoFrameDim.height
2017-07-11 04:00:22 +08:00
});
if (this.iPvisibilityTimer != null) {
clearTimeout(this.iPvisibilityTimer);
}
2018-12-18 07:06:25 +08:00
2019-07-15 01:31:34 +08:00
if (this.controls) {
this.controls.resetZoom();
}
}
2019-07-15 01:31:34 +08:00
public isVisible(): boolean {
return this.status !== LightboxStates.Closed;
}
2019-07-20 06:29:16 +08:00
public isOpen(): boolean {
return this.status === LightboxStates.Open;
}
2016-05-01 03:36:24 +08:00
2019-07-15 01:31:34 +08:00
private updatePhotoFrameDim = () => {
this.photoFrameDim.width = Math.max(window.innerWidth - this.infoPanelWidth, 0);
this.photoFrameDim.height = window.innerHeight;
this.photoFrameDim.aspect = Math.round(this.photoFrameDim.width / this.photoFrameDim.height * 100) / 100;
};
private navigateToPhoto(photoIndex: number) {
this.router.navigate([],
2019-07-20 06:29:16 +08:00
{queryParams: this.queryService.getParams(this.gridPhotoQL.toArray()[photoIndex].gridMedia.media)}).catch(console.error);
}
2016-07-06 18:53:49 +08:00
2019-07-15 01:31:34 +08:00
private showPhoto(photoIndex: number, resize: boolean = true) {
this.activePhoto = null;
this.changeDetector.detectChanges();
this.updateActivePhoto(photoIndex, resize);
}
2016-12-30 18:58:04 +08:00
2019-07-15 01:31:34 +08:00
private hideLightbox() {
if (this.controls) {
this.controls.resetZoom();
}
this.status = LightboxStates.Closing;
this.fullScreenService.exitFullScreen();
if (this.controls) {
this.controls.pause();
}
this.animating = true;
const lightboxDimension = this.activePhoto.getDimension();
lightboxDimension.top -= PageHelper.ScrollY;
this.blackCanvasOpacity = 0;
2019-07-20 06:29:16 +08:00
this.animatePhoto(this.calcLightBoxPhotoDimension(this.activePhoto.gridMedia.media), this.activePhoto.getDimension());
2019-07-15 01:31:34 +08:00
this.animateLightbox(<Dimension>{
top: 0,
left: 0,
width: this.photoFrameDim.width,
height: this.photoFrameDim.height
}, lightboxDimension).onDone(() => {
this.status = LightboxStates.Closed;
this.activePhoto = null;
this.activePhotoId = null;
this.overlayService.hideOverlay();
});
this.hideInfoPanel(false);
2018-05-14 10:10:56 +08:00
}
2018-05-04 06:23:48 +08:00
private updateActivePhoto(photoIndex: number, resize: boolean = true) {
const pcList = this.gridPhotoQL.toArray();
2016-12-30 18:58:04 +08:00
2018-05-04 06:23:48 +08:00
if (photoIndex < 0 || photoIndex > this.gridPhotoQL.length) {
2018-11-05 02:28:32 +08:00
throw new Error('Can\'t find the media');
2018-05-04 06:23:48 +08:00
}
this.activePhotoId = photoIndex;
this.activePhoto = pcList[photoIndex];
if (resize) {
2019-07-20 06:29:16 +08:00
this.animatePhoto(this.calcLightBoxPhotoDimension(this.activePhoto.gridMedia.media));
2018-05-04 06:23:48 +08:00
}
this.navigation.hasPrev = photoIndex > 0;
this.navigation.hasNext = photoIndex + 1 < pcList.length;
const to = this.activePhoto.getDimension();
// if target image out of screen -> scroll to there
2019-07-15 01:31:34 +08:00
if (PageHelper.ScrollY > to.top || PageHelper.ScrollY + this.photoFrameDim.height < to.top) {
PageHelper.ScrollY = to.top;
2016-05-01 03:36:24 +08:00
}
}
2018-11-05 02:28:32 +08:00
private findPhotoComponent(media: MediaDTO): GalleryPhotoComponent {
2018-05-04 06:23:48 +08:00
const galleryPhotoComponents = this.gridPhotoQL.toArray();
for (let i = 0; i < galleryPhotoComponents.length; i++) {
2019-07-20 06:29:16 +08:00
if (galleryPhotoComponents[i].gridMedia.media === media) {
2018-05-04 06:23:48 +08:00
return galleryPhotoComponents[i];
}
2018-05-04 06:23:48 +08:00
}
return null;
}
2018-11-05 02:28:32 +08:00
private calcLightBoxPhotoDimension(photo: MediaDTO): Dimension {
2018-05-04 06:23:48 +08:00
let width = 0;
let height = 0;
const photoAspect = photo.metadata.size.width / photo.metadata.size.height;
2019-07-15 01:31:34 +08:00
const windowAspect = this.photoFrameDim.aspect;
2018-05-04 06:23:48 +08:00
if (photoAspect < windowAspect) {
2019-07-15 01:31:34 +08:00
width = Math.round(photo.metadata.size.width * (this.photoFrameDim.height / photo.metadata.size.height));
height = this.photoFrameDim.height;
2018-05-04 06:23:48 +08:00
} else {
2019-07-15 01:31:34 +08:00
width = this.photoFrameDim.width;
height = Math.round(photo.metadata.size.height * (this.photoFrameDim.width / photo.metadata.size.width));
2018-05-04 06:23:48 +08:00
}
2019-07-15 01:31:34 +08:00
const top = (this.photoFrameDim.height / 2 - height / 2);
const left = (this.photoFrameDim.width / 2 - width / 2);
2018-05-04 06:23:48 +08:00
return <Dimension>{top: top, left: left, width: width, height: height};
}
}