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

    
51
  showEntity = {};
52
  showPage = {};
53
  specialAnnouncementContent: string = null;
54
  activeRouteEnabled = false;
55

    
56

    
57
  constructor(private router: Router,
58
              private  route: ActivatedRoute,
59
              private config: ConfigurationService) {
60
  }
61

    
62
  ngOnInit() {
63
    this.activeRouteEnabled = false;
64
    //this.subscriptions = this.route.queryParams.subscribe(params => {
65
    //console.log("params: ",params);
66
    this.initialize();
67
    //});
68
  }
69

    
70
  ngOnDestroy() {
71
    for (let sub of this.subs) {
72
      sub.unsubscribe();
73
    }
74
  }
75

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

    
107
                this.showEntity['' + data['entities'][i]['pid'] + ''] = data['entities'][i]['isEnabled'];
108
              }
109
            }
110
            if(data['pages']) {
111
              for (var i = 0; i < data['pages'].length; i++) {
112
                this.showPage[data['pages'][i]['route']] = data['pages'][i]['isEnabled'];
113

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

    
124

    
125
  isEnabled(required, enabled) {
126
    if (!required) {
127
      return true;
128
    }
129

    
130

    
131
    for (let requiredEntity of required) {
132
      if (typeof enabled[requiredEntity] === 'undefined' || enabled[requiredEntity] == false) {
133
        return false;
134
      }
135
    }
136
    return true;
137
  }
138

    
139
  isAtleastOneEnabled(required, enabled) {
140
    if (!required || required.length == 0) {
141
      return true;
142
    }
143

    
144
    var count = required.length;
145
    for (let requiredEntity of required) {
146
      if (typeof enabled[requiredEntity] === 'undefined' || enabled[requiredEntity] == false) {
147
        count--;
148
      }
149
    }
150
    return (count > 0) ? true : false;
151
  }
152

    
153
  private handleError(message: string, error) {
154
    console.error('NavigationBar (component): ' + message, error);
155
  }
156

    
157
  getCurrentRoute() {
158
    return this.router.url.split('?')[0];
159
  }
160

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