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 {SideBarService} from "../library/sharedComponents/sidebar/sideBar.service";
|
|
|
|
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-11-13 15:19:04 +01:00
|
|
|
import {Subscriber} from "rxjs";
|
2019-11-04 12:50:25 +01:00
|
|
|
|
|
|
|
declare var UIkit;
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'topic',
|
|
|
|
templateUrl: './topic.component.html',
|
|
|
|
})
|
|
|
|
export class TopicComponent implements OnInit, OnDestroy {
|
2019-11-13 15:19:04 +01:00
|
|
|
public subscriptions: any[] = [];
|
2019-11-08 16:47:47 +01:00
|
|
|
public properties: EnvProperties;
|
2019-11-04 12:50:25 +01:00
|
|
|
public loading: boolean = true;
|
|
|
|
public stakeholder: Stakeholder;
|
2019-11-13 11:26:50 +01:00
|
|
|
/**
|
|
|
|
* Current topic
|
|
|
|
**/
|
2019-11-13 15:19:04 +01:00
|
|
|
public topicIndex: number = 0;
|
2019-11-05 15:59:06 +01:00
|
|
|
public topic: Topic = null;
|
2019-11-13 11:26:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* categoryIndex: Current category to be edited, selectedCategoryIndex: selected on menu(opened)
|
|
|
|
*/
|
2019-11-13 15:19:04 +01:00
|
|
|
public categoryIndex: number = 0;
|
|
|
|
public selectedCategoryIndex: number = 0;
|
2019-11-05 15:59:06 +01:00
|
|
|
public copyCategory: Category = null;
|
2019-11-13 11:26:50 +01:00
|
|
|
/**
|
|
|
|
* Current Subcategory to be edited
|
|
|
|
*/
|
2019-11-13 15:19:04 +01:00
|
|
|
public subCategoryIndex: number = 0;
|
2019-11-05 15:59:06 +01:00
|
|
|
public copySubCategory: SubCategory = null;
|
2019-11-13 11:26:50 +01:00
|
|
|
/**
|
|
|
|
* Current drop element and index of topic, category or subcategory to be deleted.
|
|
|
|
*/
|
2019-11-12 16:55:37 +01:00
|
|
|
public element: any;
|
|
|
|
public index: number;
|
2019-11-13 11:26:50 +01:00
|
|
|
/**
|
|
|
|
* Check form validity
|
|
|
|
*/
|
|
|
|
public valid = true;
|
|
|
|
public toggle: boolean = false;
|
2019-11-12 16:55:37 +01:00
|
|
|
|
|
|
|
@ViewChild('deleteTopicModal') deleteTopicModal: AlertModal;
|
|
|
|
@ViewChild('deleteCategoryModal') deleteCategoryModal: AlertModal;
|
|
|
|
@ViewChild('deleteSubcategoryModal') deleteSubcategoryModal: AlertModal;
|
|
|
|
|
2019-11-04 12:50:25 +01:00
|
|
|
constructor(
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private router: Router,
|
|
|
|
private title: Title,
|
|
|
|
private sideBarService: SideBarService,
|
|
|
|
private stakeholderService: StakeholderService) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public ngOnInit() {
|
|
|
|
this.route.data
|
|
|
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
|
|
|
this.properties = data.envSpecific;
|
|
|
|
this.route.params.subscribe( params => {
|
2019-11-13 15:19:04 +01:00
|
|
|
this.subscriptions.push(this.stakeholderService.getStakeholderAsObservable().subscribe(stakeholder => {
|
2019-11-04 12:50:25 +01:00
|
|
|
if (stakeholder) {
|
2019-11-11 14:28:36 +01:00
|
|
|
this.stakeholder = HelperFunctions.copy(stakeholder);
|
2019-11-05 15:59:06 +01:00
|
|
|
this.topicIndex = this.stakeholder.topics.findIndex(topic => topic.alias === params['topic']);
|
|
|
|
if(this.topicIndex === -1) {
|
2019-11-04 12:50:25 +01:00
|
|
|
this.navigateToError();
|
2019-11-05 15:59:06 +01:00
|
|
|
} else {
|
2019-11-14 13:42:41 +01:00
|
|
|
this.title.setTitle(stakeholder.index_shortName + ' | ' + this.stakeholder.topics[this.topicIndex].name);
|
2019-11-08 16:47:47 +01:00
|
|
|
this.toggle = true;
|
2019-11-04 12:50:25 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-13 15:19:04 +01:00
|
|
|
}));
|
2019-11-04 12:50:25 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public ngOnDestroy() {
|
2019-11-13 15:19:04 +01:00
|
|
|
this.subscriptions.forEach( value => {
|
|
|
|
if(value instanceof Subscriber) {
|
|
|
|
value.unsubscribe();
|
|
|
|
}
|
|
|
|
});
|
2019-11-04 12:50:25 +01:00
|
|
|
}
|
|
|
|
|
2019-11-05 15:59:06 +01:00
|
|
|
public hide(element) {
|
2019-11-14 16:20:51 +01:00
|
|
|
this.valid = true;
|
2019-11-05 15:59:06 +01:00
|
|
|
UIkit.drop(element).hide();
|
2019-11-04 12:50:25 +01:00
|
|
|
}
|
|
|
|
|
2019-11-05 15:59:06 +01:00
|
|
|
public show(element) {
|
|
|
|
UIkit.drop(element).show();
|
2019-11-04 12:50:25 +01:00
|
|
|
}
|
|
|
|
|
2019-11-13 11:26:50 +01:00
|
|
|
public editTopicOpen(element) {
|
|
|
|
if(element.className.indexOf('uk-open') !== -1) {
|
|
|
|
this.hide(element);
|
|
|
|
} else {
|
|
|
|
this.topic = HelperFunctions.copy(this.stakeholder.topics[this.topicIndex]);
|
|
|
|
this.show(element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public saveTopic(element) {
|
2019-11-13 15:19:04 +01:00
|
|
|
if(this.topic.name && this.topic.name !== '') {
|
2019-11-13 11:26:50 +01:00
|
|
|
if(!this.topic.alias) {
|
|
|
|
this.topic.alias = this.topic.name.toLowerCase().trim();
|
|
|
|
}
|
2019-11-24 17:30:04 +01:00
|
|
|
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.topic, callback,true);
|
2019-11-13 11:26:50 +01:00
|
|
|
} else {
|
|
|
|
this.valid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 15:25:19 +01:00
|
|
|
public deleteTopicOpen(name: string, element) {
|
|
|
|
this.deleteOpen(name,'topic', this.deleteTopicModal, element, this.topicIndex);
|
2019-11-13 11:26:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public deleteTopic() {
|
|
|
|
let path: string[] = [
|
2019-11-24 17:30:04 +01:00
|
|
|
this.stakeholder._id,
|
|
|
|
this.stakeholder.topics[this.topicIndex]._id
|
2019-11-13 11:26:50 +01:00
|
|
|
];
|
2019-11-24 17:30:04 +01:00
|
|
|
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-13 11:26:50 +01:00
|
|
|
}
|
|
|
|
|
2019-11-05 15:59:06 +01:00
|
|
|
public toggleCategory(index: number) {
|
2019-11-08 16:47:47 +01:00
|
|
|
if(this.selectedCategoryIndex !== index) {
|
|
|
|
this.selectedCategoryIndex = index;
|
2019-11-05 15:59:06 +01:00
|
|
|
this.toggle = true;
|
|
|
|
} else {
|
|
|
|
this.toggle = !this.toggle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 16:20:51 +01:00
|
|
|
public editCategoryOpen(element, index:number = -1) {
|
|
|
|
if(element.className.indexOf('uk-open') !== -1) {
|
|
|
|
this.hide(element);
|
2019-11-05 15:59:06 +01:00
|
|
|
} else {
|
2019-11-14 16:20:51 +01:00
|
|
|
if(index === -1) {
|
|
|
|
this.copyCategory = new Category(null, null, null, true, true);
|
2019-11-05 15:59:06 +01:00
|
|
|
} else {
|
2019-11-11 14:28:36 +01:00
|
|
|
this.copyCategory = HelperFunctions.copy(this.stakeholder.topics[this.topicIndex].categories[index]);
|
2019-11-05 15:59:06 +01:00
|
|
|
}
|
2019-11-14 16:20:51 +01:00
|
|
|
this.show(element);
|
2019-11-05 15:59:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public saveCategory(element, index = -1) {
|
|
|
|
if(this.copyCategory.name && this.copyCategory.name !== '') {
|
2019-11-12 16:55:37 +01:00
|
|
|
if(!this.copyCategory.alias) {
|
|
|
|
this.copyCategory.alias = this.copyCategory.name.toLowerCase();
|
|
|
|
}
|
2019-11-24 17:30:04 +01:00
|
|
|
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);
|
|
|
|
};
|
2019-11-05 15:59:06 +01:00
|
|
|
if(index === -1) {
|
2019-11-24 17:30:04 +01:00
|
|
|
this.save('Category has been successfully created', element, path, this.copyCategory, callback);
|
2019-11-05 15:59:06 +01:00
|
|
|
} else {
|
2019-11-24 17:30:04 +01:00
|
|
|
this.save('Category has been successfully saved', element, path, this.copyCategory, callback);
|
2019-11-05 15:59:06 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.valid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 15:25:19 +01:00
|
|
|
public deleteCategoryOpen(name: string, element, index) {
|
|
|
|
this.deleteOpen(name,'category', this.deleteCategoryModal, element, index);
|
2019-11-12 16:55:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public deleteCategory() {
|
2019-11-13 11:26:50 +01:00
|
|
|
let path: string[] = [
|
2019-11-24 17:30:04 +01:00
|
|
|
this.stakeholder._id,
|
|
|
|
this.stakeholder.topics[this.topicIndex]._id,
|
|
|
|
this.stakeholder.topics[this.topicIndex].categories[this.index]._id
|
2019-11-13 11:26:50 +01:00
|
|
|
];
|
2019-11-24 17:30:04 +01:00
|
|
|
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-12 16:55:37 +01:00
|
|
|
}
|
|
|
|
|
2019-11-14 16:20:51 +01:00
|
|
|
public editSubCategoryOpen(element, index:number = -1) {
|
|
|
|
if(element.className.indexOf('uk-open') !== -1) {
|
|
|
|
this.hide(element);
|
2019-11-05 15:59:06 +01:00
|
|
|
} else {
|
2019-11-14 16:20:51 +01:00
|
|
|
if(index === -1) {
|
|
|
|
this.copySubCategory = new SubCategory(null, null, null, true, true);
|
2019-11-05 15:59:06 +01:00
|
|
|
} else {
|
2019-11-11 14:28:36 +01:00
|
|
|
this.copySubCategory = HelperFunctions.copy(this.stakeholder.topics[this.topicIndex].
|
2019-11-05 15:59:06 +01:00
|
|
|
categories[this.categoryIndex].subCategories[index]);
|
|
|
|
}
|
2019-11-14 16:20:51 +01:00
|
|
|
this.show(element);
|
2019-11-05 15:59:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public saveSubCategory(element, index = -1) {
|
|
|
|
if(this.copySubCategory.name && this.copySubCategory.name !== '') {
|
2019-11-12 16:55:37 +01:00
|
|
|
if(!this.copySubCategory.alias) {
|
|
|
|
this.copySubCategory.alias = this.copySubCategory.name.toLowerCase();
|
|
|
|
}
|
2019-11-24 17:30:04 +01:00
|
|
|
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);
|
|
|
|
};
|
2019-11-05 15:59:06 +01:00
|
|
|
if(index === -1) {
|
2019-11-24 17:30:04 +01:00
|
|
|
this.save('Subcategory has been successfully created', element, path, this.copySubCategory, callback);
|
2019-11-05 15:59:06 +01:00
|
|
|
} else {
|
2019-11-24 17:30:04 +01:00
|
|
|
this.save('Subcategory has been successfully saved', element, path, this.copySubCategory, callback);
|
2019-11-05 15:59:06 +01:00
|
|
|
}
|
|
|
|
this.hide(element);
|
|
|
|
} else {
|
|
|
|
this.valid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 15:25:19 +01:00
|
|
|
public deleteSubcategoryOpen(name: string, element, index) {
|
|
|
|
this.deleteOpen(name,'subcategory', this.deleteSubcategoryModal, element, index);
|
2019-11-12 16:55:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public deleteSubcategory() {
|
2019-11-13 11:26:50 +01:00
|
|
|
let path: string[] = [
|
2019-11-24 17:30:04 +01:00
|
|
|
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
|
2019-11-13 11:26:50 +01:00
|
|
|
];
|
2019-11-24 17:30:04 +01:00
|
|
|
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-11-04 12:50:25 +01:00
|
|
|
private navigateToError() {
|
|
|
|
this.router.navigate(['/error'], {queryParams: {'page': this.router.url}});
|
|
|
|
}
|
2019-11-05 15:59:06 +01:00
|
|
|
|
2019-11-19 15:25:19 +01:00
|
|
|
private deleteOpen(name: string, type: string, modal: AlertModal, element, index) {
|
2019-11-12 16:55:37 +01:00
|
|
|
this.element = element;
|
|
|
|
this.index = index;
|
2019-11-19 15:25:19 +01:00
|
|
|
modal.alertTitle = 'Delete ' + name;
|
2019-11-12 16:55:37 +01:00
|
|
|
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-24 17:30:04 +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);
|
2019-11-12 16:55:37 +01:00
|
|
|
UIkit.notification(message, {
|
|
|
|
status: 'success',
|
|
|
|
timeout: 3000,
|
|
|
|
pos: 'top-left'
|
|
|
|
});
|
|
|
|
if(redirect) {
|
2019-11-24 17:30:04 +01:00
|
|
|
this.router.navigate(['../' + saveElement.alias], {
|
2019-11-12 16:55:37 +01:00
|
|
|
relativeTo: this.route
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.hide(element);
|
|
|
|
}, error => {
|
|
|
|
UIkit.notification(error.error.message, {
|
|
|
|
status: 'danger',
|
|
|
|
timeout: 3000,
|
|
|
|
pos: 'top-left'
|
|
|
|
});
|
|
|
|
this.hide(element);
|
|
|
|
});
|
2019-11-08 16:47:47 +01:00
|
|
|
}
|
|
|
|
|
2019-11-24 17:30:04 +01:00
|
|
|
private delete(message: string, path: string[], callback: Function, redirect = false) {
|
|
|
|
this.stakeholderService.deleteElement(this.properties.monitorServiceAPIURL, path).subscribe(() => {
|
|
|
|
callback();
|
2019-11-13 11:26:50 +01:00
|
|
|
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-05 15:59:06 +01:00
|
|
|
back() {
|
|
|
|
this.router.navigate(['../'], {
|
|
|
|
relativeTo: this.route
|
|
|
|
});
|
|
|
|
}
|
2019-11-08 16:47:47 +01:00
|
|
|
|
|
|
|
chooseSubcategory(categoryIndex: number, subcategoryIndex: number) {
|
|
|
|
this.categoryIndex = categoryIndex;
|
|
|
|
this.subCategoryIndex = subcategoryIndex;
|
|
|
|
}
|
2019-11-04 12:50:25 +01:00
|
|
|
}
|