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
  public isAuthorized: boolean = false;
49
  subs: Subscription[] = [];
50
  showEntity = {};
51
  showPage = {};
52
  specialAnnouncementContent: string = null;
53
  
54
  
55
  constructor(private router: Router,
56
              private  route: ActivatedRoute,
57
              private config: ConfigurationService) {
58
  }
59
  
60
  ngOnInit() {
61
    this.initialize();
62
  }
63
  
64
  ngOnDestroy() {
65
    for (let sub of this.subs) {
66
      sub.unsubscribe();
67
    }
68
  }
69
  
70
  initialize() {
71
    if ((['provide', 'develop']).indexOf(this.portal) != -1) {
72
      this.header = {
73
        route: '/',
74
        url: null,
75
        title: this.portal,
76
        logoUrl: this.logoPath + 'logo-large-' + this.portal + '.png',
77
        logoSmallUrl: this.logoPath + 'logo-small-' + this.portal + '.png',
78
        position: 'left',
79
        badge: true
80
      };
81
    } else if (this.stakeholder) {
82
      this.header = {
83
        route: '',
84
        url: null,
85
        title: this.stakeholder.name,
86
        logoUrl: this.stakeholder.logoUrl,
87
        logoSmallUrl: this.stakeholder.logoUrl,
88
        position: 'left',
89
        badge: true
90
      };
91
    }
92
    this.isAuthorized = Session.isClaimsCurator(this.user) || Session.isPortalAdministrator(this.user);
93
    if (this.properties.adminToolsAPIURL && this.communityId) {
94
      //this.config.getCommunityInformation(this.properties, this.communityId).subscribe(data => {
95
      this.subs.push(this.config.communityInformationState.subscribe(data => {
96
          if (data) {
97
            if (data['entities']) {
98
              for (var i = 0; i < data['entities'].length; i++) {
99
                
100
                this.showEntity['' + data['entities'][i]['pid'] + ''] = data['entities'][i]['isEnabled'];
101
              }
102
            }
103
            if (data['pages']) {
104
              for (var i = 0; i < data['pages'].length; i++) {
105
                this.showPage[data['pages'][i]['route']] = data['pages'][i]['isEnabled'];
106
                
107
              }
108
            }
109
          }
110
        },
111
        error => {
112
          this.handleError('Error getting community information (e.g. pages,entities) for community with id: ' + this.communityId, error);
113
        }));
114
    }
115
  }
116
  
117
  
118
  isEnabled(required, enabled) {
119
    if (!required) {
120
      return true;
121
    }
122
    
123
    
124
    for (let requiredEntity of required) {
125
      if (typeof enabled[requiredEntity] === 'undefined' || enabled[requiredEntity] == false) {
126
        return false;
127
      }
128
    }
129
    return true;
130
  }
131
  
132
  isAtleastOneEnabled(required, enabled) {
133
    if (!required || required.length == 0) {
134
      return true;
135
    }
136
    
137
    var count = required.length;
138
    for (let requiredEntity of required) {
139
      if (typeof enabled[requiredEntity] === 'undefined' || enabled[requiredEntity] == false) {
140
        count--;
141
      }
142
    }
143
    return (count > 0) ? true : false;
144
  }
145
  
146
  private handleError(message: string, error) {
147
    console.error('NavigationBar (component): ' + message, error);
148
  }
149
  
150
  get currentRoute() {
151
    return {
152
      route: this.router.url.split('?')[0].split('#')[0],
153
      fragment: this.route.snapshot.fragment
154
    }
155
  }
156
  
157
  isTheActiveMenu(menu: RootMenuItem): boolean {
158
    if (!menu.rootItem.markAsActive) {
159
      return false;
160
    }
161
    if (this.isTheActiveMenuItem(menu.rootItem)) {
162
      return true;
163
    } else if (menu.items.length > 0) {
164
      for (let menuItem of menu.items) {
165
        if (this.isTheActiveMenuItem(menuItem)) {
166
          return true;
167
        }
168
      }
169
    }
170
    return false;
171
  }
172
  
173
  isTheActiveMenuItem(menuItem: MenuItem): boolean {
174
    let currentRoute = this.currentRoute;
175
    return (menuItem.route == currentRoute.route || menuItem.route == (currentRoute.route + "/")) &&
176
      currentRoute.fragment == menuItem.fragment;
177
  }
178
}
(6-6/7)