Project

General

Profile

1
import {ChangeDetectorRef, Component, HostListener, 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
import {ConfigurationService} from "./openaireLibrary/utils/configuration/configuration.service";
17

    
18

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

    
248
      }
249
      this.userMenuItems.push(new MenuItem("", "User information", "", "/user-info", false, [], [], {}));
250
    }
251
    if (this.stakeholder) {
252
      if (!this.isDashboard) {
253
        this.menuHeader = {
254
          route: null,
255
          url: "https://" + (this.properties.environment == 'beta' ? 'beta.' : '') + 'monitor.openaire.eu',
256
          title: "Monitor",
257
          logoUrl: 'assets/common-assets/logo-large-monitor.png',
258
          logoSmallUrl: "assets/common-assets/logo-small-monitor.png",
259
          position: 'left',
260
          badge: true,
261
          stickyAnimation: false
262
        };
263
        this.menuItems.push({
264
          rootItem: new MenuItem("dashboard", "Dashboard",
265
            "", "/" + this.stakeholder.alias, false, [], null, {}), items: []
266
        });
267
        if(this.stakeholder.type === "funder") {
268
          this.menuItems.push({
269
            rootItem: new MenuItem("develop", "Develop",
270
              "", "/" + this.stakeholder.alias + "/develop", false, [], null, {}), items: []
271
          });
272
        }
273
        if (this.isCurator()) {
274
          this.menuItems.push({
275
            rootItem: new MenuItem("manage", "Manage",
276
              "", "/admin", false, [], null, {}), items: []
277
          });
278
        }
279
      }
280
      else if (this.isFrontPage) {
281
        this.menuHeader = {
282
          route: "/" + this.stakeholder.alias,
283
          url: null,
284
          title: this.stakeholder.name,
285
          logoUrl: null,
286
          logoSmallUrl: null,
287
          position: 'center',
288
          badge: false,
289
          stickyAnimation: false
290
        };
291
        if(this.stakeholder.type === "funder") {
292
          this.menuItems.push({
293
            rootItem: new MenuItem("develop", "Develop",
294
              "", "/" + this.stakeholder.alias + "/develop", false, [], null, {}), items: []
295
          });
296
        }
297
        if (this.isCurator()) {
298
          this.menuItems.push({
299
            rootItem: new MenuItem("manage", "Manage",
300
              "", "/admin", false, [], null, {}), items: []
301
          });
302
        }
303
        
304
        if (this.isPublicOrIsMember(this.stakeholder.visibility)) {
305
          this.specialSideBarMenuItem = new MenuItem("search", "Search research outcomes", "", this.properties.searchLinkToResults, false, [], null, {});
306
          this.specialSideBarMenuItem.icon = '<span uk-icon="search"></span>';
307
        } else {
308
          this.specialSideBarMenuItem = null;
309
        }
310
      } else {
311
        this.menuHeader = {
312
          route: "/admin/" + this.stakeholder.alias,
313
          url: null,
314
          title: 'Admin - ' + this.stakeholder.name,
315
          logoUrl: null,
316
          logoSmallUrl: null,
317
          position: 'center',
318
          badge: false,
319
          stickyAnimation: false
320
        };
321
        this.menuItems.push({
322
          rootItem: new MenuItem("", "Dashboard",
323
            "", '/' + this.stakeholder.alias + '/', false, [], null, {}), items: []
324
        });
325
        this.adminMenuItems.push(new MenuItem("general", "General", "", "/admin/" + this.stakeholder.alias, false, [], [], {}, "<i uk-icon=\"image\"></i>"));
326
        this.adminMenuItems.push(new MenuItem("indicators", "Indicators", "", "/admin/" + this.stakeholder.alias + '/indicators', false, [], [], {}, "<i uk-icon=\"image\"></i>"));
327
        this.adminMenuItems.push(new MenuItem("users", "Users", "", "/admin/" + this.stakeholder.alias + "/users", false, [], [], {}, "<i uk-icon=\"users\"></i>"));
328
        if(Session.isPortalAdministrator(this.user) ) {
329
          this.adminMenuItems.push(new MenuItem("monitorOptions", "Pages & Entities", "", "/admin-tools/" + this.stakeholder.alias +  "/pages", false, [], [],  {communityId:this.stakeholder.alias}));
330
        }
331
        this.specialSideBarMenuItem = new MenuItem("back", "Manage profiles", "", "/admin", false, [], null, {});
332
        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>';
333
        this.specialSideBarMenuItem.customClass = 'uk-text-uppercase uk-text-bold uk-text-secondary';
334
      }
335
    } else {
336
      if (this.isFrontPage || !this.hasAdminMenu) {
337
        this.menuHeader = {
338
          route: null,
339
          url: "https://" + (this.properties.environment == 'beta' ? 'beta.' : '') + 'monitor.openaire.eu',
340
          title: "Monitor",
341
          logoUrl: 'assets/common-assets/logo-large-monitor.png',
342
          logoSmallUrl: "assets/common-assets/logo-small-monitor.png",
343
          position: 'left',
344
          badge: true,
345
          stickyAnimation: false
346
        };
347
        this.menuItems.push({
348
          rootItem: new MenuItem("about", "About",
349
            "https://" + (this.properties.environment == 'beta' ? 'beta.' : '') + 'monitor.openaire.eu/about/learn-how', "", false, [], null, {}),
350
          items: []
351
        });
352
        this.menuItems.push({
353
          rootItem: new MenuItem("browse", "Browse",
354
            "https://" + (this.properties.environment == 'beta' ? 'beta.' : '') + 'monitor.openaire.eu/browse', "", false, [], null, {}),
355
          items: []
356
        });
357
        this.menuItems.push({
358
          rootItem: new MenuItem("contact", "Contact us",
359
            "https://" + (this.properties.environment == 'beta' ? 'beta.' : '') + 'monitor.openaire.eu/contact-us', "", false, [], null, {}),
360
          items: []
361
        });
362
      } else {
363
        this.menuHeader = {
364
          route: "/",
365
          url: null,
366
          title: "Monitor Dashboard",
367
          logoUrl: null,
368
          logoSmallUrl: null,
369
          position: 'center',
370
          badge: false,
371
          stickyAnimation: false
372
        };
373
        this.adminMenuItems = [];
374
        this.specialSideBarMenuItem = null;
375
        this.adminMenuItems.push(new MenuItem("stakeholders", "Manage profiles", "", "/admin", false, [], [], {}, "<i uk-icon=\"cog\"></i>"));
376
//        if(Session.isPortalAdministrator(this.user) ) {
377
//          let adminOptions = new MenuItem("adminOptions", "Admin Options","", "/admin-tools/admin-pages", false, [], [], {});
378
// /*         adminOptions.items.push(new MenuItem("pages", "Pages", "", "/admin-tools/pages", false, [], [], {}));
379
//          adminOptions.items.push(new MenuItem("portals", "Portals", "", "/admin-tools/portals", false, [], [], {}));
380
//          adminOptions.items.push(new MenuItem("entities", "Entities", "", "/admin-tools/entities", false, [], [], {}));
381
//          // adminOptions.items.push(new MenuItem("classes", "Class help texts", "", "/admin-tools/classes", false, [], [], {}));*/
382
//          this.adminMenuItems.push(adminOptions);
383
//          let monitorOptions = new MenuItem("monitorOptions", "Monitor Options","", "/admin-tools/admin-pages", false, [], [], {communityId: 'monitor'});
384
//         /* monitorOptions.items.push(new MenuItem("pages", "Pages", "", "/admin-tools/pages", false, [], [], {communityId: 'monitor'}));
385
//          monitorOptions.items.push(new MenuItem("entities", "Entities", "", "/admin-tools/entities", false, [], [], {communityId: 'monitor'}));
386
//          // monitorOptions.items.push(new MenuItem("classes", "Class help texts", "", "/admin-tools/classContents", false, [], [], {communityId: 'monitor'}));
387
//          monitorOptions.items.push(new MenuItem("helptexts", "Help texts", "", "/admin-tools/helptexts", false, [], [], {communityId: 'monitor'}));*/
388
//          this.adminMenuItems.push(monitorOptions);
389
//        }
390
        
391
      }
392
    }
