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
import {Stakeholder} from "../monitor/entities/stakeholder";
8
import {Subscription} from "rxjs";
9

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

    
36
  logosrc: string = "";
37
  logoUrl:string;
38
  logoRoute:string;
39
  logoName:string;
40

    
41

    
42
  public isAuthorized: boolean = false;
43
  subs: Subscription[] = [];
44

    
45
  showEntity = {};
46
  showPage = {};
47
  specialAnnouncementContent: string = null;
48
  activeRouteEnabled = false;
49

    
50

    
51
  constructor(private router: Router,
52
              private  route: ActivatedRoute,
53
              private config: ConfigurationService) {
54
  }
55

    
56
  ngOnInit() {
57
    this.activeRouteEnabled = false;
58
    //this.sub = this.route.queryParams.subscribe(params => {
59
      //console.log("params: ",params);
60
      this.initialize();
61
    //});
62
  }
63

    
64
  ngOnDestroy() {
65
    for (let sub of this.subs) {
66
      sub.unsubscribe();
67
    }
68
  }
69

    
70
  initialize() {
71
    this.activeRouteEnabled = false;
72
    this.isAuthorized = Session.isClaimsCurator(this.user) || Session.isPortalAdministrator(this.user);
73
    if (this.properties.adminToolsAPIURL && this.communityId) {
74
      //this.config.getCommunityInformation(this.properties, this.communityId).subscribe(data => {
75
      this.subs.push(this.config.communityInformationState.subscribe(data => {
76
        if(data) {
77
          for (var i = 0; i < data['entities'].length; i++) {
78

    
79
            this.showEntity["" + data['entities'][i]["pid"] + ""] = data['entities'][i]["isEnabled"];
80
          }
81
          for (var i = 0; i < data['pages'].length; i++) {
82
            this.showPage[data['pages'][i]["route"]] = data['pages'][i]["isEnabled"];
83

    
84
          }
85
        }
86
      },
87
      error => {
88
        this.handleError("Error getting community information (e.g. pages,entities) for community with id: " + this.communityId, error);
89
      }));
90
    }
91
    if(!this.community){
92
      this.logosrc = this.logoPath + "logo-large-"+this.portal+".png";
93
      this.logoRoute = "/";
94
      this.logoName = "OpenAIRE";
95

    
96
    }else if( this.community){
97
      this.logosrc = this.community.logoUrl;
98
      if(this.homeurl){
99
        this.logoRoute = "/";
100
      }else{
101
        this.logoUrl = 'https://'+(this.properties.environment =='beta'?'beta.':'')+this.community.id+'.openaire.eu';
102
      }
103
      this.logoName = this.community.name;
104
    }
105

    
106
  }
107

    
108
  onClick(id: string) {
109
    var el: HTMLElement = document.getElementById(id);
110
    el.classList.remove('uk-open');
111
  }
112

    
113
  isEnabled(required, enabled) {
114
    if (!required) {
115
      return true;
116
    }
117

    
118

    
119
    for (let requiredEntity of required) {
120
      if (typeof enabled[requiredEntity] === "undefined" || enabled[requiredEntity] == false) {
121
        return false;
122
      }
123
    }
124
    return true;
125
  }
126

    
127
  isAtleastOneEnabled(required, enabled) {
128
    if (!required || required.length == 0) {
129
      return true;
130
    }
131

    
132
    var count = required.length;
133
    for (let requiredEntity of required) {
134
      if (typeof enabled[requiredEntity] === "undefined" || enabled[requiredEntity] == false) {
135
        count--;
136
      }
137
    }
138
    return (count > 0) ? true : false;
139
  }
140

    
141
  private handleError(message: string, error) {
142
    console.error("NavigationBar (component): " + message, error);
143
  }
144

    
145
  getCurrentRoute() {
146
    return this.router.url.split('?')[0];
147
  }
148

    
149
  isTheActiveMenu(menu: RootMenuItem): boolean {
150
    let currentRoute = this.getCurrentRoute();
151
    if (!menu.rootItem.markAsActive) {
152
      return false;
153
    }
154
    if (currentRoute == menu.rootItem.route) {
155
      this.activeRouteEnabled = true;
156
      return true;
157
    } else if (menu.items.length > 0) {
158
      for (let menuItem of menu.items) {
159
        if (menuItem.route == currentRoute) {
160
          this.activeRouteEnabled = true;
161
          return true;
162
        }
163
      }
164
    }
165
    return false;
166
  }
167
}
(8-8/9)