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

216 lines
6.3 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,
HostListener
2016-05-04 23:20:21 +08:00
} from "@angular/core";
import {Photo} from "../../../../common/entities/Photo";
import {GridRowBuilder} from "./GridRowBuilder";
import {GalleryLightboxComponent} from "../lightbox/lightbox.gallery.component";
2016-05-12 17:00:46 +08:00
import {GridPhoto} from "./GridPhoto";
import {GalleryPhotoComponent} from "./photo/photo.grid.gallery.component";
import {Config} from "../../config/Config";
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>;
2016-05-10 03:43:52 +08:00
@Input() photos:Array<Photo>;
@Input() lightbox:GalleryLightboxComponent;
2016-05-04 23:20:21 +08:00
photosToRender:Array<GridPhoto> = [];
containerWidth:number = 0;
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
2016-07-05 18:34:11 +08:00
onScrollFired = false;
constructor() {
}
2016-05-04 23:20:21 +08:00
ngOnChanges() {
2016-07-03 21:41:12 +08:00
if (this.isAfterViewInit === false) {
2016-06-25 20:13:06 +08:00
return;
2016-06-26 17:08:05 +08:00
}
this.updateContainerWidth();
2016-06-26 17:17:30 +08:00
this.sortPhotos();
this.mergeNewPhotos();
setImmediate(() => {
this.renderPhotos();
});
}
2016-06-25 20:13:06 +08:00
@HostListener('window:resize')
onResize() {
2016-07-03 21:41:12 +08:00
if (this.isAfterViewInit === false) {
2016-06-25 20:13:06 +08:00
return;
}
this.updateContainerWidth();
2016-06-26 17:17:30 +08:00
this.sortPhotos();
this.clearRenderedPhotos();
setImmediate(() => {
this.renderPhotos();
});
}
2016-05-04 23:20:21 +08:00
2016-06-25 20:13:06 +08:00
isAfterViewInit:boolean = false;
2016-05-04 23:20:21 +08:00
ngAfterViewInit() {
this.lightbox.gridPhotoQL = this.gridPhotoQL;
2016-05-12 17:00:46 +08:00
//TODO: implement scroll detection
this.updateContainerWidth();
2016-07-05 00:12:58 +08:00
this.sortPhotos();
2016-06-26 17:17:30 +08:00
this.clearRenderedPhotos();
setImmediate(() => {
this.renderPhotos();
});
2016-06-25 20:13:06 +08:00
this.isAfterViewInit = true;
}
2016-05-04 23:20:21 +08:00
2016-06-26 17:17:30 +08:00
private sortPhotos() {
2016-06-26 17:08:05 +08:00
//sort pohots by date
this.photos.sort((a:Photo, b:Photo) => {
2016-07-05 00:12:58 +08:00
return a.metadata.creationDate.getTime() - b.metadata.creationDate.getTime();
});
2016-06-26 17:08:05 +08:00
2016-06-26 17:17:30 +08:00
}
private clearRenderedPhotos() {
this.photosToRender = [];
this.renderedPhotoIndex = 0;
}
private mergeNewPhotos() {
2016-06-26 17:08:05 +08:00
//merge new data with old one
let lastSameIndex = 0;
let lastRowId = null;
for (let i = 0; i < this.photos.length && i < this.photosToRender.length; i++) {
//thIf a photo changed the whole row has to be removed
if (this.photosToRender[i].rowId != lastRowId) {
lastSameIndex = i;
lastRowId = this.photosToRender[i].rowId;
}
2016-07-03 21:41:12 +08:00
if (this.photosToRender[i].equals(this.photos[i]) === false) {
2016-06-26 17:08:05 +08:00
break;
}
}
if (lastSameIndex > 0) {
this.photosToRender.splice(lastSameIndex, this.photosToRender.length - lastSameIndex);
this.renderedPhotoIndex = lastSameIndex;
} else {
2016-06-26 17:17:30 +08:00
this.clearRenderedPhotos();
2016-06-26 17:08:05 +08:00
}
}
2016-06-26 17:17:30 +08:00
private renderedPhotoIndex:number = 0;
2016-07-05 18:34:11 +08:00
private renderPhotos() {
if (this.containerWidth == 0 || this.renderedPhotoIndex >= this.photos.length || !this.shouldRenderMore()) {
return;
}
let renderedContentHeight = 0;
2016-06-25 20:13:06 +08:00
while (this.renderedPhotoIndex < this.photos.length && this.shouldRenderMore(renderedContentHeight) === true) {
2016-07-05 18:34:11 +08:00
let ret = this.renderARow();
if (ret === null) {
throw new Error("Gridphotos rendering failed");
}
renderedContentHeight += ret;
2016-05-04 23:20:21 +08:00
}
}
2016-05-04 23:20:21 +08:00
/**
* Returns true, if scroll is >= 70% to render more images.
* Or of onscroll renderin is off: return always to render all the images at once
* @param offset Add height to the client height (conent is not yet added to the dom, but calculate with it)
* @returns {boolean}
*/
private shouldRenderMore(offset:number = 0):boolean {
2016-07-03 21:41:12 +08:00
return Config.Client.enableOnScrollRendering === false ||
2016-07-05 18:34:11 +08:00
window.scrollY >= (document.body.clientHeight + offset - window.innerHeight) * 0.7
2016-06-25 20:13:06 +08:00
|| (document.body.clientHeight + offset) * 0.85 < window.innerHeight;
}
2016-07-05 18:34:11 +08:00
@HostListener('window:scroll')
onScroll() {
2016-07-05 18:34:11 +08:00
if (!this.onScrollFired) {
window.requestAnimationFrame(() => {
this.renderPhotos();
if (Config.Client.enableOnScrollThumbnailPrioritising === true) {
this.gridPhotoQL.toArray().forEach((pc:GalleryPhotoComponent) => {
pc.onScroll();
});
}
this.onScrollFired = false;
});
2016-07-05 18:34:11 +08:00
this.onScrollFired = true;
}
}
public renderARow():number {
2016-07-05 18:34:11 +08:00
if (this.renderedPhotoIndex >= this.photos.length) {
return null;
2016-07-05 00:12:58 +08:00
}
2016-07-05 18:34:11 +08:00
let maxRowHeight = window.innerHeight / this.MIN_ROW_COUNT;
let minRowHeight = window.innerHeight / this.MAX_ROW_COUNT;
let photoRowBuilder = new GridRowBuilder(this.photos, this.renderedPhotoIndex, this.IMAGE_MARGIN, this.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, this.renderedPhotoIndex));
});
this.renderedPhotoIndex += photoRowBuilder.getPhotoRow().length;
return rowHeight;
}
private updateContainerWidth():number {
2016-05-04 23:20:21 +08:00
if (!this.gridContainer) {
return;
2016-05-04 23:20:21 +08:00
}
this.containerWidth = this.gridContainer.nativeElement.clientWidth;
2016-04-28 04:37:07 +08:00
}
}