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

197 lines
6.1 KiB
TypeScript
Raw Normal View History

2016-07-06 18:53:49 +08:00
import {Component, QueryList, Output, EventEmitter, HostListener, ElementRef, ViewChild} from "@angular/core";
2016-12-28 03:55:51 +08:00
import {PhotoDTO} from "../../../../common/entities/PhotoDTO";
2016-12-27 06:36:38 +08:00
import {GalleryPhotoComponent} from "../grid/photo/photo.grid.gallery.component";
2016-05-01 03:36:24 +08:00
import {Dimension} from "../../model/IRenderable";
2016-07-06 18:53:49 +08:00
import {FullScreenService} from "../fullscreen.service";
@Component({
selector: 'gallery-lightbox',
styleUrls: ['app/gallery/lightbox/lightbox.gallery.component.css'],
2016-05-13 20:27:00 +08:00
templateUrl: 'app/gallery/lightbox/lightbox.gallery.component.html',
})
2016-05-01 03:36:24 +08:00
export class GalleryLightboxComponent {
@Output('onLastElement') onLastElement = new EventEmitter();
2016-05-13 20:27:00 +08:00
public navigation = {hasPrev: true, hasNext: true};
2016-12-30 18:58:04 +08: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 06:36:38 +08:00
private activePhoto: GalleryPhotoComponent;
public gridPhotoQL: QueryList<GalleryPhotoComponent>;
private visible = false;
2016-05-01 03:36:24 +08:00
2016-12-27 06:36:38 +08:00
@ViewChild("root") elementRef: ElementRef;
2016-05-01 03:36:24 +08:00
2016-07-06 18:53:49 +08:00
2016-12-27 06:36:38 +08:00
constructor(private fullScreenService: FullScreenService) {
2016-05-01 00:33:07 +08:00
2016-05-01 03:36:24 +08:00
}
2016-05-13 05:00:38 +08:00
public nextImage() {
2016-12-30 18:58:04 +08:00
this.disableAnimation();
2016-05-13 05:00:38 +08:00
let pcList = this.gridPhotoQL.toArray();
for (let i = 0; i < pcList.length; i++) {
if (pcList[i] === this.activePhoto) {
if (i + 1 < pcList.length) {
this.showPhoto(pcList[i + 1]);
if (i + 3 === pcList.length) {
this.onLastElement.emit({}); //trigger to render more photos if there are
}
}
2016-05-13 05:00:38 +08:00
return;
}
}
}
public prevImage() {
2016-12-30 18:58:04 +08:00
this.disableAnimation();
2016-05-13 05:00:38 +08:00
let pcList = this.gridPhotoQL.toArray();
for (let i = 0; i < pcList.length; i++) {
if (pcList[i] === this.activePhoto) {
if (i > 0) {
this.showPhoto(pcList[i - 1]);
}
2016-05-13 05:00:38 +08:00
return;
}
}
}
2016-12-27 06:36:38 +08:00
private showPhoto(photoComponent: GalleryPhotoComponent) {
2016-12-30 18:58:04 +08:00
let pcList = this.gridPhotoQL.toArray();
2016-07-05 04:10:30 +08:00
2016-12-30 18:58:04 +08:00
let index = pcList.indexOf(photoComponent);
if (index == -1) {
throw new Error("Can't find the photo");
}
2016-07-05 04:10:30 +08:00
2016-12-30 18:58:04 +08:00
this.photoDimension = this.calcLightBoxPhotoDimension(photoComponent.gridPhoto.photo);
this.navigation.hasPrev = index > 0;
this.navigation.hasNext = index + 1 < pcList.length;
this.activePhoto = photoComponent;
2016-07-05 04:10:30 +08:00
}
2016-12-28 03:55:51 +08:00
public show(photo: PhotoDTO) {
2016-12-30 18:58:04 +08:00
this.enableAnimation();
this.visible = true;
2016-05-13 20:27:00 +08:00
let selectedPhoto = this.findPhotoComponent(photo);
2016-05-01 03:36:24 +08:00
if (selectedPhoto === null) {
2016-12-30 18:58:04 +08:00
throw new Error("Can't find Photo");
}
2016-12-30 18:58:04 +08:00
this.lightboxDimension = selectedPhoto.getDimension();
this.lightboxDimension.top -= this.getBodyScrollTop();
this.blackCanvasOpacity = 0;
this.photoDimension = selectedPhoto.getDimension();
2016-12-27 06:36:38 +08:00
document.getElementsByTagName('body')[0].style.overflow = 'hidden';
2016-12-30 18:58:04 +08:00
setImmediate(() => {
this.lightboxDimension = <Dimension>{
top: 0,
left: 0,
width: this.getScreenWidth(),
height: this.getScreenHeight()
};
this.blackCanvasOpacity = 1.0;
this.showPhoto(selectedPhoto);
});
}
public hide() {
2016-12-30 18:58:04 +08:00
this.enableAnimation();
2016-07-06 18:53:49 +08:00
this.fullScreenService.exitFullScreen();
let to = this.activePhoto.getDimension();
//iff target image out of screen -> scroll to there
if (this.getBodyScrollTop() > to.top || this.getBodyScrollTop() + this.getScreenHeight() < to.top) {
this.setBodyScrollTop(to.top);
}
2016-05-01 03:36:24 +08:00
2016-12-30 18:58:04 +08: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;
document.getElementsByTagName('body')[0].style.overflow = 'scroll';
}, 500);
2016-06-16 22:24:47 +08:00
2016-05-01 03:36:24 +08:00
}
2016-07-06 18:53:49 +08:00
2016-12-27 06:36:38 +08:00
private findPhotoComponent(photo: any) {
2016-05-01 03:36:24 +08:00
let galleryPhotoComponents = this.gridPhotoQL.toArray();
for (let i = 0; i < galleryPhotoComponents.length; i++) {
2016-05-12 17:00:46 +08:00
if (galleryPhotoComponents[i].gridPhoto.photo == photo) {
2016-07-05 04:10:30 +08:00
return galleryPhotoComponents[i];
2016-05-01 03:36:24 +08:00
}
}
2016-07-05 04:10:30 +08:00
return null;
2016-05-01 03:36:24 +08:00
}
2016-07-05 19:33:09 +08:00
@HostListener('window:keydown', ['$event'])
2016-12-27 06:36:38 +08:00
onKeyPress(e: KeyboardEvent) {
let event: KeyboardEvent = window.event ? <any>window.event : e;
switch (event.keyCode) {
case 37:
this.prevImage();
break;
case 39:
this.nextImage();
break;
}
}
2016-05-01 00:33:07 +08:00
2016-12-30 18:58:04 +08:00
private enableAnimation() {
this.transition = null;
}
private disableAnimation() {
this.transition = "initial";
}
2016-12-27 06:36:38 +08:00
private getBodyScrollTop(): number {
2016-07-05 18:34:11 +08:00
return window.scrollY;
2016-05-01 03:36:24 +08:00
}
2016-12-27 06:36:38 +08:00
private setBodyScrollTop(value: number) {
2016-07-05 18:34:11 +08:00
window.scrollTo(window.scrollX, value);
}
2016-05-01 03:36:24 +08:00
private getScreenWidth() {
return window.innerWidth;
}
private getScreenHeight() {
return window.innerHeight;
}
2016-05-01 03:36:24 +08:00
2016-05-09 23:04:56 +08:00
2016-12-28 03:55:51 +08:00
private calcLightBoxPhotoDimension(photo: PhotoDTO): Dimension {
2016-05-01 03:36:24 +08: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 23:04:56 +08:00
height = this.getScreenHeight();
2016-05-01 03:36:24 +08:00
} else {
2016-05-09 23:04:56 +08:00
width = this.getScreenWidth();
height = Math.round(photo.metadata.size.height * (this.getScreenWidth() / photo.metadata.size.width));
2016-05-01 03:36:24 +08:00
}
let top = (this.getScreenHeight() / 2 - height / 2);
let left = (this.getScreenWidth() / 2 - width / 2);
2016-12-30 18:58:04 +08:00
return <Dimension>{top: top, left: left, width: width, height: height};
}
}