Project

General

Profile

1
import {ChangeDetectorRef, Component, OnDestroy, OnInit} from '@angular/core';
2
import {ActivatedRoute, NavigationEnd, Params, Router} from '@angular/router';
3
import {EnvProperties} from './openaireLibrary/utils/properties/env-properties';
4
import {EnvironmentSpecificService} from './openaireLibrary/utils/properties/environment-specific.service';
5
import {Session, User} from './openaireLibrary/login/utils/helper.class';
6
import {UserManagementService} from "./openaireLibrary/services/user-management.service";
7
import {StakeholderService} from "./openaireLibrary/monitor/services/stakeholder.service";
8
import {BehaviorSubject, Subscriber} from "rxjs";
9
import {LayoutService} from "./openaireLibrary/dashboard/sharedComponents/sidebar/layout.service";
10
import {MenuItem, RootMenuItem} from "./openaireLibrary/sharedComponents/menu";
11
import {Stakeholder, Topic, Visibility} from "./openaireLibrary/monitor/entities/stakeholder";
12
import {LinksResolver} from "./search/links-resolver";
13
import {Header} from "./openaireLibrary/sharedComponents/navigationBar.component";
14
import {arrow_left} from "./openaireLibrary/utils/icons/icons";
15
import {properties} from "../environments/environment";
16

    
17

    
18
@Component({
19
  selector: 'app-root',
20
  templateUrl: './app.component.html'
21
})
22
export class AppComponent implements OnInit, OnDestroy {
23
  properties: EnvProperties = properties;
24
  user: User;
25
  params: BehaviorSubject<Params> = new BehaviorSubject<Params>(null);
26
  hasSidebar: boolean = false;
27
  hasHeader: boolean = false;
28
  hasAdminMenu: boolean = false;
29
  hasMiniMenu: boolean = false;
30
  isFrontPage: boolean = false;
31
  isViewPublic: boolean = false;
32
  sideBarItems: MenuItem[] = [];
33
  specialSideBarMenuItem: MenuItem = null;
34
  menuItems: RootMenuItem[] = [];
35
  menuHeader: Header = {
36
    route: "/",
37
    url: null,
38
    title: "Default menu header",
39
    logoUrl: null,
40
    logoSmallUrl: null,
41
    position: 'center',
42
    badge: false,
43
    stickyAnimation: false
44
  };
45
  
46
  userMenuItems: MenuItem[] = [];
47
  adminMenuItems: MenuItem[] = [];
48
  stakeholder: Stakeholder = null;
49
  activeTopic: Topic = null;
50
  loading: boolean = true;
51
  paramsResolved: boolean = false;
52
  innerWidth;
53
  private subscriptions: any[] = [];
54
  
55
  constructor(private route: ActivatedRoute,
56
              private propertiesService: EnvironmentSpecificService,
57
              private router: Router,
58
              private userManagementService: UserManagementService,
59
              private layoutService: LayoutService,
60
              private stakeholderService: StakeholderService,
61
              private cdr: ChangeDetectorRef) {
62
    this.subscriptions.push(this.router.events.subscribe(event => {
63
      if (event instanceof NavigationEnd) {
64
        let r = this.route;
65
        while (r.firstChild) {
66
          r = r.firstChild;
67
        }
68
        let params = r.snapshot.params;
69
        this.paramsResolved = true;
70
        this.params.next(params);
71
      }
72
    }));
73
  }
74
  
75
  ngOnInit() {
76
    if (window) {
77
      this.innerWidth = window.innerWidth;
78
    }
79
    this.subscriptions.push(this.layoutService.hasSidebar.subscribe(hasSidebar => {
80
      this.hasSidebar = hasSidebar;
81
      if (this.hasSidebar === false) {
82
        this.layoutService.setOpen(false);
83
      }
84
      this.cdr.detectChanges();
85
    }));
86
    this.subscriptions.push(this.layoutService.hasHeader.subscribe(hasHeader => {
87
      this.hasHeader = hasHeader;
88
      this.cdr.detectChanges();
89
    }));
90
    this.subscriptions.push(this.layoutService.hasAdminMenu.subscribe(hasAdminMenu => {
91
      this.hasAdminMenu = hasAdminMenu;
92
      this.cdr.detectChanges();
93
    }));
94
    this.subscriptions.push(this.layoutService.hasMiniMenu.subscribe(hasMiniMenu => {
95
      this.hasMiniMenu = hasMiniMenu;
96
      this.cdr.detectChanges();
97
    }));
98
    this.subscriptions.push(this.layoutService.isFrontPage.subscribe(isFrontPage => {
99
      this.isFrontPage = isFrontPage;
100
      this.cdr.detectChanges();
101
    }));
102
    this.route.queryParams.subscribe(params => {
103
      this.isViewPublic = (params['view'] == 'public');
104
    });
105
    this.layoutService.setOpen(false);
106
    this.subscriptions.push(this.params.subscribe(params => {
107
      if (this.paramsResolved) {
108
        this.loading = true;
109
        this.layoutService.setSmallScreen((this.innerWidth && this.innerWidth < 1219));
110
        this.layoutService.setOpen(!(this.innerWidth && this.innerWidth < 1219));
111
        let isSearch = this.router.url.includes('search');
112
        if (params && params['stakeholder']) {
113
          this.stakeholder = this.stakeholderService.stakeholder;
114
          if (!this.stakeholder || this.stakeholderService.stakeholder.alias !== params['stakeholder']) {
115
            this.subscriptions.push(this.stakeholderService.getStakeholder(params['stakeholder']).subscribe(stakeholder => {
116
              if (stakeholder) {
117
                this.stakeholder = stakeholder;
118
                LinksResolver.setProperties(this.stakeholder.alias);
119
                this.buildMenu();
120
                if (isSearch) {
121
                  this.activeTopic = null;
122
                } else if (params && params['topic'] && !this.activeTopic) {
123
                  this.activeTopic = this.stakeholder.topics.find(topic => topic.alias === decodeURIComponent(params['topic']) && this.isPublicOrIsMember(topic.visibility));
124
                } else {
125
                  this.activeTopic = this.stakeholder.topics.find(topic => this.isPublicOrIsMember(topic.visibility));
126
                }
127
                this.setSideBar();
128
                this.loading = false;
129
              } else {
130
                LinksResolver.resetProperties();
131
                this.navigateToError();
132
                this.buildMenu();
133
                this.loading = false;
134
              }
135
            }, error => {
136
              LinksResolver.resetProperties();
137
              this.navigateToError();
138
              this.buildMenu();
139
              this.loading = false;
140
            }));
141
          } else {
142
            this.buildMenu();
143
            if (isSearch) {
144
              this.activeTopic = null;
145
            } else if (params && params['topic']) {
146
              this.activeTopic = this.stakeholder.topics.find(topic => topic.alias === decodeURIComponent(params['topic']) && this.isPublicOrIsMember(topic.visibility));
147
            } else {
148
              this.activeTopic = this.stakeholder.topics.find(topic => this.isPublicOrIsMember(topic.visibility));
149
            }
150
            this.setSideBar();
151
            this.loading = false;
152
          }
153
        } else {
154
          LinksResolver.resetProperties();
155
          this.stakeholderService.setStakeholder(null);
156
          this.layoutService.setOpen(!(this.innerWidth && this.innerWidth < 1219));
157
          this.stakeholder = null;
158
          this.buildMenu();
159
          this.loading = false;
160
        }
161
      }
162
    }));
163
    this.subscriptions.push(this.userManagementService.getUserInfo().subscribe(user => {
164
      this.user = user;
165
      if (user) {
166
        this.buildMenu();
167
      }
168
    }, error => {
169
      console.log("App couldn't fetch properties");
170
      console.log(error);
171
    }));
172
  }
173
  
174
  public ngOnDestroy() {
175
    this.subscriptions.forEach(value => {
176
      if (value instanceof Subscriber) {
177
        value.unsubscribe();
178
      }
179
    });
180
    this.userManagementService.clearSubscriptions();
181
    this.layoutService.clearSubscriptions();
182
    this.stakeholderService.clearSubscriptions();
183
  }
184
  
185
  private navigateToError() {
186
    this.router.navigate(['/error'], {queryParams: {'page': this.properties.baseLink + this.router.url}});
187
  }
188
  
189
  public get open() {
190
    return this.layoutService.open;
191
  }
192
  
193
  public toggleOpen(event: MouseEvent) {
194
    event.preventDefault();
195
    this.layoutService.setOpen(!this.open);
196
  }
197
  
198
  private setSideBar() {
199
    let items: MenuItem[] = [];
200
    if(this.isPublicOrIsMember(this.stakeholder.visibility)) {
201
      this.stakeholder.topics.forEach((topic) => {
202
        if (this.isPublicOrIsMember(topic.visibility)) {
203
          let topicItem: MenuItem = new MenuItem(topic.alias, topic.name, "", (
204
            '/' + this.stakeholder.alias + '/' + topic.alias),
205
            null, [], [], {});
206
          topicItem.icon = topic.icon;
207
          items.push(topicItem);
208
        }
209
      });
210
      if (items.length === 0) {
211
        items.push(new MenuItem('noTopics', 'No topics available yet', "", "", false, [], [], {}));
212
      }
213
    }else{
214
      let topicItem: MenuItem = new MenuItem("private", "Private Data", "","", null, [], [], {});
215
      items.push(topicItem);
216
    }
217
    this.sideBarItems = items;
218
  }
219
  
220
  buildMenu() {
221
    this.menuItems = [];
222
    this.adminMenuItems = [];
223
    this.userMenuItems = [];
224
    if (this.user) {
225
      if (this.isCurator()) {
226
        this.userMenuItems.push(new MenuItem("", "Manage profiles",
227
          "", "/admin", false, [], [], {}));
228
      }
229
      this.userMenuItems.push(new MenuItem("", "User information", "", "/user-info", false, [], [], {}));
230
    }
231
    if (this.stakeholder) {
232
      if (this.isFrontPage) {
233
        this.menuHeader = {
234
          route: "/" + this.stakeholder.alias,
235
          url: null,
236
          title: this.stakeholder.name,
237
          logoUrl: null,
238
          logoSmallUrl: null,
239
          position: 'center',
240
          badge: false,
241
          stickyAnimation: false
242
        };
243
        if (this.isCurator()) {
244
          this.menuItems.push({
245
            rootItem: new MenuItem("manage", "Manage",
246
              "", "/admin", false, [], null, {}), items: []
247
          });
248
          
249
        }
250

    
251
        if(this.isPublicOrIsMember(this.stakeholder.visibility)) {
252
          this.specialSideBarMenuItem = new MenuItem("search", "Search research outcomes", "", this.properties.searchLinkToResults, false, [], null, this.createSearchParameters());
253
          this.specialSideBarMenuItem.icon = '<span uk-icon="search"></span>';
254
        }else{
255
          this.specialSideBarMenuItem =null;
256
        }
257
        
258
      } else {
259
        this.menuHeader = {
260
          route: "/admin/" + this.stakeholder.alias,
261
          url: null,
262
          title: 'Admin - ' + this.stakeholder.name,
263
          logoUrl: null,
264
          logoSmallUrl: null,
265
          position: 'center',
266
          badge: false,
267
          stickyAnimation: false
268
        };
269
        this.menuItems.push({
270
          rootItem: new MenuItem("", "Dashboard",
271
            "", '/' + this.stakeholder.alias + '/', false, [], null, {}), items: []
272
        });
273
        this.adminMenuItems.push(new MenuItem("general", "General", "", "/admin/" + this.stakeholder.alias, false, [], [], {}, "<i uk-icon=\"image\"></i>"));
274
        this.adminMenuItems.push(new MenuItem("indicators", "Indicators", "", "/admin/" + this.stakeholder.alias + '/indicators', false, [], [], {}, "<i uk-icon=\"image\"></i>"));
275
        this.adminMenuItems.push(new MenuItem("users", "Users", "", "/admin/" + this.stakeholder.alias + "/users", false, [], [], {}, "<i uk-icon=\"users\"></i>"));
276
        this.specialSideBarMenuItem = new MenuItem("back", "Manage profiles", "", "/admin", false, [], null, {});
277
        this.specialSideBarMenuItem.icon = '<span class="uk-icon-button small uk-icon uk-button-secondary">' + arrow_left.data + '</span>'; // '<span class="uk-icon-button uk-icon portal-button  " uk-icon="chevron-left"></span>';
278
        this.specialSideBarMenuItem.customClass = 'uk-text-uppercase uk-text-bold uk-text-secondary';
279
      }
280
    } else {
281
      if (this.isFrontPage || !this.hasAdminMenu) {
282
        this.menuHeader = {
283
          route: null,
284
          url: "https://" + (this.properties.environment == 'beta' ? 'beta.' : '') + 'monitor.openaire.eu',
285
          title: "Monitor",
286
          logoUrl: 'assets/common-assets/logo-large-monitor.png',
287
          logoSmallUrl: "assets/common-assets/logo-small-monitor.png",
288
          position: 'left',
289
          badge: true,
290
          stickyAnimation: false
291
        };
292
        this.menuItems.push({
293
          rootItem: new MenuItem("about", "About",
294
            "https://" + (this.properties.environment == 'beta' ? 'beta.' : '') + 'monitor.openaire.eu/about/learn-how', "", false, [], null, {}), items: []
295
        });
296
        this.menuItems.push({
297
          rootItem: new MenuItem("browse", "Browse",
298
            "https://" + (this.properties.environment == 'beta' ? 'beta.' : '') + 'monitor.openaire.eu/browse', "", false, [], null, {}), items: []
299
        });
300
        this.menuItems.push({
301
          rootItem: new MenuItem("contact", "Contact us",
302
            "https://" + (this.properties.environment == 'beta' ? 'beta.' : '') + 'monitor.openaire.eu/contact-us', "", false, [], null, {}), items: []
303
        });
304
      } else {
305
        this.menuHeader = {
306
          route: "/",
307
          url: null,
308
          title: "Monitor Dashboard",
309
          logoUrl: null,
310
          logoSmallUrl: null,
311
          position: 'center',
312
          badge: false,
313
          stickyAnimation: false
314
        };
315
        this.adminMenuItems = [];
316
        this.specialSideBarMenuItem = null;
317
        this.adminMenuItems.push(new MenuItem("stakeholders", "Manage profiles", "", "/admin", false, [], [], {}, "<i uk-icon=\"cog\"></i>"));
318
        /*let adminOptions = new MenuItem("adminOptions", "Admin Options", "", null, false, [], [], {})
319
        adminOptions.items.push(new MenuItem("pages", "Pages", "", "/pages", false, [], [], {}));
320
        adminOptions.items.push(new MenuItem("portals", "Portals", "", "/portals", false, [], [], {}));
321
        adminOptions.items.push(new MenuItem("entities", "Entities", "", "/entities", false, [], [], {}));
322
        adminOptions.items.push(new MenuItem("classes", "Class help texts", "", "/classes", false, [], [], {}));
323
        this.adminMenuItems.push(adminOptions);
324
        let monitorOptions = new MenuItem("monitorOptions", "Monitor Options", "", null, false, [], [], {})
325
        monitorOptions.items.push(new MenuItem("pages", "Pages", "", "/pages", false, [], [], {communityId: 'monitor'}));
326
        monitorOptions.items.push(new MenuItem("entities", "Entities", "", "/entities", false, [], [], {communityId: 'monitor'}));
327
        monitorOptions.items.push(new MenuItem("classes", "Class help texts", "", "/classContents", false, [], [], {communityId: 'monitor'}));
328
        monitorOptions.items.push(new MenuItem("helptexts", "Help texts", "", "/helptexts", false, [], [], {communityId: 'monitor'}));
329
        this.adminMenuItems.push(monitorOptions);*/
330
        
331
      }
332
    }
333
    
334
  }
335
  public isCurator() {
336
    return this.user && (Session.isPortalAdministrator(this.user) || Session.isMonitorCurator(this.user) || Session.isKindOfMonitorManager(this.user));
337
  }
338
  public isManager(stakeholder:Stakeholder) {
339
    return this.user && (Session.isPortalAdministrator(this.user) || Session.isCurator(stakeholder.type, this.user) || Session.isManager(stakeholder.type, stakeholder.alias, this.user));
340
  }
341
  
342
  private resolvePageInner() {
343
    if(document !== undefined) {
344
      let header = document.getElementById('pager_header_content');
345
      let inner = document.getElementById('page_content_inner');
346
      if(header) {
347
        inner.setAttribute('style', '{margin-top:' + header.offsetHeight+ '}');
348
      } else {
349
        inner.setAttribute('style', '{margin-top:' + 0 + '}');
350
      }
351
    }
352
  }
353
  
354
  public isPublicOrIsMember(visibility: Visibility): boolean {
355
    if (visibility == "PRIVATE" || (this.isViewPublic && visibility != "PUBLIC")) {
356
        return false;
357
      }
358
      return true;
359
  }
360
  
361
  createSearchParameters() {
362
    if (!this.stakeholder) {
363
      return {};
364
    }
365
    if (this.stakeholder.type == "funder") {
366
      return {"relfunder": encodeURIComponent("\"" + this.stakeholder.index_id + "||" + this.stakeholder.index_name + "||" + this.stakeholder.index_shortName + "\"")};
367
    } else if (this.stakeholder.type == "ri") {
368
      // https://beta.explore.openaire.eu/search/find/research-outcomes?f0=q&fv0=&resultbestaccessright=%22Open%20Access%22&community=%22mes%7C%7CEuropean%20Marine%20Science%22&qf=true
369
      return {"community": encodeURIComponent("\"" + this.stakeholder.index_id + "||" + this.stakeholder.index_name + "\"")};
370
    } else if (this.stakeholder.type == "organization") {
371
      return {"cf": true};
372
    }
373
  }
374
}
(3-3/5)