1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08: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-28 04:37:07 +08: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-28 04:37:07 +08: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-05-01 03:36:24 +08:00
this.gridPhotoQL.changes.subscribe(
(x)=> {
2016-05-01 03:36:24 +08: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-05-01 03:36:24 +08: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-28 04:37:07 +08: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-28 04:37:07 +08:00
return this.gridContainer.nativeElement.clientWidth;
}
2016-04-28 04:37:07 +08:00
}
class GridPhoto {
constructor(public photo:Photo, public renderWidth:number, public renderHeight:number){
}
}