import {ChangeDetectorRef, Component, OnDestroy, OnInit} from '@angular/core'; import {ActivatedRoute, NavigationEnd, Params, Router} from '@angular/router'; import {EnvProperties} from './openaireLibrary/utils/properties/env-properties'; import {EnvironmentSpecificService} from './openaireLibrary/utils/properties/environment-specific.service'; import {Session, User} from './openaireLibrary/login/utils/helper.class'; import {UserManagementService} from "./openaireLibrary/services/user-management.service"; import {StakeholderService} from "./openaireLibrary/monitor/services/stakeholder.service"; import {BehaviorSubject, Subscriber} from "rxjs"; import {LayoutService} from "./openaireLibrary/dashboard/sharedComponents/sidebar/layout.service"; import {MenuItem} from "./openaireLibrary/sharedComponents/menu"; import {Stakeholder, Topic} from "./openaireLibrary/monitor/entities/stakeholder"; import {LinksResolver} from "./search/links-resolver"; @Component({ selector: 'app-root', templateUrl: './app.component.html' }) export class AppComponent implements OnInit, OnDestroy { properties: EnvProperties; user: User; params: BehaviorSubject = new BehaviorSubject(null); hasSidebar: boolean = false; hasHeader: boolean = false; hasAdminMenu: boolean = false; hasMiniMenu: boolean = false; isFrontPage: boolean = false; isViewPublic: boolean = false; sideBarItems: MenuItem[] = []; userMenuItems: MenuItem[] = [new MenuItem("", "My profile", "", "", false, [], [], {})]; adminMenuItems: MenuItem[] = []; stakeholder: Stakeholder = null; activeTopic: Topic = null; private subscriptions: any[] = []; constructor(private route: ActivatedRoute, private propertiesService: EnvironmentSpecificService, private router: Router, private userManagementService: UserManagementService, private layoutService: LayoutService, private stakeholderService: StakeholderService, private cdr: ChangeDetectorRef) { this.subscriptions.push(this.router.events.subscribe(event => { if (event instanceof NavigationEnd) { let r = this.route; while (r.firstChild) { r = r.firstChild; } let params = r.snapshot.params; this.params.next(params); } })); } ngOnInit() { this.subscriptions.push(this.layoutService.hasSidebar.subscribe(hasSidebar => { this.hasSidebar = hasSidebar; if (this.hasSidebar === false) { this.layoutService.setOpen(false); } this.cdr.detectChanges(); })); this.subscriptions.push(this.layoutService.hasHeader.subscribe(hasHeader => { this.hasHeader = hasHeader; this.cdr.detectChanges(); })); this.subscriptions.push(this.layoutService.hasAdminMenu.subscribe(hasAdminMenu => { this.hasAdminMenu = hasAdminMenu; this.cdr.detectChanges(); })); this.subscriptions.push(this.layoutService.hasMiniMenu.subscribe(hasMiniMenu => { this.hasMiniMenu = hasMiniMenu; this.cdr.detectChanges(); })); this.subscriptions.push(this.layoutService.isFrontPage.subscribe(isFrontPage => { this.isFrontPage = isFrontPage; this.cdr.detectChanges(); })); this.route.queryParams.subscribe(params => { this.isViewPublic = (params['view'] == 'public'); }); this.layoutService.setOpen(false); this.propertiesService.loadEnvironment() .then(properties => { this.properties = properties; this.subscriptions.push(this.params.subscribe(params => { let isSearch = this.router.url.includes('search'); if (params && params['stakeholder']) { if (!this.stakeholderService.stakeholder || this.stakeholderService.stakeholder.alias !== params['stakeholder']) { this.stakeholderService.getStakeholder(this.properties.monitorServiceAPIURL, params['stakeholder']).subscribe(stakeholder => { this.stakeholderService.setStakeholder(stakeholder); this.layoutService.setOpen(true); this.stakeholder = stakeholder; LinksResolver.setProperties(this.stakeholder.alias); if(isSearch) { this.activeTopic = null; } else if (params && params['topic'] && !this.activeTopic) { this.activeTopic = this.stakeholder.topics.find(topic => topic.alias === decodeURIComponent(params['topic']) && this.isPublicOrIsMember(topic.isPublic) && topic.isActive); } else { this.activeTopic = this.stakeholder.topics.find(topic => this.isPublicOrIsMember(topic.isPublic) && topic.isActive); } this.setSideBar(); }, error => { this.stakeholderService.setStakeholder(null); LinksResolver.resetProperties(); this.navigateToError(); }); } else { if(isSearch) { this.activeTopic = null; } else if (params && params['topic']) { this.activeTopic = this.stakeholder.topics.find(topic => topic.alias === decodeURIComponent(params['topic']) && this.isPublicOrIsMember(topic.isPublic) && topic.isActive); } else { this.activeTopic = this.stakeholder.topics.find(topic => this.isPublicOrIsMember(topic.isPublic) && topic.isActive); } } } else { LinksResolver.resetProperties(); this.stakeholderService.setStakeholder(null); this.layoutService.setOpen(true); this.stakeholder = null; } })); this.subscriptions.push(this.userManagementService.getUserInfo().subscribe(user => { this.user = user; this.buildMenu(); }, error => { console.log("App couldn't fetch properties"); console.log(error); })); }); } public ngOnDestroy() { this.subscriptions.forEach(value => { if (value instanceof Subscriber) { value.unsubscribe(); } }); } private navigateToError() { this.router.navigate(['/error'], {queryParams: {'page': this.router.url}}); } public get open() { return this.layoutService.open; } public toggleOpen(event: MouseEvent) { event.preventDefault(); this.layoutService.setOpen(!this.open); } private setSideBar() { let items: MenuItem[] = []; this.stakeholder.topics.forEach((topic) => { if (this.isPublicOrIsMember(topic.isPublic) && topic.isActive) { let topicItem: MenuItem = new MenuItem(topic.alias, topic.name, "", ( '/' + this.stakeholder.alias + '/' + topic.alias), null, [], [], {}); topicItem.icon = topic.icon; items.push(topicItem); } }); if (items.length === 0) { items.push(new MenuItem('noTopics', 'No topics available yet', "", "", false, [], [], {})); } this.sideBarItems = items; } buildMenu() { this.userMenuItems = []; /* if (Session.isPortalAdministrator(this.user)) { this.userMenuItems.push(new MenuItem("", "Manage helptexts", "", "/helptexts", true, [], [], {communityId:'openaire'})) }*/ if (this.isAdmin()) { this.userMenuItems.push(new MenuItem("", "Manage profiles", "", "/admin", true, [], [], {})) } if (this.user) { this.userMenuItems.push(new MenuItem("", "User information", "", "/user-info", false, [], [], {})); } if (this.adminMenuItems.length == 0) { //nstructor(id: string, name: string, route: string, items: Item[], icon, open: boolean) { this.adminMenuItems.push(new MenuItem("stakeholders", "Manage profiles", "", "/admin", false, [], [], {}, "settings")); /*let adminOptions = new MenuItem("adminOptions", "Admin Options", "", "", false, [], [], {}) adminOptions.items.push(new MenuItem("pages", "Pages", "", "/pages", false, [], [], {})); adminOptions.items.push(new MenuItem("portals", "Portals", "", "/portals", false, [], [], {})); adminOptions.items.push(new MenuItem("entities", "Entities", "", "/entities", false, [], [], {})); adminOptions.items.push(new MenuItem("classes", "Class help texts", "", "/classes", false, [], [], {})); this.adminMenuItems.push(adminOptions); let monitorOptions = new MenuItem("monitorOptions", "Monitor Options", "", "", false, [], [], {}) monitorOptions.items.push(new MenuItem("pages", "Pages", "", "/pages", false, [], [], {communityId: 'openaire'})); monitorOptions.items.push(new MenuItem("entities", "Entities", "", "/entities", false, [], [], {communityId: 'openaire'})); monitorOptions.items.push(new MenuItem("classes", "Class help texts", "", "/classContents", false, [], [], {communityId: 'openaire'})); monitorOptions.items.push(new MenuItem("helptexts", "Help texts", "", "/helptexts", false, [], [], {communityId: 'openaire'})); this.adminMenuItems.push(monitorOptions);*/ } } public isAdmin() { return this.user && (Session.isPortalAdministrator(this.user) || Session.isCommunityCurator(this.user) || Session.isMonitorCurator(this.user)); } public isPublicOrIsMember(isPublic: boolean): boolean { if (isPublic) { return true; } else { if (this.isViewPublic) { // preview for not members return false; } else if (this.isAdmin()) { // if user is member, return true return true; } return false; } } createSearchParameters(){ if(!this.stakeholder){ return {}; } if(this.stakeholder.type == "funder"){ return { "relfunder":encodeURIComponent("\"" + this.stakeholder.index_id + "||"+this.stakeholder.index_name + "||"+this.stakeholder.index_shortName + "\"" )}; }else if(this.stakeholder.type == "ri"){ // 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 return { "community":encodeURIComponent("\"" + this.stakeholder.index_id + "||"+this.stakeholder.index_name + "\"" )}; }else if(this.stakeholder.type == "organization"){ return { "cf":true}; } } }