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

116 lines
4.1 KiB
TypeScript

import { Injectable, OnInit, PLATFORM_ID, Inject,InjectionToken , Optional, Injector } from '@angular/core';
import { isPlatformBrowser} from '@angular/common';
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';
import { DOCUMENT } from '@angular/platform-browser';
@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: Http, @Inject(PLATFORM_ID) private platformId: string, private injector: Injector) {
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(location)
.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(location)
.map((data) => {
var properties:EnvProperties=data.json();
properties.domain = domain;
this.envSpecific = properties;
return properties;
})
}
// console.log('subscribeEnvironment: already loaded ');
return Observable.of(this.envSpecific);
}
getFullUrl(){
var location ="";
var domain = "";
if (typeof document == 'undefined') {
let req = this.injector.get('request');
domain = req.get('host').split(":")[0];
location = (domain.indexOf(".openaire.eu")!=-1?"https://":"http://")+ req.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');
domain = req.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);
});
}
}