openaire-library/dashboard/plugins/components/openaireProducts/plugin-openaire-products.fo...

64 lines
2.5 KiB
TypeScript

import {Component} from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {PluginOpenAIREProducts} from "./plugin-openaire-products.component";
import {PluginBaseFormComponent, PluginEditEvent} from "../../utils/base-plugin.form.component";
@Component({
selector: 'plugin-openaire-products-form',
template: `
<div *ngIf="pluginObject" class="uk-padding-xsmall">
<plugin-field-edit [value]="pluginObject.title"
type="text" field="title" (changed)="valueChanged($event)" ></plugin-field-edit>
<div class="uk-margin-medium-top uk-text-muted">
Select services:
</div>
<ng-container *ngFor="let service of services">
<div class="uk-grid uk-grid-small uk-margin-xsmall-top">
<div class="uk-text-small uk-width-3-4">{{service.name}}</div>
<div class=" uk-width-1-4">
<plugin-field-edit [value]=" pluginObject.serviceIdsArray.indexOf(service.id)!=-1"
type="boolean" field="serviceIdsArray" (changed)="serviceChanged(service.id,$event)" >
</plugin-field-edit>
</div>
</div>
</ng-container>
</div>
`,
})
//TODO make it extend PluginOpenaireProductsComponent (to avoid call in constructor..)
export class PluginOpenaireProductsFormComponent extends PluginBaseFormComponent<PluginOpenAIREProducts>{
default = new PluginOpenAIREProducts();
services = [];
excludedServiceIds = ["openaire_login","research_community_dashboard"]
constructor(http:HttpClient) {
super()
this.subscriptions.push(http.get("https://explore.openaire.eu/cache/get?url="+ encodeURIComponent("https://catalogue.openaire.eu/api/catalogue-resources?from=0&quantity=100&order=asc&orderField=name")).subscribe(res =>{
this.services = res["results"].map( x=> {
x.id = x.id.split("openaire.")[1]
return x;
});
this.services = this.services.filter(x=> this.excludedServiceIds.indexOf(x.id) ==-1);
}))
}
serviceChanged(id,$event:PluginEditEvent){
let index = this.pluginObject.serviceIdsArray.indexOf(id);
if(index !=-1){
this.pluginObject.serviceIdsArray.splice(index,1);
}else{
this.pluginObject.serviceIdsArray.push(id);
}
$event.value =this.pluginObject.serviceIdsArray;
this.valuesChanged.emit({field:$event.field, value: $event.value, type: 'parent'})
}
}