Update library. Fix an expression error in advanced-criteria. Add message if there are no subjects in each category.

This commit is contained in:
Konstantinos Triantafyllou 2023-06-27 12:39:52 +03:00
parent 94fc9bda53
commit 500dd61f9b
4 changed files with 376 additions and 353 deletions

@ -1 +1 @@
Subproject commit 0face4b57f291ea0168fe0d8fe32b51ebb1850d8 Subproject commit 8d8eda416b4873c50f689db54444f135396db56d

View File

@ -48,7 +48,7 @@ import {CommunityInfo} from "../../openaireLibrary/connect/community/communityIn
</div> </div>
</div> </div>
<fs-modal #filtersModal (okEmitter)="saveCriteria()" (cancelEmitter)="criteria.reset()" <fs-modal #filtersModal (okEmitter)="saveCriteria()" (cancelEmitter)="criteria.reset()"
[okButtonDisabled]="criteria && criteria.disabled"> [okButtonDisabled]="!criteria || criteria.disabled">
<div *ngIf="selectionCriteria" class="uk-container uk-container-large"> <div *ngIf="selectionCriteria" class="uk-container uk-container-large">
<criteria #criteria [height]="filtersModal.bodyHeight" [entityType]="'criterion'" <criteria #criteria [height]="filtersModal.bodyHeight" [entityType]="'criterion'"
[entityTypePlural]="'criteria'" [selectionCriteria]="selectionCriteria"> [entityTypePlural]="'criteria'" [selectionCriteria]="selectionCriteria">

View File

