2016-05-01 00:01:54 +08:00
|
|
|
///<reference path="../../../browser.d.ts"/>
|
|
|
|
|
2016-07-04 22:58:10 +08:00
|
|
|
import {Component, QueryList, Output, EventEmitter} from "@angular/core";
|
2016-05-01 00:01:54 +08:00
|
|
|
import {Photo} from "../../../../common/entities/Photo";
|
2016-05-12 17:00:46 +08:00
|
|
|
import {GalleryPhotoComponent} from "../grid/photo/photo.grid.gallery.component.ts";
|
2016-06-16 22:24:47 +08:00
|
|
|
import {BrowserDomAdapter} from "@angular/platform-browser/src/browser/browser_adapter";
|
2016-05-01 03:36:24 +08:00
|
|
|
import {Dimension} from "../../model/IRenderable";
|
2016-05-13 20:27:00 +08:00
|
|
|
import {GalleryLightboxPhotoComponent} from "./photo/photo.lightbox.gallery.component";
|
2016-05-01 00:01:54 +08:00
|
|
|
|
|
|
|
@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',
|
|
|
|
directives: [GalleryLightboxPhotoComponent]
|
2016-05-01 00:01:54 +08:00
|
|
|
})
|
2016-05-01 03:36:24 +08:00
|
|
|
export class GalleryLightboxComponent {
|
2016-07-04 22:58:10 +08:00
|
|
|
@Output('onLastElement') onLastElement = new EventEmitter();
|
2016-05-01 00:01:54 +08:00
|
|
|
|
2016-05-13 20:27:00 +08:00
|
|
|
public navigation = {hasPrev: true, hasNext: true};
|
2016-07-04 22:58:10 +08:00
|
|
|
public photoDimension:Dimension = new Dimension(0, 0, 0, 0);
|
|
|
|
|
2016-05-13 20:27:00 +08:00
|
|
|
private activePhoto:GalleryPhotoComponent;
|
2016-05-01 00:01:54 +08:00
|
|
|
public gridPhotoQL:QueryList<GalleryPhotoComponent>;
|
|
|
|
|
2016-05-01 00:33:07 +08:00
|
|
|
private dom:BrowserDomAdapter;
|
2016-05-01 03:36:24 +08:00
|
|
|
|
|
|
|
|
2016-06-16 22:24:47 +08:00
|
|
|
constructor() {
|
2016-05-01 00:01:54 +08:00
|
|
|
this.dom = new BrowserDomAdapter();
|
2016-05-01 00:33:07 +08:00
|
|
|
|
2016-05-01 03:36:24 +08:00
|
|
|
|
2016-05-01 00:01:54 +08:00
|
|
|
}
|
|
|
|
|
2016-05-13 05:00:38 +08:00
|
|
|
public nextImage() {
|
|
|
|
|
|
|
|
let pcList = this.gridPhotoQL.toArray();
|
|
|
|
for (let i = 0; i < pcList.length; i++) {
|
|
|
|
if (pcList[i] === this.activePhoto && i + 1 < pcList.length) {
|
|
|
|
this.activePhoto = pcList[i + 1];
|
2016-05-13 20:27:00 +08:00
|
|
|
this.navigation.hasPrev = true;
|
2016-05-13 05:00:38 +08:00
|
|
|
|
2016-07-04 22:58:10 +08:00
|
|
|
this.navigation.hasNext = i + 2 < pcList.length;
|
2016-05-13 05:00:38 +08:00
|
|
|
|
2016-07-04 22:58:10 +08:00
|
|
|
if (i + 3 === pcList.length) {
|
|
|
|
this.onLastElement.emit({}); //trigger to render more photos if there are
|
|
|
|
}
|
|
|
|
|
|
|
|
this.photoDimension = this.calcLightBoxPhotoDimension(this.activePhoto.gridPhoto.photo);
|
2016-05-13 05:00:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public prevImage() {
|
|
|
|
let pcList = this.gridPhotoQL.toArray();
|
|
|
|
for (let i = 0; i < pcList.length; i++) {
|
|
|
|
if (pcList[i] === this.activePhoto && i > 0) {
|
|
|
|
this.activePhoto = pcList[i - 1];
|
2016-05-13 20:27:00 +08:00
|
|
|
this.navigation.hasNext = true;
|
2016-07-04 22:58:10 +08:00
|
|
|
this.navigation.hasPrev = i - 1 > 0;
|
2016-05-13 05:00:38 +08:00
|
|
|
|
2016-07-04 22:58:10 +08:00
|
|
|
this.photoDimension = this.calcLightBoxPhotoDimension(this.activePhoto.gridPhoto.photo);
|
2016-05-13 05:00:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-01 03:36:24 +08:00
|
|
|
public show(photo:Photo) {
|
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-05-01 00:01:54 +08:00
|
|
|
throw new Error("Can't find Photo");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.dom.setStyle(this.dom.query('body'), 'overflow', 'hidden');
|
|
|
|
this.activePhoto = selectedPhoto;
|
2016-07-04 22:58:10 +08:00
|
|
|
this.photoDimension = this.calcLightBoxPhotoDimension(this.activePhoto.gridPhoto.photo);
|
2016-05-13 20:27:00 +08:00
|
|
|
|
2016-05-01 00:01:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public hide() {
|
|
|
|
let to = this.activePhoto.getDimension();
|
2016-07-04 22:58:10 +08:00
|
|
|
|
2016-05-14 03:40:04 +08:00
|
|
|
//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
|
|
|
|
|
|
|
this.dom.setStyle(this.dom.query('body'), 'overflow', 'auto');
|
2016-07-04 22:58:10 +08:00
|
|
|
this.activePhoto = null;
|
2016-06-16 22:24:47 +08:00
|
|
|
|
2016-05-01 03:36:24 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-13 20:27:00 +08:00
|
|
|
private findPhotoComponent(photo) {
|
2016-05-01 03:36:24 +08:00
|
|
|
let galleryPhotoComponents = this.gridPhotoQL.toArray();
|
|
|
|
let selectedPhoto:GalleryPhotoComponent = null;
|
|
|
|
for (let i = 0; i < galleryPhotoComponents.length; i++) {
|
2016-05-12 17:00:46 +08:00
|
|
|
if (galleryPhotoComponents[i].gridPhoto.photo == photo) {
|
2016-05-01 03:36:24 +08:00
|
|
|
selectedPhoto = galleryPhotoComponents[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return selectedPhoto;
|
|
|
|
}
|
|
|
|
|
2016-05-01 00:33:07 +08:00
|
|
|
|
2016-05-01 03:36:24 +08:00
|
|
|
private getBodyScrollTop() {
|
|
|
|
return this.dom.getProperty(this.dom.query('body'), 'scrollTop');
|
|
|
|
}
|
|
|
|
|
2016-05-14 03:40:04 +08:00
|
|
|
private setBodyScrollTop(value) {
|
|
|
|
return this.dom.setProperty(this.dom.query('body'), 'scrollTop', value);
|
|
|
|
}
|
|
|
|
|
2016-05-01 03:36:24 +08:00
|
|
|
private getScreenWidth() {
|
|
|
|
return window.innerWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
private getScreenHeight() {
|
|
|
|
return window.innerHeight;
|
2016-05-01 00:01:54 +08:00
|
|
|
}
|
2016-05-01 03:36:24 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
|
|
|
|
private calcLightBoxPhotoDimension(photo:Photo):Dimension {
|
2016-05-01 03:36:24 +08:00
|
|
|
let width = 0;
|
|
|
|
let height = 0;
|
2016-05-13 00:24:26 +08:00
|
|
|
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();
|
2016-05-13 00:24:26 +08:00
|
|
|
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-09 23:04:56 +08:00
|
|
|
return new Dimension(top, left, width, height);
|
2016-05-01 00:01:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|