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
  logosrc: string = "";
33
  logoUrl:string;
34
  logoRoute:string;
35
  logoName:string;
36

    
37

    
38
  public isAuthorized: boolean = false;
39
  sub: any;
40
  showEntity = {};
41
  showPage = {};
42
  specialAnnouncementContent: string = null;
43
  activeRouteEnabled = false;
44

    
45

    
46
  constructor(private router: Router,
47
              private  route: ActivatedRoute,
48
              private config: ConfigurationService) {
49
  }
50

    
51
  ngOnInit() {
52
    this.activeRouteEnabled = false;
53
    //this.sub = this.route.queryParams.subscribe(params => {
54
      //console.log("params: ",params);
55
      this.initialize();
56
    //});
57
  }
58

    
59
  ngOnDestroy() {
60
    if(this.sub) {
61
      this.sub.unsubscribe();
62
    }
63
  }
64

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

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

    
77
          }
78

    
79
        },
80
        error => {
81
          this.handleError("Error getting community information (e.g. pages,entities) for community with id: " + this.communityId, error);
82
        });
83
    }
84
    if(!this.community){
85
      this.logosrc = this.logoPath + "logo-large-"+this.portal+".png";
86
      this.logoRoute = "/";
87
      this.logoName = "OpenAIRE";
88

    
89
    }else if( this.community){
90
      this.logosrc = this.community.logoUrl;
91
      if(this.homeurl){
92
        this.logoRoute = "/";
93
      }else{
94
        this.logoUrl = 'https://'+(this.properties.environment =='beta'?'beta.':'')+this.community.id+'.openaire.eu';
95
      }
96
      this.logoName = this.community.name;
97
    }
98

    
99
  }
100

    
101
  onClick(id: string) {
102
    var el: HTMLElement = document.getElementById(id);
103
    el.classList.remove('uk-open');
104
  }
105

    
106
  isEnabled(required, enabled) {
107
    if (!required) {
108
      return true;
109
    }
110

    
111

    
112
    for (let requiredEntity of required) {
113
      if (typeof enabled[requiredEntity] === "undefined" || enabled[requiredEntity] == false) {
114
        return false;
115
      }
116
    }
117
    return true;
118
  }
119

    
120
  isAtleastOneEnabled(required, enabled) {
121
    if (!required || required.length == 0) {
122
      return true;
123
    }
124

    
125
    var count = required.length;
126
    for (let requiredEntity of required) {
127
      if (typeof enabled[requiredEntity] === "undefined" || enabled[requiredEntity] == false) {
128
        count--;
129
      }
130
    }
131
    return (count > 0) ? true : false;
132
  }
133

    
134
  private handleError(message: string, error) {
135
    console.error("NavigationBar (component): " + message, error);
136
  }
137

    
138
  getCurrentRoute() {
139
    return this.router.url.split('?')[0];
140
  }
141

    
142
  isTheActiveMenu(menu: RootMenuItem): boolean {
143
    let currentRoute = this.getCurrentRoute();
144
    if (!menu.rootItem.markAsActive) {
145
      return false;
146
    }
147
    if (currentRoute == menu.rootItem.route) {
148
      this.activeRouteEnabled = true;
149
      return true;
150
    } else if (menu.items.length > 0) {
151
      for (let menuItem of menu.items) {
152
        if (menuItem.route == currentRoute) {
153
          this.activeRouteEnabled = true;
154
          return true;
155
        }
156
      }
157
    }
158
    return false;
159
  }
160
}
(8-8/9)