grid performance improvement

This commit is contained in:
Braun Patrik 2016-06-25 14:13:06 +02:00
parent 5740675344
commit 1bcf6f4fa7
5 changed files with 44 additions and 30 deletions

View File

@ -6,10 +6,14 @@ class ProjectPathClass {
public ImageFolder:string; public ImageFolder:string;
public ThumbnailFolder:string; public ThumbnailFolder:string;
isAbsolutePath(pathStr) {
return path.resolve(pathStr) === path.normalize(pathStr).replace(RegExp(pathStr.sep + '$'), '');
}
constructor() { constructor() {
this.Root = path.join(__dirname, "/../"); this.Root = path.join(__dirname, "/../");
this.ImageFolder = path.join(this.Root, Config.Server.imagesFolder); this.ImageFolder = this.isAbsolutePath(Config.Server.imagesFolder) ? Config.Server.imagesFolder : path.join(this.Root, Config.Server.imagesFolder);
this.ThumbnailFolder = path.join(this.Root, Config.Server.thumbnailFolder); this.ThumbnailFolder = this.isAbsolutePath(Config.Server.thumbnailFolder) ? Config.Server.thumbnailFolder : path.join(this.Root, Config.Server.thumbnailFolder);
} }
} }

View File

@ -15,13 +15,14 @@ import {
PositionMetaData, PositionMetaData,
GPSMetadata GPSMetadata
} from "../../common/entities/Photo"; } from "../../common/entities/Photo";
import {ProjectPath} from "../ProjectPath";
export class DiskManager { export class DiskManager {
public static scanDirectory(relativeDirectoryName, cb:(error:any, result:Directory) => void) { public static scanDirectory(relativeDirectoryName, cb:(error:any, result:Directory) => void) {
console.log("DiskManager: scanDirectory"); console.log("DiskManager: scanDirectory");
let directoryName = path.basename(relativeDirectoryName); let directoryName = path.basename(relativeDirectoryName);
let directoryParent = path.join(path.dirname(relativeDirectoryName), "/"); let directoryParent = path.join(path.dirname(relativeDirectoryName), "/");
let absoluteDirectoryName = path.join(__dirname, "/../../demo/images", relativeDirectoryName); let absoluteDirectoryName = path.join(ProjectPath.ImageFolder, relativeDirectoryName);
let directory = new Directory(1, directoryName, directoryParent, new Date(), [], []); let directory = new Directory(1, directoryName, directoryParent, new Date(), [], []);

View File

@ -1,4 +1,4 @@
<div #gridContainer (window:resize)="onResize()"> <div #gridContainer>
<gallery-grid-photo <gallery-grid-photo
*ngFor="let gridPhoto of photosToRender" *ngFor="let gridPhoto of photosToRender"
(click)="lightbox.show(gridPhoto.photo)" (click)="lightbox.show(gridPhoto.photo)"

View File

@ -42,13 +42,22 @@ export class GalleryGridComponent implements OnChanges,AfterViewInit {
} }
ngOnChanges() { ngOnChanges() {
if (this.isAfterViewInit == false) {
return;
}
this.onPhotosChanged(); this.onPhotosChanged();
} }
@HostListener('window:resize')
onResize() { onResize() {
if (this.isAfterViewInit == false) {
return;
}
this.onPhotosChanged(); this.onPhotosChanged();
} }
isAfterViewInit:boolean = false;
ngAfterViewInit() { ngAfterViewInit() {
this.lightbox.gridPhotoQL = this.gridPhotoQL; this.lightbox.gridPhotoQL = this.gridPhotoQL;
@ -56,6 +65,7 @@ export class GalleryGridComponent implements OnChanges,AfterViewInit {
this.onPhotosChanged(); this.onPhotosChanged();
this.isAfterViewInit = true;
} }
@ -89,7 +99,7 @@ export class GalleryGridComponent implements OnChanges,AfterViewInit {
let renderedContentHeight = 0; let renderedContentHeight = 0;
while (this.renderedPhotoIndex < this.photos.length && this.shouldRenderMore(renderedContentHeight)) { while (this.renderedPhotoIndex < this.photos.length && this.shouldRenderMore(renderedContentHeight) === true) {
let photoRowBuilder = new GridRowBuilder(this.photos, this.renderedPhotoIndex, this.IMAGE_MARGIN, this.getContainerWidth()); let photoRowBuilder = new GridRowBuilder(this.photos, this.renderedPhotoIndex, this.IMAGE_MARGIN, this.getContainerWidth());
photoRowBuilder.addPhotos(this.TARGET_COL_COUNT); photoRowBuilder.addPhotos(this.TARGET_COL_COUNT);
@ -105,20 +115,23 @@ export class GalleryGridComponent implements OnChanges,AfterViewInit {
renderedContentHeight += rowHeight; renderedContentHeight += rowHeight;
this.renderedPhotoIndex += photoRowBuilder.getPhotoRow().length; this.renderedPhotoIndex += photoRowBuilder.getPhotoRow().length;
} }
} }
private shouldRenderMore(offset:number = 0):boolean { private shouldRenderMore(offset:number = 0):boolean {
return document.body.scrollTop >= (document.body.clientHeight + offset - window.innerHeight) * 0.7 return document.body.scrollTop >= (document.body.clientHeight + offset - window.innerHeight) * 0.7
|| document.body.clientHeight + offset < window.innerHeight; || (document.body.clientHeight + offset) * 0.85 < window.innerHeight;
} }
@HostListener('window:scroll') @HostListener('window:scroll')
onScroll() { onScroll() {
this.renderPhotos(); this.renderPhotos();
this.gridPhotoQL.toArray().forEach((pc:GalleryPhotoComponent) => {
pc.onScroll();
});
} }
private getContainerWidth():number { private getContainerWidth():number {

View File

@ -47,7 +47,7 @@ export class GalleryPhotoComponent implements IRenderable, OnInit, AfterViewInit
SearchTypes:any = []; SearchTypes:any = [];
searchEnabled:boolean = true; searchEnabled:boolean = true;
scrollListener = null; wasInView:boolean = null;
constructor(private thumbnailService:ThumbnailLoaderService, private renderer:Renderer) { constructor(private thumbnailService:ThumbnailLoaderService, private renderer:Renderer) {
this.SearchTypes = SearchTypes; this.SearchTypes = SearchTypes;
@ -88,23 +88,17 @@ export class GalleryPhotoComponent implements IRenderable, OnInit, AfterViewInit
this.image.show = true; this.image.show = true;
this.loading.show = false; this.loading.show = false;
this.thumbnailTask = null; this.thumbnailTask = null;
if (this.scrollListener) {
this.scrollListener();
}
}, },
onError: (error)=> {//onError onError: (error)=> {//onError
this.thumbnailTask = null; this.thumbnailTask = null;
if (this.scrollListener) {
this.scrollListener();
}
//TODO: handle error //TODO: handle error
console.error("something bad happened"); console.error("something bad happened");
console.error(error); console.error(error);
} }
}; };
this.scrollListener = this.renderer.listenGlobal('window', 'scroll', () => { /* this.scrollListener = this.renderer.listenGlobal('window', 'scroll', () => {
this.onScroll(); this.onScroll();
}); });*/
if (this.gridPhoto.isReplacementThumbnailAvailable()) { if (this.gridPhoto.isReplacementThumbnailAvailable()) {
this.thumbnailTask = this.thumbnailService.loadImage(this.gridPhoto, ThumbnailLoadingPriority.medium, listener); this.thumbnailTask = this.thumbnailService.loadImage(this.gridPhoto, ThumbnailLoadingPriority.medium, listener);
} else { } else {
@ -121,9 +115,6 @@ export class GalleryPhotoComponent implements IRenderable, OnInit, AfterViewInit
this.thumbnailService.removeTask(this.thumbnailTask); this.thumbnailService.removeTask(this.thumbnailTask);
this.thumbnailTask = null; this.thumbnailTask = null;
} }
if (this.scrollListener) {
this.scrollListener();
}
} }
@ -132,19 +123,24 @@ export class GalleryPhotoComponent implements IRenderable, OnInit, AfterViewInit
&& document.body.scrollTop + window.innerHeight > this.container.nativeElement.offsetTop; && document.body.scrollTop + window.innerHeight > this.container.nativeElement.offsetTop;
} }
onScroll() { onScroll() {
if (this.thumbnailTask != null) { if (this.thumbnailTask != null) {
if (this.isInView() == true) { let isInView = this.isInView();
if (this.gridPhoto.isReplacementThumbnailAvailable()) { if (this.wasInView != isInView) {
this.thumbnailTask.priority = ThumbnailLoadingPriority.medium; this.wasInView = isInView;
if (isInView == true) {
if (this.gridPhoto.isReplacementThumbnailAvailable()) {
this.thumbnailTask.priority = ThumbnailLoadingPriority.medium;
} else {
this.thumbnailTask.priority = ThumbnailLoadingPriority.high;
}
} else { } else {
this.thumbnailTask.priority = ThumbnailLoadingPriority.high; if (this.gridPhoto.isReplacementThumbnailAvailable()) {
} this.thumbnailTask.priority = ThumbnailLoadingPriority.low;
} else { } else {
if (this.gridPhoto.isReplacementThumbnailAvailable()) { this.thumbnailTask.priority = ThumbnailLoadingPriority.medium;
this.thumbnailTask.priority = ThumbnailLoadingPriority.low; }
} else {
this.thumbnailTask.priority = ThumbnailLoadingPriority.medium;
} }
} }
} }