1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2024-12-16 11:37:13 +02:00
pigallery2/frontend/app/gallery/grid/grid.gallery.component.ts

107 lines
3.3 KiB
TypeScript
Raw Normal View History

///<reference path="../../../browser.d.ts"/>
import {
Component, Input, ElementRef, OnChanges, ViewChild, ViewChildren, QueryList, Output, AfterViewInit, EventEmitter
} from 'angular2/core';
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";
import {GalleryLightboxComponent} from "../lightbox/lightbox.gallery.component";
import {Observable} from "rxjs/Observable";
@Component({
selector: 'gallery-grid',
templateUrl: 'app/gallery/grid/grid.gallery.component.html',
styleUrls: ['app/gallery/grid/grid.gallery.component.css'],
directives:[GalleryPhotoComponent]
})
export class GalleryGridComponent implements OnChanges,AfterViewInit{
2016-04-27 22:37:07 +02:00
@ViewChild('gridContainer') gridContainer:ElementRef;
@ViewChildren(GalleryPhotoComponent) gridPhotoQL:QueryList<GalleryPhotoComponent>;
@Input() directory:Directory;
@Input() lightbox:GalleryLightboxComponent;
photosToRender:Array<GridPhoto> = [];
private IMAGE_MARGIN = 2;
private TARGET_COL_COUNT = 5;
private MIN_ROW_COUNT = 2;
private MAX_ROW_COUNT = 5;
constructor() {
}
ngOnChanges(){
this.renderPhotos();
}
ngAfterViewInit(){
this.lightbox.gridPhotoQL = this.gridPhotoQL;
2016-04-30 21:36:24 +02:00
this.gridPhotoQL.changes.subscribe(
(x)=> {
2016-04-30 21:36:24 +02:00
if(this.gridPhotoQL.length < this.photosToRender.length){
return;
}
if(this.renderedConteinerWidth != this.getContainerWidth()){
this.renderPhotos();
}
});
}
private renderedConteinerWidth = 0;
private renderPhotos() {
let maxRowHeight = window.innerHeight / this.MIN_ROW_COUNT;
let minRowHeight = window.innerHeight / this.MAX_ROW_COUNT;
let containerWidth = this.getContainerWidth();
this.renderedConteinerWidth = containerWidth;
this.photosToRender = [];
let i = 0;
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());
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);
this.photosToRender.push(new GridPhoto(photo,imageWidth,imageHeight));
});
i+= photoRowBuilder.getPhotoRow().length;
}
}
onResize() {
this.renderPhotos();
}
private getContainerWidth(): number{
if(!this.gridContainer){
return 0;
}
2016-04-27 22:37:07 +02:00
return this.gridContainer.nativeElement.clientWidth;
}
2016-04-27 22:37:07 +02:00
}
class GridPhoto {
constructor(public photo:Photo, public renderWidth:number, public renderHeight:number){
}
}