1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/frontend/app/gallery/thumnailManager.service.ts

223 lines
5.6 KiB
TypeScript
Raw Normal View History

2018-03-31 03:30:30 +08:00
import {Injectable} from '@angular/core';
import {ThumbnailLoaderService, ThumbnailLoadingListener, ThumbnailLoadingPriority, ThumbnailTaskEntity} from './thumnailLoader.service';
2018-11-05 02:28:32 +08:00
import {Media} from './Media';
import {MediaIcon} from './MediaIcon';
2017-03-20 07:01:41 +08:00
@Injectable()
export class ThumbnailManagerService {
constructor(private thumbnailLoader: ThumbnailLoaderService) {
}
2017-03-20 07:01:41 +08:00
2018-11-05 02:28:32 +08:00
public getThumbnail(photo: Media): Thumbnail {
return new Thumbnail(photo, this.thumbnailLoader);
}
2018-11-05 02:28:32 +08:00
public getLazyThumbnail(photo: Media): Thumbnail {
2017-07-24 04:36:53 +08:00
return new Thumbnail(photo, this.thumbnailLoader, false);
}
2018-11-05 02:28:32 +08:00
public getIcon(photo: MediaIcon) {
return new IconThumbnail(photo, this.thumbnailLoader);
}
}
export abstract class ThumbnailBase {
protected available = false;
protected src: string = null;
2018-11-02 17:40:09 +08:00
public loading = false;
protected error = false;
protected onLoad: Function = null;
2017-07-24 04:36:53 +08:00
protected thumbnailTask: ThumbnailTaskEntity = null;
protected constructor(protected thumbnailService: ThumbnailLoaderService) {
}
abstract set Visible(visible: boolean);
set OnLoad(onLoad: Function) {
this.onLoad = onLoad;
}
2017-07-18 05:11:35 +08:00
get Available(): boolean {
return this.available;
}
2017-07-18 05:11:35 +08:00
get Src(): string {
return this.src;
}
2017-07-18 05:11:35 +08:00
get Loading(): boolean {
return this.loading;
}
2017-07-18 05:11:35 +08:00
get Error(): boolean {
return this.error;
}
destroy() {
if (this.thumbnailTask != null) {
this.thumbnailService.removeTask(this.thumbnailTask);
this.thumbnailTask = null;
}
}
2017-03-20 07:01:41 +08:00
}
export class IconThumbnail extends ThumbnailBase {
2017-03-20 07:01:41 +08:00
2018-11-05 02:28:32 +08:00
constructor(private media: MediaIcon, thumbnailService: ThumbnailLoaderService) {
super(thumbnailService);
2018-03-31 03:30:30 +08:00
this.src = '';
2017-06-22 03:16:04 +08:00
this.error = false;
2018-11-05 02:28:32 +08:00
if (this.media.isIconAvailable()) {
this.src = this.media.getIconPath();
this.available = true;
if (this.onLoad) {
this.onLoad();
}
}
2018-11-05 02:28:32 +08:00
if (!this.media.isIconAvailable()) {
2017-06-11 04:56:23 +08:00
setTimeout(() => {
const listener: ThumbnailLoadingListener = {
onStartedLoading: () => { // onLoadStarted
this.loading = true;
},
onLoad: () => {// onLoaded
2018-11-05 02:28:32 +08:00
this.src = this.media.getIconPath();
if (this.onLoad) {
this.onLoad();
}
this.available = true;
this.loading = false;
this.thumbnailTask = null;
},
onError: (error) => {// onError
this.thumbnailTask = null;
2017-06-22 03:16:04 +08:00
this.loading = false;
this.error = true;
}
};
2018-11-05 02:28:32 +08:00
this.thumbnailTask = this.thumbnailService.loadIcon(this.media, ThumbnailLoadingPriority.high, listener);
2017-03-20 07:01:41 +08:00
2017-06-11 04:56:23 +08:00
}, 0);
}
}
set Visible(visible: boolean) {
if (!this.thumbnailTask) {
return;
}
if (visible === true) {
this.thumbnailTask.priority = ThumbnailLoadingPriority.high;
} else {
this.thumbnailTask.priority = ThumbnailLoadingPriority.medium;
}
}
}
export class Thumbnail extends ThumbnailBase {
2018-11-05 02:28:32 +08:00
constructor(private media: Media, thumbnailService: ThumbnailLoaderService, autoLoad: boolean = true) {
super(thumbnailService);
2018-11-05 02:28:32 +08:00
if (this.media.isThumbnailAvailable()) {
this.src = this.media.getThumbnailPath();
this.available = true;
if (this.onLoad) {
this.onLoad();
}
2018-11-05 02:28:32 +08:00
} else if (this.media.isReplacementThumbnailAvailable()) {
this.src = this.media.getReplacementThumbnailPath();
this.available = true;
}
2017-07-24 04:36:53 +08:00
if (autoLoad) {
this.load();
}
}
2017-07-26 05:40:07 +08:00
set CurrentlyWaiting(value: boolean) {
if (!this.thumbnailTask) {
return;
}
2017-07-26 05:40:07 +08:00
if (value === true) {
2018-11-05 02:28:32 +08:00
if (this.media.isReplacementThumbnailAvailable()) {
2017-07-26 05:40:07 +08:00
this.thumbnailTask.priority = ThumbnailLoadingPriority.medium;
} else {
this.thumbnailTask.priority = ThumbnailLoadingPriority.extraHigh;
}
} else {
2018-11-05 02:28:32 +08:00
if (this.media.isReplacementThumbnailAvailable()) {
2017-07-26 05:40:07 +08:00
this.thumbnailTask.priority = ThumbnailLoadingPriority.low;
} else {
this.thumbnailTask.priority = ThumbnailLoadingPriority.medium;
}
}
}
set Visible(visible: boolean) {
if (!this.thumbnailTask) {
return;
}
if (visible === true) {
2018-11-05 02:28:32 +08:00
if (this.media.isReplacementThumbnailAvailable()) {
this.thumbnailTask.priority = ThumbnailLoadingPriority.medium;
} else {
this.thumbnailTask.priority = ThumbnailLoadingPriority.high;
}
} else {
2018-11-05 02:28:32 +08:00
if (this.media.isReplacementThumbnailAvailable()) {
this.thumbnailTask.priority = ThumbnailLoadingPriority.low;
} else {
this.thumbnailTask.priority = ThumbnailLoadingPriority.medium;
}
}
}
2017-07-24 04:36:53 +08:00
public load() {
2018-11-05 02:28:32 +08:00
if (!this.media.isThumbnailAvailable() && this.thumbnailTask == null) {
2017-07-26 05:40:07 +08:00
// setTimeout(() => {
const listener: ThumbnailLoadingListener = {
onStartedLoading: () => { // onLoadStarted
2018-03-31 03:30:30 +08:00
this.loading = true;
},
onLoad: () => {// onLoaded
2018-11-05 02:28:32 +08:00
this.src = this.media.getThumbnailPath();
if (this.onLoad) {
this.onLoad();
}
2018-03-31 03:30:30 +08:00
this.available = true;
this.loading = false;
this.thumbnailTask = null;
},
onError: (error) => {// onError
2018-03-31 03:30:30 +08:00
this.thumbnailTask = null;
this.loading = false;
this.error = true;
2017-03-20 07:01:41 +08:00
}
2018-03-31 03:30:30 +08:00
};
2018-11-05 02:28:32 +08:00
if (this.media.isReplacementThumbnailAvailable()) {
this.thumbnailTask = this.thumbnailService.loadImage(this.media, ThumbnailLoadingPriority.medium, listener);
2018-03-31 03:30:30 +08:00
} else {
2018-11-05 02:28:32 +08:00
this.thumbnailTask = this.thumbnailService.loadImage(this.media, ThumbnailLoadingPriority.high, listener);
2018-03-31 03:30:30 +08:00
}
2017-07-26 05:40:07 +08:00
// }, 0);
2017-03-20 07:01:41 +08:00
}
}
2017-03-20 07:01:41 +08:00
}