import { AfterViewInit, Component, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'; import { MatAccordion } from '@angular/material'; import { ActivatedRoute, Params } from '@angular/router'; import { TranslateService } from '@ngx-translate/core'; import { Observable } from 'rxjs'; import { BaseComponent } from '../../../core/common/base/base.component'; import { ProjectStateType } from '../../../core/common/enum/project-state-type'; import { DatasetProfileModel } from '../../../core/model/dataset/dataset-profile'; import { ExternalSourceItemModel } from '../../../core/model/external-sources/external-source-item'; import { ProjectListingModel } from '../../../core/model/project/project-listing'; import { ExploreDatasetCriteriaModel } from '../../../core/query/explore-dataset/explore-dataset-criteria'; import { ProjectCriteria } from '../../../core/query/project/project-criteria'; import { TagCriteria } from '../../../core/query/tag/tag-criteria'; import { DatasetService } from '../../../core/services/dataset/dataset.service'; import { ExternalSourcesService } from '../../../core/services/external-sources/external-sources.service'; import { ProjectService } from '../../../core/services/project/project.service'; import { RequestItem } from '../../../core/query/request-item'; import { DataTableRequest } from '../../../core/model/data-table/data-table-request'; import { AuthService } from '../../../core/services/auth/auth.service'; import { MultipleAutoCompleteConfiguration } from '../../../library/auto-complete/multiple/multiple-auto-complete-configuration'; import { DmpService } from '../../../core/services/dmp/dmp.service'; import { OrganisationCriteria } from '../../../core/query/organisation/organisation-criteria'; import { OrganisationService } from '../../../core/services/organisation/organisation.service'; import { OrganizationModel } from '../../../core/model/organisation/organization'; import { DataTableData } from '../../../core/model/data-table/data-table-data'; import { DmpListingModel } from '../../../core/model/dmp/dmp-listing'; import { ExploreDmpCriteriaModel } from '../../../core/query/explore-dmp/explore-dmp-criteria'; import { DmpModel } from '../../../core/model/dmp/dmp'; import { BaseCriteriaComponent } from '../../misc/criteria/base-criteria.component'; import { ValidationErrorModel } from '../../../common/forms/validation/error-model/validation-error-model'; import { takeUntil } from 'rxjs/operators'; @Component({ selector: 'app-explore-dataset-filters-component', templateUrl: './explore-dataset-filters.component.html', styleUrls: ['./explore-dataset-filters.component.scss'] }) export class ExploreDatasetFiltersComponent extends BaseComponent implements OnInit, AfterViewInit { @Input() facetCriteria = new ExploreDatasetCriteriaModel(); @Output() facetCriteriaChange = new EventEmitter(); public filteringTagsAsync = false; public filteredTags: ExternalSourceItemModel[]; ProjectStateType = ProjectStateType; projects: Observable; profiles: Observable; dmpOrganisations: Observable; dmpIds: Observable>; projectOptions: Observable; projectStateOptions: Observable; filteringOrganisationsAsync = false; @ViewChild('facetAccordion') accordion: MatAccordion; displayProjectStateValue = (option) => option['value']; displayProjectStateLabel = (option) => option['label']; displayProjectValue = (option) => option['id']; displayProjectLabel = (option) => option['label']; displayProfileValue = (option) => option['id']; displayProfileLabel = (option) => option['label']; displayDmpOrganisationsValue = (option) => option['id']; displayDmpOrganisationsLabel = (option) => option['name']; tagsAutoCompleteConfiguration = { filterFn: this.filterTags.bind(this), initialItems: (excludedItems: any[]) => this.filterTags('').map(result => result.filter(resultItem => excludedItems.map(x => x.id).indexOf(resultItem.id) === -1)), displayFn: (item) => item['name'], titleFn: (item) => item['name'] }; dmpAutoCompleteConfiguration: MultipleAutoCompleteConfiguration = { filterFn: (x, excluded) => this.filterDmps(x).map(x => x.data), initialItems: (extraData) => this.filterDmps('').map(x => x.data), displayFn: (item) => item['label'], titleFn: (item) => item['label'] }; projectAutoCompleteConfiguration: MultipleAutoCompleteConfiguration = { filterFn: this.filterProject.bind(this), initialItems: (excludedItems: any[]) => this.filterProject('') .map(result => result.filter(resultItem => excludedItems.map(x => x.id).indexOf(resultItem.id) === -1)), displayFn: (item) => item['label'], titleFn: (item) => item['label'] } profileAutoCompleteConfiguration: MultipleAutoCompleteConfiguration = { filterFn: this.filterProfile.bind(this), initialItems: (excludedItems: any[]) => this.getProfiles() .map(result => result.filter(resultItem => excludedItems.map(x => x.id).indexOf(resultItem.id) === -1)), displayFn: (item) => item['label'], titleFn: (item) => item['label'] }; organizationAutoCompleteConfiguration: MultipleAutoCompleteConfiguration = { filterFn: this.filterOrganisation.bind(this), initialItems: (excludedItems: any[]) => this.getOrganisations() .map(result => result.filter(resultItem => excludedItems.map(x => x.id).indexOf(resultItem.id) === -1)), displayFn: (item) => item['name'], titleFn: (item) => item['name'] } constructor( public activatedRoute: ActivatedRoute, public projectService: ProjectService, public dmpService: DmpService, public organisationService: OrganisationService, public languageService: TranslateService, public datasetProfileService: DatasetService, public externalSourcesService: ExternalSourcesService, private authentication: AuthService, ) { super(); } ngOnInit() { setTimeout(x => { this.projectStateOptions = Observable.of( [ { label: this.languageService.instant('FACET-SEARCH.PROJECT-STATUS.OPTIONS.INACTIVE'), value: ProjectStateType.Finished }, { label: this.languageService.instant('FACET-SEARCH.PROJECT-STATUS.OPTIONS.ACTIVE'), value: ProjectStateType.OnGoing }, ]); }); this.profiles = this.datasetProfileService.getDatasetProfiles(); this.dmpOrganisations = this.externalSourcesService.searchDMPOrganizations(''); } ngAfterViewInit(): void { // this.accordion.openAll(); } public projectStatusChanged(event) { this.facetCriteria.projectStatus = event.value; if (!event.source.checked) { this.facetCriteria.projectStatus = null; this.projects = Observable.of([]); this.facetCriteria.projects = []; } // if (event.option.selected) { if (event.source.checked) { // const projectCriteria = new ProjectCriteria(); // projectCriteria.projectStateType = this.facetCriteria.projectStatus; //projectCriteria['length'] = 10; const fields: Array = new Array(); fields.push('asc'); const dataTableRequest: DataTableRequest = new DataTableRequest(0, null, { fields: fields }); dataTableRequest.criteria = new ProjectCriteria(); dataTableRequest.criteria.projectStateType = this.facetCriteria.projectStatus; dataTableRequest.criteria['length'] = 10; this.projects = this.projectService.getPublicPaged(dataTableRequest).map(x => x.data); this.facetCriteria.projects = []; } this.facetCriteriaChange.emit(this.facetCriteria); } public projectChanged(event: any) { const eventValue = event.option.value.id; if (event.option.selected) { this.facetCriteria.projects.push(eventValue); } if (!event.option.selected) { const index = this.facetCriteria.projects.indexOf(eventValue); this.facetCriteria.projects.splice(index, 1); } this.facetCriteriaChange.emit(this.facetCriteria); } removeProject(project) { this.facetCriteria.projects.splice(this.facetCriteria.projects.indexOf(project), 1); this.facetCriteriaChange.emit(this.facetCriteria); } public profileChanged(event: any) { const eventValue = event.option.value.id; if (event.option.selected) { this.facetCriteria.datasetProfile.push(eventValue); } if (!event.option.selected) { const index = this.facetCriteria.datasetProfile.indexOf(eventValue); this.facetCriteria.datasetProfile.splice(index, 1); } this.facetCriteriaChange.emit(this.facetCriteria); } getProfiles() { return this.datasetProfileService.getDatasetProfiles(); } removeProfile(profile) { this.facetCriteria.datasetProfile.splice(this.facetCriteria.datasetProfile.indexOf(profile), 1); this.facetCriteriaChange.emit(this.facetCriteria); } getOrganisations() { const fields: Array = new Array(); fields.push('asc'); const dataTableRequest: DataTableRequest = new DataTableRequest(0, null, { fields: fields }); dataTableRequest.criteria = new OrganisationCriteria(); dataTableRequest.criteria.labelLike = ''; return this.organisationService.searchPublicOrganisations(dataTableRequest).map(x => x.data); } public dmpOrganisationChanged(event: any) { const eventValue = event.option.value.id; if (event.option.selected) { this.facetCriteria.dmpOrganisations.push(eventValue); } if (!event.option.selected) { const index = this.facetCriteria.dmpOrganisations.indexOf(eventValue); this.facetCriteria.dmpOrganisations.splice(index, 1); } this.facetCriteriaChange.emit(this.facetCriteria); } public projectSearch(value: string): Observable { const projectCriteria = new ProjectCriteria(); projectCriteria.projectStateType = this.facetCriteria.projectStatus; projectCriteria['length'] = 10; projectCriteria.like = value; const fields: Array = new Array(); fields.push('asc'); const dataTableRequest: DataTableRequest = new DataTableRequest(0, null, { fields: fields }); dataTableRequest.criteria = projectCriteria; //const dataTableRequest: RequestItem = { criteria: projectCriteria }; //return this.projectService.getPaged(dataTableRequest, "autocomplete").map(x => x.data); return this.projectService.getPublicPaged(dataTableRequest).map(x => x.data); } public dmpOrganisationSearch(value: string): Observable { return this.externalSourcesService.searchDMPOrganizations(value); } removeOrganisation(organisation) { this.facetCriteria.dmpOrganisations.splice(this.facetCriteria.dmpOrganisations.indexOf(organisation), 1); this.facetCriteriaChange.emit(this.facetCriteria); } public profileSearch(value: string) { return this.datasetProfileService.getDatasetProfiles(); } public controlModified() { this.facetCriteriaChange.emit(this.facetCriteria); } onProjectOptionSelected(items: ProjectListingModel[]) { this.facetCriteria.projects.splice(0); this.facetCriteria.projects.push(...items.map(x => x.id)); this.facetCriteriaChange.emit(this.facetCriteria); } onProjectOptionRemoved(item: ProjectListingModel) { const index = this.facetCriteria.projects.indexOf(item.id); if (index >= 0) { this.facetCriteria.projects.splice(index, 1); this.facetCriteriaChange.emit(this.facetCriteria); } } onDmpOptionSelected(items: DmpListingModel[]) { this.facetCriteria.dmpIds.splice(0); this.facetCriteria.dmpIds.push(...items.map(x => x.id)); this.facetCriteriaChange.emit(this.facetCriteria); } onDmpOptionRemoved(item: DmpListingModel) { const index = this.facetCriteria.dmpIds.indexOf(item.id); if (index >= 0) { this.facetCriteria.dmpIds.splice(index, 1); this.facetCriteriaChange.emit(this.facetCriteria); } } onProfileOptionSelected(items: DatasetProfileModel[]) { this.facetCriteria.datasetProfile.splice(0); this.facetCriteria.datasetProfile.push(...items.map(x => x.id)); this.facetCriteriaChange.emit(this.facetCriteria); } onProfileOptionRemoved(item: DatasetProfileModel) { const index = this.facetCriteria.datasetProfile.indexOf(item.id); if (index >= 0) { this.facetCriteria.datasetProfile.splice(index, 1); this.facetCriteriaChange.emit(this.facetCriteria); } } onOrganizationOptionSelected(items: OrganizationModel[]) { this.facetCriteria.dmpOrganisations.splice(0); this.facetCriteria.dmpOrganisations.push(...items.map(x => x.id)); this.facetCriteriaChange.emit(this.facetCriteria); } onOrganizationOptionRemoved(item: OrganizationModel) { const index = this.facetCriteria.dmpOrganisations.indexOf(item.id); if (index >= 0) { this.facetCriteria.dmpOrganisations.splice(index, 1); this.facetCriteriaChange.emit(this.facetCriteria); } } filterDmps(value: string): Observable> { const fields: Array = new Array(); fields.push('asc'); const dmpDataTableRequest: DataTableRequest = new DataTableRequest(0, null, { fields: fields }); dmpDataTableRequest.criteria = new ExploreDatasetCriteriaModel(); dmpDataTableRequest.criteria.like = value; return this.dmpService.getPublicPaged(dmpDataTableRequest, "autocomplete") } filterTags(value: string): Observable { this.filteredTags = undefined; this.filteringTagsAsync = true; const requestItem: RequestItem = new RequestItem(); const criteria: TagCriteria = new TagCriteria(); criteria.like = value; requestItem.criteria = criteria; return this.externalSourcesService.searchDatasetTags(requestItem); // .subscribe(items => { // this.filteredTags = items; // this.filteringTagsAsync = false; // }); } filterProject(query: string) { const fields: Array = new Array(); fields.push('asc'); const projectRequestItem: DataTableRequest = new DataTableRequest(0, null, { fields: fields }); projectRequestItem.criteria = new ProjectCriteria(); projectRequestItem.criteria.like = query; return this.projectService.getPublicPaged(projectRequestItem).map(x => x.data); } filterProfile(query: string) { const fields: Array = new Array(); fields.push('asc'); const profileRequestItem: DataTableRequest = new DataTableRequest(0, null, { fields: fields }); profileRequestItem.criteria = new ExploreDatasetCriteriaModel(); profileRequestItem.criteria.like = query; return this.dmpService.getPublicPaged(profileRequestItem, "listing").map(x => x.data); } filterOrganisation(value: string) { this.filteringOrganisationsAsync = true; const fields: Array = new Array(); fields.push('asc'); const dataTableRequest: DataTableRequest = new DataTableRequest(0, null, { fields: fields }); dataTableRequest.criteria = new OrganisationCriteria(); dataTableRequest.criteria.labelLike = value; return this.organisationService.searchPublicOrganisations(dataTableRequest).map(x => x.data); } }