Project

General

Profile

1
import {Component, Input, OnChanges, OnDestroy, OnInit, SimpleChanges} from '@angular/core';
2
import {ActivatedRoute, Router} from '@angular/router';
3
import {Session, User} from '../login/utils/helper.class';
4
import {ConfigurationService} from '../utils/configuration/configuration.service';
5
import {MenuItem, RootMenuItem} from './menu';
6
import {EnvProperties} from '../utils/properties/env-properties';
7
import {Stakeholder} from '../monitor/entities/stakeholder';
8
import {Subscription} from 'rxjs';
9

    
10
export interface Header {
11
  route?: string,
12
  url?: string,
13
  title: string,
14
  logoUrl: string,
15
  logoSmallUrl: string,
16
  position: 'left' | 'center' | 'right',
17
  badge: boolean
18
}
19

    
20
@Component({
21
  selector: 'navbar',
22
  templateUrl: 'navigationBar.component.html'
23
})
24
export class NavigationBarComponent implements OnInit, OnDestroy{
25
  @Input() portal: string = 'connect';
26
  @Input() dark: boolean = false;
27
  @Input() onlyTop: boolean;
28
  @Input() logoPath: string = 'assets/common-assets/';
29
  @Input() userMenu: boolean = true;
30
  @Input() showHomeMenuItem: boolean = false;
31
  @Input() communityId;
32
  @Input() stakeholder: Stakeholder;
33
  @Input() showCommunityName: boolean = false;
34
  @Input() userMenuItems: MenuItem[];
35
  @Input() menuItems: RootMenuItem [];
36
  @Input() header: Header;
37
  @Input() showMenu: boolean = true;
38
  @Input() homeurl: boolean = true;
39
  @Input() properties: EnvProperties;
40
  @Input() user: User;
41
  @Input() enableSearch: boolean = false;
42
  @Input() searchRoute: string = '/search/find';
43
  @Input() searchPlaceHolder: string = 'Search for research results';
44
  @Input() showLogo: boolean = true;
45
  @Input() offCanvasFlip: boolean = false;
46
  keyword: string = '';
47

    
48
  logosrc: string = '';
49
  logoUrl: string;
50
  logoRoute: string;
51
  logoName: string;
52

    
53

    
54
  public isAuthorized: boolean = false;
55
  subs: Subscription[] = [];
56

    
57
  showEntity = {};
58
  showPage = {};
59
  specialAnnouncementContent: string = null;
60
  activeRouteEnabled = false;
61

    
62

    
63
  constructor(private router: Router,
64
              private  route: ActivatedRoute,
65
              private config: ConfigurationService) {
66
  }
67

    
68
  ngOnInit() {
69
    this.activeRouteEnabled = false;
70
    //this.sub = this.route.queryParams.subscribe(params => {
71
    //console.log("params: ",params);
72
    this.initialize();
73
    //});
74
  }
75

    
76
  ngOnDestroy() {
77
    for (let sub of this.subs) {
78
      sub.unsubscribe();
79
    }
80
  }
81

    
82
  initialize() {
83
    if ((['explore', 'monitor', 'provide', 'develop', 'usage-counts', 'graph']).indexOf(this.portal) != -1) {
84
      this.header = {
85
        route: '/',
86
        url: null,
87
        title: this.portal,
88
        logoUrl: this.logoPath + 'logo-large-' + this.portal + '.png',
89
        logoSmallUrl: this.logoPath + 'logo-small-' + this.portal + '.png',
90
        position: 'left',
91
        badge: true
92
      };
93
    } else if (this.stakeholder) {
94
      this.header = {
95
        route: '',
96
        url: null,
97
        title: this.stakeholder.name,
98
        logoUrl: this.stakeholder.logoUrl,
99
        logoSmallUrl: this.stakeholder.logoUrl,
100
        position: 'left',
101
        badge: true
102
      };
103
    }
104
    this.activeRouteEnabled = false;
105
    this.isAuthorized = Session.isClaimsCurator(this.user) || Session.isPortalAdministrator(this.user);
106
    if (this.properties.adminToolsAPIURL && this.communityId) {
107
      //this.config.getCommunityInformation(this.properties, this.communityId).subscribe(data => {
108
      this.subs.push(this.config.communityInformationState.subscribe(data => {
109
          if (data) {
110
            for (var i = 0; i < data['entities'].length; i++) {
111

    
112
              this.showEntity['' + data['entities'][i]['pid'] + ''] = data['entities'][i]['isEnabled'];
113
            }
114
            for (var i = 0; i < data['pages'].length; i++) {
115
              this.showPage[data['pages'][i]['route']] = data['pages'][i]['isEnabled'];
116

    
117
            }
118
          }
119
        },
120
        error => {
121
          this.handleError('Error getting community information (e.g. pages,entities) for community with id: ' + this.communityId, error);
122
        }));
123
    }
124
  }
125

    
126
  onClick(id: string) {
127
    var el: HTMLElement = document.getElementById(id);
128
    el.classList.remove('uk-open');
129
  }
130

    
131
  isEnabled(required, enabled) {
132
    if (!required) {
133
      return true;
134
    }
135

    
136

    
137
    for (let requiredEntity of required) {
138
      if (typeof enabled[requiredEntity] === 'undefined' || enabled[requiredEntity] == false) {
139
        return false;
140
      }
141
    }
142
    return true;
143
  }
144

    
145
  isAtleastOneEnabled(required, enabled) {
146
    if (!required || required.length == 0) {
147
      return true;
148
    }
149

    
150
    var count = required.length;
151
    for (let requiredEntity of required) {
152
      if (typeof enabled[requiredEntity] === 'undefined' || enabled[requiredEntity] == false) {
153
        count--;
154
      }
155
    }
156
    return (count > 0) ? true : false;
157
  }
158

    
159
  private handleError(message: string, error) {
160
    console.error('NavigationBar (component): ' + message, error);
161
  }
162

    
163
  getCurrentRoute() {
164
    return this.router.url.split('?')[0];
165
  }
166

    
167
  isTheActiveMenu(menu: RootMenuItem): boolean {
168
    let currentRoute = this.getCurrentRoute();
169
    if (!menu.rootItem.markAsActive) {
170
      return false;
171
    }
172
    if (currentRoute == menu.rootItem.route) {
173
      this.activeRouteEnabled = true;
174
      return true;
175
    } else if (menu.items.length > 0) {
176
      for (let menuItem of menu.items) {
177
        if (menuItem.route == currentRoute) {
178
          this.activeRouteEnabled = true;
179
          return true;
180
        }
181
      }
182
    }
183
    return false;
184
  }
185
}
(8-8/9)