Project

General

Profile

1
import {Component, Input} 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

    
8
@Component({
9
  selector: 'navbar',
10
  templateUrl: 'navigationBar.component.html'
11
})
12
export class NavigationBarComponent {
13
  @Input() portal: string = "connect";
14
  @Input() onlyTop: boolean;
15
  @Input() logoPath: string = "assets/common-assets/";
16
  @Input() userMenu: boolean = true;
17
  @Input() showHomeMenuItem: boolean = false;
18
  @Input() communityId;
19
  @Input() showCommunityName: boolean = false;
20
  @Input() userMenuItems: MenuItem[];
21
  @Input() menuItems: RootMenuItem [];
22
  @Input() community: { id: string, name: string, logoUrl: string };
23
  @Input() showMenu: boolean = true;
24
  @Input() homeurl: boolean = true;
25
  @Input() properties: EnvProperties;
26
  @Input() user: User;
27
  @Input() enableSearch: boolean = false;
28
  @Input() searchRoute: string = "/search/find";
29
  @Input() searchPlaceHolder: string = "Search for research results";
30
  keyword: string = "";
31

    
32

    
33
  public isAuthorized: boolean = false;
34
  sub: any;
35
  isClient: boolean = false;
36
  showEntity = {};
37
  showPage = {};
38
  specialAnnouncementContent: string = null;
39
  activeRouteEnabled = false;
40

    
41

    
42
  constructor(private router: Router,
43
              private  route: ActivatedRoute,
44
              private config: ConfigurationService) {
45
  }
46

    
47
  ngOnInit() {
48
    //console.log(this.menuItems);
49
    if (typeof document !== 'undefined') {
50
      try {
51
        this.isClient = true;
52
      } catch (e) {
53
      }
54
    }
55
    this.activeRouteEnabled = false;
56
    this.sub = this.route.queryParams.subscribe(params => {
57
      this.initialize();
58
    });
59

    
60
  }
61

    
62
  ngOnDestroy() {
63
    this.sub.unsubscribe();
64
  }
65

    
66
  initialize() {
67
    this.activeRouteEnabled = false;
68
    this.isAuthorized = Session.isClaimsCurator(this.user) || Session.isPortalAdministrator(this.user);
69
    if (this.properties.adminToolsAPIURL && this.communityId) {
70
      this.config.getCommunityInformation(this.properties, this.communityId).subscribe(data => {
71
          for (var i = 0; i < data['entities'].length; i++) {
72

    
73
            this.showEntity["" + data['entities'][i]["pid"] + ""] = data['entities'][i]["isEnabled"];
74
          }
75
          for (var i = 0; i < data['pages'].length; i++) {
76
            this.showPage[data['pages'][i]["route"]] = data['pages'][i]["isEnabled"];
77

    
78
          }
79

    
80
        },
81
        error => {
82
          this.handleError("Error getting community information (e.g. pages,entities) for community with id: " + this.communityId, error);
83
        });
84
    }
85

    
86
  }
87

    
88
  onClick(id: string) {
89
    var el: HTMLElement = document.getElementById(id);
90
    el.classList.remove('uk-open');
91
  }
92

    
93
  isEnabled(required, enabled) {
94
    if (!required) {
95
      return true;
96
    }
97

    
98

    
99
    for (let requiredEntity of required) {
100
      if (typeof enabled[requiredEntity] === "undefined" || enabled[requiredEntity] == false) {
101
        return false;
102
      }
103
    }
104
    return true;
105
  }
106

    
107
  isAtleastOneEnabled(required, enabled) {
108
    if (!required || required.length == 0) {
109
      return true;
110
    }
111

    
112
    var count = required.length;
113
    for (let requiredEntity of required) {
114
      if (typeof enabled[requiredEntity] === "undefined" || enabled[requiredEntity] == false) {
115
        count--;
116
      }
117
    }
118
    return (count > 0) ? true : false;
119
  }
120

    
121
  private handleError(message: string, error) {
122
    console.error("NavigationBar (component): " + message, error);
123
  }
124

    
125
  getCurrentRoute() {
126
    return this.router.url.split('?')[0];
127
  }
128

    
129
  isTheActiveMenu(menu: RootMenuItem): boolean {
130
    let currentRoute = this.getCurrentRoute();
131
    if (!menu.rootItem.markAsActive) {
132
      return false;
133
    }
134
    if (currentRoute == menu.rootItem.route) {
135
      this.activeRouteEnabled = true;
136
      return true;
137
    } else if (menu.items.length > 0) {
138
      for (let menuItem of menu.items) {
139
        if (menuItem.route == currentRoute) {
140
          this.activeRouteEnabled = true;
141
          return true;
142
        }
143
      }
144
    }
145
    return false;
146
  }
147
}
(8-8/9)