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

139 lines
3.8 KiB
TypeScript
Raw Normal View History

///<reference path="../../../browser.d.ts"/>
import {
2016-05-04 17:20:21 +02:00
Component,
Input,
ElementRef,
OnChanges,
ViewChild,
ViewChildren,
QueryList,
AfterViewInit
} from "@angular/core";
import {Photo} from "../../../../common/entities/Photo";
import {GridRowBuilder} from "./GridRowBuilder";
import {GalleryLightboxComponent} from "../lightbox/lightbox.gallery.component";
2016-05-12 11:00:46 +02:00
import {GridPhoto} from "./GridPhoto";
import {GalleryPhotoComponent} from "./photo/photo.grid.gallery.component";
2016-05-04 17:20:21 +02:00
@Component({
selector: 'gallery-grid',
templateUrl: 'app/gallery/grid/grid.gallery.component.html',
styleUrls: ['app/gallery/grid/grid.gallery.component.css'],
2016-05-04 17:20:21 +02:00
directives: [GalleryPhotoComponent]
})
2016-05-04 17:20:21 +02:00
export class GalleryGridComponent implements OnChanges,AfterViewInit {
2016-04-27 22:37:07 +02:00
@ViewChild('gridContainer') gridContainer:ElementRef;
2016-05-04 17:20:21 +02:00
@ViewChildren(GalleryPhotoComponent) gridPhotoQL:QueryList<GalleryPhotoComponent>;
2016-05-09 21:43:52 +02:00
@Input() photos:Array<Photo>;
@Input() lightbox:GalleryLightboxComponent;
2016-05-04 17:20:21 +02:00
photosToRender:Array<GridPhoto> = [];
2016-05-04 17:20:21 +02:00
private IMAGE_MARGIN = 2;
private TARGET_COL_COUNT = 5;
private MIN_ROW_COUNT = 2;
private MAX_ROW_COUNT = 5;
2016-05-04 17:20:21 +02:00
constructor() {
}
2016-05-04 17:20:21 +02:00
ngOnChanges() {
2016-05-09 21:43:52 +02:00
this.renderPhotos();
}
onResize() {
this.renderPhotos();
}
2016-05-04 17:20:21 +02:00
ngAfterViewInit() {
this.lightbox.gridPhotoQL = this.gridPhotoQL;
2016-05-12 11:00:46 +02:00
//TODO: implement scroll detection
/* this.gridPhotoQL.changes.subscribe(
(x)=> {
console.log("changed");
if (!this.directory || this.gridPhotoQL.length < this.directory.photos.length) {
console.log("bad");
console.log(this.directory ? this.gridPhotoQL.length + " < "+this.directory.photos.length : "no dir");
return;
}
if (this.renderedContainerWidth != this.getContainerWidth()) {
this.renderPhotos();
}
},
(err) => {
console.log('Error: %s', err);
},
() =>{
console.log('Completed');
}
); */
setImmediate(() => {
this.renderPhotos();
});
}
2016-05-04 17:20:21 +02:00
private renderedContainerWidth = 0;
2016-05-04 17:20:21 +02:00
private renderPhotos() {
if (this.getContainerWidth() == 0) {
return;
}
2016-05-04 17:20:21 +02:00
let maxRowHeight = window.innerHeight / this.MIN_ROW_COUNT;
let minRowHeight = window.innerHeight / this.MAX_ROW_COUNT;
let containerWidth = this.getContainerWidth();
this.renderedContainerWidth = containerWidth;
2016-05-04 17:20:21 +02:00
this.photos.sort((a:Photo, b:Photo) => {
if (a.metadata.creationDate > b.metadata.creationDate) {
return 1;
}
if (a.metadata.creationDate < b.metadata.creationDate) {
return -1;
}
// a must be equal to b
return 0;
});
2016-05-04 17:20:21 +02:00
this.photosToRender = [];
let i = 0;
2016-05-09 21:43:52 +02:00
while (i < this.photos.length) {
2016-05-09 21:43:52 +02:00
let photoRowBuilder = new GridRowBuilder(this.photos, i, this.IMAGE_MARGIN, containerWidth);
2016-05-04 17:20:21 +02:00
photoRowBuilder.addPhotos(this.TARGET_COL_COUNT);
photoRowBuilder.adjustRowHeightBetween(minRowHeight, maxRowHeight);
2016-05-04 17:20:21 +02:00
let rowHeight = photoRowBuilder.calcRowHeight();
let imageHeight = rowHeight - (this.IMAGE_MARGIN * 2);
2016-05-04 17:20:21 +02:00
photoRowBuilder.getPhotoRow().forEach((photo) => {
let imageWidth = imageHeight * (photo.metadata.size.width / photo.metadata.size.height);
2016-05-04 17:20:21 +02:00
this.photosToRender.push(new GridPhoto(photo, imageWidth, imageHeight));
});
2016-05-04 17:20:21 +02:00
i += photoRowBuilder.getPhotoRow().length;
}
}
2016-05-04 17:20:21 +02:00
private getContainerWidth():number {
if (!this.gridContainer) {
return 0;
2016-05-04 17:20:21 +02:00
}
2016-04-27 22:37:07 +02:00
return this.gridContainer.nativeElement.clientWidth;
}
}