1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2025-01-14 14:43:17 +08:00

improving thumbnail loading

This commit is contained in:
Braun Patrik 2016-06-23 22:25:16 +02:00
parent 8f4237ae80
commit 1de7dabe52
5 changed files with 59 additions and 31 deletions

View File

@ -1,5 +1,5 @@
<div class="static" *ngIf="!animate"><span class="glyphicon glyphicon-picture" aria-hidden="true"></span></div> <div class="static" *ngIf="animate == false"><span class="glyphicon glyphicon-picture" aria-hidden="true"></span></div>
<div class="sk-cube-grid animate" *ngIf="animate"> <div class="sk-cube-grid animate" *ngIf="animate == true">
<div class="sk-cube sk-cube1"></div> <div class="sk-cube sk-cube1"></div>
<div class="sk-cube sk-cube2"></div> <div class="sk-cube sk-cube2"></div>
<div class="sk-cube sk-cube3"></div> <div class="sk-cube sk-cube3"></div>

View File

@ -1,6 +1,6 @@
///<reference path="../../../../../browser.d.ts"/> ///<reference path="../../../../../browser.d.ts"/>
import {Component} from "@angular/core"; import {Component, Input} from "@angular/core";
@Component({ @Component({
selector: 'gallery-grid-photo-loading', selector: 'gallery-grid-photo-loading',
@ -9,10 +9,8 @@ import {Component} from "@angular/core";
}) })
export class GalleryPhotoLoadingComponent { export class GalleryPhotoLoadingComponent {
animate = false; @Input() animate:boolean;
startAnimation() {
this.animate = true;
}
} }

View File

@ -1,11 +1,11 @@
<div class="photo-container" (mouseover)="hover()" (mouseout)="mouseOut()"> <div class="photo-container" (mouseover)="hover()" (mouseout)="mouseOut()">
<img #image [src]="imageSrc" [hidden]="!showImage"> <img #img [src]="image.src" [hidden]="!image.show">
<gallery-grid-photo-loading #loading [hidden]="!showLoading"> <gallery-grid-photo-loading [animate]="loading.animate" *ngIf="loading.show">
</gallery-grid-photo-loading> </gallery-grid-photo-loading>
<!--Info box --> <!--Info box -->
<div #info [hidden]="!showImage" class="info" [style.margin-top.px]="-infoStyle.height" <div #info class="info" [style.margin-top.px]="-infoStyle.height"
[style.background]="infoStyle.background"> [style.background]="infoStyle.background">
<div class="photo-name">{{gridPhoto.photo.name}}</div> <div class="photo-name">{{gridPhoto.photo.name}}</div>

View File

