2016-04-09 15:19:25 +02:00
|
|
|
///<reference path="../../../browser.d.ts"/>
|
|
|
|
|
2016-04-28 23:25:25 +02:00
|
|
|
import {
|
2016-05-04 17:20:21 +02:00
|
|
|
Component,
|
|
|
|
Input,
|
|
|
|
ElementRef,
|
|
|
|
OnChanges,
|
|
|
|
ViewChild,
|
|
|
|
ViewChildren,
|
|
|
|
QueryList,
|
|
|
|
AfterViewInit
|
|
|
|
} from "@angular/core";
|
2016-04-09 15:19:25 +02:00
|
|
|
import {Photo} from "../../../../common/entities/Photo";
|
|
|
|
import {GridRowBuilder} from "./GridRowBuilder";
|
2016-04-30 18:01:54 +02:00
|
|
|
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
|
|
|
|
2016-04-09 15:19:25 +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-04-09 15:19:25 +02:00
|
|
|
})
|
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>;
|
2016-04-30 18:01:54 +02:00
|
|
|
@Input() lightbox:GalleryLightboxComponent;
|
2016-05-04 17:20:21 +02:00
|
|
|
|
2016-04-09 15:19:25 +02:00
|
|
|
photosToRender:Array<GridPhoto> = [];
|
2016-05-04 17:20:21 +02:00
|
|
|
|
2016-04-09 15:19:25 +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
|
|
|
|
2016-04-30 18:01:54 +02:00
|
|
|
constructor() {
|
2016-04-09 15:19:25 +02:00
|
|
|
}
|
|
|
|
|
2016-05-04 17:20:21 +02:00
|
|
|
ngOnChanges() {
|
2016-05-09 21:43:52 +02:00
|
|
|
this.renderPhotos();
|
2016-05-09 19:14:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onResize() {
|
2016-04-09 15:19:25 +02:00
|
|
|
this.renderPhotos();
|
|
|
|
}
|
2016-05-04 17:20:21 +02:00
|
|
|
|
|
|
|
ngAfterViewInit() {
|
2016-04-30 18:01:54 +02:00
|
|
|
this.lightbox.gridPhotoQL = this.gridPhotoQL;
|
2016-05-12 11:00:46 +02:00
|
|
|
|
|
|
|
//TODO: implement scroll detection
|
2016-05-09 19:14:33 +02:00
|
|
|
/* 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-04-30 18:01:54 +02:00
|
|
|
}
|
2016-05-04 17:20:21 +02:00
|
|
|
|
2016-05-09 19:14:33 +02:00
|
|
|
|
|
|
|
private renderedContainerWidth = 0;
|
2016-04-09 15:19:25 +02:00
|
|
|
|
2016-05-04 17:20:21 +02:00
|
|
|
private renderPhotos() {
|
2016-05-09 19:14:33 +02:00
|
|
|
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();
|
2016-05-09 19:14:33 +02:00
|
|
|
this.renderedContainerWidth = containerWidth;
|
2016-05-04 17:20:21 +02:00
|
|
|
|
|
|
|
this.photosToRender = [];
|
|
|
|
let i = 0;
|
2016-04-18 21:27:15 +02:00
|
|
|
|
2016-05-09 21:43:52 +02:00
|
|
|
while (i < this.photos.length) {
|
2016-04-09 15:19:25 +02:00
|
|
|
|
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-04-09 15:19:25 +02:00
|
|
|
|
2016-05-04 17:20:21 +02:00
|
|
|
let rowHeight = photoRowBuilder.calcRowHeight();
|
|
|
|
let imageHeight = rowHeight - (this.IMAGE_MARGIN * 2);
|
2016-04-09 15:19:25 +02:00
|
|
|
|
2016-05-04 17:20:21 +02:00
|
|
|
photoRowBuilder.getPhotoRow().forEach((photo) => {
|
|
|
|
let imageWidth = imageHeight * (photo.width / photo.height);
|
|
|
|
this.photosToRender.push(new GridPhoto(photo, imageWidth, imageHeight));
|
|
|
|
});
|
2016-04-09 15:19:25 +02:00
|
|
|
|
2016-05-04 17:20:21 +02:00
|
|
|
i += photoRowBuilder.getPhotoRow().length;
|
|
|
|
}
|
|
|
|
}
|
2016-04-09 15:19:25 +02:00
|
|
|
|
2016-05-04 17:20:21 +02:00
|
|
|
|
|
|
|
private getContainerWidth():number {
|
|
|
|
if (!this.gridContainer) {
|
2016-04-30 18:01:54 +02:00
|
|
|
return 0;
|
2016-05-04 17:20:21 +02:00
|
|
|
}
|
2016-04-27 22:37:07 +02:00
|
|
|
return this.gridContainer.nativeElement.clientWidth;
|
|
|
|
}
|
|
|
|
|
2016-04-09 15:19:25 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|