2019-11-12 16:55:37 +01:00
|
|
|
import {Component, OnDestroy, OnInit, ViewChild} from '@angular/core';
|
2019-11-04 12:50:25 +01:00
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
|
|
|
import {Title} from '@angular/platform-browser';
|
|
|
|
import {EnvProperties} from '../openaireLibrary/utils/properties/env-properties';
|
2019-11-05 15:59:06 +01:00
|
|
|
import {Category, Stakeholder, SubCategory, Topic} from "../utils/entities/stakeholder";
|
2019-11-04 12:50:25 +01:00
|
|
|
import {StakeholderService} from "../services/stakeholder.service";
|
2019-11-11 14:28:36 +01:00
|
|
|
import {HelperFunctions} from "../openaireLibrary/utils/HelperFunctions.class";
|
2019-11-12 16:55:37 +01:00
|
|
|
import {AlertModal} from "../openaireLibrary/utils/modal/alert";
|
2019-12-06 14:52:26 +01:00
|
|
|
import {Subscriber, Subscription} from "rxjs";
|
2019-11-26 20:11:24 +01:00
|
|
|
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
|
2019-12-06 14:52:26 +01:00
|
|
|
import {IndicatorUtils, StakeholderUtils} from "../utils/indicator-utils";
|
2019-11-04 12:50:25 +01:00
|
|
|
|
|
|
|
declare var UIkit;
|
|
|
|
|
|
|
|
@Component({
|
2019-12-06 14:52:26 +01:00
|
|
|
selector: 'topic',
|
|
|
|
templateUrl: './topic.component.html',
|
2019-11-04 12:50:25 +01:00
|
|
|
})
|
|
|
|
export class TopicComponent implements OnInit, OnDestroy {
|
2019-12-06 14:52:26 +01:00
|
|
|
public subscriptions: any[] = [];
|
|
|
|
public properties: EnvProperties;
|
|
|
|
public indicatorUtils: IndicatorUtils = new IndicatorUtils();
|
|
|
|
public stakeholderUtils: StakeholderUtils = new StakeholderUtils();
|
|
|
|
public loading: boolean = true;
|
|
|
|
public stakeholder: Stakeholder;
|
|
|
|
/**
|
|
|
|
* Current topic
|
|
|
|
**/
|
|
|
|
public topicIndex: number = 0;
|
|
|
|
public topicFb: FormGroup;
|
2019-11-13 11:26:50 +01:00
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
/**
|
|
|
|
* categoryIndex: Current category to be edited, selectedCategoryIndex: selected on menu(opened)
|
|
|
|
*/
|
|
|
|
public categoryIndex: number = 0;
|
|
|
|
public selectedCategoryIndex: number = 0;
|
|
|
|
public categoryFb: FormGroup;
|
|
|
|
/**
|
|
|
|
* Current Subcategory to be edited
|
|
|
|
*/
|
|
|
|
public subCategoryIndex: number = 0;
|
|
|
|
public subcategoryFb: FormGroup;
|
|
|
|
/**
|
|
|
|
* Current drop element and index of topic, category or subcategory to be deleted.
|
|
|
|
*/
|
|
|
|
public element: any;
|
|
|
|
public index: number;
|
|
|
|
/**
|
|
|
|
* Check form validity
|
|
|
|
*/
|
|
|
|
public toggle: boolean = false;
|
2019-11-12 16:55:37 +01:00
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
@ViewChild('deleteTopicModal') deleteTopicModal: AlertModal;
|
|
|
|
@ViewChild('deleteCategoryModal') deleteCategoryModal: AlertModal;
|
|
|
|
@ViewChild('deleteSubcategoryModal') deleteSubcategoryModal: AlertModal;
|
2019-11-12 16:55:37 +01:00
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
constructor(
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private router: Router,
|
|
|
|
private title: Title,
|
|
|
|
private fb: FormBuilder,
|
|
|
|
private stakeholderService: StakeholderService) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public ngOnInit() {
|
|
|
|
this.route.data
|
|
|
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
|
|
|
this.properties = data.envSpecific;
|
|
|
|
let subscription: Subscription;
|
2019-12-06 16:46:38 +01:00
|
|
|
this.route.params.subscribe(params => {
|
2019-12-06 14:52:26 +01:00
|
|
|
if(subscription) {
|
|
|
|
subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
subscription = this.stakeholderService.getStakeholderAsObservable().subscribe(stakeholder => {
|
|
|
|
if (stakeholder) {
|
|
|
|
this.stakeholder = HelperFunctions.copy(stakeholder);
|
|
|
|
this.topicIndex = this.stakeholder.topics.findIndex(topic => topic.alias === params['topic']);
|
|
|
|
if (this.topicIndex === -1) {
|
|
|
|
this.navigateToError();
|
|
|
|
} else {
|
|
|
|
this.title.setTitle(stakeholder.index_shortName + ' | ' + this.stakeholder.topics[this.topicIndex].name);
|
|
|
|
this.toggle = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-12-06 16:46:38 +01:00
|
|
|
this.subscriptions.push(subscription);
|
|
|
|
});
|
2019-12-06 14:52:26 +01:00
|
|
|
});
|
|
|
|
}
|
2019-11-04 12:50:25 +01:00
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
public ngOnDestroy() {
|
|
|
|
this.subscriptions.forEach(value => {
|
|
|
|
if (value instanceof Subscriber) {
|
|
|
|
value.unsubscribe();
|
2019-11-04 12:50:25 +01:00
|
|
|
}
|
|
|
|
});
|
2019-12-06 14:52:26 +01:00
|
|
|
}
|
2019-11-04 12:50:25 +01:00
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
public hide(element) {
|
|
|
|
UIkit.drop(element).hide();
|
|
|
|
}
|
2019-11-04 12:50:25 +01:00
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
public show(element) {
|
|
|
|
UIkit.drop(element).show();
|
|
|
|
}
|
2019-11-26 20:11:24 +01:00
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
private buildTopic(topic: Topic) {
|
|
|
|
let topics = this.stakeholder.topics.filter(element => element._id !== topic._id);
|
|
|
|
this.topicFb = this.fb.group({
|
|
|
|
_id: this.fb.control(topic._id),
|
|
|
|
name: this.fb.control(topic.name, Validators.required),
|
|
|
|
description: this.fb.control(topic.description),
|
|
|
|
alias: this.fb.control(topic.alias, [
|
|
|
|
Validators.required,
|
|
|
|
this.stakeholderUtils.aliasValidator(topics)
|
|
|
|
]
|
|
|
|
),
|
|
|
|
isActive: this.fb.control(topic.isActive),
|
|
|
|
isPublic: this.fb.control(topic.isPublic),
|
|
|
|
isDefault: this.fb.control(topic.isDefault),
|
|
|
|
categories: this.fb.control(topic.categories)
|
|
|
|
});
|
|
|
|
this.subscriptions.push(this.topicFb.get('name').valueChanges.subscribe(value => {
|
|
|
|
let i = 1;
|
|
|
|
value = this.stakeholderUtils.generateAlias(value);
|
|
|
|
this.topicFb.controls['alias'].setValue(value);
|
|
|
|
while (this.topicFb.get('alias').invalid) {
|
|
|
|
this.topicFb.controls['alias'].setValue(value + i);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}));
|
2019-11-13 11:26:50 +01:00
|
|
|
}
|
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
public editTopicOpen(element) {
|
|
|
|
if (element.className.indexOf('uk-open') !== -1) {
|
|
|
|
this.hide(element);
|
|
|
|
} else {
|
|
|
|
this.buildTopic(this.stakeholder.topics[this.topicIndex]);
|
|
|
|
this.show(element);
|
|
|
|
}
|
2019-11-13 11:26:50 +01:00
|
|
|
}
|
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
public saveTopic(element) {
|
|
|
|
if (!this.topicFb.invalid) {
|
|
|
|
let path = [this.stakeholder._id];
|
|
|
|
let callback = (topic: Topic): void => {
|
|
|
|
this.stakeholder.topics[this.topicIndex] = topic;
|
|
|
|
this.stakeholderService.setStakeholder(this.stakeholder);
|
|
|
|
};
|
|
|
|
this.save('Topic has been successfully saved', element, path, this.topicFb.value, callback, true);
|
|
|
|
}
|
|
|
|
}
|
2019-11-13 11:26:50 +01:00
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
public deleteTopicOpen(name: string, element) {
|
|
|
|
this.deleteOpen(name, 'topic', this.deleteTopicModal, element, this.topicIndex);
|
|
|
|
}
|
2019-11-13 11:26:50 +01:00
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
public deleteTopic() {
|
|
|
|
let path: string[] = [
|
|
|
|
this.stakeholder._id,
|
|
|
|
this.stakeholder.topics[this.topicIndex]._id
|
|
|
|
];
|
|
|
|
let callback = (): void => {
|
|
|
|
this.stakeholder.topics.splice(this.topicIndex, 1);
|
|
|
|
this.stakeholderService.setStakeholder(this.stakeholder);
|
|
|
|
};
|
|
|
|
this.delete('Topic has been successfully be deleted', path, callback, true);
|
2019-11-05 15:59:06 +01:00
|
|
|
}
|
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
public toggleCategory(index: number) {
|
|
|
|
if (this.selectedCategoryIndex !== index) {
|
|
|
|
this.selectedCategoryIndex = index;
|
|
|
|
this.toggle = true;
|
|
|
|
} else {
|
|
|
|
this.toggle = !this.toggle;
|
|
|
|
}
|
|
|
|
}
|
2019-11-26 20:11:24 +01:00
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
private buildCategory(category: Category) {
|
|
|
|
let categories = this.stakeholder.topics[this.topicIndex].categories.filter(element => element._id !== category._id);
|
|
|
|
this.categoryFb = this.fb.group({
|
|
|
|
_id: this.fb.control(category._id),
|
|
|
|
name: this.fb.control(category.name, Validators.required),
|
|
|
|
description: this.fb.control(category.description),
|
|
|
|
alias: this.fb.control(category.alias, [
|
|
|
|
Validators.required,
|
|
|
|
this.stakeholderUtils.aliasValidator(categories)
|
|
|
|
]
|
|
|
|
),
|
|
|
|
isActive: this.fb.control(category.isActive),
|
|
|
|
isPublic: this.fb.control(category.isPublic),
|
|
|
|
isDefault: this.fb.control(category.isDefault),
|
|
|
|
subCategories: this.fb.control(category.subCategories)
|
|
|
|
});
|
|
|
|
this.subscriptions.push(this.categoryFb.get('name').valueChanges.subscribe(value => {
|
|
|
|
let i = 1;
|
|
|
|
value = this.stakeholderUtils.generateAlias(value);
|
|
|
|
this.categoryFb.controls['alias'].setValue(value);
|
|
|
|
while (this.categoryFb.get('alias').invalid) {
|
|
|
|
this.categoryFb.controls['alias'].setValue(value + i);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}));
|
2019-11-05 15:59:06 +01:00
|
|
|
}
|
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
public editCategoryOpen(element, index: number = -1) {
|
|
|
|
if (element.className.indexOf('uk-open') !== -1) {
|
|
|
|
this.hide(element);
|
2019-11-24 17:30:04 +01:00
|
|
|
} else {
|
2019-12-06 14:52:26 +01:00
|
|
|
if (index === -1) {
|
|
|
|
this.buildCategory(new Category(null, null, null, true, true));
|
|
|
|
} else {
|
|
|
|
this.buildCategory(this.stakeholder.topics[this.topicIndex].categories[index]);
|
|
|
|
}
|
|
|
|
this.show(element);
|
2019-11-24 17:30:04 +01:00
|
|
|
}
|
2019-11-05 15:59:06 +01:00
|
|
|
}
|
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
public saveCategory(element, index = -1) {
|
|
|
|
if (!this.categoryFb.invalid) {
|
|
|
|
let path = [this.stakeholder._id, this.stakeholder.topics[this.topicIndex]._id];
|
|
|
|
let callback = (category: Category): void => {
|
|
|
|
if (index === -1) {
|
|
|
|
this.stakeholder.topics[this.topicIndex].categories.push(category);
|
|
|
|
} else {
|
|
|
|
this.stakeholder.topics[this.topicIndex].categories[index] = HelperFunctions.copy(category);
|
|
|
|
}
|
|
|
|
this.stakeholderService.setStakeholder(this.stakeholder);
|
|
|
|
};
|
|
|
|
if (index === -1) {
|
|
|
|
this.save('Category has been successfully created', element, path, this.categoryFb.value, callback);
|
|
|
|
} else {
|
|
|
|
this.save('Category has been successfully saved', element, path, this.categoryFb.value, callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-12 16:55:37 +01:00
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
public deleteCategoryOpen(name: string, element, index) {
|
|
|
|
this.deleteOpen(name, 'category', this.deleteCategoryModal, element, index);
|
|
|
|
}
|
2019-11-12 16:55:37 +01:00
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
public deleteCategory() {
|
|
|
|
let path: string[] = [
|
|
|
|
this.stakeholder._id,
|
|
|
|
this.stakeholder.topics[this.topicIndex]._id,
|
|
|
|
this.stakeholder.topics[this.topicIndex].categories[this.index]._id
|
|
|
|
];
|
|
|
|
let callback = (): void => {
|
|
|
|
this.stakeholder.topics[this.topicIndex].categories.splice(this.index, 1);
|
|
|
|
this.stakeholderService.setStakeholder(this.stakeholder);
|
|
|
|
};
|
|
|
|
this.delete('Category has been successfully be deleted', path, callback);
|
|
|
|
}
|
2019-11-26 20:11:24 +01:00
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
private buildSubcategory(subCategory: SubCategory) {
|
|
|
|
let subCategories = this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories.filter(element => element._id !== subCategory._id);
|
|
|
|
this.subcategoryFb = this.fb.group({
|
|
|
|
_id: this.fb.control(subCategory._id),
|
|
|
|
name: this.fb.control(subCategory.name, Validators.required),
|
|
|
|
description: this.fb.control(subCategory.description),
|
|
|
|
alias: this.fb.control(subCategory.alias, [
|
|
|
|
Validators.required,
|
|
|
|
this.stakeholderUtils.aliasValidator(subCategories)
|
|
|
|
]
|
|
|
|
),
|
|
|
|
isActive: this.fb.control(subCategory.isActive),
|
|
|
|
isPublic: this.fb.control(subCategory.isPublic),
|
|
|
|
isDefault: this.fb.control(subCategory.isDefault),
|
|
|
|
charts: this.fb.control(subCategory.charts),
|
|
|
|
numbers: this.fb.control(subCategory.numbers)
|
|
|
|
});
|
|
|
|
this.subscriptions.push(this.subcategoryFb.get('name').valueChanges.subscribe(value => {
|
|
|
|
let i = 1;
|
|
|
|
value = this.stakeholderUtils.generateAlias(value);
|
|
|
|
this.subcategoryFb.controls['alias'].setValue(value);
|
|
|
|
while (this.subcategoryFb.get('alias').invalid) {
|
|
|
|
this.subcategoryFb.controls['alias'].setValue(value + i);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}));
|
2019-11-05 15:59:06 +01:00
|
|
|
}
|
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
public editSubCategoryOpen(element, index: number = -1) {
|
|
|
|
if (element.className.indexOf('uk-open') !== -1) {
|
|
|
|
this.hide(element);
|
2019-11-24 17:30:04 +01:00
|
|
|
} else {
|
2019-12-06 14:52:26 +01:00
|
|
|
if (index === -1) {
|
|
|
|
this.buildSubcategory(new SubCategory(null, null, null, true, true));
|
|
|
|
} else {
|
|
|
|
this.buildSubcategory(this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories[index]);
|
|
|
|
}
|
|
|
|
this.show(element);
|
2019-11-24 17:30:04 +01:00
|
|
|
}
|
2019-11-05 15:59:06 +01:00
|
|
|
}
|
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
public saveSubCategory(element, index = -1) {
|
|
|
|
if (!this.subcategoryFb.invalid) {
|
|
|
|
let path: string[] = [
|
|
|
|
this.stakeholder._id,
|
|
|
|
this.stakeholder.topics[this.topicIndex]._id,
|
|
|
|
this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex]._id,
|
|
|
|
];
|
|
|
|
let callback = (subCategory: SubCategory): void => {
|
|
|
|
if (index === -1) {
|
|
|
|
this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories.push(subCategory);
|
|
|
|
} else {
|
|
|
|
this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories[index] = subCategory;
|
|
|
|
}
|
|
|
|
this.stakeholderService.setStakeholder(this.stakeholder);
|
|
|
|
};
|
|
|
|
if (index === -1) {
|
|
|
|
this.save('Subcategory has been successfully created', element, path, this.subcategoryFb.value, callback);
|
|
|
|
} else {
|
|
|
|
this.save('Subcategory has been successfully saved', element, path, this.subcategoryFb.value, callback);
|
|
|
|
}
|
|
|
|
this.hide(element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public deleteSubcategoryOpen(name: string, element, index) {
|
|
|
|
this.deleteOpen(name, 'subcategory', this.deleteSubcategoryModal, element, index);
|
|
|
|
}
|
2019-11-12 16:55:37 +01:00
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
public deleteSubcategory() {
|
|
|
|
let path: string[] = [
|
|
|
|
this.stakeholder._id,
|
|
|
|
this.stakeholder.topics[this.topicIndex]._id,
|
|
|
|
this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex]._id,
|
|
|
|
this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories[this.index]._id
|
|
|
|
];
|
|
|
|
let callback = (): void => {
|
|
|
|
this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories.splice(this.index, 1);
|
|
|
|
this.stakeholderService.setStakeholder(this.stakeholder);
|
|
|
|
};
|
|
|
|
this.delete('Subcategory has been successfully be deleted', path, callback);
|
|
|
|
}
|
2019-11-05 15:59:06 +01:00
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
private navigateToError() {
|
|
|
|
this.router.navigate(['/error'], {queryParams: {'page': this.router.url}});
|
|
|
|
}
|
2019-11-05 15:59:06 +01:00
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
private deleteOpen(name: string, type: string, modal: AlertModal, element, index) {
|
|
|
|
this.element = element;
|
|
|
|
this.index = index;
|
|
|
|
modal.alertTitle = 'Delete ' + name;
|
|
|
|
modal.cancelButtonText = 'No';
|
|
|
|
modal.okButtonText = 'Yes';
|
|
|
|
modal.message = 'This ' + type + ' will permanently be deleted. Are you sure you want to proceed?';
|
|
|
|
modal.open();
|
|
|
|
}
|
2019-11-12 16:55:37 +01:00
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
private save(message: string, element, path: string[], saveElement: any, callback: Function, redirect = false) {
|
|
|
|
this.stakeholderService.saveElement(this.properties.monitorServiceAPIURL, saveElement, path).subscribe(saveElement => {
|
|
|
|
callback(saveElement);
|
|
|
|
UIkit.notification(message, {
|
|
|
|
status: 'success',
|
|
|
|
timeout: 3000,
|
|
|
|
pos: 'top-left'
|
|
|
|
});
|
|
|
|
if (redirect) {
|
|
|
|
this.router.navigate(['../' + saveElement.alias], {
|
|
|
|
relativeTo: this.route
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.hide(element);
|
|
|
|
}, error => {
|
|
|
|
UIkit.notification(error.error.message, {
|
|
|
|
status: 'danger',
|
|
|
|
timeout: 3000,
|
|
|
|
pos: 'top-left'
|
|
|
|
});
|
|
|
|
this.hide(element);
|
2019-11-12 16:55:37 +01:00
|
|
|
});
|
2019-12-06 14:52:26 +01:00
|
|
|
}
|
2019-11-08 16:47:47 +01:00
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
private delete(message: string, path: string[], callback: Function, redirect = false) {
|
|
|
|
this.stakeholderService.deleteElement(this.properties.monitorServiceAPIURL, path).subscribe(() => {
|
|
|
|
callback();
|
|
|
|
UIkit.notification(message, {
|
|
|
|
status: 'success',
|
|
|
|
timeout: 3000,
|
|
|
|
pos: 'top-left'
|
|
|
|
});
|
|
|
|
if (redirect) {
|
|
|
|
this.back();
|
|
|
|
}
|
|
|
|
this.hide(this.element);
|
|
|
|
}, error => {
|
|
|
|
UIkit.notification(error.error.message, {
|
|
|
|
status: 'danger',
|
|
|
|
timeout: 3000,
|
|
|
|
pos: 'top-left'
|
|
|
|
});
|
|
|
|
this.hide(this.element);
|
|
|
|
});
|
|
|
|
}
|
2019-11-13 11:26:50 +01:00
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
back() {
|
|
|
|
this.router.navigate(['../'], {
|
|
|
|
relativeTo: this.route
|
|
|
|
});
|
|
|
|
}
|
2019-11-08 16:47:47 +01:00
|
|
|
|
2019-12-06 14:52:26 +01:00
|
|
|
chooseSubcategory(categoryIndex: number, subcategoryIndex: number) {
|
|
|
|
this.categoryIndex = categoryIndex;
|
|
|
|
this.subCategoryIndex = subcategoryIndex;
|
|
|
|
}
|
2019-11-04 12:50:25 +01:00
|
|
|
}
|