2022-07-29 11:15:54 +02:00
|
|
|
import {Injectable} from "@angular/core";
|
2022-09-20 17:00:19 +02:00
|
|
|
import {MenuItem} from "../../sharedComponents/menu";
|
2022-07-29 11:15:54 +02:00
|
|
|
import {Option} from "../../sharedComponents/input/input.component";
|
|
|
|
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";
|
2023-11-13 14:00:05 +01:00
|
|
|
import {StakeholderConfiguration} from "../../monitor-admin/utils/indicator-utils";
|
2022-07-29 11:15:54 +02:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class ResourcesService {
|
|
|
|
|
|
|
|
private subscription: Subscription;
|
|
|
|
private routes = ResourcesService.types.map(type => '/indicators/' + type.value);
|
|
|
|
|
2023-11-13 14:00:05 +01:00
|
|
|
public static readonly types: Option[] = StakeholderConfiguration.TYPES;
|
2022-07-29 11:15:54 +02:00
|
|
|
|
|
|
|
constructor(private http: HttpClient) {
|
|
|
|
}
|
|
|
|
|
2023-07-04 13:20:50 +02:00
|
|
|
private async getResourcesItemsAsync(prefix = '', portal: string = null, target = '_self'): Promise<MenuItem[]> {
|
2022-08-02 22:13:12 +02:00
|
|
|
let items = [
|
|
|
|
new MenuItem("methodology", "Methodology", "", "", false, [],
|
2022-07-29 11:15:54 +02:00
|
|
|
null, {}, null, null, null, null, '_self'),
|
2022-10-24 15:37:05 +02:00
|
|
|
ResourcesService.setLink(new MenuItem("methodological-approach", "Methodological Approach",
|
2023-07-04 13:20:50 +02:00
|
|
|
"", "", false, [], null, {}, null, null, null, null, target),
|
2022-10-24 15:37:05 +02:00
|
|
|
prefix + "/methodology/methodological-approach", portal),
|
|
|
|
ResourcesService.setLink(new MenuItem("terminology", "Terminology and construction",
|
2023-07-04 13:20:50 +02:00
|
|
|
"", "", false, [], null, {}, null, null, null, null, target),
|
2022-10-24 15:37:05 +02:00
|
|
|
prefix + "/methodology/terminology", portal)];
|
2022-08-05 14:50:53 +02:00
|
|
|
items.push(new MenuItem("indicators-page", "Indicators",
|
|
|
|
"", "", false, [], null, {}));
|
|
|
|
items.push(ResourcesService.setLink(new MenuItem("indicator-themes", "Indicator Themes",
|
2023-07-04 13:20:50 +02:00
|
|
|
"", "", false, [], null, {}, null, null, null, null, target), prefix + "/indicators/themes", portal));
|
2022-09-23 11:24:15 +02:00
|
|
|
let promise = new Promise<void>(resolve => {
|
2022-07-29 11:15:54 +02:00
|
|
|
this.isPagesEnabled().subscribe(status => {
|
|
|
|
ResourcesService.types.forEach((type, index) => {
|
2022-08-02 22:13:12 +02:00
|
|
|
if (status[index]) {
|
2022-08-05 14:50:53 +02:00
|
|
|
items.push(ResourcesService.setLink(
|
2022-07-29 11:15:54 +02:00
|
|
|
new MenuItem("indicators-" + type.value, type.label,
|
2023-07-04 13:20:50 +02:00
|
|
|
"", "", false, [], null, {}, null, null, null, null, target),
|
2022-07-29 11:15:54 +02:00
|
|
|
prefix + "/indicators/" + type.value, portal)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
resolve();
|
|
|
|
}, error => {
|
|
|
|
console.error(error);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
await promise;
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
2023-07-04 13:20:50 +02:00
|
|
|
setResources(items: MenuItem[], prefix = '', portal: string = null, target = '_self') {
|
2022-08-02 22:13:12 +02:00
|
|
|
if (this.subscription) {
|
2022-07-29 11:15:54 +02:00
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
let resources = new MenuItem('resources', 'Resources', "", "", false, [], null, {});
|
|
|
|
let index = items.push(resources) - 1;
|
2023-07-04 13:20:50 +02:00
|
|
|
this.subscription = from(this.getResourcesItemsAsync(prefix, portal, target)).subscribe(resourcesItems => {
|
2022-08-02 22:13:12 +02:00
|
|
|
items[index].items = resourcesItems;
|
2022-07-29 11:15:54 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public isPagesEnabled() {
|
|
|
|
let url = properties.adminToolsAPIURL + "/monitor/monitor/pages";
|
|
|
|
return this.http.get<Page[]>((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);
|
2022-08-02 22:13:12 +02:00
|
|
|
if (index !== -1) {
|
2022-07-29 11:15:54 +02:00
|
|
|
result[index] = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static setLink(item: MenuItem, route: string, portal: string = null) {
|
2022-08-02 22:13:12 +02:00
|
|
|
if (portal) {
|
|
|
|
item.url = portal + route + (item.fragment ? '#' + item.fragment : '');
|
2022-07-29 11:15:54 +02:00
|
|
|
} else {
|
|
|
|
item.route = route;
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
}
|