2018-06-12 12:40:51 +02:00
|
|
|
import { Injectable, OnInit, PLATFORM_ID, Inject,InjectionToken , Optional } 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';
|
|
|
|
import { Observable, Subscription, BehaviorSubject } from 'rxjs/Rx';
|
|
|
|
import 'rxjs/add/operator/map';
|
|
|
|
import 'rxjs/add/operator/toPromise';
|
|
|
|
|
|
|
|
import { EnvProperties } from './env-properties';
|
2018-03-20 14:46:05 +01:00
|
|
|
import { DOCUMENT } from '@angular/platform-browser';
|
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;
|
|
|
|
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";
|
|
|
|
|
|
|
|
|
2018-06-12 12:40:51 +02:00
|
|
|
private serverUrl = "http://scoobydoo.di.uoa.gr:4000";
|
|
|
|
// private serverUrl = "https://beta.explore.openaire.eu";
|
|
|
|
// private serverUrl = "https://beta.connect.openaire.eu";
|
|
|
|
constructor(private http: Http, @Inject(PLATFORM_ID) private platformId: string) {
|
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
|
|
|
}
|
|
|
|
public loadEnvironment() {
|
|
|
|
// Only want to do this once - if root page is revisited, it calls this again.
|
|
|
|
if (this.envSpecific === null || this.envSpecific === undefined) {
|
2018-06-12 12:40:51 +02:00
|
|
|
var location = this.propertiesUrl;
|
|
|
|
if(typeof document == 'undefined'){
|
|
|
|
location = this.serverUrl + this.propertiesUrl;
|
2018-02-05 14:14:59 +01:00
|
|
|
|
2018-06-12 12:40:51 +02:00
|
|
|
}else{
|
|
|
|
location = document.location.protocol +"//" + document.location.host+this.propertiesUrl;
|
|
|
|
}
|
|
|
|
console.log('Loading '+ location);
|
|
|
|
return this.http.get(location)
|
2018-02-05 14:14:59 +01:00
|
|
|
.map((data) => data.json())
|
|
|
|
.toPromise<EnvProperties>();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve(this.envSpecificNull);
|
|
|
|
}
|
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) {
|
|
|
|
console.log('Loading env-properties.json');
|
|
|
|
|
|
|
|
return this.http.get(this.propertiesUrl)
|
|
|
|
.map((data) => data.json());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return Observable.of(this.envSpecific);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|