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";
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2020-07-13 00:19:30 +02:00
|
|
|
@Injectable({ providedIn: 'root' })
|
2020-11-11 15:43:13 +01:00
|
|
|
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;
|
2020-09-24 13:18:24 +02:00
|
|
|
private source: Observable<Portal> = null;
|
2020-07-13 00:19:30 +02:00
|
|
|
private promise: Promise<boolean> = null;
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2019-06-03 15:20:36 +02:00
|
|
|
constructor(private http: HttpClient ) {}
|
2020-07-13 00:19:30 +02:00
|
|
|
|
|
|
|
ngOnDestroy() {
|
2020-11-11 15:43:13 +01:00
|
|
|
this.clearSubscriptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
clearSubscriptions() {
|
2020-07-13 00:19:30 +02:00
|
|
|
if(this.sub) {
|
|
|
|
this.sub.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2019-06-05 15:33:18 +02:00
|
|
|
getCommunityInformation(properties:EnvProperties, community:string){
|
2020-09-24 13:18:24 +02:00
|
|
|
let url = properties.adminToolsAPIURL +"/"+properties.adminToolsPortalType+"/" + community + '/full';
|
2020-07-13 00:19:30 +02:00
|
|
|
return this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
|
2019-06-03 15:20:36 +02:00
|
|
|
//.map(res => res.json());
|
2018-02-05 14:14:59 +01:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:12:31 +01:00
|
|
|
public initStaticCommunityInformation(communityInformation: Portal) {
|
|
|
|
this.promise = new Promise<any>((resolve => {
|
|
|
|
this.communityInformation.next(communityInformation);
|
|
|
|
resolve();
|
|
|
|
}));
|
|
|
|
}
|
2020-07-13 00:19:30 +02:00
|
|
|
public initCommunityInformation(properties:EnvProperties, community:string) {
|
2020-07-15 16:14:40 +02:00
|
|
|
if(community == null) return;
|
|
|
|
|
2020-09-24 13:18:24 +02:00
|
|
|
let url = properties.adminToolsAPIURL +"/" + properties.adminToolsPortalType + "/" + community + "/full";
|
2020-07-13 00:19:30 +02:00
|
|
|
this.promise = new Promise<any>((resolve => {
|
2020-09-24 13:18:24 +02:00
|
|
|
this.source = this.http.get<Portal>((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
|
2020-07-13 00:19:30 +02:00
|
|
|
|
|
|
|
this.sub = this.source
|
|
|
|
.subscribe(
|
2020-09-24 13:18:24 +02:00
|
|
|
(communityInformation: Portal) => {
|
2020-07-13 00:19:30 +02:00
|
|
|
this.communityInformation.next(communityInformation);
|
|
|
|
resolve();
|
2020-07-13 12:13:42 +02:00
|
|
|
},
|
|
|
|
error => {
|
2020-08-11 14:16:50 +02:00
|
|
|
this.communityInformation.error(error);
|
2020-07-13 12:13:42 +02:00
|
|
|
resolve();
|
2020-07-13 00:19:30 +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();
|
|
|
|
}
|
|
|
|
|
2020-04-07 13:15:28 +02:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2018-02-05 14:14:59 +01:00
|
|
|
isEntityEnabled(APIUrl:string, community:string,entity: string){
|
2019-02-14 11:15:44 +01:00
|
|
|
//console.log("isEntityEnabled: "+entity);
|
2017-12-19 13:53:46 +01:00
|
|
|
let url = "isEntityEnabled-"+entity;
|
|
|
|
|
|
|
|
// if(entity == "publication" || entity == "dataset" || entity == "datasource"){
|
|
|
|
// return Observable.of(new Object()).mapTo(false);
|
|
|
|
// }
|
|
|
|
// return Observable.of(new Object()).mapTo(true);
|
2018-02-05 14:14:59 +01:00
|
|
|
return this.http.get(APIUrl + "/page")
|
2019-06-03 15:20:36 +02:00
|
|
|
.pipe(map(res => true));
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
|
2020-07-13 00:19:30 +02:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2019-06-05 15:33:18 +02:00
|
|
|
isPageEnabled(properties:EnvProperties, community:string,router: string){
|
2020-04-07 13:15:28 +02:00
|
|
|
let page_route: string = router.split('?')[0].substring(1);
|
2020-07-13 00:19:30 +02:00
|
|
|
|
2020-09-24 13:18:24 +02:00
|
|
|
let url = properties.adminToolsAPIURL + "/"+properties.adminToolsPortalType+"/" + community+"/pages?page_route="+page_route;
|
2020-07-13 00:19:30 +02:00
|
|
|
return this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
|
2019-06-03 15:20:36 +02:00
|
|
|
//.map(res => res.json())
|
|
|
|
.pipe(map(res => {
|
2019-02-26 14:57:04 +01:00
|
|
|
let result = false;
|
2020-04-07 13:15:28 +02:00
|
|
|
if(res['length'] >0 && res[0].route == page_route){
|
2018-02-05 14:14:59 +01:00
|
|
|
result = res[0].isEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2020-04-07 13:15:28 +02:00
|
|
|
}));//.do(res => {console.log("Route "+page_route +" is "+res)});
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2018-02-05 14:14:59 +01:00
|
|
|
|
2020-07-13 00:19:30 +02:00
|
|
|
async isPageEnabledByStateAsync(properties: EnvProperties, community:string, page_route: string) {
|
|
|
|
if(!this.promise) {
|
|
|
|
this.initCommunityInformation(properties, community);
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.promise;
|
2020-11-11 15:43:13 +01:00
|
|
|
if(this.sub){
|
|
|
|
this.sub.unsubscribe();
|
|
|
|
}
|
2020-07-13 00:19:30 +02:00
|
|
|
return this.filtering(page_route);
|
|
|
|
}
|
|
|
|
|
|
|
|
isPageEnabledByState(properties: EnvProperties, community:string, router: string): Observable<boolean> {
|
|
|
|
let page_route: string = router.split('?')[0].substring(1);
|
|
|
|
return from(this.isPageEnabledByStateAsync(properties, community, page_route));
|
|
|
|
}
|
|
|
|
|
|
|
|
filtering(page_route: string) {
|
2020-09-24 13:18:24 +02:00
|
|
|
let community: Portal = this.communityInformation.getValue();
|
2020-07-13 00:19:30 +02:00
|
|
|
let pages: Page[] = <Page[]>community.pages;
|
|
|
|
if (pages) {
|
2021-04-23 18:25:52 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 13:15:28 +02:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
getMainPageContent(APIUrl:string, community:string,){
|
2018-02-05 14:14:59 +01:00
|
|
|
return this.http.get(APIUrl + "/page")
|
2019-06-03 15:20:36 +02:00
|
|
|
.pipe(map(res => true));
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-04-07 13:15:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
getSpecialAnouncementContent(APIUrl:string, community:string,){
|
2018-02-05 14:14:59 +01:00
|
|
|
return this.http.get(APIUrl + "/page")
|
2019-06-03 15:20:36 +02:00
|
|
|
.pipe(map(res => ""));
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-04-07 13:15:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
getHelpPageContent(APIUrl:string, community:string, router:string){
|
2018-02-05 14:14:59 +01:00
|
|
|
return this.http.get(APIUrl + "/page")
|
2019-06-03 15:20:36 +02:00
|
|
|
.pipe(map(res => true));
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2018-02-05 14:14:59 +01:00
|
|
|
// 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
|
|
|
|
|
|
|
|
|
|
|
}
|