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

64 lines
1.9 KiB
TypeScript
Raw Normal View History

2016-12-27 21:55:51 +02:00
import {PhotoDTO} from "../../../../common/entities/PhotoDTO";
2016-05-09 17:04:56 +02:00
export class GridRowBuilder {
2016-12-27 21:55:51 +02:00
private photoRow: Array<PhotoDTO> = [];
2016-05-09 17:04:56 +02:00
private photoIndex:number = 0; //index of the last pushed photo to the photoRow
2016-05-09 17:04:56 +02:00
2016-12-27 21:55:51 +02:00
constructor(private photos: Array<PhotoDTO>, private startIndex: number, private photoMargin: number, private containerWidth: number) {
this.photoIndex = startIndex;
}
2016-05-09 17:04:56 +02:00
public addPhotos(number:number) {
for (let i = 0; i < number; i++) {
this.addPhoto();
}
}
2016-05-09 17:04:56 +02:00
public addPhoto():boolean {
if (this.photoIndex + 1 > this.photos.length) {
return false;
}
this.photoRow.push(this.photos[this.photoIndex]);
this.photoIndex++;
return true;
}
2016-05-09 17:04:56 +02:00
public removePhoto():boolean {
if (this.photoIndex - 1 < this.startIndex) {
return false;
}
this.photoIndex--;
this.photoRow.pop();
return true;
}
2016-12-27 21:55:51 +02:00
public getPhotoRow(): Array<PhotoDTO> {
return this.photoRow;
}
2016-05-09 17:04:56 +02:00
public adjustRowHeightBetween(minHeight:number, maxHeight:number) {
while (this.calcRowHeight() > maxHeight && this.addPhoto() === true) { //row too high -> add more images
}
while (this.calcRowHeight() < minHeight && this.removePhoto() === true) { //roo too small -> remove images
}
//keep at least one photo int thr row
2016-05-09 17:04:56 +02:00
if (this.photoRow.length <= 0) {
this.addPhoto();
}
}
public calcRowHeight():number {
let width = 0;
2016-05-09 17:04:56 +02:00
for (let i = 0; i < this.photoRow.length; i++) {
width += ((this.photoRow[i].metadata.size.width) / (this.photoRow[i].metadata.size.height)); //summing up aspect ratios
}
let height = (this.containerWidth - this.photoRow.length * (this.photoMargin * 2) - 1) / width; //cant be equal -> width-1
2016-05-09 17:04:56 +02:00
return height + (this.photoMargin * 2);
};
}