1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/frontend/app/ui/gallery/grid/GridRowBuilder.ts
Patrik J. Braun 0d3b8823e4 ui directory refactoring
adding thumbnail loader for faces
2019-03-03 10:30:12 +01:00

73 lines
2.0 KiB
TypeScript

import {PhotoDTO} from '../../../../../common/entities/PhotoDTO';
import {MediaDTO} from '../../../../../common/entities/MediaDTO';
export class GridRowBuilder {
private photoRow: MediaDTO[] = [];
private photoIndex = 0; // index of the last pushed media to the photoRow
constructor(private photos: MediaDTO[],
private startIndex: number,
private photoMargin: number,
private containerWidth: number) {
this.photoIndex = startIndex;
if (this.containerWidth <= 0) {
throw new Error('container width cant be <=0, got:' + this.containerWidth);
}
}
public addPhotos(number: number) {
for (let i = 0; i < number; i++) {
this.addPhoto();
}
}
private addPhoto(): boolean {
if (this.photoIndex + 1 > this.photos.length) {
return false;
}
this.photoRow.push(this.photos[this.photoIndex]);
this.photoIndex++;
return true;
}
public removePhoto(): boolean {
if (this.photoIndex - 1 < this.startIndex) {
return false;
}
this.photoIndex--;
this.photoRow.pop();
return true;
}
public getPhotoRow(): MediaDTO[] {
return this.photoRow;
}
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 media int thr row
if (this.photoRow.length <= 0) {
this.addPhoto();
}
}
public calcRowHeight(): number {
let width = 0;
for (let i = 0; i < this.photoRow.length; i++) {
const size = MediaDTO.getRotatedSize(this.photoRow[i]);
width += (size.width / size.height); // summing up aspect ratios
}
const height = (this.containerWidth - this.photoRow.length * (this.photoMargin * 2) - 1) / width; // cant be equal -> width-1
return height + (this.photoMargin * 2);
}
}