1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2024-12-14 11:23:17 +02:00
pigallery2/frontend/app/gallery/lightbox/lightbox.gallery.component.ts

242 lines
7.6 KiB
TypeScript
Raw Normal View History

import {
ChangeDetectorRef,
Component,
ElementRef,
EventEmitter,
HostListener,
Output,
QueryList,
ViewChild
} from "@angular/core";
2016-12-27 21:55:51 +02:00
import {PhotoDTO} from "../../../../common/entities/PhotoDTO";
2016-12-27 00:36:38 +02:00
import {GalleryPhotoComponent} from "../grid/photo/photo.grid.gallery.component";
2016-04-30 21:36:24 +02:00
import {Dimension} from "../../model/IRenderable";
2016-07-06 12:53:49 +02:00
import {FullScreenService} from "../fullscreen.service";
import {OverlayService} from "../overlay.service";
import {Subscription} from "rxjs";
@Component({
selector: 'gallery-lightbox',
styleUrls: ['app/gallery/lightbox/lightbox.gallery.component.css'],
2016-05-13 14:27:00 +02:00
templateUrl: 'app/gallery/lightbox/lightbox.gallery.component.html',
})
2016-04-30 21:36:24 +02:00
export class GalleryLightboxComponent {
@Output('onLastElement') onLastElement = new EventEmitter();
2016-05-13 14:27:00 +02:00
public navigation = {hasPrev: true, hasNext: true};
2016-12-30 12:58:04 +02:00
public photoDimension: Dimension = <Dimension>{top: 0, left: 0, width: 0, height: 0};
public lightboxDimension: Dimension = <Dimension>{top: 0, left: 0, width: 0, height: 0};
private transition: string = "";
public blackCanvasOpacity: any = 0;
2016-12-27 00:36:38 +02:00
private activePhoto: GalleryPhotoComponent;
private gridPhotoQL: QueryList<GalleryPhotoComponent>;
private visible = false;
private changeSubscription: Subscription = null;
2016-04-30 21:36:24 +02:00
2016-12-27 00:36:38 +02:00
@ViewChild("root") elementRef: ElementRef;
2016-04-30 21:36:24 +02:00
2016-07-06 12:53:49 +02:00
constructor(private fullScreenService: FullScreenService, private changeDetector: ChangeDetectorRef, private overlayService: OverlayService) {
}
2016-04-30 18:33:07 +02:00
2016-04-30 21:36:24 +02:00
//noinspection JSUnusedGlobalSymbols
@HostListener('window:resize', ['$event'])
onResize() {
if (this.activePhoto) {
this.disableAnimation();
this.lightboxDimension.width = this.getScreenWidth();
this.lightboxDimension.height = this.getScreenHeight();
this.updateActivePhoto(this.activePhotoId);
}
}
2016-05-12 23:00:38 +02:00
public nextImage() {
2016-12-30 12:58:04 +02:00
this.disableAnimation();
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
2016-05-12 23:00:38 +02:00
}
return;
2016-05-12 23:00:38 +02:00
}
console.warn("can't find photo to show next");
2016-05-12 23:00:38 +02:00
}
public prevImage() {
2016-12-30 12:58:04 +02:00
this.disableAnimation();
if (this.activePhotoId > 0) {
this.showPhoto(this.activePhotoId - 1);
return;
2016-05-12 23:00:38 +02:00
}
console.warn("can't find photo to show prev");
2016-05-12 23:00:38 +02:00
}
activePhotoId: number = null;
private showPhoto(photoIndex: number) {
this.activePhoto = null;
this.changeDetector.detectChanges();
this.updateActivePhoto(photoIndex);
}
private updateActivePhoto(photoIndex: number) {
2016-12-30 12:58:04 +02:00
let pcList = this.gridPhotoQL.toArray();
2016-07-04 22:10:30 +02:00
if (photoIndex < 0 || photoIndex > this.gridPhotoQL.length) {
2016-12-30 12:58:04 +02:00
throw new Error("Can't find the photo");
}
this.activePhotoId = photoIndex;
this.activePhoto = pcList[photoIndex];
this.photoDimension = 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.getScreenHeight() < to.top) {
this.setBodyScrollTop(to.top);
}
2016-07-04 22:10:30 +02:00
}
2016-12-27 21:55:51 +02:00
public show(photo: PhotoDTO) {
2016-12-30 12:58:04 +02:00
this.enableAnimation();
this.visible = true;
2016-05-13 14:27:00 +02:00
let selectedPhoto = this.findPhotoComponent(photo);
2016-04-30 21:36:24 +02:00
if (selectedPhoto === null) {
2016-12-30 12:58:04 +02:00
throw new Error("Can't find Photo");
}
2016-12-30 12:58:04 +02:00
this.lightboxDimension = selectedPhoto.getDimension();
this.lightboxDimension.top -= this.getBodyScrollTop();
this.blackCanvasOpacity = 0;
this.photoDimension = selectedPhoto.getDimension();
//disable scroll
this.overlayService.showOverlay();
2016-12-30 12:58:04 +02:00
setImmediate(() => {
this.lightboxDimension = <Dimension>{
top: 0,
left: 0,
width: this.getScreenWidth(),
height: this.getScreenHeight()
};
this.blackCanvasOpacity = 1.0;
this.showPhoto(this.gridPhotoQL.toArray().indexOf(selectedPhoto));
2016-12-30 12:58:04 +02:00
});
}
public hide() {
2016-12-30 12:58:04 +02:00
this.enableAnimation();
2016-07-06 12:53:49 +02:00
this.fullScreenService.exitFullScreen();
2016-04-30 21:36:24 +02:00
2016-12-30 12:58:04 +02:00
this.lightboxDimension = this.activePhoto.getDimension();
this.lightboxDimension.top -= this.getBodyScrollTop();
this.blackCanvasOpacity = 0;
this.photoDimension = this.activePhoto.getDimension();
setTimeout(() => {
this.visible = false;
this.activePhoto = null;
this.overlayService.hideOverlay();
2016-12-30 12:58:04 +02:00
}, 500);
2016-06-16 16:24:47 +02:00
2016-04-30 21:36:24 +02:00
}
2016-07-06 12:53:49 +02: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);
}
});
}
2016-12-27 00:36:38 +02:00
private findPhotoComponent(photo: any) {
2016-04-30 21:36:24 +02:00
let galleryPhotoComponents = this.gridPhotoQL.toArray();
for (let i = 0; i < galleryPhotoComponents.length; i++) {
2016-05-12 11:00:46 +02:00
if (galleryPhotoComponents[i].gridPhoto.photo == photo) {
2016-07-04 22:10:30 +02:00
return galleryPhotoComponents[i];
2016-04-30 21:36:24 +02:00
}
}
2016-07-04 22:10:30 +02:00
return null;
2016-04-30 21:36:24 +02:00
}
//noinspection JSUnusedGlobalSymbols
2016-07-05 13:33:09 +02:00
@HostListener('window:keydown', ['$event'])
2016-12-27 00:36:38 +02:00
onKeyPress(e: KeyboardEvent) {
if (this.visible != true) {
return;
}
2016-12-27 00:36:38 +02:00
let event: KeyboardEvent = window.event ? <any>window.event : e;
switch (event.keyCode) {
case 37:
if (this.activePhotoId > 0) {
this.prevImage();
}
break;
case 39:
if (this.activePhotoId < this.gridPhotoQL.length - 1) {
this.nextImage();
}
break;
case 27: //escape
this.hide();
break;
}
}
2016-04-30 18:33:07 +02:00
2016-12-30 12:58:04 +02:00
private enableAnimation() {
this.transition = null;
}
private disableAnimation() {
this.transition = "initial";
}
2016-12-27 00:36:38 +02:00
private getBodyScrollTop(): number {
2016-07-05 12:34:11 +02:00
return window.scrollY;
2016-04-30 21:36:24 +02:00
}
2016-12-27 00:36:38 +02:00
private setBodyScrollTop(value: number) {
2016-07-05 12:34:11 +02:00
window.scrollTo(window.scrollX, value);
}
2016-04-30 21:36:24 +02:00
private getScreenWidth() {
return window.innerWidth;
}
private getScreenHeight() {
return window.innerHeight;
}
2016-04-30 21:36:24 +02:00
2016-05-09 17:04:56 +02:00
2016-12-27 21:55:51 +02:00
private calcLightBoxPhotoDimension(photo: PhotoDTO): Dimension {
2016-04-30 21:36:24 +02:00
let width = 0;
let height = 0;
if (photo.metadata.size.height > photo.metadata.size.width) {
width = Math.round(photo.metadata.size.width * (this.getScreenHeight() / photo.metadata.size.height));
2016-05-09 17:04:56 +02:00
height = this.getScreenHeight();
2016-04-30 21:36:24 +02:00
} else {
2016-05-09 17:04:56 +02:00
width = this.getScreenWidth();
height = Math.round(photo.metadata.size.height * (this.getScreenWidth() / photo.metadata.size.width));
2016-04-30 21:36:24 +02:00
}
let top = (this.getScreenHeight() / 2 - height / 2);
let left = (this.getScreenWidth() / 2 - width / 2);
2016-12-30 12:58:04 +02:00
return <Dimension>{top: top, left: left, width: width, height: height};
}
}