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.loading = false;
151
          }
152
        } else {
153
          LinksResolver.resetProperties();
154
          this.stakeholderService.setStakeholder(null);
155
          this.layoutService.setOpen(!(this.innerWidth && this.innerWidth < 1219));
156
          this.stakeholder = null;
157
          this.buildMenu();
158
          this.loading = false;
159
        }
160
      }
161
    }));
162
    this.subscriptions.push(this.userManagementService.getUserInfo().subscribe(user => {
163
      this.user = user;
164
      if (user) {
165
        this.buildMenu();
166
      }
167
    }, error => {
168
      console.log("App couldn't fetch properties");
169
      console.log(error);
170
    }));
171
  }
172
  
173
  public ngOnDestroy() {
174
    this.subscriptions.forEach(value => {
175
      if (value instanceof Subscriber) {
176
        value.unsubscribe();
177
      }
178
    });
179
  }
180
  
181
  private navigateToError() {
182
    this.router.navigate(['/error'], {queryParams: {'page': this.properties.baseLink + this.router.url}});
183
  }
184
  
185
  public get open() {
186
    return this.layoutService.open;
187
  }
188
  
189
  public toggleOpen(event: MouseEvent) {
190
    event.preventDefault();
191
    this.layoutService.setOpen(!this.open);
192
  }
193
  
194
  private setSideBar() {
195
    let items: MenuItem[] = [];
196
    if(this.isPublicOrIsMember(this.stakeholder.visibility)) {
197
      this.stakeholder.topics.forEach((topic) => {
198
        if (this.isPublicOrIsMember(topic.visibility)) {
199
          let topicItem: MenuItem = new MenuItem(topic.alias, topic.name, "", (
200
            '/' + this.stakeholder.alias + '/' + topic.alias),
201
            null, [], [], {});
202
          topicItem.icon = topic.icon;
203
          items.push(topicItem);
204
        }
205
      });
206
      if (items.length === 0) {
207
        items.push(new MenuItem('noTopics', 'No topics available yet', "", "", false, [], [], {}));
208
      }
209
    }else{
210
      let topicItem: MenuItem = new MenuItem("private", "Private Data", "","", null, [], [], {});
211
      items.push(topicItem);
212
    }
213
    this.sideBarItems = items;
214
  }
215
  
216
  buildMenu() {
217
    this.menuItems = [];
218
    this.adminMenuItems = [];
219
    this.userMenuItems = [];
220
    if (this.user) {
221
      if (this.isCurator()) {
222
        this.userMenuItems.push(new MenuItem("", "Manage profiles",
223
          "", "/admin", false, [], [], {}));
224
      }
225
      this.userMenuItems.push(new MenuItem("", "User information", "", "/user-info", false, [], [], {}));
226
    }
227
    if (this.stakeholder) {
228
      if (this.isFrontPage) {
229
        this.setSideBar();
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)