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

348 lines
9.7 KiB
TypeScript
Raw Normal View History

import {
ChangeDetectorRef,
Component,
ElementRef,
EventEmitter,
HostListener,
OnDestroy,
Output,
QueryList,
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";
import {OverlayService} from "../overlay.service";
import {Subscription} from "rxjs";
2017-07-11 04:00:22 +08:00
import {animate, AnimationBuilder, style} from "@angular/animations";
import {GalleryLightboxPhotoComponent} from "./photo/photo.lightbox.gallery.component";
@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 {
@Output('onLastElement') onLastElement = new EventEmitter();
@ViewChild("root") elementRef: ElementRef;
2017-07-11 04:00:22 +08:00
@ViewChild("photo") photoElement: GalleryLightboxPhotoComponent;
@ViewChild("lightbox") lightboxElement: ElementRef;
public navigation = {hasPrev: true, hasNext: true};
public blackCanvasOpacity: any = 0;
public activePhoto: GalleryPhotoComponent;
private gridPhotoQL: QueryList<GalleryPhotoComponent>;
public visible = false;
private changeSubscription: Subscription = null;
2016-05-01 03:36:24 +08:00
public infoPanelVisible = false;
public infoPanelWidth = 0;
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
2016-05-01 03:36:24 +08:00
ngOnDestroy(): void {
if (this.changeSubscription != null) {
this.changeSubscription.unsubscribe();
}
}
//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
}
console.warn("can't find photo to show next");
}
2016-05-13 05:00:38 +08:00
public prevImage() {
if (this.activePhotoId > 0) {
this.showPhoto(this.activePhotoId - 1);
return;
2016-05-13 05:00:38 +08:00
}
console.warn("can't find photo to show prev");
}
2016-05-13 05:00:38 +08:00
activePhotoId: number = null;
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);
}
2017-07-11 04:00:22 +08:00
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];
2017-07-11 04:00:22 +08:00
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.getScreenHeight() < to.top) {
this.setBodyScrollTop(to.top);
}
}
2017-07-11 04:00:22 +08:00
startPhotoDimension: Dimension = <Dimension>{top: 0, left: 0, width: 0, height: 0};
public show(photo: PhotoDTO) {
2017-07-11 04:00:22 +08:00
console.log(this.photoElement);
this.visible = true;
let selectedPhoto = this.findPhotoComponent(photo);
if (selectedPhoto === null) {
throw new Error("Can't find Photo");
}
2017-07-11 04:00:22 +08:00
const lightboxDimension = selectedPhoto.getDimension();
lightboxDimension.top -= this.getBodyScrollTop();
this.animatePhoto(selectedPhoto.getDimension(), this.calcLightBoxPhotoDimension(selectedPhoto.gridPhoto.photo));
this.animateLightbox(
lightboxDimension,
<Dimension>{
top: 0,
left: 0,
width: this.getScreenWidth(),
height: this.getScreenHeight()
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);
}
public hide() {
this.fullScreenService.exitFullScreen();
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,
width: this.getScreenWidth(),
height: this.getScreenHeight()
}, lightboxDimension);
setTimeout(() => {
this.visible = false;
this.activePhoto = null;
this.overlayService.hideOverlay();
}, 500);
2017-07-11 04:41:30 +08:00
this.hideInfoPanel(false);
}
2017-07-11 04:00:22 +08:00
animatePhoto(from: Dimension, to: Dimension = from) {
this._builder.build([
style(<any>Dimension.toString(from)),
animate(300,
style(<any>Dimension.toString(to)))
])
.create(this.photoElement.elementRef.nativeElement)
.play();
}
animateLightbox(from: Dimension = <Dimension>{
top: 0,
left: 0,
width: this.getScreenWidth(),
height: this.getScreenHeight()
}, to: Dimension = from) {
this._builder.build([
style(<any>Dimension.toString(from)),
animate(300,
style(<any>Dimension.toString(to)))
])
.create(this.lightboxElement.nativeElement)
.play();
}
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,
width: this.getScreenWidth() + 400,
height: this.getScreenHeight()
},
<Dimension>{
top: 0,
left: 0,
width: this.getScreenWidth(),
height: this.getScreenHeight()
});
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,
width: this.getScreenWidth() - 400,
height: this.getScreenHeight()
},
<Dimension>{
top: 0,
left: 0,
width: this.getScreenWidth(),
height: this.getScreenHeight()
});
}
}
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
private getScreenWidth() {
return Math.max(window.innerWidth - this.infoPanelWidth, 0);
}
2016-07-06 18:53:49 +08:00
private getScreenHeight() {
return window.innerHeight;
}
2016-12-30 18:58:04 +08:00
private calcLightBoxPhotoDimension(photo: PhotoDTO): Dimension {
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));
height = this.getScreenHeight();
} else {
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-05-01 03:36:24 +08:00
return <Dimension>{top: top, left: left, width: width, height: height};
}
}