Project

General

Profile

1 57600 k.triantaf
import {ChangeDetectorRef, Component, OnDestroy, OnInit} from '@angular/core';
2 57707 k.triantaf
import {ActivatedRoute, NavigationEnd, RouteConfigLoadEnd, Router} from '@angular/router';
3 57449 argiro.kok
4
import {EnvProperties} from './openaireLibrary/utils/properties/env-properties';
5
import {EnvironmentSpecificService} from './openaireLibrary/utils/properties/environment-specific.service';
6 57496 k.triantaf
import {User} from './openaireLibrary/login/utils/helper.class';
7 57449 argiro.kok
import {UserManagementService} from "./openaireLibrary/services/user-management.service";
8 57496 k.triantaf
import {StakeholderService} from "./services/stakeholder.service";
9 57600 k.triantaf
import {Subscriber} from "rxjs";
10 57706 k.triantaf
import {LayoutService} from "./library/sharedComponents/sidebar/layout.service";
11 57449 argiro.kok
12 57673 k.triantaf
13 57449 argiro.kok
@Component({
14
  selector: 'app-root',
15 57496 k.triantaf
  templateUrl: './app.component.html'
16 57449 argiro.kok
})
17 57673 k.triantaf
export class AppComponent implements OnInit, OnDestroy {
18 57449 argiro.kok
  properties: EnvProperties;
19
  user: User;
20
  loginCheck: boolean = false;
21 57496 k.triantaf
  hasSidebar: boolean = false;
22 57706 k.triantaf
  hasHeader: boolean = false;
23 57697 k.triantaf
  private subscriptions: any[] = [];
24 57449 argiro.kok
25 57496 k.triantaf
  constructor(private  route: ActivatedRoute,
26
              private propertiesService: EnvironmentSpecificService,
27
              private router: Router,
28
              private userManagementService: UserManagementService,
29 57706 k.triantaf
              private layoutService: LayoutService,
30 57496 k.triantaf
              private stakeholderService: StakeholderService,
31
              private cdr: ChangeDetectorRef) {
32 57449 argiro.kok
  }
33
34
  ngOnInit() {
35 57514 k.triantaf
    this.propertiesService.loadEnvironment()
36
      .then(properties => {
37
        this.properties = properties;
38
        this.router.events.forEach((event) => {
39
          if (event instanceof NavigationEnd) {
40
            let r = this.route;
41
            while (r.firstChild) {
42
              r = r.firstChild;
43 57496 k.triantaf
            }
44 57514 k.triantaf
            r.params.subscribe(params => {
45 57673 k.triantaf
              if (params['stakeholder']) {
46
                if (!this.stakeholderService.stakeholder ||
47 57514 k.triantaf
                  this.stakeholderService.stakeholder.alias !== params['stakeholder']) {
48 57673 k.triantaf
                  this.stakeholderService.getStakeholder(this.properties.monitorServiceAPIURL, params['stakeholder']).subscribe(stakeholder => {
49
                    this.stakeholderService.setStakeholder(stakeholder);
50 57706 k.triantaf
                    this.layoutService.setOpen(true);
51 57673 k.triantaf
                  });
52 57514 k.triantaf
                }
53
              } else {
54
                this.stakeholderService.setStakeholder(null);
55
              }
56
            });
57 57496 k.triantaf
          }
58
        });
59
        this.userManagementService.getUserInfo(this.properties.userInfoUrl).subscribe(user => {
60 57449 argiro.kok
          this.user = user;
61
          this.loginCheck = true;
62
        }, error => {
63
          console.log("App couldn't fetch properties");
64
          console.log(error);
65
        });
66 57496 k.triantaf
      });
67 57706 k.triantaf
    this.subscriptions.push(this.layoutService.hasSidebar.subscribe(hasSidebar => {
68 57496 k.triantaf
      this.hasSidebar = hasSidebar;
69
      this.cdr.detectChanges();
70 57600 k.triantaf
    }));
71 57706 k.triantaf
    this.subscriptions.push(this.layoutService.hasHeader.subscribe(hasHeader => {
72
      this.hasHeader = hasHeader;
73
      this.cdr.detectChanges();
74
    }));
75 57600 k.triantaf
  }
76
77
  public ngOnDestroy() {
78 57673 k.triantaf
    this.subscriptions.forEach(value => {
79
      if (value instanceof Subscriber) {
80 57600 k.triantaf
        value.unsubscribe();
81
      }
82 57496 k.triantaf
    });
83 57449 argiro.kok
  }
84
85 57496 k.triantaf
  public get open() {
86 57706 k.triantaf
    return this.layoutService.open;
87 57496 k.triantaf
  }
88 57449 argiro.kok
89 57496 k.triantaf
  public toggleOpen(event = null) {
90 57673 k.triantaf
    if (!event) {
91 57706 k.triantaf
      this.layoutService.setOpen(!this.open);
92 57673 k.triantaf
    } else if (event && event['value'] === true) {
93 57706 k.triantaf
      this.layoutService.setOpen(false);
94 57496 k.triantaf
    }
95 57449 argiro.kok
  }
96
}