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

114 lines
3.9 KiB
TypeScript
Raw Normal View History

2016-05-09 23:04:56 +08:00
import {Injectable} from "@angular/core";
2016-12-27 06:36:38 +08:00
import {NetworkService} from "../model/network/network.service";
2016-03-20 23:54:30 +08:00
import {Message} from "../../../common/entities/Message";
import {ContentWrapper} from "../../../common/entities/ConentWrapper";
2016-12-28 03:55:51 +08:00
import {PhotoDTO} from "../../../common/entities/PhotoDTO";
import {DirectoryDTO} from "../../../common/entities/DirectoryDTO";
2016-05-16 17:03:11 +08:00
import {SearchTypes} from "../../../common/entities/AutoCompleteItem";
2016-06-26 17:08:05 +08:00
import {GalleryCacheService} from "./cache.gallery.service";
2016-03-20 23:54:30 +08:00
@Injectable()
2016-05-09 23:04:56 +08:00
export class GalleryService {
2017-03-20 07:01:41 +08:00
public content: ContentWrapper;
2016-12-28 03:55:51 +08:00
private lastDirectory: DirectoryDTO;
2017-03-20 07:01:41 +08:00
private searchId: any;
2016-05-10 03:43:52 +08:00
2017-03-20 07:01:41 +08:00
constructor(private networkService: NetworkService, private galleryCacheService: GalleryCacheService) {
this.content = new ContentWrapper();
2016-03-20 23:54:30 +08:00
}
2017-03-20 07:01:41 +08:00
lastRequest: {directory: string} = {
2016-06-26 17:08:05 +08:00
directory: null
};
2017-03-20 07:01:41 +08:00
public getDirectory(directoryName: string): Promise<Message<ContentWrapper>> {
2016-05-17 05:15:03 +08:00
this.content = new ContentWrapper();
2016-06-26 17:08:05 +08:00
this.content.directory = this.galleryCacheService.getDirectory(directoryName);
this.content.searchResult = null;
this.lastRequest.directory = directoryName;
return this.networkService.getJson("/gallery/content/" + directoryName).then(
2017-03-20 07:01:41 +08:00
(message: Message<ContentWrapper>) => {
if (!message.error && message.result) {
2016-05-17 05:15:03 +08:00
2016-06-26 17:08:05 +08:00
this.galleryCacheService.setDirectory(message.result.directory); //save it before adding references
if (this.lastRequest.directory != directoryName) {
return;
}
2017-03-20 07:01:41 +08:00
//Add references
2017-03-18 07:11:53 +08:00
let addDir = (dir: DirectoryDTO) => {
dir.photos.forEach((photo: PhotoDTO) => {
photo.directory = dir;
});
dir.directories.forEach((directory: DirectoryDTO) => {
addDir(directory);
directory.parent = dir;
});
};
addDir(message.result.directory);
2016-06-26 17:08:05 +08:00
2016-07-05 00:12:58 +08:00
2016-05-10 03:43:52 +08:00
this.lastDirectory = message.result.directory;
this.content = message.result;
}
return message;
});
}
2016-05-10 03:43:52 +08:00
//TODO: cache
2017-03-20 07:01:41 +08:00
public search(text: string, type?: SearchTypes): Promise<Message<ContentWrapper>> {
clearTimeout(this.searchId);
2016-05-10 03:43:52 +08:00
if (text === null || text === '') {
return Promise.resolve(new Message(null, null));
}
2016-05-16 17:03:11 +08:00
2016-05-17 01:36:04 +08:00
let queryString = "/search/" + text;
2016-05-16 17:03:11 +08:00
if (type) {
queryString += "?type=" + type;
}
2016-06-26 17:08:05 +08:00
return this.networkService.getJson(queryString).then(
2017-03-20 07:01:41 +08:00
(message: Message<ContentWrapper>) => {
if (!message.error && message.result) {
this.content = message.result;
}
return message;
});
}
2016-05-10 03:43:52 +08:00
//TODO: cache (together with normal search)
2017-03-20 07:01:41 +08:00
public instantSearch(text: string): Promise<Message<ContentWrapper>> {
2016-05-10 03:43:52 +08:00
if (text === null || text === '') {
this.content.directory = this.lastDirectory;
this.content.searchResult = null;
clearTimeout(this.searchId);
return Promise.resolve(new Message(null, null));
}
if (this.searchId != null) {
clearTimeout(this.searchId);
}
this.searchId = setTimeout(() => {
this.search(text);
this.searchId = null;
}, 3000); //TODO: set timeout to config
2016-06-26 17:08:05 +08:00
return this.networkService.getJson("/instant-search/" + text).then(
2017-03-20 07:01:41 +08:00
(message: Message<ContentWrapper>) => {
if (!message.error && message.result) {
this.content = message.result;
}
return message;
});
2016-05-10 03:43:52 +08:00
2016-03-20 23:54:30 +08:00
}
}