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

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