393
    
394
  }
395
  
396
  public isCurator() {
397
    return this.user && (Session.isPortalAdministrator(this.user) || Session.isMonitorCurator(this.user) || Session.isKindOfMonitorManager(this.user));
398
  }
399
  
400
  public isManager(stakeholder: Stakeholder) {
401
    return this.user && (Session.isPortalAdministrator(this.user) || Session.isCurator(stakeholder.type, this.user) || Session.isManager(stakeholder.type, stakeholder.alias, this.user));
402
  }
403
  
404
  private resolvePageInner() {
405
    if (document !== undefined) {
406
      let header = document.getElementById('pager_header_content');
407
      let inner = document.getElementById('page_content_inner');
408
      if (header) {
409
        inner.setAttribute('style', '{margin-top:' + header.offsetHeight + '}');
410
      } else {
411
        inner.setAttribute('style', '{margin-top:' + 0 + '}');
412
      }
413
    }
414
  }
415
  
416
  public isPublicOrIsMember(visibility: Visibility): boolean {
417
    if (visibility == "PRIVATE" || (this.isViewPublic && visibility != "PUBLIC")) {
418
      return false;
419
    }
420
    return true;
421
  }
422
  setProperties(type, communityId){
423
    this.properties.adminToolsCommunity = communityId;
424
    this.properties.adminToolsPortalType = type;
425
    this.configurationService.initCommunityInformation(this.properties, this.properties.adminToolsCommunity);
426
  }
427
  resetProperties(){
428
    this.properties.adminToolsCommunity = "monitor";
429
    this.properties.adminToolsPortalType = "monitor";
430
    this.configurationService.initCommunityInformation(this.properties, this.properties.adminToolsCommunity);
431
  }
432
  
433
  /*  createSearchParameters() {
434
      if (!this.stakeholder) {
435
        return {};
436
      }
437
      if (this.stakeholder.type == "funder") {
438
        return {"relfunder": encodeURIComponent("\"" + this.stakeholder.index_id + "||" + this.stakeholder.index_name + "||" + this.stakeholder.index_shortName + "\"")};
439
      } else if (this.stakeholder.type == "ri") {
440
        return {"community": encodeURIComponent("\"" + this.stakeholder.index_id + "||" + this.stakeholder.index_name + "\"")};
441
      } else if (this.stakeholder.type == "organization") {
442
        return {"cf": true};
443
      }
444
    }*/
445
}
(3-3/5)