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

304 lines
9.0 KiB
TypeScript
Raw Normal View History

import {
AfterViewInit,
ChangeDetectorRef,
Component,
ElementRef,
HostListener,
Input,
OnChanges,
2017-07-20 04:40:27 +08:00
OnDestroy,
QueryList,
ViewChild,
ViewChildren,
OnInit
2018-03-31 03:30:30 +08:00
} from '@angular/core';
import {PhotoDTO} from '../../../../common/entities/PhotoDTO';
import {GridRowBuilder} from './GridRowBuilder';
import {GalleryLightboxComponent, LightboxStates} from '../lightbox/lightbox.gallery.component';
2018-03-31 03:30:30 +08:00
import {GridPhoto} from './GridPhoto';
import {GalleryPhotoComponent} from './photo/photo.grid.gallery.component';
import {OverlayService} from '../overlay.service';
import {Config} from '../../../../common/config/public/Config';
2018-05-10 01:56:02 +08:00
import {PageHelper} from '../../model/page.helper';
import {Subscription} from 'rxjs';
import {ActivatedRoute, Params, Router} from '@angular/router';
import {QueryService} from '../../model/query.service';
import {SimpleChanges} from '@angular/core';
2016-05-04 23:20:21 +08:00
@Component({
2018-05-04 07:17:08 +08:00
selector: 'app-gallery-grid',
templateUrl: './grid.gallery.component.html',
styleUrls: ['./grid.gallery.component.css'],
})
export class GalleryGridComponent implements OnChanges, OnInit, AfterViewInit, OnDestroy {
2016-04-28 04:37:07 +08:00
@ViewChild('gridContainer') gridContainer: ElementRef;
@ViewChildren(GalleryPhotoComponent) gridPhotoQL: QueryList<GalleryPhotoComponent>;
2018-05-17 06:52:08 +08:00
private scrollListenerPhotos: GalleryPhotoComponent[] = [];
2016-05-04 23:20:21 +08:00
@Input() photos: Array<PhotoDTO>;
@Input() lightbox: GalleryLightboxComponent;
2016-05-04 23:20:21 +08:00
photosToRender: Array<GridPhoto> = [];
2018-05-04 07:17:08 +08:00
containerWidth = 0;
2018-05-14 10:10:56 +08:00
screenHeight = 0;
2016-05-04 23:20:21 +08:00
2017-07-14 05:39:09 +08:00
public 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
private onScrollFired = false;
2017-07-20 04:40:27 +08:00
private helperTime = null;
2018-05-04 07:17:08 +08:00
isAfterViewInit = false;
private renderedPhotoIndex = 0;
routeSubscription: Subscription = null;
delayedRenderUpToPhoto: string = null;
2016-07-05 18:34:11 +08:00
2017-07-20 04:40:27 +08:00
constructor(private overlayService: OverlayService,
private changeDetector: ChangeDetectorRef,
public queryService: QueryService,
private router: Router,
private route: ActivatedRoute) {
}
ngOnInit() {
this.routeSubscription = this.route.queryParams.subscribe((params: Params) => {
if (params[QueryService.PHOTO_PARAM] && params[QueryService.PHOTO_PARAM] !== '') {
this.delayedRenderUpToPhoto = params[QueryService.PHOTO_PARAM];
if (!this.photos || this.photos.length === 0) {
return;
}
this.renderUpToPhoto(params[QueryService.PHOTO_PARAM]);
}
});
}
ngOnChanges() {
if (this.isAfterViewInit === false) {
return;
}
2018-05-14 10:10:56 +08:00
this.updateContainerDimensions();
this.sortPhotos();
this.mergeNewPhotos();
2017-07-20 04:40:27 +08:00
this.helperTime = setTimeout(() => {
this.renderPhotos();
if (this.delayedRenderUpToPhoto) {
this.renderUpToPhoto(this.delayedRenderUpToPhoto);
}
}, 0);
}
2017-07-20 04:40:27 +08:00
ngOnDestroy() {
if (this.helperTime != null) {
clearTimeout(this.helperTime);
}
}
@HostListener('window:resize')
onResize() {
if (this.isAfterViewInit === false) {
return;
}
2018-05-04 07:17:08 +08:00
// render the same amount of images on resize
const renderedIndex = this.renderedPhotoIndex;
2018-05-14 10:10:56 +08:00
// do not rerender if container is not changes
if (this.updateContainerDimensions() === false) {
return;
}
this.sortPhotos();
this.renderPhotos(renderedIndex);
}
ngAfterViewInit() {
this.lightbox.setGridPhotoQL(this.gridPhotoQL);
2018-05-17 06:52:08 +08:00
if (Config.Client.enableOnScrollThumbnailPrioritising === true) {
this.gridPhotoQL.changes.subscribe(() => {
this.scrollListenerPhotos = this.gridPhotoQL.filter(pc => pc.ScrollListener);
});
}
2018-05-14 10:10:56 +08:00
this.updateContainerDimensions();
this.sortPhotos();
this.clearRenderedPhotos();
2017-07-20 04:40:27 +08:00
this.helperTime = setTimeout(() => {
this.renderPhotos();
}, 0);
this.isAfterViewInit = true;
}
private renderUpToPhoto(photoName: string) {
const index = this.photos.findIndex(p => p.name === photoName);
if (index === -1) {
this.router.navigate([], {queryParams: this.queryService.getParams()});
return;
}
while (this.renderedPhotoIndex < index && this.renderARow()) {
}
}
2018-05-04 07:17:08 +08:00
public renderARow(): number {
if (this.renderedPhotoIndex >= this.photos.length
|| this.containerWidth === 0) {
2018-05-04 07:17:08 +08:00
return null;
}
2018-05-14 10:10:56 +08:00
let maxRowHeight = this.screenHeight / this.MIN_ROW_COUNT;
const minRowHeight = this.screenHeight / this.MAX_ROW_COUNT;
2018-05-04 07:17:08 +08:00
const photoRowBuilder = new GridRowBuilder(this.photos,
this.renderedPhotoIndex,
this.IMAGE_MARGIN,
this.containerWidth - this.overlayService.getPhantomScrollbarWidth()
);
photoRowBuilder.addPhotos(this.TARGET_COL_COUNT);
photoRowBuilder.adjustRowHeightBetween(minRowHeight, maxRowHeight);
// little trick: We don't want too big single images. But if a little extra height helps fit the row, its ok
if (photoRowBuilder.getPhotoRow().length > 1) {
maxRowHeight *= 1.2;
}
const rowHeight = Math.min(photoRowBuilder.calcRowHeight(), maxRowHeight);
const imageHeight = rowHeight - (this.IMAGE_MARGIN * 2);
photoRowBuilder.getPhotoRow().forEach((photo) => {
const imageWidth = imageHeight * (photo.metadata.size.width / photo.metadata.size.height);
this.photosToRender.push(new GridPhoto(photo, imageWidth, imageHeight, this.renderedPhotoIndex));
});
2018-05-04 07:17:08 +08:00
this.renderedPhotoIndex += photoRowBuilder.getPhotoRow().length;
return rowHeight;
}
private clearRenderedPhotos() {
this.photosToRender = [];
this.renderedPhotoIndex = 0;
this.changeDetector.detectChanges();
}
2018-05-04 07:17:08 +08:00
private sortPhotos() {
// sort photos by date
2018-05-04 07:17:08 +08:00
this.photos.sort((a: PhotoDTO, b: PhotoDTO) => {
return a.metadata.creationDate - b.metadata.creationDate;
});
}
private mergeNewPhotos() {
2018-05-04 07:17:08 +08:00
// merge new data with old one
let lastSameIndex = 0;
let lastRowId = null;
2018-05-14 10:10:56 +08:00
for (let i = 0; i < this.photos.length && i < this.photosToRender.length; ++i) {
2018-05-14 10:10:56 +08:00
// If a photo changed the whole row has to be removed
2018-05-04 07:17:08 +08:00
if (this.photosToRender[i].rowId !== lastRowId) {
lastSameIndex = i;
lastRowId = this.photosToRender[i].rowId;
}
if (this.photosToRender[i].equals(this.photos[i]) === false) {
break;
}
}
if (lastSameIndex > 0) {
this.photosToRender.splice(lastSameIndex, this.photosToRender.length - lastSameIndex);
this.renderedPhotoIndex = lastSameIndex;
} else {
this.clearRenderedPhotos();
}
}
2016-05-04 23:20:21 +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
* @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 ||
2018-05-10 01:56:02 +08:00
PageHelper.ScrollY >= (document.body.clientHeight + offset - window.innerHeight) * 0.7
|| (document.body.clientHeight + offset) * 0.85 < window.innerHeight;
}
@HostListener('window:scroll')
onScroll() {
2018-05-17 06:52:08 +08:00
if (!this.onScrollFired &&
// should we trigger this at all?
(this.renderedPhotoIndex < this.photos.length || this.scrollListenerPhotos.length > 0)) {
window.requestAnimationFrame(() => {
this.renderPhotos();
if (Config.Client.enableOnScrollThumbnailPrioritising === true) {
2018-05-17 06:52:08 +08:00
this.scrollListenerPhotos.forEach((pc: GalleryPhotoComponent) => {
pc.onScroll();
});
2018-05-17 06:52:08 +08:00
this.scrollListenerPhotos = this.scrollListenerPhotos.filter(pc => pc.ScrollListener);
2016-05-04 23:20:21 +08:00
}
2018-05-17 06:52:08 +08:00
this.onScrollFired = false;
});
this.onScrollFired = true;
2016-05-04 23:20:21 +08:00
}
}
2018-05-04 07:17:08 +08:00
private renderPhotos(numberOfPhotos: number = 0) {
if (this.containerWidth === 0 ||
this.renderedPhotoIndex >= this.photos.length ||
!this.shouldRenderMore()) {
return;
}
2018-05-04 07:17:08 +08:00
let renderedContentHeight = 0;
2018-05-04 07:17:08 +08:00
while (this.renderedPhotoIndex < this.photos.length &&
(this.shouldRenderMore(renderedContentHeight) === true ||
this.renderedPhotoIndex < numberOfPhotos)) {
const ret = this.renderARow();
if (ret === null) {
throw new Error('Grid photos rendering failed');
}
renderedContentHeight += ret;
2017-07-17 03:03:37 +08:00
}
}
2018-05-14 10:10:56 +08:00
private updateContainerDimensions(): boolean {
if (!this.gridContainer) {
2018-05-14 10:10:56 +08:00
return false;
2016-04-28 04:37:07 +08:00
}
2018-05-14 10:10:56 +08:00
const pre = PageHelper.isScrollYVisible();
PageHelper.showScrollY();
2018-05-14 10:10:56 +08:00
// if the width changed a bit or the height changed a lot
if (this.containerWidth !== this.gridContainer.nativeElement.clientWidth
|| this.screenHeight < window.innerHeight * 0.75
|| this.screenHeight > window.innerHeight * 1.25) {
this.screenHeight = window.innerHeight;
this.containerWidth = this.gridContainer.nativeElement.clientWidth;
this.clearRenderedPhotos();
if (!pre) {
PageHelper.hideScrollY();
}
2018-05-14 10:10:56 +08:00
return true;
}
if (!pre) {
PageHelper.hideScrollY();
}
2018-05-14 10:10:56 +08:00
return false;
}
2016-04-28 04:37:07 +08:00
}