2019-06-03 15:20:36 +02:00
|
|
|
import {Injectable, OnInit, PLATFORM_ID, Inject, InjectionToken, Optional, Injector, inject} from '@angular/core';
|
2018-02-15 11:36:12 +01:00
|
|
|
import { isPlatformBrowser} from '@angular/common';
|
2018-02-05 14:14:59 +01:00
|
|
|
import { Http, Response, Headers, RequestOptions } from '@angular/http';
|
2019-06-03 15:20:36 +02:00
|
|
|
import {HttpClient} from "@angular/common/http";
|
|
|
|
import { Observable, Subscription, BehaviorSubject , of } from 'rxjs';
|
|
|
|
|
|
|
|
import {Request} from 'express';
|
|
|
|
import {REQUEST} from '@nguniversal/express-engine/tokens';
|
2018-02-05 14:14:59 +01:00
|
|
|
|
|
|
|
import { EnvProperties } from './env-properties';
|
2019-06-03 15:20:36 +02:00
|
|
|
import { DOCUMENT } from '@angular/common';
|
|
|
|
import {map} from "rxjs/operators";
|
|
|
|
import {Type} from "@angular/compiler";
|
|
|
|
|
|
|
|
//export const REQUEST_TOKEN = new InjectionToken<Request>('request');
|
2018-02-05 14:14:59 +01:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class EnvironmentSpecificService {
|
2018-06-12 12:40:51 +02:00
|
|
|
|
2018-02-05 14:14:59 +01:00
|
|
|
public envSpecific: EnvProperties;
|
2019-03-01 12:29:52 +01:00
|
|
|
public domain: string;
|
2018-02-05 14:14:59 +01:00
|
|
|
public envSpecificNull: EnvProperties = null;
|
2018-02-15 11:36:12 +01:00
|
|
|
testBrowser: boolean;
|
2018-02-05 14:14:59 +01:00
|
|
|
private envSpecificSubject: BehaviorSubject<EnvProperties> = new BehaviorSubject<EnvProperties>(null);
|
2018-03-20 14:46:05 +01:00
|
|
|
private propertiesUrl = "/assets/env-properties.json";
|
|
|
|
|
2019-06-03 15:20:36 +02:00
|
|
|
constructor(private http: HttpClient, @Inject(PLATFORM_ID) private platformId: string, /*private injector: Injector,*/
|
|
|
|
@Optional() @Inject(REQUEST) private request: Request) {
|
2018-02-15 11:36:12 +01:00
|
|
|
this.testBrowser = isPlatformBrowser(platformId);
|
|
|
|
if (this.testBrowser) {
|
|
|
|
//this is only executed on the browser
|
|
|
|
}
|
2018-02-05 14:14:59 +01:00
|
|
|
}
|
2019-06-03 15:20:36 +02:00
|
|
|
|
2018-02-05 14:14:59 +01:00
|
|
|
public loadEnvironment() {
|
|
|
|
// Only want to do this once - if root page is revisited, it calls this again.
|
|
|
|
|
2019-03-01 12:29:52 +01:00
|
|
|
if (this.envSpecific === null || this.envSpecific === undefined) {
|
|
|
|
var domain = this.getDomain();
|
|
|
|
var location = this.getFullUrl();
|
|
|
|
// console.log('loadEnvironment: Loading '+ location);
|
2019-06-03 15:20:36 +02:00
|
|
|
return this.http.get<EnvProperties>(location)
|
|
|
|
.pipe(map((data) => {
|
|
|
|
var properties:EnvProperties=data;//.json();
|
2019-03-01 12:29:52 +01:00
|
|
|
properties.domain = domain;
|
|
|
|
this.envSpecific = properties;
|
|
|
|
return properties;
|
2019-06-03 15:20:36 +02:00
|
|
|
}))
|
2018-02-05 14:14:59 +01:00
|
|
|
.toPromise<EnvProperties>();
|
|
|
|
}
|
2019-03-01 12:29:52 +01:00
|
|
|
// console.log('loadEnvironment: already loaded ');
|
|
|
|
return Promise.resolve(this.envSpecific);
|
2018-02-05 14:14:59 +01:00
|
|
|
}
|
2019-03-01 12:29:52 +01:00
|
|
|
|
2018-03-15 10:53:07 +01:00
|
|
|
public subscribeEnvironment() {
|
|
|
|
// Only want to do this once - if root page is revisited, it calls this again.
|
|
|
|
if (this.envSpecific === null || this.envSpecific === undefined) {
|
2019-03-01 12:29:52 +01:00
|
|
|
var domain = this.getDomain();
|
|
|
|
var location = this.getFullUrl();
|
|
|
|
// console.log('subscribeEnvironment: Loading '+ location);
|
2019-06-03 15:20:36 +02:00
|
|
|
return this.http.get<EnvProperties>(location)
|
|
|
|
.pipe(map((data) => {
|
|
|
|
var properties:EnvProperties=data;//.json();
|
2019-03-01 12:29:52 +01:00
|
|
|
properties.domain = domain;
|
|
|
|
this.envSpecific = properties;
|
|
|
|
return properties;
|
2019-06-03 15:20:36 +02:00
|
|
|
}))
|
2018-03-15 10:53:07 +01:00
|
|
|
|
|
|
|
}
|
2019-03-01 12:29:52 +01:00
|
|
|
// console.log('subscribeEnvironment: already loaded ');
|
2019-06-03 15:20:36 +02:00
|
|
|
return of(this.envSpecific);
|
2018-03-15 10:53:07 +01:00
|
|
|
}
|
2019-03-01 12:29:52 +01:00
|
|
|
getFullUrl(){
|
|
|
|
var location ="";
|
|
|
|
var domain = "";
|
|
|
|
if (typeof document == 'undefined') {
|
2019-06-03 15:20:36 +02:00
|
|
|
//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;
|
2019-03-01 12:29:52 +01:00
|
|
|
}else{
|
2019-06-03 15:20:36 +02:00
|
|
|
location = document.location.protocol +"//" + document.location.host+this.propertiesUrl;
|
|
|
|
domain = document.location.hostname;
|
2019-03-01 12:29:52 +01:00
|
|
|
}
|
|
|
|
return location;
|
|
|
|
}
|
|
|
|
getDomain(){
|
|
|
|
var domain = "";
|
|
|
|
if (typeof document == 'undefined') {
|
2019-06-03 15:20:36 +02:00
|
|
|
//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;
|
2019-03-01 12:29:52 +01:00
|
|
|
}
|
|
|
|
return domain;
|
|
|
|
}
|
2018-02-05 14:14:59 +01:00
|
|
|
|
|
|
|
public setEnvProperties(es: EnvProperties) {
|
|
|
|
// This has already been set so bail out.
|
|
|
|
if (es === null || es === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.envSpecific = es;
|
2019-02-14 11:15:44 +01:00
|
|
|
//console.log(this.envSpecific);
|
2018-02-05 14:14:59 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|