@ -17,13 +17,20 @@ import {GalleryPhotoLoadingComponent} from "./loading/loading.photo.grid.gallery
}) })
export class GalleryPhotoComponent implements IRenderable, AfterViewInit { export class GalleryPhotoComponent implements IRenderable, AfterViewInit {
@Input() gridPhoto:GridPhoto; @Input() gridPhoto:GridPhoto;
@ViewChild("image") imageRef:ElementRef; @ViewChild("img") imageRef:ElementRef;
@ViewChild("info") infoDiv:ElementRef; @ViewChild("info") infoDiv:ElementRef;
@ViewChild(GalleryPhotoLoadingComponent) loading:GalleryPhotoLoadingComponent;
imageSrc = "#";
showImage = false; image = {
showLoading = false; src: "#",
show: false
};
loading = {
animate: false,
show: false
};
infoStyle = { infoStyle = {
height: 0, height: 0,
background: "rgba(0,0,0,0.0)" background: "rgba(0,0,0,0.0)"
@ -41,22 +48,24 @@ export class GalleryPhotoComponent implements IRenderable, AfterViewInit {
//schedule change after Angular checks the model //schedule change after Angular checks the model
setImmediate(() => { setImmediate(() => {
if (this.gridPhoto.isThumbnailAvailable()) { if (this.gridPhoto.isThumbnailAvailable()) {
this.imageSrc = this.gridPhoto.getThumbnailPath(); this.image.src = this.gridPhoto.getThumbnailPath();
this.showImage = true; this.image.show = true;
this.showLoading = false; this.loading.show = false;
} else { } else {
this.showLoading = true; this.loading.show = true;
this.thumbnailService.loadImage(this.gridPhoto, this.thumbnailService.loadImage(this.gridPhoto,
()=> { //onLoadStarted ()=> { //onLoadStarted
this.loading.startAnimation(); this.loading.animate = true;
}, },
()=> {//onLoaded ()=> {//onLoaded
this.imageSrc = this.gridPhoto.getThumbnailPath(); this.image.src = this.gridPhoto.getThumbnailPath();
this.showImage = true; this.image.show = true;
this.showLoading = false; this.loading.show = false;
}, },
()=> {//onError (error)=> {//onError
//TODO: handle error
console.error("something bad happened"); console.error("something bad happened");
console.error(error);
}); });
} }
}); });

View File

@ -13,21 +13,33 @@ export class ThumbnailLoaderService {
constructor() { constructor() {
} }
loadImage(gridPhoto:GridPhoto, onStartedLoading, onLoad, onError):void { removeTasks() {
this.que = [];
}
loadImage(gridPhoto:GridPhoto, onStartedLoading:()=>void, onLoad:()=>void, onError:(error)=>void):void {
let tmp:ThumbnailTask = null; let tmp:ThumbnailTask = null;
//is image already qued?
for (let i = 0; i < this.que.length; i++) { for (let i = 0; i < this.que.length; i++) {
if (this.que[i].gridPhoto.getThumbnailPath() == gridPhoto.getThumbnailPath()) { if (this.que[i].gridPhoto.getThumbnailPath() == gridPhoto.getThumbnailPath()) {
tmp = this.que[i]; tmp = this.que[i];
break; break;
} }
} }
//add to previous
if (tmp != null) { if (tmp != null) {
tmp.onStartedLoading.push(onStartedLoading); tmp.onStartedLoading.push(onStartedLoading);
tmp.onLoad.push(onLoad); tmp.onLoad.push(onLoad);
tmp.onError.push(onError); tmp.onError.push(onError);
} else { if (tmp.inProgress == true) {
onStartedLoading();
}
} else {//create new task
this.que.push({ this.que.push({
gridPhoto: gridPhoto, gridPhoto: gridPhoto,
inProgress: false,
onStartedLoading: [onStartedLoading], onStartedLoading: [onStartedLoading],
onLoad: [onLoad], onLoad: [onLoad],
onError: [onError] onError: [onError]
@ -43,20 +55,28 @@ export class ThumbnailLoaderService {
return; return;
} }
this.runningRequests++; this.runningRequests++;
let task = this.que.shift(); let task = this.que[0];
task.onStartedLoading.forEach(cb=>cb()); task.onStartedLoading.forEach(cb=>cb());
task.inProgress = true;
let curImg = new Image(); let curImg = new Image();
curImg.src = task.gridPhoto.getThumbnailPath(); curImg.src = task.gridPhoto.getThumbnailPath();
curImg.onload = () => { curImg.onload = () => {
task.gridPhoto.thumbnailLoaded(); task.gridPhoto.thumbnailLoaded();
task.onLoad.forEach(cb=>cb()); task.onLoad.forEach(cb=>cb());
this.que.shift();
this.runningRequests--; this.runningRequests--;
this.run(); this.run();
}; };
curImg.onerror = (error) => { curImg.onerror = (error) => {
task.onLoad.forEach(cb=>cb(error)); task.onLoad.forEach(cb=>cb(error));
this.que.shift();
this.runningRequests--; this.runningRequests--;
this.run(); this.run();
}; };
@ -66,6 +86,7 @@ export class ThumbnailLoaderService {
interface ThumbnailTask { interface ThumbnailTask {
gridPhoto:GridPhoto; gridPhoto:GridPhoto;
inProgress:boolean;
onStartedLoading:Array<Function>; onStartedLoading:Array<Function>;
onLoad:Array<Function>; onLoad:Array<Function>;
onError:Array<Function>; onError:Array<Function>;