Project

General

Profile

1
import {ChangeDetectorRef, Component, OnDestroy, OnInit} from '@angular/core';
2
import {
3
    ActivatedRoute,
4
    NavigationEnd,
5
    NavigationError,
6
    NavigationStart,
7
    RouteConfigLoadEnd,
8
    Router
9
} from '@angular/router';
10

    
11
import {EnvProperties} from './openaireLibrary/utils/properties/env-properties';
12
import {EnvironmentSpecificService} from './openaireLibrary/utils/properties/environment-specific.service';
13
import {Session, User} from './openaireLibrary/login/utils/helper.class';
14
import {UserManagementService} from "./openaireLibrary/services/user-management.service";
15
import {StakeholderService} from "./services/stakeholder.service";
16
import {Subscriber} from "rxjs";
17
import {LayoutService} from "./library/sharedComponents/sidebar/layout.service";
18
import {MenuItem} from "./openaireLibrary/sharedComponents/menu";
19

    
20

    
21
@Component({
22
    selector: 'app-root',
23
    templateUrl: './app.component.html'
24
})
25
export class AppComponent implements OnInit, OnDestroy {
26
    properties: EnvProperties;
27
    user: User;
28
    loginCheck: boolean = false;
29
    hasSidebar: boolean = false;
30
    hasHeader: boolean = false;
31
    userMenuItems: MenuItem[] = [new MenuItem("", "My profile", "", "", false, [], [], {})];
32
    private subscriptions: any[] = [];
33

    
34
    constructor(private  route: ActivatedRoute,
35
                private propertiesService: EnvironmentSpecificService,
36
                private router: Router,
37
                private userManagementService: UserManagementService,
38
                private layoutService: LayoutService,
39
                private stakeholderService: StakeholderService,
40
                private cdr: ChangeDetectorRef) {
41
    }
42

    
43
    ngOnInit() {
44
        this.subscriptions.push(this.layoutService.hasSidebar.subscribe(hasSidebar => {
45
            this.hasSidebar = hasSidebar;
46
            this.cdr.detectChanges();
47
        }));
48
        this.subscriptions.push(this.layoutService.hasHeader.subscribe(hasHeader => {
49
            this.hasHeader = hasHeader;
50
            this.cdr.detectChanges();
51
        }));
52
        this.propertiesService.loadEnvironment()
53
            .then(properties => {
54
                this.properties = properties;
55
                this.subscriptions.push(this.router.events.subscribe(() => {
56
                    let r = this.route;
57
                    while (r.firstChild) {
58
                        r = r.firstChild;
59
                    }
60
                    this.subscriptions.push(r.params.subscribe(params => {
61
                        if (params['stakeholder']) {
62
                            if (!this.stakeholderService.stakeholder ||
63
                                this.stakeholderService.stakeholder.alias !== params['stakeholder']) {
64
                                this.stakeholderService.getStakeholder(this.properties.monitorServiceAPIURL, params['stakeholder']).subscribe(stakeholder => {
65
                                    this.stakeholderService.setStakeholder(stakeholder);
66
                                    this.layoutService.setOpen(true);
67
                                });
68
                            }
69
                        } else {
70
                            this.stakeholderService.setStakeholder(null);
71
                        }
72
                    }));
73
                }));
74
                this.subscriptions.push(this.userManagementService.getUserInfo(this.properties.userInfoUrl).subscribe(user => {
75
                    this.user = user;
76
                    this.loginCheck = true;
77
                    this.buildMenu();
78
                }, error => {
79
                    console.log("App couldn't fetch properties");
80
                    console.log(error);
81
                }));
82
            });
83
    }
84

    
85
    public ngOnDestroy() {
86
        this.subscriptions.forEach(value => {
87
            if (value instanceof Subscriber) {
88
                value.unsubscribe();
89
            }
90
        });
91
    }
92

    
93
    public get open() {
94
        return this.layoutService.open;
95
    }
96

    
97
    public toggleOpen(event = null) {
98
        if (!event) {
99
            this.layoutService.setOpen(!this.open);
100
        } else if (event && event['value'] === true) {
101
            this.layoutService.setOpen(false);
102
        }
103
    }
104

    
105

    
106
  buildMenu() {
107
    if (Session.isPortalAdministrator(this.user)) {
108
      this.userMenuItems.push(new MenuItem("", "Manage helptexts",
109
        ((this.properties.environment == "beta") ? "https://beta.admin.connect.openaire.eu" : "https://admin.explore.openaire.eu") + "/dashboard?communityId=openaire", "", true, [], [], {}))
110

    
111
    }
112
    if(this.user) {
113
      this.userMenuItems.push(new MenuItem("", "User information", "", "/user-info", false, [], [], {}));
114
    }
115
  }
116
}
(3-3/5)