2016-04-09 15:19:25 +02:00
|
|
|
///<reference path="../../../browser.d.ts"/>
|
|
|
|
|
2016-04-28 23:25:25 +02:00
|
|
|
import {
|
2016-04-30 18:01:54 +02:00
|
|
|
Component, Input, ElementRef, OnChanges, ViewChild, ViewChildren, QueryList, Output, AfterViewInit, EventEmitter
|
2016-04-28 23:25:25 +02:00
|
|
|
} from 'angular2/core';
|
2016-04-09 15:19:25 +02:00
|
|
|
import {Directory} from "../../../../common/entities/Directory";
|
|
|
|
import {Photo} from "../../../../common/entities/Photo";
|
|
|
|
import {GalleryPhotoComponent} from "../photo/photo.gallery.component";
|
|
|
|
import {GridRowBuilder} from "./GridRowBuilder";
|
2016-04-27 22:37:07 +02:00
|
|
|
import {AnimationBuilder} from "angular2/animate";
|
|
|
|
import {Utils} from "../../../../common/Utils";
|
2016-04-30 18:01:54 +02:00
|
|
|
import {GalleryLightboxComponent} from "../lightbox/lightbox.gallery.component";
|
|
|
|
import {Observable} from "rxjs/Observable";
|
2016-04-09 15:19:25 +02:00
|
|
|
@Component({
|
|
|
|
selector: 'gallery-grid',
|
|
|
|
templateUrl: 'app/gallery/grid/grid.gallery.component.html',
|
|
|
|
styleUrls: ['app/gallery/grid/grid.gallery.component.css'],
|
|
|
|
directives:[GalleryPhotoComponent]
|
|
|
|
})
|
2016-04-30 18:01:54 +02:00
|
|
|
export class GalleryGridComponent implements OnChanges,AfterViewInit{
|
2016-04-27 22:37:07 +02:00
|
|
|
|
|
|
|
@ViewChild('gridContainer') gridContainer:ElementRef;
|
2016-04-30 18:01:54 +02:00
|
|
|
@ViewChildren(GalleryPhotoComponent) gridPhotoQL:QueryList<GalleryPhotoComponent>;
|
|
|
|
|
2016-04-09 15:19:25 +02:00
|
|
|
@Input() directory:Directory;
|
2016-04-30 18:01:54 +02:00
|
|
|
@Input() lightbox:GalleryLightboxComponent;
|
|
|
|
|
2016-04-09 15:19:25 +02:00
|
|
|
photosToRender:Array<GridPhoto> = [];
|
2016-04-30 18:01:54 +02:00
|
|
|
|
2016-04-09 15:19:25 +02:00
|
|
|
private IMAGE_MARGIN = 2;
|
|
|
|
private TARGET_COL_COUNT = 5;
|
|
|
|
private MIN_ROW_COUNT = 2;
|
|
|
|
private MAX_ROW_COUNT = 5;
|
|
|
|
|
2016-04-30 18:01:54 +02:00
|
|
|
constructor() {
|
2016-04-09 15:19:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnChanges(){
|
|
|
|
this.renderPhotos();
|
|
|
|
}
|
|
|
|
|
2016-04-30 18:01:54 +02:00
|
|
|
ngAfterViewInit(){
|
|
|
|
this.lightbox.gridPhotoQL = this.gridPhotoQL;
|
2016-04-30 21:36:24 +02:00
|
|
|
this.gridPhotoQL.changes.subscribe(
|
2016-04-30 18:01:54 +02:00
|
|
|
(x)=> {
|
2016-04-30 21:36:24 +02:00
|
|
|
if(this.gridPhotoQL.length < this.photosToRender.length){
|
|
|
|
return;
|
|
|
|
}
|
2016-04-30 18:01:54 +02:00
|
|
|
if(this.renderedConteinerWidth != this.getContainerWidth()){
|
|
|
|
this.renderPhotos();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private renderedConteinerWidth = 0;
|
2016-04-09 15:19:25 +02:00
|
|
|
private renderPhotos() {
|
|
|
|
let maxRowHeight = window.innerHeight / this.MIN_ROW_COUNT;
|
|
|
|
let minRowHeight = window.innerHeight / this.MAX_ROW_COUNT;
|
2016-04-30 18:01:54 +02:00
|
|
|
let containerWidth = this.getContainerWidth();
|
|
|
|
this.renderedConteinerWidth = containerWidth;
|
2016-04-09 15:19:25 +02:00
|
|
|
|
|
|
|
this.photosToRender = [];
|
|
|
|
let i = 0;
|
2016-04-18 21:27:15 +02:00
|
|
|
|
2016-04-09 15:19:25 +02:00
|
|
|
while (i < this.directory.photos.length ) {
|
|
|
|
|
2016-04-30 21:36:24 +02:00
|
|
|
let photoRowBuilder = new GridRowBuilder(this.directory.photos,i,this.IMAGE_MARGIN,this.getContainerWidth());
|
2016-04-09 15:19:25 +02:00
|
|
|
photoRowBuilder.addPhotos(this.TARGET_COL_COUNT);
|
|
|
|
photoRowBuilder.adjustRowHeightBetween(minRowHeight,maxRowHeight);
|
|
|
|
|
|
|
|
let rowHeight = photoRowBuilder.calcRowHeight();
|
|
|
|
let imageHeight = rowHeight - (this.IMAGE_MARGIN * 2);
|
|
|
|
|
|
|
|
photoRowBuilder.getPhotoRow().forEach((photo) => {
|
2016-04-27 22:37:07 +02:00
|
|
|
let imageWidth = imageHeight * (photo.width / photo.height);
|
2016-04-09 15:19:25 +02:00
|
|
|
this.photosToRender.push(new GridPhoto(photo,imageWidth,imageHeight));
|
|
|
|
});
|
|
|
|
|
2016-04-30 18:01:54 +02:00
|
|
|
i+= photoRowBuilder.getPhotoRow().length;
|
2016-04-09 15:19:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onResize() {
|
|
|
|
this.renderPhotos();
|
|
|
|
}
|
|
|
|
|
|
|
|
private getContainerWidth(): number{
|
2016-04-30 18:01:54 +02:00
|
|
|
if(!this.gridContainer){
|
|
|
|
return 0;
|
2016-05-03 14:29:24 +02:00
|
|
|
}
|
2016-04-27 22:37:07 +02:00
|
|
|
return this.gridContainer.nativeElement.clientWidth;
|
|
|
|
}
|
|
|
|
|
2016-04-28 23:25:25 +02:00
|
|
|
|
2016-04-27 22:37:07 +02:00
|
|
|
|
2016-04-09 15:19:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class GridPhoto {
|
|
|
|
constructor(public photo:Photo, public renderWidth:number, public renderHeight:number){
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|