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

154 lines
5.0 KiB
TypeScript
Raw Normal View History

2017-07-04 01:17:49 +08:00
import {Component, OnDestroy, OnInit, ViewChild} from "@angular/core";
2016-12-27 06:36:38 +08:00
import {AuthenticationService} from "../model/network/authentication.service";
2017-06-04 21:25:08 +08:00
import {ActivatedRoute, Params, Router} from "@angular/router";
2016-03-20 23:54:30 +08:00
import {GalleryService} from "./gallery.service";
import {GalleryGridComponent} from "./grid/grid.gallery.component";
import {GallerySearchComponent} from "./search/search.gallery.component";
2016-05-16 17:03:11 +08:00
import {SearchTypes} from "../../../common/entities/AutoCompleteItem";
2017-06-04 21:25:08 +08:00
import {Config} from "../../../common/config/public/Config";
2017-06-21 17:46:23 +08:00
import {DirectoryDTO} from "../../../common/entities/DirectoryDTO";
2017-06-22 03:32:57 +08:00
import {SearchResultDTO} from "../../../common/entities/SearchResult";
2017-07-04 01:17:49 +08:00
import {ShareService} from "./share.service";
2017-07-09 18:03:17 +08:00
import {NavigationService} from "../model/navigation.service";
import {UserRoles} from "../../../common/entities/UserDTO";
import {Observable} from "rxjs/Rx";
2016-03-13 18:28:29 +08:00
@Component({
selector: 'gallery',
templateUrl: './gallery.component.html',
styleUrls: ['./gallery.component.css']
2016-03-13 18:28:29 +08:00
})
2017-07-04 01:17:49 +08:00
export class GalleryComponent implements OnInit, OnDestroy {
@ViewChild(GallerySearchComponent) search: GallerySearchComponent;
@ViewChild(GalleryGridComponent) grid: GalleryGridComponent;
2016-05-09 23:04:56 +08:00
2017-07-09 18:03:17 +08:00
public showSearchBar: boolean = false;
public showShare: boolean = false;
2017-06-21 17:46:23 +08:00
public directories: DirectoryDTO[] = [];
2017-06-22 03:32:57 +08:00
public isPhotoWithLocation = false;
2017-07-09 18:03:17 +08:00
private $counter;
2017-07-04 01:17:49 +08:00
private subscription = {
content: null,
2017-07-09 18:03:17 +08:00
route: null,
timer: null
2017-07-04 01:17:49 +08:00
};
2017-07-09 18:03:17 +08:00
public countDown = null;
2016-05-16 17:03:11 +08:00
constructor(public _galleryService: GalleryService,
private _authService: AuthenticationService,
private _router: Router,
2017-07-04 01:17:49 +08:00
private shareService: ShareService,
2017-07-09 18:03:17 +08:00
private _route: ActivatedRoute,
private _navigation: NavigationService) {
}
2017-07-09 18:03:17 +08:00
updateTimer(t: number) {
if (this.shareService.sharing.value == null) {
return;
}
t = Math.floor((this.shareService.sharing.value.expires - Date.now()) / 1000);
this.countDown = {};
this.countDown.day = Math.floor(t / 86400);
t -= this.countDown.day * 86400;
this.countDown.hour = Math.floor(t / 3600) % 24;
t -= this.countDown.hour * 3600;
this.countDown.minute = Math.floor(t / 60) % 60;
t -= this.countDown.minute * 60;
this.countDown.second = t % 60;
}
2017-07-04 01:17:49 +08:00
2017-07-09 18:03:17 +08:00
async ngOnInit() {
await this.shareService.wait();
2017-07-04 01:17:49 +08:00
if (!this._authService.isAuthenticated() &&
(!this.shareService.isSharing() ||
2017-07-09 18:03:17 +08:00
(this.shareService.isSharing() && Config.Client.Sharing.passwordProtected == true))) {
return this._navigation.toLogin();
}
2017-07-09 18:03:17 +08:00
this.showSearchBar = Config.Client.Search.searchEnabled && this._authService.isAuthorized(UserRoles.Guest);
this.showShare = Config.Client.Sharing.enabled && this._authService.isAuthorized(UserRoles.User);
2017-07-04 01:17:49 +08:00
this.subscription.content = this._galleryService.content.subscribe(this.onContentChange);
this.subscription.route = this._route.params.subscribe(this.onRoute);
2017-06-21 17:46:23 +08:00
2017-07-09 18:03:17 +08:00
if (this.shareService.isSharing()) {
this.$counter = Observable.interval(1000);
this.subscription.timer = this.$counter.subscribe((x) => this.updateTimer(x));
}
2017-07-04 01:17:49 +08:00
}
2017-07-04 01:17:49 +08:00
ngOnDestroy() {
if (this.subscription.content !== null) {
this.subscription.content.unsubscribe();
}
if (this.subscription.route !== null) {
this.subscription.route.unsubscribe();
}
}
2016-05-16 17:03:11 +08:00
2017-07-04 01:17:49 +08:00
private onContentChange = (content) => {
const dirSorter = (a: DirectoryDTO, b: DirectoryDTO) => {
return a.name.localeCompare(b.name);
};
2016-05-16 17:03:11 +08:00
2017-07-04 01:17:49 +08:00
const tmp = <DirectoryDTO | SearchResultDTO>(content.searchResult || content.directory || {
directories: [],
photos: []
});
this.directories = tmp.directories.sort(dirSorter);
this.isPhotoWithLocation = false;
for (let i = 0; i < tmp.photos.length; i++) {
if (tmp.photos[i].metadata &&
tmp.photos[i].metadata.positionData &&
tmp.photos[i].metadata.positionData.GPSData &&
tmp.photos[i].metadata.positionData.GPSData.longitude
) {
this.isPhotoWithLocation = true;
break;
}
}
};
private onRoute = async (params: Params) => {
console.log("onRoute", params);
const searchText = params['searchText'];
if (searchText && searchText != "") {
console.log("searching");
let typeString = params['type'];
if (typeString && typeString != "") {
console.log("with type");
let type: SearchTypes = <any>SearchTypes[typeString];
this._galleryService.search(searchText, type);
return;
}
2016-12-27 06:36:38 +08:00
2017-07-04 01:17:49 +08:00
this._galleryService.search(searchText);
return;
}
2016-05-16 17:03:11 +08:00
2017-07-04 01:17:49 +08:00
if (params['sharingKey'] && params['sharingKey'] != "") {
2017-07-09 18:03:17 +08:00
const sharing = await this.shareService.getSharing();
2017-07-04 01:17:49 +08:00
this._router.navigate(['/gallery', sharing.path], {queryParams: {sk: this.shareService.getSharingKey()}});
return;
}
2017-07-04 01:17:49 +08:00
let directoryName = params['directory'];
directoryName = directoryName || "";
2016-05-16 17:03:11 +08:00
2017-07-04 01:17:49 +08:00
this._galleryService.getDirectory(directoryName);
2016-05-16 17:03:11 +08:00
2017-07-04 01:17:49 +08:00
};
2017-06-22 03:32:57 +08:00
onLightboxLastElement() {
this.grid.renderARow();
}
2016-03-13 18:28:29 +08:00
}