import {Component, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core'; import {ActivatedRoute, Router} from "@angular/router"; import {Subscriber} from 'rxjs'; import {ErrorCodes} from '../../openaireLibrary/utils/properties/errorCodes'; import {SearchUtilsClass} from '../../openaireLibrary/searchPages/searchUtils/searchUtils.class'; import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties'; import {ManageCommunityProjectsService} from '../../services/manageProjects.service'; import {SearchCommunityProjectsService} from '../../openaireLibrary/connect/projects/searchProjects.service'; import {RouterHelper} from '../../openaireLibrary/utils/routerHelper.class'; import {FormArray, FormBuilder, FormGroup} from "@angular/forms"; import {properties} from "../../../environments/environment"; import {CommunityService} from "../../openaireLibrary/connect/community/community.service"; import {Option} from "../../openaireLibrary/sharedComponents/input/input.component"; import {SearchInputComponent} from "../../openaireLibrary/sharedComponents/search-input/search-input.component"; import {DropdownFilterComponent} from "../../openaireLibrary/utils/dropdown-filter/dropdown-filter.component"; import {OpenaireEntities} from "../../openaireLibrary/utils/properties/searchFields"; import {CommunityInfo} from "../../openaireLibrary/connect/community/communityInfo"; import {HelperFunctions} from "../../openaireLibrary/utils/HelperFunctions.class"; import {NotificationHandler} from "../../openaireLibrary/utils/notification-handler"; import {ClearCacheService} from "../../openaireLibrary/services/clear-cache.service"; @Component({ selector: 'remove-projects', templateUrl: './remove-projects.component.html' }) export class RemoveProjectsComponent implements OnInit { @Input() public community: CommunityInfo; @Input() public loading: boolean = true; @Output() addProjects: EventEmitter = new EventEmitter(); private subscriptions: any[] = []; public routerHelper: RouterHelper = new RouterHelper(); public projectUrl = "https://" + ((properties.environment == "beta" || properties.environment == "development") ? "beta." : "") + "explore.openaire.eu" + properties.searchLinkToProject.split("?")[0]; @Output() communityProjectsChanged = new EventEmitter(); @Input() public communityProjects = []; public previewCommunityProjects = []; public communitySearchUtils: SearchUtilsClass = new SearchUtilsClass(); public errorCodes: ErrorCodes; properties: EnvProperties = properties; private selectedCommunityProject: any; @ViewChild('AlertModalDeleteCommunity') alertModalDeleteCommunity; /* Paging */ page: number = 1; resultsPerPage: number = properties.resultsPerPage; /* Search */ @ViewChild('searchInputComponent') searchInputComponent: SearchInputComponent; filterForm: FormGroup; public fundersCtrl: FormArray; private searchText: RegExp = new RegExp(''); public keyword: string = ''; selectedFunders: string[] = []; allOptions: Option[] = []; sortOptions: Option[] = [ //{label:"Title (desc) ", value:{ sort: "title",descending: true }}, {label: "Title ", value: {sort: "title", descending: false}}, //{label:"Grant ID (desc) ", value:{ sort: "grant",descending: true }}, {label: "Grant ID ", value: {sort: "grant", descending: false}}, //{label:"Funder (desc) ", value:{ sort: "funder",descending: true }}, {label: "Funder ", value: {sort: "funder", descending: false}} ]; public openaireEntiites = OpenaireEntities; constructor(private route: ActivatedRoute, private _router: Router, private _fb: FormBuilder, private communityService: CommunityService, private _manageCommunityProjectsService: ManageCommunityProjectsService, private _searchCommunityProjectsService: SearchCommunityProjectsService, private _clearCacheService: ClearCacheService) { this.errorCodes = new ErrorCodes(); this.communitySearchUtils.status = this.errorCodes.LOADING; } ngOnInit() { this.communitySearchUtils.keyword = ""; this.fundersCtrl = this._fb.array([]); this.filterForm = this._fb.group({ keyword: [''], funder: this.fundersCtrl, sort: this._fb.control(this.sortOptions[0].value) }); this.subscriptions.push(this.filterForm.get('keyword').valueChanges.subscribe(value => { this.searchText = new RegExp(value, 'i'); this.page = 1; this.applyFilters(); })); this.subscriptions.push(this.filterForm.get('funder').valueChanges.subscribe(value => { this.page = 1; this.applyFilters(); })); this.subscriptions.push(this.filterForm.get('sort').valueChanges.subscribe(value => { this.page = 1; this.sort(); })); this.subscriptions.push(this.communityService.getCommunityAsObservable().subscribe(community => { if (community) { this.community = community; this.projectUrl = "https://" + ((this.properties.environment == "beta" || this.properties.environment == "development") ? "beta." : "") + this.community.communityId + ".openaire.eu" + this.properties.searchLinkToProject.split("?")[0]; this.keyword = ''; this._getCommunityProjects(); } })); } public ngOnDestroy() { this.subscriptions.forEach(sub => { if (sub instanceof Subscriber) { sub.unsubscribe(); } }); } totalPages(): number { let totalPages: any = this.communitySearchUtils.totalResults / (this.resultsPerPage); if (!(Number.isInteger(totalPages))) { totalPages = (parseInt(totalPages, 10) + 1); } return totalPages; } public confirmedDeleteProject(data: any) { this.subscriptions.push(this._manageCommunityProjectsService.removeProject(this.properties, this.community.communityId, this.selectedCommunityProject.id).subscribe( data => { let index = this.communityProjects.indexOf(this.selectedCommunityProject); this.communityProjects.splice(index, 1); this.applyFilters(); this._clearCacheService.purgeBrowserCache(this.openaireEntiites.PROJECT+" removed", this.community.communityId); NotificationHandler.rise(OpenaireEntities.PROJECT+' successfully removed!'); this.communityProjectsChanged.emit({ value: this.communityProjects, }); this.communitySearchUtils.totalResults--; this.communitySearchUtils.page = 1; }, err => { this.handleError('An error has been occurred. Try again later!'); console.error(err); } )); } public removeProject(communityProject: any) { this.selectedCommunityProject = communityProject; this.alertModalDeleteCommunity.cancelButton = true; this.alertModalDeleteCommunity.okButton = true; this.alertModalDeleteCommunity.alertTitle = "Remove "+OpenaireEntities.PROJECT; let title = ""; if (communityProject.name) { title = communityProject.name; } if (communityProject.name && communityProject.acronym) { title += " ("; } if (communityProject.acronym) { title += communityProject.acronym; } if (communityProject.name && communityProject.acronym) { title += ")"; } this.alertModalDeleteCommunity.message = OpenaireEntities.PROJECT; if (title) { this.alertModalDeleteCommunity.message += " " + title + " "; } this.alertModalDeleteCommunity.message += "will be removed from your "+OpenaireEntities.COMMUNITY.toLowerCase()+". Are you sure?"; this.alertModalDeleteCommunity.okButtonText = "Yes"; this.alertModalDeleteCommunity.open(); } public _getCommunityProjects() { this.communitySearchUtils.status = this.errorCodes.LOADING; this.communityProjects = []; this.communitySearchUtils.totalResults = 0; this.communitySearchUtils.page = 1; this.communitySearchUtils.keyword = ""; this.subscriptions.push(this._searchCommunityProjectsService.searchProjects(this.properties, this.community.communityId).subscribe( data => { this.communityProjects = data; this.previewCommunityProjects = this.communityProjects; this.sort(); this.communitySearchUtils.totalResults = data.length; this.communitySearchUtils.status = this.errorCodes.DONE; this.communityProjectsChanged.emit({ value: this.communityProjects, }); this.createFunderFilter(); this.loading = false; }, err => { console.error(err); //TODO check erros (service not available, bad request) if (err.status == '404') { this.communitySearchUtils.status = this.errorCodes.NOT_FOUND; } else if (err.status == '500') { this.communitySearchUtils.status = this.errorCodes.ERROR; } else { this.communitySearchUtils.status = this.errorCodes.NOT_AVAILABLE; } this.loading = false; } )); } public createFunderFilter(): void { let funders: Set = new Set(); this.allOptions = []; let i; for (i = 0; i < this.communityProjects.length; i++) { let funder = this.communityProjects[i].funder; if (funder && !funders.has(funder)) { funders.add(funder); this.allOptions.push({label: funder, value: {id: funder, label: funder}}); } } } public updatePage($event) { HelperFunctions.scroll(); this.page = $event.value; } addNew() { this.addProjects.emit(); } public applyFilters() { this.previewCommunityProjects = this.communityProjects.filter(project => { return (this.filterCommunityProjectByKeyword(project) && this.filterCommunityProjectByFunder(project)); }); // check paging here!!! if (this.previewCommunityProjects.slice((this.page - 1) * this.resultsPerPage, this.page * this.resultsPerPage).length == 0) { this.page = 1; } this.sort(); } public filterCommunityProjectByKeyword(project): boolean { return this.searchText.toString() === '' || ((project.name + " " + project.acronym + " " + project.grantId + " " + project.funder)).match(this.searchText) != null; } public filterCommunityProjectByFunder(project): boolean { if (this.fundersCtrl.getRawValue().length == 0) { return true; } for (let funder of this.fundersCtrl.getRawValue()) { if (project.funder.toLowerCase().indexOf(funder.label.toLowerCase()) != -1) { return true; } } return false; } private sort() { let sortOption: { sort: string, descending: boolean } = this.filterForm.get('sort').value; this.previewCommunityProjects.sort((left, right): number => { if (sortOption.sort == "title") { if ((left.name + left.acronym) > (right.name + right.acronym)) { return sortOption.descending ? -1 : 1; } else if ((left.name + left.acronym) < (right.name + right.acronym)) { return sortOption.descending ? 1 : -1; } } else if (sortOption.sort == "grant") { if (left.grantId > right.grantId) { return sortOption.descending ? -1 : 1; } else if (left.grantId < right.grantId) { return sortOption.descending ? 1 : -1; } } else if (sortOption.sort == "funder") { if (left.funder > right.funder) { return sortOption.descending ? -1 : 1; } else if (left.funder < right.funder) { return sortOption.descending ? 1 : -1; } } return 0; }); } public onSearchClose() { this.communitySearchUtils.keyword = this.filterForm.get('keyword').value; } public resetInput() { this.communitySearchUtils.keyword = null; this.searchInputComponent.reset() } handleError(message: string) { NotificationHandler.rise(message, 'danger'); } select(value: string, event, dropdownFilter: DropdownFilterComponent) { if(event.target instanceof HTMLInputElement) { dropdownFilter.closeDropdown(); if(event.target.checked && !this.selectedFunders.find(entity => value === entity)) { this.selectedFunders.push(value); this.fundersCtrl.setControl(this.fundersCtrl.value.length, this._fb.control(value)); } else if(!event.target.checked) { let index = this.selectedFunders.indexOf(value); if(index !== -1) { this.selectedFunders.splice(index, 1); this.fundersCtrl.removeAt(index); } } } } isSelected(value: string) { return this.filterForm && this.filterForm.get('funder').value.find(funder => funder === value) } }