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.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
  }
181
  
182
  private navigateToError() {
183
    this.router.navigate(['/error'], {queryParams: {'page': this.properties.baseLink + this.router.url}});
184
  }
185
  
186
  public get open() {
187
    return this.layoutService.open;
188
  }
189
  
190
  public toggleOpen(event: MouseEvent) {
191
    event.preventDefault();
192
    this.layoutService.setOpen(!this.open);
193
  }
194
  
195
  private setSideBar() {
196
    let items: MenuItem[] = [];
197
    if(this.isPublicOrIsMember(this.stakeholder.visibility)) {
198
      this.stakeholder.topics.forEach((topic) => {
199
        if (this.isPublicOrIsMember(topic.visibility)) {
200
          let topicItem: MenuItem = new MenuItem(topic.alias, topic.name, "", (
201
            '/' + this.stakeholder.alias + '/' + topic.alias),
202
            null, [], [], {});
203
          topicItem.icon = topic.icon;
204
          items.push(topicItem);
205
        }
206
      });
207
      if (items.length === 0) {
208
        items.push(new MenuItem('noTopics', 'No topics available yet', "", "", false, [], [], {}));
209
      }
210
    }else{
211
      let topicItem: MenuItem = new MenuItem("private", "Private Data", "","", null, [], [], {});
212
      items.push(topicItem);
213
    }
214
    this.sideBarItems = items;
215
  }
216
  
217
  buildMenu() {
218
    this.menuItems = [];
219
    this.adminMenuItems = [];
220
    this.userMenuItems = [];
221
    if (this.user) {
222
      if (this.isCurator()) {
223
        this.userMenuItems.push(new MenuItem("", "Manage profiles",
224
          "", "/admin", false, [], [], {}));
225
      }
226
      this.userMenuItems.push(new MenuItem("", "User information", "", "/user-info", false, [], [], {}));
227
    }
228
    if (this.stakeholder) {
229
      if (this.isFrontPage) {
230
        this.menuHeader = {
231
          route: "/" + this.stakeholder.alias,
232
          url: null,
233
          title: this.stakeholder.name,
234
          logoUrl: null,
235
          logoSmallUrl: null,
236
          position: 'center',
237
          badge: false,
238
          stickyAnimation: false
239
        };
240
        if (this.isCurator()) {
241
          this.menuItems.push({
242
            rootItem: new MenuItem("manage", "Manage",
243
              "", "/admin", false, [], null, {}), items: []
244
          });
245
          
246
        }
247

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