openaire-library/utils/properties/environment-specific.servic...

126 lines
4.5 KiB
TypeScript

import {Injectable, OnInit, PLATFORM_ID, Inject, InjectionToken, Optional, Injector, inject} from '@angular/core';
import { isPlatformBrowser} from '@angular/common';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import {HttpClient} from "@angular/common/http";
import { Observable, Subscription, BehaviorSubject , of } from 'rxjs';
import {Request} from 'express';
import {REQUEST} from '@nguniversal/express-engine/tokens';
import { EnvProperties } from './env-properties';
import { DOCUMENT } from '@angular/common';
import {map} from "rxjs/operators";
import {Type} from "@angular/compiler";
//export const REQUEST_TOKEN = new InjectionToken<Request>('request');
@Injectable()
export class EnvironmentSpecificService {
public envSpecific: EnvProperties;
public domain: string;
public envSpecificNull: EnvProperties = null;
testBrowser: boolean;
private envSpecificSubject: BehaviorSubject<EnvProperties> = new BehaviorSubject<EnvProperties>(null);
private propertiesUrl = "/assets/env-properties.json";
constructor(private http: HttpClient, @Inject(PLATFORM_ID) private platformId: string, /*private injector: Injector,*/
@Optional() @Inject(REQUEST) private request: Request) {
this.testBrowser = isPlatformBrowser(platformId);
if (this.testBrowser) {
//this is only executed on the browser
}
}
public loadEnvironment() {
// Only want to do this once - if root page is revisited, it calls this again.
if (this.envSpecific === null || this.envSpecific === undefined) {
var domain = this.getDomain();
var location = this.getFullUrl();
// console.log('loadEnvironment: Loading '+ location);
return this.http.get<EnvProperties>(location)
.pipe(map((data) => {
var properties:EnvProperties=data;//.json();
properties.domain = domain;
this.envSpecific = properties;
return properties;
}))
.toPromise<EnvProperties>();
}
// console.log('loadEnvironment: already loaded ');
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) {
var domain = this.getDomain();
var location = this.getFullUrl();
// console.log('subscribeEnvironment: Loading '+ location);
return this.http.get<EnvProperties>(location)
.pipe(map((data) => {
var properties:EnvProperties=data;//.json();
properties.domain = domain;
this.envSpecific = properties;
return properties;
}))
}
// console.log('subscribeEnvironment: already loaded ');
return of(this.envSpecific);
}
getFullUrl(){
var location ="";
var domain = "";
if (typeof document == 'undefined') {
//let req = this.injector.get('request');
//let req: Request = this.injector.get(REQUEST_TOKEN);
domain = this.request.get('host').split(":")[0];
location = (domain.indexOf(".openaire.eu")!=-1?"https://":"http://")+ this.request.get('host') + this.propertiesUrl;
}else{
location = document.location.protocol +"//" + document.location.host+this.propertiesUrl;
domain = document.location.hostname;
}
return location;
}
getDomain(){
var domain = "";
if (typeof document == 'undefined') {
//let req = this.injector.get('request');
//let req: Request = this.injector.get(REQUEST_TOKEN);
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;
//console.log(this.envSpecific);
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);
});
}
}