2021-05-19 13:40:29 +02:00
|
|
|
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";
|
2022-07-05 00:31:56 +02:00
|
|
|
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";
|
2022-07-13 19:26:10 +02:00
|
|
|
import {NotificationHandler} from "../../openaireLibrary/utils/notification-handler";
|
2021-05-19 13:40:29 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'remove-projects',
|
|
|
|
templateUrl: './remove-projects.component.html'
|
|
|
|
})
|
|
|
|
export class RemoveProjectsComponent implements OnInit {
|
2022-07-05 00:31:56 +02:00
|
|
|
@Input() public community: CommunityInfo;
|
|
|
|
@Input() public loading: boolean = true;
|
|
|
|
@Output() addProjects: EventEmitter<void> = new EventEmitter();
|
2021-05-19 13:40:29 +02:00
|
|
|
|
|
|
|
private subscriptions: any[] = [];
|
|
|
|
|
|
|
|
public routerHelper: RouterHelper = new RouterHelper();
|
2022-07-13 19:26:10 +02:00
|
|
|
public projectUrl = "https://" + ((properties.environment == "beta" || properties.environment == "development") ? "beta." : "") + "explore.openaire.eu" + properties.searchLinkToProject.split("?")[0];
|
2021-05-19 13:40:29 +02:00
|
|
|
|
|
|
|
@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 = '';
|
2022-07-05 00:31:56 +02:00
|
|
|
selectedFunders: string[] = [];
|
2021-05-19 13:40:29 +02:00
|
|
|
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}}
|
|
|
|
];
|
|
|
|
|
2022-07-05 00:31:56 +02:00
|
|
|
public openaireEntiites = OpenaireEntities;
|
2021-05-19 13:40:29 +02:00
|
|
|
|
|
|
|
constructor(private route: ActivatedRoute, private _router: Router,
|
|
|
|
private _fb: FormBuilder,
|
|
|
|
private communityService: CommunityService,
|
|
|
|
private _manageCommunityProjectsService: ManageCommunityProjectsService,
|
|
|
|
private _searchCommunityProjectsService: SearchCommunityProjectsService) {
|
|
|
|
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) {
|
2022-07-05 00:31:56 +02:00
|
|
|
this.community = community;
|
2021-05-19 13:40:29 +02:00
|
|
|
this.projectUrl = "https://"
|
|
|
|
+ ((this.properties.environment == "beta" || this.properties.environment == "development") ? "beta." : "")
|
2022-07-05 00:31:56 +02:00
|
|
|
+ this.community.communityId + ".openaire.eu" + this.properties.searchLinkToProject.split("?")[0];
|
2021-05-19 13:40:29 +02:00
|
|
|
|
|
|
|
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) {
|
2022-07-11 17:48:24 +02:00
|
|
|
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();
|
2022-07-13 19:26:10 +02:00
|
|
|
NotificationHandler.rise(OpenaireEntities.PROJECT+' successfully removed!');
|
2022-07-11 17:48:24 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
));
|
2021-05-19 13:40:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public removeProject(communityProject: any) {
|
2022-07-11 17:48:24 +02:00
|
|
|
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 += " (";
|
2021-05-19 13:40:29 +02:00
|
|
|
}
|
2022-07-11 17:48:24 +02:00
|
|
|
if (communityProject.acronym) {
|
|
|
|
title += communityProject.acronym;
|
|
|
|
}
|
|
|
|
if (communityProject.name && communityProject.acronym) {
|
|
|
|
title += ")";
|
|
|
|
}
|
|
|
|
this.alertModalDeleteCommunity.message = OpenaireEntities.PROJECT;
|
|
|
|
if (title) {
|
|
|
|
this.alertModalDeleteCommunity.message += " <b>" + title + "</b> ";
|
|
|
|
}
|
|
|
|
this.alertModalDeleteCommunity.message += "will be removed from your "+OpenaireEntities.COMMUNITY.toLowerCase()+". Are you sure?";
|
|
|
|
this.alertModalDeleteCommunity.okButtonText = "Yes";
|
|
|
|
this.alertModalDeleteCommunity.open();
|
2021-05-19 13:40:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public _getCommunityProjects() {
|
2022-07-11 17:48:24 +02:00
|
|
|
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;
|
2021-05-19 13:40:29 +02:00
|
|
|
}
|
2022-07-11 17:48:24 +02:00
|
|
|
|
|
|
|
this.loading = false;
|
|
|
|
}
|
|
|
|
));
|
2021-05-19 13:40:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public createFunderFilter(): void {
|
|
|
|
let funders: Set<string> = new Set<string>();
|
|
|
|
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) {
|
2022-07-05 00:31:56 +02:00
|
|
|
HelperFunctions.scroll();
|
2021-05-19 13:40:29 +02:00
|
|
|
this.page = $event.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
addNew() {
|
2022-07-05 00:31:56 +02:00
|
|
|
this.addProjects.emit();
|
2021-05-19 13:40:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public applyFilters() {
|
|
|
|
this.previewCommunityProjects = this.communityProjects.filter(project => {
|
2022-07-13 19:26:10 +02:00
|
|
|
return (this.filterCommunityProjectByKeyword(project) && this.filterCommunityProjectByFunder(project));
|
2021-05-19 13:40:29 +02:00
|
|
|
});
|
|
|
|
// 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 {
|
2022-07-13 19:26:10 +02:00
|
|
|
return this.searchText.toString() === ''
|
2021-05-19 13:40:29 +02:00
|
|
|
|| ((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) {
|
2022-07-13 19:26:10 +02:00
|
|
|
NotificationHandler.rise(message, 'danger');
|
2021-05-19 13:40:29 +02:00
|
|
|
}
|
2022-07-05 00:31:56 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
}
|