Project

General

Profile

1
import {ChangeDetectorRef, Component, OnDestroy, OnInit} from '@angular/core';
2
import {ActivatedRoute, NavigationEnd, Router} from '@angular/router';
3

    
4
import {EnvProperties} from './openaireLibrary/utils/properties/env-properties';
5
import {EnvironmentSpecificService} from './openaireLibrary/utils/properties/environment-specific.service';
6
import {User} from './openaireLibrary/login/utils/helper.class';
7
import {UserManagementService} from "./openaireLibrary/services/user-management.service";
8
import {SideBarService} from "./library/sharedComponents/sidebar/sideBar.service";
9
import {StakeholderService} from "./services/stakeholder.service";
10
import {Subscriber} from "rxjs";
11
import {StakeholderCreator} from "./utils/entities/stakeholderCreator";
12

    
13
@Component({
14
  selector: 'app-root',
15
  templateUrl: './app.component.html'
16
})
17
export class AppComponent implements OnInit, OnDestroy{
18
  properties: EnvProperties;
19
  user: User;
20
  loginCheck: boolean = false;
21
  hasSidebar: boolean = false;
22
  public subscriptions: any[] = [];
23

    
24
  constructor(private  route: ActivatedRoute,
25
              private propertiesService: EnvironmentSpecificService,
26
              private router: Router,
27
              private userManagementService: UserManagementService,
28
              private sidebarService: SideBarService,
29
              private stakeholderService: StakeholderService,
30
              private cdr: ChangeDetectorRef) {
31
  }
32

    
33
  ngOnInit() {
34
    this.propertiesService.loadEnvironment()
35
      .then(properties => {
36
        this.properties = properties;
37
        this.router.events.forEach((event) => {
38
          if (event instanceof NavigationEnd) {
39
            let r = this.route;
40
            while (r.firstChild) {
41
              r = r.firstChild;
42
            }
43
            r.params.subscribe(params => {
44
              if(params['stakeholder']) {
45
                if(!this.stakeholderService.stakeholder ||
46
                  this.stakeholderService.stakeholder.alias !== params['stakeholder']) {
47
                  // this.stakeholderService.getStakeholder(this.properties.monitorServiceAPIURL, params['stakeholder']).subscribe(stakeholder => {
48
                    // stakeholder = Stakeholder.createECStakeholder();
49
                   let  stakeHolder = StakeholderCreator.createFunderFromDefaultProfile("ec","funder","ec__________::EC","European Commission","EC",false,"ec",true,true);
50
                  this.stakeholderService.setStakeholder(stakeHolder);
51
                    this.sidebarService.setOpen(true);
52
                  // });
53
                }
54
              } else {
55
                this.stakeholderService.setStakeholder(null);
56
              }
57
            });
58
          }
59
        });
60
        this.userManagementService.getUserInfo(this.properties.userInfoUrl).subscribe(user => {
61
          this.user = user;
62
          this.loginCheck = true;
63
        }, error => {
64
          console.log("App couldn't fetch properties");
65
          console.log(error);
66
        });
67
      });
68
    this.subscriptions.push(this.sidebarService.hasSidebar.subscribe(hasSidebar => {
69
      this.hasSidebar = hasSidebar;
70
      this.cdr.detectChanges();
71
    }));
72
  }
73

    
74
  public ngOnDestroy() {
75
    this.subscriptions.forEach( value => {
76
      if(value instanceof Subscriber) {
77
        value.unsubscribe();
78
      }
79
    });
80
  }
81

    
82
  public get open() {
83
    return this.sidebarService.open;
84
  }
85

    
86
  public toggleOpen(event = null) {
87
    if(!event) {
88
      this.sidebarService.setOpen(!this.open);
89
    } else if(event && event['value'] === true) {
90
      this.sidebarService.setOpen(false);
91
    }
92
  }
93
}
(3-3/5)