import {Injectable} from "@angular/core"; import {MenuItem, RootMenuItem} from "../../sharedComponents/menu"; import {Option} from "../../sharedComponents/input/input.component"; import {StakeholderEntities} from "../entities/stakeholder"; import {from, Subscription} from "rxjs"; import {HttpClient} from "@angular/common/http"; import {properties} from "../../../../environments/environment"; import {Page} from "../../utils/entities/adminTool/page"; import {map} from "rxjs/operators"; @Injectable({ providedIn: 'root' }) export class ResourcesService { private subscription: Subscription; private routes = ResourcesService.types.map(type => '/indicators/' + type.value); public static readonly types: Option[] = [ {value: 'funder', label: StakeholderEntities.FUNDERS}, {value: 'ri', label: StakeholderEntities.RIS}, {value: 'project', label: StakeholderEntities.PROJECTS}, {value: 'organization', label: StakeholderEntities.ORGANIZATIONS} ]; constructor(private http: HttpClient) { } private async getResourcesItemsAsync(prefix = '', portal: string = null): Promise { let items = []; let methodology = ResourcesService.setLink(new MenuItem("methodology", "Methodology", "", "", false, [], null, {}, null, null, null, null, '_self'), prefix + "/methodology", portal); methodology.items = [ResourcesService.setLink(new MenuItem("methodology", "Terminology and construction", "", "", false, [], null, {}, null, null, null, null, '_self'), prefix + "/methodology", portal), ResourcesService.setLink(new MenuItem("methodology", "See how it works", "", "", false, [], null, {}, null, "how", null, null, '_self'), prefix + "/methodology", portal)]; items.push(methodology); let indicators = new MenuItem("indicators-page", "Indicators", "", "", false, [], null, {}, null, null, null, null, '_self'); let promise = new Promise(resolve => { this.isPagesEnabled().subscribe(status => { ResourcesService.types.forEach((type, index) => { if(status[index]) { indicators.items.push(ResourcesService.setLink( new MenuItem("indicators-" + type.value, type.label, "", "", false, [], null, {}, null, null, null, null, '_self'), prefix + "/indicators/" + type.value, portal) ); } }); resolve(); }, error => { console.error(error); resolve(); }); }) await promise; if(indicators.items.length > 0) { indicators.url = indicators.items[0].url; indicators.route = indicators.items[0].route; items.push(indicators); } return items; } /** * @deprecated * */ setResourcesDeprecated(items: RootMenuItem[], prefix = '', portal: string = null) { if(this.subscription) { this.subscription.unsubscribe(); } let resources = new MenuItem('resources', 'Resources', "", "", false, [], null, {}); let index = items.push({rootItem: resources, items: []}) - 1; this.subscription = from(this.getResourcesItemsAsync(prefix, portal)).subscribe(resourcesItems => { items[index].items = resourcesItems; }); } setResources(items: MenuItem[], prefix = '', portal: string = null) { if(this.subscription) { this.subscription.unsubscribe(); } let resources = new MenuItem('resources', 'Resources', "", "", false, [], null, {}); let index = items.push(resources) - 1; this.subscription = from(this.getResourcesItemsAsync(prefix, portal)).subscribe(resourcesItems => { items[index].items = resourcesItems; }); } public isPagesEnabled() { let url = properties.adminToolsAPIURL + "/monitor/monitor/pages"; return this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url) .pipe(map(pages => { let result = this.routes.map(() => false); pages.forEach(page => { let index = this.routes.findIndex(route => route === page.route && page.isEnabled); if(index !== -1) { result[index] = true; } }); return result; })); } private static setLink(item: MenuItem, route: string, portal: string = null) { if(portal) { item.url = portal + route + (item.fragment?'#' + item.fragment:''); } else { item.route = route; } return item; } }