Project

General

Profile

1
import {ChangeDetectorRef, Component, OnDestroy, OnInit} from '@angular/core';
2
import {ActivatedRoute, NavigationEnd, Params, Router} from '@angular/router';
3
import {EnvProperties} from './openaireLibrary/utils/properties/env-properties';
4
import {EnvironmentSpecificService} from './openaireLibrary/utils/properties/environment-specific.service';
5
import {Session, User} from './openaireLibrary/login/utils/helper.class';
6
import {UserManagementService} from "./openaireLibrary/services/user-management.service";
7
import {StakeholderService} from "./services/stakeholder.service";
8
import {BehaviorSubject, Subscriber} from "rxjs";
9
import {LayoutService} from "./openaireLibrary/dashboard/sharedComponents/sidebar/layout.service";
10
import {MenuItem} from "./openaireLibrary/sharedComponents/menu";
11
import {Stakeholder} from "./utils/entities/stakeholder";
12

    
13

    
14
@Component({
15
  selector: 'app-root',
16
  templateUrl: './app.component.html'
17
})
18
export class AppComponent implements OnInit, OnDestroy {
19
  properties: EnvProperties;
20
  user: User;
21
  params: BehaviorSubject<Params> = new BehaviorSubject<Params>(null);
22
  hasSidebar: boolean = false;
23
  hasHeader: boolean = false;
24
  hasAdminMenu: boolean = false;
25
  userMenuItems: MenuItem[] = [];
26
  adminMenuItems: MenuItem[] = [];
27
  stakeHolder:Stakeholder = null;
28
  private subscriptions: any[] = [];
29
  
30
  constructor(private route: ActivatedRoute,
31
              private propertiesService: EnvironmentSpecificService,
32
              private router: Router,
33
              private userManagementService: UserManagementService,
34
              private layoutService: LayoutService,
35
              private stakeholderService: StakeholderService,
36
              private cdr: ChangeDetectorRef) {
37
    this.subscriptions.push(this.router.events.subscribe(event => {
38
      if (event instanceof NavigationEnd) {
39
        let r = this.route;
40
        while (r.firstChild) {
41
          r = r.firstChild;
42
        }
43
        let params = r.snapshot.params;
44
        this.params.next(params);
45
      }
46
    }));
47
  }
48
  
49
  ngOnInit() {
50
    this.subscriptions.push(this.layoutService.hasSidebar.subscribe(hasSidebar => {
51
      this.hasSidebar = hasSidebar;
52
      if(this.hasSidebar === false) {
53
        this.layoutService.setOpen(false);
54
      }
55
      this.cdr.detectChanges();
56
    }));
57
    this.subscriptions.push(this.layoutService.hasHeader.subscribe(hasHeader => {
58
      this.hasHeader = hasHeader;
59
      this.cdr.detectChanges();
60
    }));
61
    this.subscriptions.push(this.layoutService.hasAdminMenu.subscribe(hasAdminMenu => {
62
      this.hasAdminMenu = hasAdminMenu;
63
      this.cdr.detectChanges();
64
    }));
65
    this.layoutService.setOpen(false);
66
    this.propertiesService.loadEnvironment()
67
      .then(properties => {
68
        this.properties = properties;
69
        this.subscriptions.push(this.params.subscribe( params => {
70
          if (params && params['stakeholder']) {
71
            if (!this.stakeholderService.stakeholder ||
72
              this.stakeholderService.stakeholder.alias !== params['stakeholder']) {
73
              this.stakeholderService.getStakeholder(this.properties.monitorServiceAPIURL, params['stakeholder']).subscribe(stakeholder => {
74
                this.stakeholderService.setStakeholder(stakeholder);
75
                this.layoutService.setOpen(true);
76
                this.stakeHolder = stakeholder;
77
              });
78
            }
79
          } else {
80
            this.stakeholderService.setStakeholder(null);
81
            this.layoutService.setOpen(true);
82
            this.stakeHolder = null;
83
          }
84
        }));
85
        this.subscriptions.push(this.userManagementService.getUserInfo().subscribe(user => {
86
          this.user = user;
87
          this.buildMenu();
88
        }, error => {
89
          console.log("App couldn't fetch properties");
90
          console.log(error);
91
        }));
92
      });
93
  }
94

    
95
  public ngOnDestroy() {
96
    this.subscriptions.forEach(value => {
97
      if (value instanceof Subscriber) {
98
        value.unsubscribe();
99
      }
100
    });
101
  }
102

    
103
  public get open() {
104
    return this.layoutService.open;
105
  }
106

    
107
  public toggleOpen(event: MouseEvent) {
108
    event.preventDefault();
109
    this.layoutService.setOpen(!this.open);
110
  }
111

    
112

    
113
  buildMenu() {
114
    this.userMenuItems = [];
115
    if (Session.isPortalAdministrator(this.user)) {
116
      this.userMenuItems.push(new MenuItem("", "Manage helptexts",
117
        "", "/helptexts", true, [], [], {communityId:'openaire'}))
118

    
119
    }
120
    if (this.user) {
121
      this.userMenuItems.push(new MenuItem("", "User information", "", "/user-info", false, [], [], {}));
122
    }
123
    if (this.adminMenuItems.length == 0) {
124
      //nstructor(id: string, name: string, route: string, items: Item[], icon, open: boolean) {
125
      this.adminMenuItems.push(new MenuItem("stakeholders", "Manage Stakeholders", "", "/admin", false, [], [], {}));
126
      let adminOptions = new MenuItem("adminOptions", "Admin Options", "", "", false, [], [], {})
127
      adminOptions.items.push(new MenuItem("pages", "Pages", "", "/pages", false, [], [], {}));
128
      adminOptions.items.push(new MenuItem("portals", "Portals", "", "/portals", false, [], [], {}));
129
      adminOptions.items.push(new MenuItem("entities", "Entities", "", "/entities", false, [], [], {}));
130
      adminOptions.items.push(new MenuItem("classes", "Class help texts", "", "/classes", false, [], [], {}));
131
      this.adminMenuItems.push(adminOptions);
132
      let monitorOptions = new MenuItem("monitorOptions", "Monitor Options", "", "", false, [], [], {})
133
      monitorOptions.items.push(new MenuItem("pages", "Pages", "", "/pages", false, [], [], {communityId: 'openaire'}));
134
      monitorOptions.items.push(new MenuItem("entities", "Entities", "", "/entities", false, [], [], {communityId: 'openaire'}));
135
      monitorOptions.items.push(new MenuItem("classes", "Class help texts", "", "/classContents", false, [], [], {communityId: 'openaire'}));
136
      monitorOptions.items.push(new MenuItem("helptexts", "Help texts", "", "/helptexts", false, [], [], {communityId: 'openaire'}));
137
      this.adminMenuItems.push(monitorOptions);
138
      // this.adminMenuItems.push(new Item("pages", "Pages", "/pages", [], null, false));
139
      // this.adminMenuItems.push(new Item("monitor", "Monitor", "/portals", [], null, false));
140
      // this.adminMenuItems.push(new Item("entities", "Entities", "/entities", [], null, false));
141
      // this.adminMenuItems.push(new Item("helptexts", "Help texts", "/helptexts?communityId=openaire", [], null, false));
142
    }
143
  }
144
}
(3-3/5)