@ -31,19 +31,19 @@
<li class="uk-active" *ngIf="displayedAllSubjects?.length" (click)="groupSubjects(displayedAllSubjects, 'all')"> <li class="uk-active" *ngIf="displayedAllSubjects?.length" (click)="groupSubjects(displayedAllSubjects, 'all')">
<a>All ({{displayedAllSubjects.length}})</a> <a>All ({{displayedAllSubjects.length}})</a>
</li> </li>
<li *ngIf="displayedSubjects?.length" (click)="groupSubjects(displayedSubjects, 'freeText')"> <li (click)="groupSubjects(displayedSubjects, 'freeText')">
<a>Free Text ({{displayedSubjects.length}})</a> <a>Free Text ({{displayedSubjects.length}})</a>
</li> </li>
<li *ngIf="displayedSdg?.length" (click)="groupSubjects(displayedSdg, 'sdg')"> <li (click)="groupSubjects(displayedSdg, 'sdg')">
<a>SDGs ({{displayedSdg.length}})</a> <a>SDGs ({{displayedSdg.length}})</a>
</li> </li>
<li *ngIf="displayedFos?.length" (click)="groupSubjects(displayedFos, 'fos')"> <li (click)="groupSubjects(displayedFos, 'fos')">
<a>Fields of Science ({{displayedFos.length}})</a> <a>Fields of Science ({{displayedFos.length}})</a>
</li> </li>
</ul> </ul>
<ul class="uk-switcher"> <ul class="uk-switcher">
<li *ngIf="displayedAllSubjects?.length"> <li>
<ng-container *ngIf="groupedAllSubjects?.length"> <ng-container *ngIf="displayedAllSubjects?.length">
<div> <div>
<ul class="uk-nav uk-nav-default uk-flex uk-flex-wrap"> <ul class="uk-nav uk-nav-default uk-flex uk-flex-wrap">
<li *ngFor="let item of groupedAllSubjects; let i = index;" class="uk-margin-right" [class.uk-margin-left]="i != 0" <li *ngFor="let item of groupedAllSubjects; let i = index;" class="uk-margin-right" [class.uk-margin-left]="i != 0"
@ -97,9 +97,14 @@
</div> </div>
</div> </div>
</ng-container> </ng-container>
<ng-container *ngIf="displayedAllSubjects?.length === 0">
<div class="uk-position-relative uk-height-medium">
<h6 class="uk-position-center uk-margin-remove">There are no subjects yet</h6>
</div>
</ng-container>
</li> </li>
<li *ngIf="displayedSubjects?.length"> <li>
<ng-container *ngIf="groupedSubjects?.length"> <ng-container *ngIf="displayedSubjects?.length">
<div> <div>
<ul class="uk-nav uk-nav-default uk-flex uk-flex-wrap"> <ul class="uk-nav uk-nav-default uk-flex uk-flex-wrap">
<li *ngFor="let item of groupedSubjects; let i = index;" class="uk-margin-right" [class.uk-margin-left]="i != 0" <li *ngFor="let item of groupedSubjects; let i = index;" class="uk-margin-right" [class.uk-margin-left]="i != 0"
@ -153,9 +158,14 @@
</div> </div>
</div> </div>
</ng-container> </ng-container>
<ng-container *ngIf="displayedSubjects?.length === 0">
<div class="uk-position-relative uk-height-medium">
<h6 class="uk-position-center uk-margin-remove">There are no subjects yet</h6>
</div>
</ng-container>
</li> </li>
<li *ngIf="displayedSdg?.length"> <li>
<ng-container *ngIf="groupedSdg?.length"> <ng-container *ngIf="displayedSdg?.length">
<div> <div>
<ul class="uk-nav uk-nav-default uk-flex uk-flex-wrap"> <ul class="uk-nav uk-nav-default uk-flex uk-flex-wrap">
<li *ngFor="let item of groupedSdg; let i = index;" class="uk-margin-right" [class.uk-margin-left]="i != 0" <li *ngFor="let item of groupedSdg; let i = index;" class="uk-margin-right" [class.uk-margin-left]="i != 0"
@ -209,9 +219,14 @@
</div> </div>
</div> </div>
</ng-container> </ng-container>
<ng-container *ngIf="displayedSdg?.length === 0">
<div class="uk-position-relative uk-height-medium">
<h6 class="uk-position-center uk-margin-remove">There are no subjects yet</h6>
</div>
</ng-container>
</li> </li>
<li *ngIf="displayedFos?.length"> <li>
<ng-container *ngIf="groupedFos?.length"> <ng-container *ngIf="displayedFos?.length">
<div> <div>
<ul class="uk-nav uk-nav-default uk-flex uk-flex-wrap"> <ul class="uk-nav uk-nav-default uk-flex uk-flex-wrap">
<li *ngFor="let item of groupedFos; let i = index;" class="uk-margin-right" [class.uk-margin-left]="i != 0" <li *ngFor="let item of groupedFos; let i = index;" class="uk-margin-right" [class.uk-margin-left]="i != 0"
@ -265,7 +280,13 @@
</div> </div>
</div> </div>
</ng-container> </ng-container>
<ng-container *ngIf="displayedFos?.length === 0">
<div class="uk-position-relative uk-height-medium">
<h6 class="uk-position-center uk-margin-remove">There are no subjects yet</h6>
</div>
</ng-container>
</li> </li>
</ul> </ul>
</div> </div>
</div> </div>

View File

