2016-04-29 05:25:25 +08:00
|
|
|
import {
|
2017-06-11 04:32:56 +08:00
|
|
|
AfterViewInit,
|
|
|
|
ChangeDetectorRef,
|
|
|
|
Component,
|
|
|
|
ElementRef,
|
|
|
|
HostListener,
|
|
|
|
Input,
|
|
|
|
OnChanges,
|
2017-07-20 04:40:27 +08:00
|
|
|
OnDestroy,
|
2017-06-11 04:32:56 +08:00
|
|
|
QueryList,
|
|
|
|
ViewChild,
|
|
|
|
ViewChildren
|
2016-05-04 23:20:21 +08:00
|
|
|
} from "@angular/core";
|
2016-12-28 03:55:51 +08:00
|
|
|
import {PhotoDTO} from "../../../../common/entities/PhotoDTO";
|
2016-04-09 21:19:25 +08:00
|
|
|
import {GridRowBuilder} from "./GridRowBuilder";
|
2016-05-01 00:01:54 +08:00
|
|
|
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";
|
2017-03-26 04:59:30 +08:00
|
|
|
import {OverlayService} from "../overlay.service";
|
2017-06-04 21:25:08 +08:00
|
|
|
import {Config} from "../../../../common/config/public/Config";
|
2016-05-04 23:20:21 +08:00
|
|
|
|
2016-04-09 21:19:25 +08:00
|
|
|
@Component({
|
2017-06-11 04:32:56 +08:00
|
|
|
selector: 'gallery-grid',
|
|
|
|
templateUrl: './grid.gallery.component.html',
|
|
|
|
styleUrls: ['./grid.gallery.component.css'],
|
2016-04-09 21:19:25 +08:00
|
|
|
})
|
2017-07-20 04:40:27 +08:00
|
|
|
export class GalleryGridComponent implements OnChanges, AfterViewInit, OnDestroy {
|
2016-04-28 04:37:07 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
@ViewChild('gridContainer') gridContainer: ElementRef;
|
|
|
|
@ViewChildren(GalleryPhotoComponent) gridPhotoQL: QueryList<GalleryPhotoComponent>;
|
2016-05-04 23:20:21 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
@Input() photos: Array<PhotoDTO>;
|
|
|
|
@Input() lightbox: GalleryLightboxComponent;
|
2016-05-04 23:20:21 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
photosToRender: Array<GridPhoto> = [];
|
|
|
|
containerWidth: number = 0;
|
2016-05-04 23:20:21 +08:00
|
|
|
|
2017-07-14 05:39:09 +08:00
|
|
|
public IMAGE_MARGIN = 2;
|
2017-06-11 04:32:56 +08:00
|
|
|
private TARGET_COL_COUNT = 5;
|
|
|
|
private MIN_ROW_COUNT = 2;
|
|
|
|
private MAX_ROW_COUNT = 5;
|
2016-05-04 23:20:21 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
private onScrollFired = false;
|
|
|
|
private scrollbarWidth = 0;
|
2017-07-20 04:40:27 +08:00
|
|
|
private helperTime = null;
|
2016-07-05 18:34:11 +08:00
|
|
|
|
2017-07-20 04:40:27 +08:00
|
|
|
constructor(private overlayService: OverlayService,
|
|
|
|
private changeDetector: ChangeDetectorRef) {
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-04-09 21:19:25 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
ngOnChanges() {
|
|
|
|
if (this.isAfterViewInit === false) {
|
|
|
|
return;
|
2016-05-10 01:14:33 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
this.updateContainerWidth();
|
|
|
|
this.sortPhotos();
|
|
|
|
this.mergeNewPhotos();
|
2017-07-20 04:40:27 +08:00
|
|
|
this.helperTime = setTimeout(() => {
|
2017-06-11 04:32:56 +08:00
|
|
|
this.renderPhotos();
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
|
2017-07-20 04:40:27 +08:00
|
|
|
ngOnDestroy() {
|
|
|
|
|
|
|
|
if (this.helperTime != null) {
|
|
|
|
clearTimeout(this.helperTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
@HostListener('window:resize')
|
|
|
|
onResize() {
|
|
|
|
if (this.isAfterViewInit === false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.updateContainerWidth();
|
|
|
|
this.sortPhotos();
|
|
|
|
//render the same amount of images on resize
|
|
|
|
let renderedIndex = this.renderedPhotoIndex;
|
|
|
|
this.clearRenderedPhotos();
|
|
|
|
this.renderPhotos(renderedIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
isAfterViewInit: boolean = false;
|
|
|
|
|
2017-07-20 04:40:27 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
ngAfterViewInit() {
|
|
|
|
this.lightbox.setGridPhotoQL(this.gridPhotoQL);
|
|
|
|
|
|
|
|
|
|
|
|
this.updateContainerWidth();
|
|
|
|
this.sortPhotos();
|
|
|
|
this.clearRenderedPhotos();
|
2017-07-20 04:40:27 +08:00
|
|
|
this.helperTime = setTimeout(() => {
|
2017-06-11 04:32:56 +08:00
|
|
|
this.renderPhotos();
|
|
|
|
}, 0);
|
|
|
|
this.isAfterViewInit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private sortPhotos() {
|
|
|
|
//sort pohots by date
|
|
|
|
this.photos.sort((a: PhotoDTO, b: PhotoDTO) => {
|
|
|
|
return a.metadata.creationDate - b.metadata.creationDate;
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private clearRenderedPhotos() {
|
|
|
|
this.photosToRender = [];
|
|
|
|
this.renderedPhotoIndex = 0;
|
|
|
|
this.changeDetector.detectChanges();
|
|
|
|
}
|
|
|
|
|
|
|
|
private mergeNewPhotos() {
|
|
|
|
//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;
|
|
|
|
}
|
|
|
|
if (this.photosToRender[i].equals(this.photos[i]) === false) {
|
|
|
|
break;
|
|
|
|
}
|
2016-04-09 21:19:25 +08:00
|
|
|
}
|
2016-05-04 23:20:21 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
if (lastSameIndex > 0) {
|
|
|
|
this.photosToRender.splice(lastSameIndex, this.photosToRender.length - lastSameIndex);
|
|
|
|
this.renderedPhotoIndex = lastSameIndex;
|
|
|
|
} else {
|
|
|
|
this.clearRenderedPhotos();
|
2016-05-01 00:01:54 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-05-04 23:20:21 +08:00
|
|
|
|
2016-05-10 01:14:33 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
private renderedPhotoIndex: number = 0;
|
2016-06-26 17:08:05 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
private renderPhotos(numberOfPhotos: number = 0) {
|
2017-06-23 03:23:26 +08:00
|
|
|
if (this.containerWidth == 0 ||
|
|
|
|
this.renderedPhotoIndex >= this.photos.length ||
|
|
|
|
!this.shouldRenderMore()) {
|
2017-06-11 04:32:56 +08:00
|
|
|
return;
|
2016-06-26 17:17:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
let renderedContentHeight = 0;
|
2016-06-26 17:08:05 +08:00
|
|
|
|
2017-06-23 03:23:26 +08:00
|
|
|
while (this.renderedPhotoIndex < this.photos.length &&
|
|
|
|
(this.shouldRenderMore(renderedContentHeight) === true ||
|
2017-07-20 04:40:27 +08:00
|
|
|
this.renderedPhotoIndex < numberOfPhotos)) {
|
2017-06-11 04:32:56 +08:00
|
|
|
let ret = this.renderARow();
|
|
|
|
if (ret === null) {
|
2018-03-30 08:30:23 +08:00
|
|
|
throw new Error("Grid photos rendering failed");
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
|
|
|
renderedContentHeight += ret;
|
2016-06-25 16:09:05 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-04-19 03:27:15 +08:00
|
|
|
|
2016-06-26 17:17:30 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
/**
|
|
|
|
* Returns true, if scroll is >= 70% to render more images.
|
2017-06-23 03:23:26 +08:00
|
|
|
* Or of onscroll rendering is off: return always to render all the images at once
|
2017-06-11 04:32:56 +08:00
|
|
|
* @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 {
|
|
|
|
return Config.Client.enableOnScrollRendering === false ||
|
|
|
|
window.scrollY >= (document.body.clientHeight + offset - window.innerHeight) * 0.7
|
|
|
|
|| (document.body.clientHeight + offset) * 0.85 < window.innerHeight;
|
2016-04-09 21:19:25 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-06-25 16:09:05 +08:00
|
|
|
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
@HostListener('window:scroll')
|
|
|
|
onScroll() {
|
|
|
|
if (!this.onScrollFired) {
|
|
|
|
window.requestAnimationFrame(() => {
|
|
|
|
this.renderPhotos();
|
2016-06-25 16:09:05 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
if (Config.Client.enableOnScrollThumbnailPrioritising === true) {
|
|
|
|
this.gridPhotoQL.toArray().forEach((pc: GalleryPhotoComponent) => {
|
|
|
|
pc.onScroll();
|
|
|
|
});
|
2016-05-04 23:20:21 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
this.onScrollFired = false;
|
|
|
|
});
|
|
|
|
this.onScrollFired = true;
|
2016-05-04 23:20:21 +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 renderARow(): number {
|
|
|
|
if (this.renderedPhotoIndex >= this.photos.length) {
|
|
|
|
return null;
|
2016-06-25 16:09:05 +08:00
|
|
|
}
|
|
|
|
|
2017-03-26 04:59:30 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
let maxRowHeight = window.innerHeight / this.MIN_ROW_COUNT;
|
|
|
|
let minRowHeight = window.innerHeight / this.MAX_ROW_COUNT;
|
2016-07-04 22:58:10 +08:00
|
|
|
|
2017-07-17 03:03:37 +08:00
|
|
|
let photoRowBuilder = new GridRowBuilder(this.photos,
|
|
|
|
this.renderedPhotoIndex,
|
|
|
|
this.IMAGE_MARGIN,
|
|
|
|
this.containerWidth - this.overlayService.getPhantomScrollbarWidth()
|
|
|
|
);
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
photoRowBuilder.addPhotos(this.TARGET_COL_COUNT);
|
|
|
|
photoRowBuilder.adjustRowHeightBetween(minRowHeight, maxRowHeight);
|
2016-07-04 22:58:10 +08:00
|
|
|
|
2017-07-17 03:03:37 +08:00
|
|
|
if (photoRowBuilder.getPhotoRow().length > 1) { //little trick: We don't want too big single images. But if a little extra height helps fit the row, its ok
|
|
|
|
maxRowHeight *= 1.2;
|
|
|
|
}
|
|
|
|
let rowHeight = Math.min(photoRowBuilder.calcRowHeight(), maxRowHeight);
|
2017-06-11 04:32:56 +08:00
|
|
|
let imageHeight = rowHeight - (this.IMAGE_MARGIN * 2);
|
2016-07-04 22:58:10 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
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));
|
|
|
|
});
|
2016-07-04 22:58:10 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
this.renderedPhotoIndex += photoRowBuilder.getPhotoRow().length;
|
|
|
|
return rowHeight;
|
|
|
|
}
|
2016-07-04 22:58:10 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
private updateContainerWidth(): number {
|
|
|
|
if (!this.gridContainer) {
|
|
|
|
return;
|
2016-04-28 04:37:07 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
this.containerWidth = this.gridContainer.nativeElement.clientWidth;
|
|
|
|
}
|
2016-04-28 04:37:07 +08:00
|
|
|
|
2016-04-09 21:19:25 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|