Project

General

Profile

1
import {Injectable, OnInit, PLATFORM_ID, Inject, InjectionToken, Optional, Injector, inject} from '@angular/core';
2
import { isPlatformBrowser} from '@angular/common';
3
import { Http, Response, Headers, RequestOptions } from '@angular/http';
4
import {HttpClient} from "@angular/common/http";
5
import { Observable, Subscription, BehaviorSubject ,  of } from 'rxjs';
6

    
7
import {Request} from 'express';
8
import {REQUEST} from '@nguniversal/express-engine/tokens';
9

    
10
import { EnvProperties } from './env-properties';
11
import { DOCUMENT } from '@angular/common';
12
import {map} from "rxjs/operators";
13
import {Type} from "@angular/compiler";
14

    
15
//export const REQUEST_TOKEN = new InjectionToken<Request>('request');
16

    
17
@Injectable()
18
export class EnvironmentSpecificService {
19

    
20
  public envSpecific: EnvProperties;
21
  public domain: string;
22
  public envSpecificNull: EnvProperties = null;
23
  testBrowser: boolean;
24
  private envSpecificSubject: BehaviorSubject<EnvProperties> = new BehaviorSubject<EnvProperties>(null);
25
  private propertiesUrl = "/assets/env-properties.json?v=1";
26

    
27
  constructor(private http: HttpClient,  @Inject(PLATFORM_ID) private platformId: string, /*private injector: Injector,*/
28
              @Optional() @Inject(REQUEST) private request: Request) {
29
      this.testBrowser = isPlatformBrowser(platformId);
30
      if (this.testBrowser) {
31
          //this is only executed on the browser
32
      }
33
  }
34

    
35
  public loadEnvironment() {
36
      // Only want to do this once - if root page is revisited, it calls this again.
37

    
38
      if (this.envSpecific === null || this.envSpecific === undefined) {
39
        var domain  = this.getDomain();
40
        var location = this.getFullUrl();
41
        // console.log('loadEnvironment: Loading  '+ location);
42
        return this.http.get<EnvProperties>(location)
43
            .pipe(map((data) => {
44
                                    var properties:EnvProperties=data;//.json();
45
                                    properties.domain = domain;
46
                                    this.envSpecific = properties;
47
                                    return properties;
48
                      }))
49
            .toPromise<EnvProperties>();
50
      }
51
      // console.log('loadEnvironment: already loaded ');
52
      return Promise.resolve(this.envSpecific);
53
  }
54

    
55
  public subscribeEnvironment() {
56
      // Only want to do this once - if root page is revisited, it calls this again.
57
      if (this.envSpecific === null || this.envSpecific === undefined) {
58
        var domain  = this.getDomain();
59
        var location = this.getFullUrl();
60
        // console.log('subscribeEnvironment: Loading  '+ location);
61
        return this.http.get<EnvProperties>(location)
62
        .pipe(map((data) => {
63
                                var properties:EnvProperties=data;//.json();
64
                                properties.domain = domain;
65
                                this.envSpecific = properties;
66
                                return properties;
67
                  }))
68

    
69
      }
70
      // console.log('subscribeEnvironment: already loaded  ');
71
      return of(this.envSpecific);
72
  }
73
  getFullUrl(){
74
    var location ="";
75
      var domain  = "";
76
    if (typeof document == 'undefined') {
77
      //let req = this.injector.get('request');
78
      //let req: Request = this.injector.get(REQUEST_TOKEN);
79
      domain = this.request.get('host').split(":")[0];
80
      location = (domain.indexOf(".openaire.eu")!=-1?"https://":"http://")+ this.request.get('host') + this.propertiesUrl;
81
     }else{
82
      location = document.location.protocol +"//" + document.location.host+this.propertiesUrl;
83
      domain = document.location.hostname;
84
   }
85
   return location;
86
  }
87
  getDomain(){
88
    var domain  = "";
89
    if (typeof document == 'undefined') {
90
      //let req = this.injector.get('request');
91
      //let req: Request = this.injector.get(REQUEST_TOKEN);
92
      domain = this.request.get('host').split(":")[0];
93
    }else{
94
      domain = document.location.hostname;
95
   }
96
   return domain;
97
  }
98

    
99
  public setEnvProperties(es: EnvProperties) {
100
    // This has already been set so bail out.
101
    if (es === null || es === undefined) {
102
        return;
103
    }
104

    
105
    this.envSpecific = es;
106
    //console.log(this.envSpecific);
107

    
108
    if (this.envSpecificSubject) {
109
        this.envSpecificSubject.next(this.envSpecific);
110
    }
111
  }
112

    
113
  /*
114
    Call this if you want to know when EnvProperties is set.
115
  */
116
  public subscribe(caller: any, callback: (caller: any, es: EnvProperties) => void) {
117
      this.envSpecificSubject
118
          .subscribe((es) => {
119
              if (es === null) {
120
                  return;
121
              }
122
              callback(caller, es);
123
          });
124
  }
125
}
(2-2/5)