1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/frontend/app/gallery/grid/grid.gallery.component.ts

130 lines
3.6 KiB
TypeScript
Raw Normal View History

///<reference path="../../../browser.d.ts"/>
import {
2016-05-04 23:20:21 +08:00
Component,
Input,
ElementRef,
OnChanges,
ViewChild,
ViewChildren,
QueryList,
AfterViewInit
} from "@angular/core";
import {Directory} from "../../../../common/entities/Directory";
import {Photo} from "../../../../common/entities/Photo";
import {GalleryPhotoComponent} from "../photo/photo.gallery.component";
import {GridRowBuilder} from "./GridRowBuilder";
import {GalleryLightboxComponent} from "../lightbox/lightbox.gallery.component";
2016-05-04 23:20:21 +08:00
@Component({
selector: 'gallery-grid',
templateUrl: 'app/gallery/grid/grid.gallery.component.html',
styleUrls: ['app/gallery/grid/grid.gallery.component.css'],
2016-05-04 23:20:21 +08:00
directives: [GalleryPhotoComponent]
})
2016-05-04 23:20:21 +08:00
export class GalleryGridComponent implements OnChanges,AfterViewInit {
2016-04-28 04:37:07 +08:00
@ViewChild('gridContainer') gridContainer:ElementRef;
2016-05-04 23:20:21 +08:00
@ViewChildren(GalleryPhotoComponent) gridPhotoQL:QueryList<GalleryPhotoComponent>;
@Input() directory:Directory;
@Input() lightbox:GalleryLightboxComponent;
2016-05-04 23:20:21 +08:00
photosToRender:Array<GridPhoto> = [];
2016-05-04 23:20:21 +08:00
private IMAGE_MARGIN = 2;
private TARGET_COL_COUNT = 5;
private MIN_ROW_COUNT = 2;
private MAX_ROW_COUNT = 5;
2016-05-04 23:20:21 +08:00
constructor() {
}
2016-05-04 23:20:21 +08:00
ngOnChanges() {
// this.renderPhotos();
}
onResize() {
this.renderPhotos();
}
2016-05-04 23:20:21 +08:00
ngAfterViewInit() {
this.lightbox.gridPhotoQL = this.gridPhotoQL;
/* 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-05-04 23:20:21 +08:00
private renderedContainerWidth = 0;
2016-05-04 23:20:21 +08:00
private renderPhotos() {
if (this.getContainerWidth() == 0) {
return;
}
2016-05-04 23:20:21 +08:00
let maxRowHeight = window.innerHeight / this.MIN_ROW_COUNT;
let minRowHeight = window.innerHeight / this.MAX_ROW_COUNT;
let containerWidth = this.getContainerWidth();
this.renderedContainerWidth = containerWidth;
2016-05-04 23:20:21 +08:00
this.photosToRender = [];
let i = 0;
2016-05-04 23:20:21 +08:00
while (i < this.directory.photos.length) {
let photoRowBuilder = new GridRowBuilder(this.directory.photos, i, this.IMAGE_MARGIN, containerWidth);
2016-05-04 23:20:21 +08:00
photoRowBuilder.addPhotos(this.TARGET_COL_COUNT);
photoRowBuilder.adjustRowHeightBetween(minRowHeight, maxRowHeight);
2016-05-04 23:20:21 +08:00
let rowHeight = photoRowBuilder.calcRowHeight();
let imageHeight = rowHeight - (this.IMAGE_MARGIN * 2);
2016-05-04 23:20:21 +08:00
photoRowBuilder.getPhotoRow().forEach((photo) => {
let imageWidth = imageHeight * (photo.width / photo.height);
this.photosToRender.push(new GridPhoto(photo, imageWidth, imageHeight));
});
2016-05-04 23:20:21 +08:00
i += photoRowBuilder.getPhotoRow().length;
}
}
2016-05-04 23:20:21 +08:00
private getContainerWidth():number {
if (!this.gridContainer) {
return 0;
2016-05-04 23:20:21 +08:00
}
2016-04-28 04:37:07 +08:00
return this.gridContainer.nativeElement.clientWidth;
}
}
class GridPhoto {
2016-05-04 23:20:21 +08:00
constructor(public photo:Photo, public renderWidth:number, public renderHeight:number) {
}
}