2018-03-31 03:30:30 +08:00
|
|
|
import {PhotoDTO} from '../../../../common/entities/PhotoDTO';
|
2016-04-09 21:19:25 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
export class GridRowBuilder {
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
private photoRow: Array<PhotoDTO> = [];
|
2016-05-09 23:04:56 +08:00
|
|
|
|
2018-05-04 07:17:08 +08:00
|
|
|
private photoIndex = 0; // index of the last pushed photo to the photoRow
|
2016-05-09 23:04:56 +08:00
|
|
|
|
|
|
|
|
2017-07-17 03:03:37 +08:00
|
|
|
constructor(private photos: Array<PhotoDTO>,
|
|
|
|
private startIndex: number,
|
|
|
|
private photoMargin: number,
|
|
|
|
private containerWidth: number) {
|
2017-06-11 04:32:56 +08:00
|
|
|
this.photoIndex = startIndex;
|
2018-05-27 08:49:55 +08:00
|
|
|
if (this.containerWidth <= 0) {
|
|
|
|
throw new Error('container width cant be <=0, got:' + this.containerWidth);
|
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-04-09 21:19:25 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public addPhotos(number: number) {
|
|
|
|
for (let i = 0; i < number; i++) {
|
|
|
|
this.addPhoto();
|
2016-04-09 21:19:25 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-04-09 21:19:25 +08:00
|
|
|
|
2017-07-17 03:03:37 +08:00
|
|
|
private addPhoto(): boolean {
|
2017-06-11 04:32:56 +08:00
|
|
|
if (this.photoIndex + 1 > this.photos.length) {
|
|
|
|
return false;
|
2016-04-09 21:19:25 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
this.photoRow.push(this.photos[this.photoIndex]);
|
|
|
|
this.photoIndex++;
|
|
|
|
return true;
|
|
|
|
}
|
2016-04-09 21:19:25 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public removePhoto(): boolean {
|
|
|
|
if (this.photoIndex - 1 < this.startIndex) {
|
|
|
|
return false;
|
2016-04-09 21:19:25 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
this.photoIndex--;
|
|
|
|
this.photoRow.pop();
|
|
|
|
return true;
|
|
|
|
}
|
2016-04-09 21:19:25 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public getPhotoRow(): Array<PhotoDTO> {
|
|
|
|
return this.photoRow;
|
|
|
|
}
|
2016-04-09 21:19:25 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public adjustRowHeightBetween(minHeight: number, maxHeight: number) {
|
2018-05-04 07:17:08 +08:00
|
|
|
while (this.calcRowHeight() > maxHeight && this.addPhoto() === true) { // row too high -> add more images
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-04-09 21:19:25 +08:00
|
|
|
|
2018-05-04 07:17:08 +08:00
|
|
|
while (this.calcRowHeight() < minHeight && this.removePhoto() === true) { // roo too small -> remove images
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-04-09 21:19:25 +08:00
|
|
|
|
2018-05-04 07:17:08 +08:00
|
|
|
// keep at least one photo int thr row
|
2017-06-11 04:32:56 +08:00
|
|
|
if (this.photoRow.length <= 0) {
|
|
|
|
this.addPhoto();
|
2016-04-09 21:19:25 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-04-09 21:19:25 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public calcRowHeight(): number {
|
|
|
|
let width = 0;
|
|
|
|
for (let i = 0; i < this.photoRow.length; i++) {
|
2018-05-04 07:17:08 +08:00
|
|
|
width += ((this.photoRow[i].metadata.size.width) / (this.photoRow[i].metadata.size.height)); // summing up aspect ratios
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2018-05-04 07:17:08 +08:00
|
|
|
const height = (this.containerWidth - this.photoRow.length * (this.photoMargin * 2) - 1) / width; // cant be equal -> width-1
|
2016-04-09 21:19:25 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
return height + (this.photoMargin * 2);
|
2018-05-13 00:19:51 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|