77 lines
2.3 KiB
TypeScript
77 lines
2.3 KiB
TypeScript
import {Inject, Injectable, Optional} from '@angular/core';
|
|
import {HttpClient} from "@angular/common/http";
|
|
import {BehaviorSubject, of} from 'rxjs';
|
|
|
|
import {Request} from 'express';
|
|
import {REQUEST} from '@nguniversal/express-engine/tokens';
|
|
|
|
import {EnvProperties} from './env-properties';
|
|
import {properties} from "../../../../environments/environment";
|
|
import {ConnectHelper} from "../../connect/connectHelper";
|
|
|
|
@Injectable()
|
|
export class EnvironmentSpecificService {
|
|
|
|
public envSpecific: EnvProperties;
|
|
public domain: string;
|
|
private envSpecificSubject: BehaviorSubject<EnvProperties> = new BehaviorSubject<EnvProperties>(null);
|
|
|
|
constructor(private http: HttpClient, @Optional() @Inject(REQUEST) private request: Request) {
|
|
if(properties.adminToolsCommunity == "connect") {
|
|
if(properties.environment == "development"){
|
|
properties.domain = "https://beta."+(ConnectHelper.getCommunityFromDomain(properties.domain)?ConnectHelper.getCommunityFromDomain(properties.domain):"connect")+".openaire.eu";
|
|
}else{
|
|
properties.domain = "https://" + this.getDomain();
|
|
}
|
|
}
|
|
}
|
|
|
|
public loadEnvironment() {
|
|
this.envSpecific = properties;
|
|
return Promise.resolve(this.envSpecific);
|
|
}
|
|
|
|
public subscribeEnvironment() {
|
|
// Only want to do this once - if root page is revisited, it calls this again.
|
|
if (this.envSpecific === null || this.envSpecific === undefined) {
|
|
this.envSpecific = properties;
|
|
}
|
|
// console.log('subscribeEnvironment: already loaded ');
|
|
return of(this.envSpecific);
|
|
}
|
|
|
|
getDomain() {
|
|
var domain = "";
|
|
if (typeof document == 'undefined') {
|
|
domain = this.request.get('host').split(":")[0];
|
|
} else {
|
|
domain = document.location.hostname;
|
|
}
|
|
return domain;
|
|
}
|
|
|
|
public setEnvProperties(es: EnvProperties) {
|
|
// This has already been set so bail out.
|
|
if (es === null || es === undefined) {
|
|
return;
|
|
}
|
|
this.envSpecific = es;
|
|
if (this.envSpecificSubject) {
|
|
this.envSpecificSubject.next(this.envSpecific);
|
|
}
|
|
}
|
|
|
|
/*
|
|
Call this if you want to know when EnvProperties is set.
|
|
*/
|
|
public subscribe(caller: any, callback: (caller: any, es: EnvProperties) => void) {
|
|
this.envSpecificSubject
|
|
.subscribe((es) => {
|
|
if (es === null) {
|
|
return;
|
|
}
|
|
callback(caller, es);
|
|
});
|
|
}
|
|
}
|