mirror of
https://github.com/xuthus83/pigallery2.git
synced 2024-11-03 21:04:03 +08:00
139 lines
3.8 KiB
TypeScript
139 lines
3.8 KiB
TypeScript
///<reference path="../../../browser.d.ts"/>
|
|
|
|
import {
|
|
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";
|
|
import {GridPhoto} from "./GridPhoto";
|
|
import {GalleryPhotoComponent} from "./photo/photo.grid.gallery.component";
|
|
|
|
@Component({
|
|
selector: 'gallery-grid',
|
|
templateUrl: 'app/gallery/grid/grid.gallery.component.html',
|
|
styleUrls: ['app/gallery/grid/grid.gallery.component.css'],
|
|
directives: [GalleryPhotoComponent]
|
|
})
|
|
export class GalleryGridComponent implements OnChanges,AfterViewInit {
|
|
|
|
@ViewChild('gridContainer') gridContainer:ElementRef;
|
|
@ViewChildren(GalleryPhotoComponent) gridPhotoQL:QueryList<GalleryPhotoComponent>;
|
|
|
|
@Input() photos:Array<Photo>;
|
|
@Input() lightbox:GalleryLightboxComponent;
|
|
|
|
photosToRender:Array<GridPhoto> = [];
|
|
|
|
private IMAGE_MARGIN = 2;
|
|
private TARGET_COL_COUNT = 5;
|
|
private MIN_ROW_COUNT = 2;
|
|
private MAX_ROW_COUNT = 5;
|
|
|
|
constructor() {
|
|
}
|
|
|
|
ngOnChanges() {
|
|
this.renderPhotos();
|
|
}
|
|
|
|
onResize() {
|
|
this.renderPhotos();
|
|
}
|
|
|
|
ngAfterViewInit() {
|
|
this.lightbox.gridPhotoQL = this.gridPhotoQL;
|
|
|
|
//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();
|
|
});
|
|
}
|
|
|
|
|
|
private renderedContainerWidth = 0;
|
|
|
|
private renderPhotos() {
|
|
if (this.getContainerWidth() == 0) {
|
|
return;
|
|
}
|
|
let maxRowHeight = window.innerHeight / this.MIN_ROW_COUNT;
|
|
let minRowHeight = window.innerHeight / this.MAX_ROW_COUNT;
|
|
let containerWidth = this.getContainerWidth();
|
|
this.renderedContainerWidth = containerWidth;
|
|
|
|
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;
|
|
});
|
|
|
|
this.photosToRender = [];
|
|
let i = 0;
|
|
|
|
while (i < this.photos.length) {
|
|
|
|
let photoRowBuilder = new GridRowBuilder(this.photos, i, this.IMAGE_MARGIN, containerWidth);
|
|
photoRowBuilder.addPhotos(this.TARGET_COL_COUNT);
|
|
photoRowBuilder.adjustRowHeightBetween(minRowHeight, maxRowHeight);
|
|
|
|
let rowHeight = photoRowBuilder.calcRowHeight();
|
|
let imageHeight = rowHeight - (this.IMAGE_MARGIN * 2);
|
|
|
|
photoRowBuilder.getPhotoRow().forEach((photo) => {
|
|
let imageWidth = imageHeight * (photo.metadata.size.width / photo.metadata.size.height);
|
|
this.photosToRender.push(new GridPhoto(photo, imageWidth, imageHeight));
|
|
});
|
|
|
|
i += photoRowBuilder.getPhotoRow().length;
|
|
}
|
|
}
|
|
|
|
|
|
private getContainerWidth():number {
|
|
if (!this.gridContainer) {
|
|
return 0;
|
|
}
|
|
return this.gridContainer.nativeElement.clientWidth;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|