2020-11-11 15:43:13 +01:00
|
|
|
import {Injectable, OnDestroy} from '@angular/core';
|
2019-06-03 15:20:36 +02:00
|
|
|
import {HttpClient} from "@angular/common/http";
|
2020-11-25 18:12:31 +01:00
|
|
|
import {BehaviorSubject, from, Observable, of, Subscription} from 'rxjs';
|
2020-11-11 15:43:13 +01:00
|
|
|
import {map} from 'rxjs/operators';
|
2019-06-05 15:33:18 +02:00
|
|
|
import {EnvProperties} from "../properties/env-properties";
|
2020-09-24 13:18:24 +02:00
|
|
|
import {Portal} from "../entities/adminTool/portal";
|
2020-07-13 00:19:30 +02:00
|
|
|
import {Page} from "../entities/adminTool/page";
|
2022-07-27 11:34:10 +02:00
|
|
|
import {properties} from "../../../../environments/environment";
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2022-07-27 11:34:10 +02:00
|
|
|
@Injectable({providedIn: 'root'})
|
|
|
|
export class ConfigurationService {
|
2020-09-24 13:18:24 +02:00
|
|
|
private communityInformation: BehaviorSubject<Portal> = new BehaviorSubject(null);
|
2020-07-13 00:19:30 +02:00
|
|
|
private sub: Subscription = null;
|
2022-09-23 11:24:15 +02:00
|
|
|
private promise: Promise<void> = null;
|
2022-07-27 11:34:10 +02:00
|
|
|
|
|
|
|
constructor(private http: HttpClient) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.clearSubscriptions();
|
|
|
|
}
|
|
|
|
|
2020-11-11 15:43:13 +01:00
|
|
|
clearSubscriptions() {
|
2022-07-27 11:34:10 +02:00
|
|
|
if (this.sub) {
|
|
|
|
this.sub.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-13 00:19:30 +02:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2022-07-27 11:34:10 +02:00
|
|
|
getCommunityInformation(properties: EnvProperties, community: string) {
|
|
|
|
let url = properties.adminToolsAPIURL + "/" + properties.adminToolsPortalType + "/" + community + '/full';
|
|
|
|
return this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
|
|
|
|
//.map(res => res.json());
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:12:31 +01:00
|
|
|
public initStaticCommunityInformation(communityInformation: Portal) {
|
2022-09-23 11:24:15 +02:00
|
|
|
this.promise = new Promise<void>((resolve => {
|
2022-07-27 11:34:10 +02:00
|
|
|
this.communityInformation.next(communityInformation);
|
|
|
|
resolve();
|
2020-11-25 18:12:31 +01:00
|
|
|
}));
|
|
|
|
}
|
2022-07-27 11:34:10 +02:00
|
|
|
|
|
|
|
public initCommunityInformation(properties: EnvProperties, community: string) {
|
|
|
|
if (community == null) return;
|
|
|
|
let url = properties.adminToolsAPIURL + "/" + properties.adminToolsPortalType + "/" + community + "/full";
|
2022-09-23 11:24:15 +02:00
|
|
|
this.promise = new Promise<void>((resolve => {
|
2022-07-27 11:34:10 +02:00
|
|
|
this.sub = this.http.get<Portal>((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url).subscribe(
|
|
|
|
(communityInformation: Portal) => {
|
|
|
|
this.communityInformation.next(communityInformation);
|
|
|
|
resolve();
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
this.communityInformation.error(error);
|
|
|
|
resolve();
|
|
|
|
});
|
2020-07-13 00:19:30 +02:00
|
|
|
}));
|
2022-07-27 11:34:10 +02:00
|
|
|
|
2020-07-13 00:19:30 +02:00
|
|
|
}
|
2022-07-27 11:34:10 +02:00
|
|
|
|
2020-09-24 13:18:24 +02:00
|
|
|
public get communityInformationState(): Observable<Portal> {
|
2020-07-13 00:19:30 +02:00
|
|
|
return this.communityInformation.asObservable();
|
|
|
|
}
|
2022-07-27 11:34:10 +02:00
|
|
|
|
2020-04-07 13:15:28 +02:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2022-07-27 11:34:10 +02:00
|
|
|
isEntityEnabled(APIUrl: string, community: string, entity: string) {
|
|
|
|
//console.log("isEntityEnabled: "+entity);
|
|
|
|
let url = "isEntityEnabled-" + entity;
|
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
// if(entity == "publication" || entity == "dataset" || entity == "datasource"){
|
|
|
|
// return Observable.of(new Object()).mapTo(false);
|
|
|
|
// }
|
|
|
|
// return Observable.of(new Object()).mapTo(true);
|
2022-07-27 11:34:10 +02:00
|
|
|
return this.http.get(APIUrl + "/page")
|
|
|
|
.pipe(map(res => true));
|
2020-07-13 00:19:30 +02:00
|
|
|
}
|
2022-07-27 11:34:10 +02:00
|
|
|
|
|
|
|
isPageEnabled(portal: string, route: string, portalType = properties.adminToolsPortalType) {
|
|
|
|
let url = properties.adminToolsAPIURL + "/" + portalType + "/" + portal + "/pages?page_route=" + route;
|
|
|
|
return this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
|
|
|
|
.pipe(map(res => {
|
|
|
|
let result = false;
|
|
|
|
if (res['length'] > 0 && res[0].route == route) {
|
|
|
|
result = res[0].isEnabled;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}));
|
2020-07-13 00:19:30 +02:00
|
|
|
}
|
2022-07-27 11:34:10 +02:00
|
|
|
|
|
|
|
async isPageEnabledByStateAsync(properties: EnvProperties, portal: string, page_route: string) {
|
2022-08-03 17:21:14 +02:00
|
|
|
if (!this.promise || (this.communityInformation && this.communityInformation.getValue() && portal !== this.communityInformation.getValue().pid)) {
|
2022-07-27 11:34:10 +02:00
|
|
|
this.initCommunityInformation(properties, portal);
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.promise;
|
|
|
|
if (this.sub) {
|
|
|
|
this.sub.unsubscribe();
|
|
|
|
}
|
|
|
|
return this.filtering(page_route);
|
|
|
|
}
|
|
|
|
|
|
|
|
isPageEnabledByState(properties: EnvProperties, portal: string, router: string): Observable<boolean> {
|
|
|
|
let page_route: string = router.split('?')[0].substring(1);
|
|
|
|
return from(this.isPageEnabledByStateAsync(properties, portal, page_route));
|
|
|
|
}
|
|
|
|
|
2020-07-13 00:19:30 +02:00
|
|
|
filtering(page_route: string) {
|
2022-07-27 11:34:10 +02:00
|
|
|
let community: Portal = this.communityInformation.getValue();
|
|
|
|
let pages: Page[] = <Page[]>community.pages;
|
|
|
|
if (pages) {
|
|
|
|
let page = pages.find((page: Page) => page.route == page_route);
|
|
|
|
return page && page.isEnabled;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-13 00:19:30 +02:00
|
|
|
}
|
2022-07-27 11:34:10 +02:00
|
|
|
|
2020-04-07 13:15:28 +02:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2022-07-27 11:34:10 +02:00
|
|
|
getMainPageContent(APIUrl: string, community: string,) {
|
|
|
|
return this.http.get(APIUrl + "/page")
|
|
|
|
.pipe(map(res => true));
|
|
|
|
}
|
|
|
|
|
2020-04-07 13:15:28 +02:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2022-07-27 11:34:10 +02:00
|
|
|
getSpecialAnouncementContent(APIUrl: string, community: string,) {
|
|
|
|
return this.http.get(APIUrl + "/page")
|
|
|
|
.pipe(map(res => ""));
|
|
|
|
}
|
|
|
|
|
2020-04-07 13:15:28 +02:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2022-07-27 11:34:10 +02:00
|
|
|
getHelpPageContent(APIUrl: string, community: string, router: string) {
|
|
|
|
return this.http.get(APIUrl + "/page")
|
|
|
|
.pipe(map(res => true));
|
|
|
|
}
|
|
|
|
|
|
|
|
// private handleError (error: Response) {
|
|
|
|
// // in a real world app, we may send the error to some remote logging infrastructure
|
|
|
|
// // instead of just logging it to the console
|
|
|
|
// console.log(error);
|
|
|
|
// return this.http.get(this.APIUrl + "/page")
|
|
|
|
// .map(res => true);
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|