You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
connect-admin/src/app/app.component.ts

247 lines
10 KiB
TypeScript

import {ChangeDetectorRef, Component, HostListener, OnInit} from '@angular/core';
import {MenuItem, RootMenuItem} from './openaireLibrary/sharedComponents/menu';
import {ActivatedRoute, Data, NavigationEnd, Params, Router} from '@angular/router';
import {EnvProperties} from './openaireLibrary/utils/properties/env-properties';
import {Session, User} from './openaireLibrary/login/utils/helper.class';
import {UserManagementService} from './openaireLibrary/services/user-management.service';
import {Header} from './openaireLibrary/sharedComponents/navigationBar.component';
import {LayoutService} from "./openaireLibrary/dashboard/sharedComponents/sidebar/layout.service";
import {properties} from "../environments/environment";
import {BehaviorSubject, Subscriber} from "rxjs";
import {CommunityInfo} from "./openaireLibrary/connect/community/communityInfo";
import {CommunityService} from "./openaireLibrary/connect/community/community.service";
import {arrow_left} from "./openaireLibrary/utils/icons/icons";
import {SmoothScroll} from "./openaireLibrary/utils/smooth-scroll";
import {ConnectHelper} from './openaireLibrary/connect/connectHelper';
import {ConfigurationService} from './openaireLibrary/utils/configuration/configuration.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
})
export class AppComponent implements OnInit {
title = 'Research Community Dashboard | Administrator';
properties: EnvProperties = properties;
user: User;
params: BehaviorSubject<Params> = new BehaviorSubject<Params>(null);
data: BehaviorSubject<Data> = new BehaviorSubject<Data>(null);
hasSidebar: boolean = false;
hasHeader: boolean = false;
hasAdminMenu: boolean = false;
isFrontPage: boolean = false;
isDashboard: boolean = false;
sideBarItems: MenuItem[] = [];
specialSideBarMenuItem: MenuItem = null;
menuItems: RootMenuItem[] = [];
menuHeader: Header = {
route: "/",
url: null,
title: "Default menu header",
logoUrl: null,
logoSmallUrl: null,
position: 'center',
badge: false,
stickyAnimation: false
};
userMenuItems: MenuItem[] = [];
loading: boolean = true;
paramsResolved: boolean = false;
innerWidth;
public community: CommunityInfo = null;
private subscriptions: any[] = [];
headerLogoUrl: string;
headerUrl: string;
constructor(private route: ActivatedRoute,
private communityService: CommunityService,
private router: Router,
private cdr: ChangeDetectorRef,
private smoothScroll: SmoothScroll,
private layoutService: LayoutService,
private userManagementService: UserManagementService,
private configurationService: ConfigurationService) {
this.subscriptions.push(this.router.events.subscribe(event => {
if (event instanceof NavigationEnd) {
let r = this.route;
while (r.firstChild) {
r = r.firstChild;
}
this.paramsResolved = true;
this.params.next(r.snapshot.params);
this.data.next(r.snapshot.data);
}
}));
}
ngOnInit() {
if (typeof document !== 'undefined' && window) {
this.innerWidth = window.innerWidth;
}
this.subscriptions.push(this.layoutService.hasSidebar.subscribe(hasSidebar => {
this.hasSidebar = hasSidebar;
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.isFrontPage.subscribe(isFrontPage => {
this.isFrontPage = isFrontPage;
this.cdr.detectChanges();
}));
this.subscriptions.push(this.layoutService.isDashboard.subscribe(isDashboard => {
this.isDashboard = isDashboard;
this.cdr.detectChanges();
}));
this.layoutService.setSmallScreen((this.innerWidth && this.innerWidth < 1219));
this.layoutService.setOpen(!(this.innerWidth && this.innerWidth < 1219));
this.subscriptions.push(this.params.subscribe(params => {
if (this.paramsResolved) {
this.loading = true;
if (params && params['community']) {
if (!this.community || this.community.communityId !== params['community']) {
this.subscriptions.push(this.communityService.getCommunity(params['community']).subscribe(community => {
if (community) {
this.community = community;
this.setProperties(community.communityId);
this.buildMenu();
this.loading = false;
} else {
this.community = null;
this.buildMenu();
this.loading = false;
}
}));
} else {
this.buildMenu();
this.loading = false;
}
} else {
this.communityService.setCommunity(null);
this.layoutService.setOpen(!(this.innerWidth && this.innerWidth < 1219));
this.community = null;
this.buildMenu();
this.loading = false;
}
}
}));
this.subscriptions.push(this.data.subscribe(data => {
if(data && data.portal) {
this.setProperties(data.portal);
this.configurationService.initCommunityInformation(this.properties, this.properties.adminToolsCommunity);
}
}));
this.subscriptions.push(this.userManagementService.getUserInfo().subscribe(user => {
this.user = user;
if (this.user) {
localStorage.setItem('user_id', this.user.id);
localStorage.setItem('mining_backend_address', this.properties.miningBackendURL);
localStorage.setItem('isCommunityManager', Session.isCommunityCurator(this.user) + '');
this.buildMenu();
}
}));
}
@HostListener('window:resize', ['$event'])
onResize(event) {
if (this.layoutService.isSmallScreen && event.target.innerWidth > 1219) {
this.layoutService.setSmallScreen(false);
} else if (!this.layoutService.isSmallScreen && event.target.innerWidth < 1219) {
this.layoutService.setSmallScreen(true);
this.layoutService.setOpen(false);
}
}
public ngOnDestroy() {
this.subscriptions.forEach(value => {
if (value instanceof Subscriber) {
value.unsubscribe();
}
});
this.communityService.clearSubscriptions();
this.userManagementService.clearSubscriptions();
this.layoutService.clearSubscriptions();
this.smoothScroll.clearSubscriptions();
}
setProperties(id, type = null){
this.properties.adminToolsCommunity = id;
if(type) {
this.properties.adminToolsPortalType = type;
} else {
ConnectHelper.setPortalTypeFromPid(id);
}
this.configurationService.initCommunityInformation(this.properties, this.properties.adminToolsCommunity);
}
public get open() {
return this.layoutService.open;
}
private buildMenu() {
this.menuItems = [];
this.userMenuItems = [];
this.sideBarItems = [];
if (this.user) {
if (this.isCurator()) {
this.userMenuItems.push(new MenuItem("", "Manage communities",
"", "/", false, [], [], {}));
}
if (Session.isPortalAdministrator(this.user)) {
this.userMenuItems.push(new MenuItem("adminOptions", "Super Admin options", "", "/admin-tools/portals", false, [], [], {}));
this.userMenuItems.push(new MenuItem("connectOptions", "Connect portal options", "", "/connect/admin-tools/pages", false, [], [], {}));
this.userMenuItems.push(new MenuItem("exploreOptions", "Explore portal options", "", "/openaire/admin-tools/pages", false, [], [], {}));
}
this.userMenuItems.push(new MenuItem("", "User information", "", "/user-info", false, [], [], {}));
}
if (this.community) {
this.headerLogoUrl = this.community.logoUrl;
this.headerUrl = 'https://' + ((properties.environment !== 'production')?'beta.':'') + this.community.communityId + '.openaire.eu';
this.menuHeader = {
route: "/" + this.community.communityId,
url: null,
title: 'Admin - ' + this.community.shortTitle,
logoUrl: null,
logoSmallUrl: null,
position: 'center',
badge: false,
stickyAnimation: false
};
this.sideBarItems.push(new MenuItem("community", "Community Info", "", "/" + this.community.communityId, false, [], [], {}, null, null, null, "/" + this.community.communityId + "/info"));
this.sideBarItems.push(new MenuItem("users", "Users", "", "/" + this.community.communityId + "/users", false, [], [], {}, null, null, null, "/" + this.community.communityId + "/users"));
this.sideBarItems.push(new MenuItem("admin-tools", "Pages & Entities", "", "/" + this.community.communityId + "/admin-tools/pages", false, [], [], {}, null, null, null, "/" + this.community.communityId + "/admin-tools"));
this.sideBarItems.push(new MenuItem("customization", "Customization", "", "/" + this.community.communityId + "/customize-layout", false, [], [], {}));
if (this.community.type === 'ri') {
this.sideBarItems.push(new MenuItem("mining", "Mining", "", "/" + this.community.communityId + "/mining/manage-profiles", false, [], [], {}, null, null, null, "/" + this.community.communityId + "/mining"));
}
this.specialSideBarMenuItem = new MenuItem("back", "Manage communities", "", "/", false, [], null, {});
this.specialSideBarMenuItem.icon = '<span class="uk-icon-button small uk-icon uk-button-secondary">' + arrow_left.data + '</span>';
this.specialSideBarMenuItem.customClass = 'uk-text-uppercase uk-text-bold uk-text-secondary';
} else {
this.headerLogoUrl = null;
this.headerUrl = 'https://' + ((properties.environment !== 'production')?'beta.':'') + 'connect.openaire.eu';
this.menuHeader = {
route: null,
url: null,
title: 'Admin - Research Community Dashboard',
logoUrl: null,
logoSmallUrl: null,
position: 'center',
badge: false,
stickyAnimation: false
};
this.sideBarItems.push(new MenuItem("communities", "Manage Communities", "", "/", false, [], [], {}));
this.specialSideBarMenuItem = null;
}
}
private isCurator() {
return this.user && (Session.isPortalAdministrator(this.user) || Session.isCommunityCurator(this.user) || Session.isKindOfCommunityManager(this.user));
}
}