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() dark: boolean = false;
17
  @Input() onlyTop: boolean;
18
  @Input() logoPath: string = "assets/common-assets/";
19
  @Input() userMenu: boolean = true;
20
  @Input() showHomeMenuItem: boolean = false;
21
  @Input() communityId;
22
  @Input() stakeholder: Stakeholder;
23
  @Input() showCommunityName: boolean = false;
24
  @Input() userMenuItems: MenuItem[];
25
  @Input() menuItems: RootMenuItem [];
26
  @Input() community: { id: string, name: string, logoUrl: string };
27
  @Input() showMenu: boolean = true;
28
  @Input() homeurl: boolean = true;
29
  @Input() properties: EnvProperties;
30
  @Input() user: User;
31
  @Input() enableSearch: boolean = false;
32
  @Input() searchRoute: string = "/search/find";
33
  @Input() searchPlaceHolder: string = "Search for research results";
34
  @Input() showLogo: boolean = true;
35
  keyword: string = "";
36

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

    
42

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

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

    
51

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

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

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

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

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

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

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

    
107
  }
108

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

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

    
119

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

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

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

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

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

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