Project

General

Profile

« Previous | Next » 

Revision 55964

[Library|Trunk]

Merge into trunk branch angular7 rev 55729

View differences:

environment-specific.service.ts
1
import { Injectable, OnInit, PLATFORM_ID, Inject,InjectionToken , Optional, Injector } from '@angular/core';
1
import {Injectable, OnInit, PLATFORM_ID, Inject, InjectionToken, Optional, Injector, inject} from '@angular/core';
2 2
import { isPlatformBrowser} from '@angular/common';
3 3
import { Http, Response, Headers, RequestOptions } from '@angular/http';
4
import { Observable, Subscription, BehaviorSubject } from 'rxjs/Rx';
5
import 'rxjs/add/operator/map';
6
import 'rxjs/add/operator/toPromise';
4
import {HttpClient} from "@angular/common/http";
5
import { Observable, Subscription, BehaviorSubject ,  of } from 'rxjs';
7 6

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

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

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

  
11 17
@Injectable()
12 18
export class EnvironmentSpecificService {
13 19

  
......
18 24
  private envSpecificSubject: BehaviorSubject<EnvProperties> = new BehaviorSubject<EnvProperties>(null);
19 25
  private propertiesUrl = "/assets/env-properties.json";
20 26

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

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

  
......
31 39
        var domain  = this.getDomain();
32 40
        var location = this.getFullUrl();
33 41
        // console.log('loadEnvironment: Loading  '+ location);
34
        return this.http.get(location)
35
            .map((data) => {
36
                                    var properties:EnvProperties=data.json();
42
        return this.http.get<EnvProperties>(location)
43
            .pipe(map((data) => {
44
                                    var properties:EnvProperties=data;//.json();
37 45
                                    properties.domain = domain;
38 46
                                    this.envSpecific = properties;
39 47
                                    return properties;
40
                      })
48
                      }))
41 49
            .toPromise<EnvProperties>();
42 50
      }
43 51
      // console.log('loadEnvironment: already loaded ');
......
50 58
        var domain  = this.getDomain();
51 59
        var location = this.getFullUrl();
52 60
        // console.log('subscribeEnvironment: Loading  '+ location);
53
        return this.http.get(location)
54
        .map((data) => {
55
                                var properties:EnvProperties=data.json();
61
        return this.http.get<EnvProperties>(location)
62
        .pipe(map((data) => {
63
                                var properties:EnvProperties=data;//.json();
56 64
                                properties.domain = domain;
57 65
                                this.envSpecific = properties;
58 66
                                return properties;
59
                  })
67
                  }))
60 68

  
61 69
      }
62 70
      // console.log('subscribeEnvironment: already loaded  ');
63
      return Observable.of(this.envSpecific);
71
      return of(this.envSpecific);
64 72
  }
65 73
  getFullUrl(){
66 74
    var location ="";
67 75
      var domain  = "";
68 76
    if (typeof document == 'undefined') {
69
         let req = this.injector.get('request');
70
         domain = req.get('host').split(":")[0];
71
         location = (domain.indexOf(".openaire.eu")!=-1?"https://":"http://")+ req.get('host') + this.propertiesUrl;
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;
72 81
     }else{
73
       location = document.location.protocol +"//" + document.location.host+this.propertiesUrl;
74
       domain = document.location.hostname;
82
      location = document.location.protocol +"//" + document.location.host+this.propertiesUrl;
83
      domain = document.location.hostname;
75 84
   }
76 85
   return location;
77 86
  }
78 87
  getDomain(){
79 88
    var domain  = "";
80 89
    if (typeof document == 'undefined') {
81
         let req = this.injector.get('request');
82
         domain = req.get('host').split(":")[0];
83
     }else{
84
       domain = document.location.hostname;
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;
85 95
   }
86 96
   return domain;
87 97
  }

Also available in: Unified diff