@ -10,7 +10,9 @@ import {AlertModal} from '../../../openaireLibrary/utils/modal/alert';
import {SearchInputComponent} from '../../../openaireLibrary/sharedComponents/search-input/search-input.component'; import {SearchInputComponent} from '../../../openaireLibrary/sharedComponents/search-input/search-input.component';
import {forkJoin, Subscription} from 'rxjs'; import {forkJoin, Subscription} from 'rxjs';
import {CommunityInfo} from '../../../openaireLibrary/connect/community/communityInfo'; import {CommunityInfo} from '../../../openaireLibrary/connect/community/communityInfo';
import {FullScreenModalComponent} from 'src/app/openaireLibrary/utils/modal/full-screen-modal/full-screen-modal.component'; import {
FullScreenModalComponent
} from 'src/app/openaireLibrary/utils/modal/full-screen-modal/full-screen-modal.component';
import {HelperFunctions} from 'src/app/openaireLibrary/utils/HelperFunctions.class'; import {HelperFunctions} from 'src/app/openaireLibrary/utils/HelperFunctions.class';
import {NotificationHandler} from "../../../openaireLibrary/utils/notification-handler"; import {NotificationHandler} from "../../../openaireLibrary/utils/notification-handler";
import {ClearCacheService} from "../../../openaireLibrary/services/clear-cache.service"; import {ClearCacheService} from "../../../openaireLibrary/services/clear-cache.service";
@ -21,361 +23,361 @@ import {InputComponent} from "../../../openaireLibrary/sharedComponents/input/in
declare var UIkit: any; declare var UIkit: any;
@Component({ @Component({
selector: 'subjects-edit-form', selector: 'subjects-edit-form',
templateUrl: './subjects-edit-form.component.html', templateUrl: './subjects-edit-form.component.html',
}) })
export class SubjectsEditFormComponent implements OnInit { export class SubjectsEditFormComponent implements OnInit {
properties: EnvProperties = properties; properties: EnvProperties = properties;
communityId: string = null; communityId: string = null;
community: CommunityInfo = null; community: CommunityInfo = null;
loading = true; loading = true;
params: any; params: any;
private subscriptions: any[] = []; private subscriptions: any[] = [];
filterForm: UntypedFormControl; filterForm: UntypedFormControl;
subjectsForm = new UntypedFormArray([]); subjectsForm = new UntypedFormArray([]);
hasChanges: boolean = false; hasChanges: boolean = false;
originalAllSubjects = []; originalAllSubjects = [];
originalSubjects = []; originalSubjects = [];
originalSdg = []; originalSdg = [];
originalFos = []; originalFos = [];
displayedAllSubjects = []; displayedAllSubjects = [];
displayedSubjects = []; displayedSubjects = [];
displayedSdg = []; displayedSdg = [];
displayedFos = []; displayedFos = [];
groupedAllSubjects = []; groupedAllSubjects = [];
groupedSubjects = []; groupedSubjects = [];
groupedSdg = []; groupedSdg = [];
groupedFos = []; groupedFos = [];
subjectsColumns = []; subjectsColumns = [];
subjectsLimit: number = 6; subjectsLimit: number = 6;
maxCharacters: number = 25; maxCharacters: number = 25;
activeTab: 'all' | 'freeText' | 'sdg' | 'fos' = 'all'; activeTab: 'all' | 'freeText' | 'sdg' | 'fos' = 'all';
indexAll: number = 0; indexAll: number = 0;
indexSubjects: number = 0; indexSubjects: number = 0;
indexSdg: number = 0; indexSdg: number = 0;
indexFos: number = 0; indexFos: number = 0;
@ViewChild('addSubjectsInput') addSubjectsInput: InputComponent; @ViewChild('addSubjectsInput') addSubjectsInput: InputComponent;
@ViewChild('editModal') editModal: AlertModal; @ViewChild('editModal') editModal: AlertModal;
@ViewChild('deleteModal') deleteModal: AlertModal; @ViewChild('deleteModal') deleteModal: AlertModal;
@ViewChild('fsModal', {static: true}) fullscreen: FullScreenModalComponent; @ViewChild('fsModal', {static: true}) fullscreen: FullScreenModalComponent;
@ViewChild('modalTabs') modalTabs: ElementRef; @ViewChild('modalTabs') modalTabs: ElementRef;
@ViewChild('sdgSelection') sdgSelection: SdgSelectionComponent; @ViewChild('sdgSelection') sdgSelection: SdgSelectionComponent;
@ViewChild('fosSelection') fosSelection: FosSelectionComponent; @ViewChild('fosSelection') fosSelection: FosSelectionComponent;
onEnter: () => void = () => { onEnter: () => void = () => {
this.addSubjectsIntoList(); this.addSubjectsIntoList();
} }
constructor(private route: ActivatedRoute, constructor(private route: ActivatedRoute,
public _fb: UntypedFormBuilder, public _fb: UntypedFormBuilder,
private title: Title, private title: Title,
private _communityService: CommunityService, private _communityService: CommunityService,
private _subjectsService: SubjectsService, private _subjectsService: SubjectsService,
private _clearCacheService: ClearCacheService) { private _clearCacheService: ClearCacheService) {
} }
ngOnInit() { ngOnInit() {
this.filterForm = this._fb.control(''); this.filterForm = this._fb.control('');
this.subscriptions.push(this.filterForm.valueChanges.subscribe(value => { this.subscriptions.push(this.filterForm.valueChanges.subscribe(value => {
this.displayedAllSubjects = this.originalAllSubjects.filter(subject => { this.displayedAllSubjects = this.originalAllSubjects.filter(subject => {
return !value || subject.toLowerCase().indexOf(value.toLowerCase()) != -1 return !value || subject.toLowerCase().indexOf(value.toLowerCase()) != -1
});
this.displayedSubjects = this.originalSubjects.filter(subject => {
return !value || subject.toLowerCase().indexOf(value.toLowerCase()) != -1
});
this.displayedSdg = this.originalSdg.filter(subject => {
return !value || subject.toLowerCase().indexOf(value.toLowerCase()) != -1
});
this.displayedFos = this.originalFos.filter(subject => {
return !value || subject.toLowerCase().indexOf(value.toLowerCase()) != -1
});
if(this.activeTab === 'all') {
this.groupSubjects(this.displayedAllSubjects, 'all');
} else if(this.activeTab === 'freeText') {
this.groupSubjects(this.displayedSubjects, 'freeText');
} else if(this.activeTab === 'sdg') {
this.groupSubjects(this.displayedSdg, 'sdg');
} else if(this.activeTab === 'fos') {
this.groupSubjects(this.displayedFos, 'fos');
}
}));
this.subscriptions.push(this.route.params.subscribe(
params => {
this.communityId = params['community'];
this.title.setTitle(this.communityId.toUpperCase() + ' | Subjects');
this.loading = true;
this.subscriptions.push(this._communityService.getCommunityAsObservable().subscribe(
community => {
this.community = community;
this.params = {
community: encodeURIComponent(
'"' + community.queryId + '"')
};
this.community.subjects.sort((n1,n2)=> {
if (n1.toLowerCase() > n2.toLowerCase()) {
return 1;
}
if (n1.toLowerCase() < n2.toLowerCase()) {
return -1;
}
return 0;
}); });
this.displayedSubjects = HelperFunctions.copy(this.community.subjects); this.displayedSubjects = this.originalSubjects.filter(subject => {
this.displayedSdg = this.community.sdg; return !value || subject.toLowerCase().indexOf(value.toLowerCase()) != -1
this.displayedFos = this.community.fos; });
this.displayedAllSubjects = [...this.displayedSubjects, ...this.displayedSdg, ...this.displayedFos]; this.displayedSdg = this.originalSdg.filter(subject => {
this.groupSubjects(this.displayedAllSubjects, 'all'); return !value || subject.toLowerCase().indexOf(value.toLowerCase()) != -1
this.originalAllSubjects = HelperFunctions.copy(this.displayedAllSubjects); });
this.originalSubjects = HelperFunctions.copy(this.displayedSubjects); this.displayedFos = this.originalFos.filter(subject => {
this.originalSdg = HelperFunctions.copy(this.displayedSdg); return !value || subject.toLowerCase().indexOf(value.toLowerCase()) != -1
this.originalFos = HelperFunctions.copy(this.displayedFos); });
this.loading = false; if (this.activeTab === 'all') {
}, this.groupSubjects(this.displayedAllSubjects, 'all');
error => this.handleUpdateError('System error retrieving community profile', error) } else if (this.activeTab === 'freeText') {
)); this.groupSubjects(this.displayedSubjects, 'freeText');
})); } else if (this.activeTab === 'sdg') {
this.groupSubjects(this.displayedSdg, 'sdg');
} else if (this.activeTab === 'fos') {
this.groupSubjects(this.displayedFos, 'fos');
}
}));
this.subscriptions.push(this.route.params.subscribe(
params => {
this.communityId = params['community'];
this.title.setTitle(this.communityId.toUpperCase() + ' | Subjects');
this.loading = true;
this.subscriptions.push(this._communityService.getCommunityAsObservable().subscribe(
community => {
this.community = community;
this.params = {
community: encodeURIComponent(
'"' + community.queryId + '"')
};
this.community.subjects.sort((n1, n2) => {
if (n1.toLowerCase() > n2.toLowerCase()) {
return 1;
}
if (n1.toLowerCase() < n2.toLowerCase()) {
return -1;
}
return 0;
});
this.displayedSubjects = HelperFunctions.copy(this.community.subjects);
this.displayedSdg = this.community.sdg;
this.displayedFos = this.community.fos;
this.displayedAllSubjects = [...this.displayedSubjects, ...this.displayedSdg, ...this.displayedFos];
this.groupSubjects(this.displayedAllSubjects, 'all');
this.originalAllSubjects = HelperFunctions.copy(this.displayedAllSubjects);
this.originalSubjects = HelperFunctions.copy(this.displayedSubjects);
this.originalSdg = HelperFunctions.copy(this.displayedSdg);
this.originalFos = HelperFunctions.copy(this.displayedFos);
this.loading = false;
},
error => this.handleUpdateError('System error retrieving community profile', error)
));
}));
}
ngOnDestroy() {
this.subscriptions.forEach(subscription => {
if (subscription instanceof Subscription) {
subscription.unsubscribe();
}
});
}
public groupSubjects(subjects: string[], type: string) {
if(subjects.length === 0) {
return [];
}
subjects.sort((a, b) => a.localeCompare(b));
if(type === 'all') {
this.indexAll = 0;
this.activeTab = 'all';
this.groupedAllSubjects = Object.values(
subjects.reduce((acc, subject) => {
let firstLetter = subject[0].toLocaleUpperCase();
if(!acc[firstLetter]) {
acc[firstLetter] = {group: firstLetter, data: [subject]};
} else {
acc[firstLetter].data.push(subject);
}
return acc;
},{})
)
if(subjects.length > 1) {
this.groupedAllSubjects.unshift({group: 'All', data: subjects});
}
}
if(type === 'freeText') {
this.indexSubjects = 0;
this.activeTab = 'freeText';
this.groupedSubjects = Object.values(
subjects.reduce((acc, subject) => {
let firstLetter = subject[0].toLocaleUpperCase();
if(!acc[firstLetter]) {
acc[firstLetter] = {group: firstLetter, data: [subject]};
} else {
acc[firstLetter].data.push(subject);
}
return acc;
},{})
)
if(subjects.length > 1) {
this.groupedSubjects.unshift({group: 'All', data: subjects});
}
}
if(type === 'sdg') {
this.indexSdg = 0;
this.activeTab = 'sdg';
this.groupedSdg = Object.values(
subjects.reduce((acc, subject) => {
let firstLetter = subject[0].toLocaleUpperCase();
if(!acc[firstLetter]) {
acc[firstLetter] = {group: firstLetter, data: [subject]};
} else {
acc[firstLetter].data.push(subject);
}
return acc;
},{})
)
if(subjects.length > 1) {
this.groupedSdg.unshift({group: 'All', data: subjects});
}
}
if(type === 'fos') {
this.indexFos = 0;
this.activeTab = 'fos';
this.groupedFos = Object.values(
subjects.reduce((acc, subject) => {
let key = subject.substring(0,2).toLocaleUpperCase();
if(!acc[key]) {
acc[key] = {group: key, data: [subject]};
} else {
acc[key].data.push(subject);
}
return acc;
},{})
)
if(subjects.length > 1) {
this.groupedFos.unshift({group: 'All', data: subjects});
}
}
}
public changeDisplayedSubjects(i, group) {
this.subjectsColumns = [];
if(this.activeTab === 'all') {
this.indexAll = i;
} else if(this.activeTab === 'freeText') {
this.indexSubjects = i;
} else if(this.activeTab === 'sdg') {
this.indexSdg = i;
} else if(this.activeTab === 'fos') {
this.indexFos = i;
} }
if(group.data.length > this.subjectsLimit && group.group != 'All') {
this.divideSubjects(group);
}
}
public divideSubjects(group) { ngOnDestroy() {
let columns = []; this.subscriptions.forEach(subscription => {
for(let i = 0; i < (group.data.length / this.subjectsLimit); i++) { if (subscription instanceof Subscription) {
columns.push(group.data.slice(i * this.subjectsLimit, ((i + 1) * this.subjectsLimit))); subscription.unsubscribe();
} }
this.subjectsColumns = columns; });
}
public getSubjectsExistOnlyInFirst(firstArray: string[], secondArray: string[]): string[] {
const difference = [];
for (let i = 0; i < firstArray.length; i++) {
if (secondArray.indexOf(firstArray[i]) === -1) {
difference.push(firstArray[i]);
}
} }
return difference;
}
public editAllSubjects() { public groupSubjects(subjects: string[], type: string) {
UIkit.tab(this.modalTabs.nativeElement).show(0); if (subjects.length === 0) {
this.displayedSubjects = HelperFunctions.copy(this.community.subjects); return [];
this.sdgSelection.reset(); }
this.sdgSelection.sdgs.markAsPristine(); subjects.sort((a, b) => a.localeCompare(b));
this.fosSelection.reset(); if (type === 'all') {
this.fosSelection.fosOptions.markAsPristine(); this.indexAll = 0;
this.fullscreen.title = "Add/Edit Subjects"; this.activeTab = 'all';
this.fullscreen.okButtonText = "Save"; this.groupedAllSubjects = Object.values(
this.fullscreen.back = true; subjects.reduce((acc, subject) => {
this.fullscreen.okButton = true; let firstLetter = subject[0].toLocaleUpperCase();
this.fullscreen.open(); if (!acc[firstLetter]) {
} acc[firstLetter] = {group: firstLetter, data: [subject]};
} else {
public addSubjectsIntoList() { acc[firstLetter].data.push(subject);
if(this.addSubjectsInput.searchControl.getRawValue()) { }
this.addSubjectsInput.add(null, true); return acc;
} }, {})
this.subjectsForm.getRawValue().forEach(element => { )
this.displayedSubjects.unshift(element); if (subjects.length > 1) {
}); this.groupedAllSubjects.unshift({group: 'All', data: subjects});
this.subjectsForm.clear(); }
this.hasChanges = true; }
} if (type === 'freeText') {
this.indexSubjects = 0;
public removeSubjectFromList(i) { this.activeTab = 'freeText';
this.displayedSubjects.splice(i, 1); this.groupedSubjects = Object.values(
this.hasChanges = true; subjects.reduce((acc, subject) => {
} let firstLetter = subject[0].toLocaleUpperCase();
if (!acc[firstLetter]) {
public saveAllSubjects() { acc[firstLetter] = {group: firstLetter, data: [subject]};
let selectedSdg = this.sdgSelection.getSelectedSubjects().map(a => a.id); } else {
let selectedFos = this.fosSelection.getSelectedSubjects().map(a => a.id); acc[firstLetter].data.push(subject);
let requests = [ }
...this.saveSubjects(this.community.subjects, this.displayedSubjects, 'subjects'), return acc;
...this.saveSubjects(this.community.sdg, selectedSdg, 'sdg'), }, {})
...this.saveSubjects(this.community.fos, selectedFos, 'fos') )
]; if (subjects.length > 1) {
this.subscriptions.push(forkJoin(requests).subscribe(res => { this.groupedSubjects.unshift({group: 'All', data: subjects});
this.afterUpdateActions(res, "updated"); }
})); }
} if (type === 'sdg') {
this.indexSdg = 0;
public saveSubjects(subjects, displayedSubjects, type: string) { this.activeTab = 'sdg';
if (this.communityId != null && this.communityId !== '') { this.groupedSdg = Object.values(
this.loading = true; subjects.reduce((acc, subject) => {
const subjectsToDeleteAr = this.getSubjectsExistOnlyInFirst(subjects, displayedSubjects); let firstLetter = subject[0].toLocaleUpperCase();
const subjectsToAddAr = this.getSubjectsExistOnlyInFirst(displayedSubjects, subjects); if (!acc[firstLetter]) {
const subjectsToDelete = this.getNonEmptyItems(subjectsToDeleteAr); acc[firstLetter] = {group: firstLetter, data: [subject]};
const subjectsToAdd = this.getNonEmptyItems(subjectsToAddAr); } else {
let requests = []; acc[firstLetter].data.push(subject);
if (subjectsToDelete.length > 0) { }
requests.push(this._subjectsService.removeSubjects( return acc;
this.properties.communityAPI + this.communityId + '/' + type, subjectsToDelete)) }, {})
} )
if (subjectsToAdd.length > 0) { if (subjects.length > 1) {
requests.push(this._subjectsService.addSubjects( this.groupedSdg.unshift({group: 'All', data: subjects});
this.properties.communityAPI + this.communityId + '/' + type, subjectsToAdd)) }
} }
return requests; if (type === 'fos') {
} this.indexFos = 0;
} this.activeTab = 'fos';
this.groupedFos = Object.values(
handleUpdateError(message: string, error = null) { subjects.reduce((acc, subject) => {
if(error) { let key = subject.substring(0, 2).toLocaleUpperCase();
console.error(error) if (!acc[key]) {
acc[key] = {group: key, data: [subject]};
} else {
acc[key].data.push(subject);
}
return acc;
}, {})
)
if (subjects.length > 1) {
this.groupedFos.unshift({group: 'All', data: subjects});
}
}
} }
NotificationHandler.rise(message, 'danger');
this.loading = false;
}
afterUpdateActions(response, message: string) { public changeDisplayedSubjects(i, group) {
response.forEach(res => { this.subjectsColumns = [];
if (res.subjects) { if (this.activeTab === 'all') {
this.community.subjects = res.subjects; this.indexAll = i;
} else if (res.sdg) { } else if (this.activeTab === 'freeText') {
this.community.sdg = res.sdg; this.indexSubjects = i;
} else if (res.fos) { } else if (this.activeTab === 'sdg') {
this.community.fos = res.fos; this.indexSdg = i;
} } else if (this.activeTab === 'fos') {
}); this.indexFos = i;
this._communityService.updateSubjects(this.community.subjects, this.community.fos, this.community.sdg); }
this._clearCacheService.purgeBrowserCache("Subjects "+message, this.communityId); if (group.data.length > this.subjectsLimit && group.group != 'All') {
NotificationHandler.rise('Subjects successfully ' + message + '!') this.divideSubjects(group);
this.loading = false; }
}
private getNonEmptyItems(data: string[]): string[] {
const length = data.length;
const arrayNonEmpty = new Array<string>();
let j = 0;
for (let i = 0; i < length; i++) {
if (this.isEmpty(data[i])) {
} else if (this.isNonEmpty(data[i])) {
arrayNonEmpty[j] = data[i];
j++;
}
} }
return arrayNonEmpty;
}
public divideSubjects(group) {
private isEmpty(data: string): boolean { let columns = [];
if (data !== undefined && !data.replace(/\s/g, '').length) { for (let i = 0; i < (group.data.length / this.subjectsLimit); i++) {
return true; columns.push(group.data.slice(i * this.subjectsLimit, ((i + 1) * this.subjectsLimit)));
} else { }
return false; this.subjectsColumns = columns;
} }
}
private isNonEmpty(data: string): boolean { public getSubjectsExistOnlyInFirst(firstArray: string[], secondArray: string[]): string[] {
if (data !== undefined && data != null) { const difference = [];
return true; for (let i = 0; i < firstArray.length; i++) {
} else { if (secondArray.indexOf(firstArray[i]) === -1) {
return false; difference.push(firstArray[i]);
}
}
return difference;
}
public editAllSubjects() {
UIkit.tab(this.modalTabs.nativeElement).show(0);
this.displayedSubjects = HelperFunctions.copy(this.community.subjects);
this.sdgSelection.reset();
this.sdgSelection.sdgs.markAsPristine();
this.fosSelection.reset();
this.fosSelection.fosOptions.markAsPristine();
this.fullscreen.title = "Add/Edit Subjects";
this.fullscreen.okButtonText = "Save";
this.fullscreen.back = true;
this.fullscreen.okButton = true;
this.fullscreen.open();
}
public addSubjectsIntoList() {
if (this.addSubjectsInput.searchControl.getRawValue()) {
this.addSubjectsInput.add(null, true);
}
this.subjectsForm.getRawValue().forEach(element => {
this.displayedSubjects.unshift(element);
});
this.subjectsForm.clear();
this.hasChanges = true;
}
public removeSubjectFromList(i) {
this.displayedSubjects.splice(i, 1);
this.hasChanges = true;
}
public saveAllSubjects() {
let selectedSdg = this.sdgSelection.getSelectedSubjects().map(a => a.id);
let selectedFos = this.fosSelection.getSelectedSubjects().map(a => a.id);
let requests = [
...this.saveSubjects(this.community.subjects, this.displayedSubjects, 'subjects'),
...this.saveSubjects(this.community.sdg, selectedSdg, 'sdg'),
...this.saveSubjects(this.community.fos, selectedFos, 'fos')
];
this.subscriptions.push(forkJoin(requests).subscribe(res => {
this.afterUpdateActions(res, "updated");
}));
}
public saveSubjects(subjects, displayedSubjects, type: string) {
if (this.communityId != null && this.communityId !== '') {
this.loading = true;
const subjectsToDeleteAr = this.getSubjectsExistOnlyInFirst(subjects, displayedSubjects);
const subjectsToAddAr = this.getSubjectsExistOnlyInFirst(displayedSubjects, subjects);
const subjectsToDelete = this.getNonEmptyItems(subjectsToDeleteAr);
const subjectsToAdd = this.getNonEmptyItems(subjectsToAddAr);
let requests = [];
if (subjectsToDelete.length > 0) {
requests.push(this._subjectsService.removeSubjects(
this.properties.communityAPI + this.communityId + '/' + type, subjectsToDelete))
}
if (subjectsToAdd.length > 0) {
requests.push(this._subjectsService.addSubjects(
this.properties.communityAPI + this.communityId + '/' + type, subjectsToAdd))
}
return requests;
}
}
handleUpdateError(message: string, error = null) {
if (error) {
console.error(error)
}
NotificationHandler.rise(message, 'danger');
this.loading = false;
}
afterUpdateActions(response, message: string) {
response.forEach(res => {
if (res.subjects) {
this.community.subjects = res.subjects;
} else if (res.sdg) {
this.community.sdg = res.sdg;
} else if (res.fos) {
this.community.fos = res.fos;
}
});
this._communityService.updateSubjects(this.community.subjects, this.community.fos, this.community.sdg);
this._clearCacheService.purgeBrowserCache("Subjects " + message, this.communityId);
NotificationHandler.rise('Subjects successfully ' + message + '!')
this.loading = false;
}
private getNonEmptyItems(data: string[]): string[] {
const length = data.length;
const arrayNonEmpty = new Array<string>();
let j = 0;
for (let i = 0; i < length; i++) {
if (this.isEmpty(data[i])) {
} else if (this.isNonEmpty(data[i])) {
arrayNonEmpty[j] = data[i];
j++;
}
}
return arrayNonEmpty;
}
private isEmpty(data: string): boolean {
if (data !== undefined && !data.replace(/\s/g, '').length) {
return true;
} else {
return false;
}
}
private isNonEmpty(data: string): boolean {
if (data !== undefined && data != null) {
return true;
} else {
return false;
}
} }
}
} }