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
  stickyAnimation?:boolean
19
}
20

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

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

    
54

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

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

    
63

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

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

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

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

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

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

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

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

    
137

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

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

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

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

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

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