monitor-dashboard/src/app/topic/topic.component.ts

340 lines
11 KiB
TypeScript
Raw Normal View History

import {Component, OnDestroy, OnInit, ViewChild} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {Title} from '@angular/platform-browser';
import {EnvProperties} from '../openaireLibrary/utils/properties/env-properties';
import {Category, Stakeholder, SubCategory, Topic} from "../utils/entities/stakeholder";
import {SideBarService} from "../library/sharedComponents/sidebar/sideBar.service";
import {StakeholderService} from "../services/stakeholder.service";
import {HelperFunctions} from "../openaireLibrary/utils/HelperFunctions.class";
import {AlertModal} from "../openaireLibrary/utils/modal/alert";
import {Subscriber} from "rxjs";
declare var UIkit;
@Component({
selector: 'topic',
templateUrl: './topic.component.html',
})
export class TopicComponent implements OnInit, OnDestroy {
public subscriptions: any[] = [];
public properties: EnvProperties;
public loading: boolean = true;
public stakeholder: Stakeholder;
/**
* Current topic
**/
public topicIndex: number = 0;
public topic: Topic = null;
/**
* categoryIndex: Current category to be edited, selectedCategoryIndex: selected on menu(opened)
*/
public categoryIndex: number = 0;
public selectedCategoryIndex: number = 0;
public copyCategory: Category = null;
/**
* Current Subcategory to be edited
*/
public subCategoryIndex: number = 0;
public copySubCategory: SubCategory = null;
/**
* Current drop element and index of topic, category or subcategory to be deleted.
*/
public element: any;
public index: number;
/**
* Check form validity
*/
public valid = true;
public toggle: boolean = false;
@ViewChild('deleteTopicModal') deleteTopicModal: AlertModal;
@ViewChild('deleteCategoryModal') deleteCategoryModal: AlertModal;
@ViewChild('deleteSubcategoryModal') deleteSubcategoryModal: AlertModal;
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 => {
this.subscriptions.push(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;
}
}
}));
});
});
}
public ngOnDestroy() {
this.subscriptions.forEach( value => {
if(value instanceof Subscriber) {
value.unsubscribe();
}
});
}
public hide(element) {
this.valid = true;
UIkit.drop(element).hide();
}
public show(element) {
UIkit.drop(element).show();
}
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) {
if(this.topic.name && this.topic.name !== '') {
if(!this.topic.alias) {
this.topic.alias = this.topic.name.toLowerCase().trim();
}
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);
} else {
this.valid = false;
}
}
public deleteTopicOpen(name: string, element) {
this.deleteOpen(name,'topic', this.deleteTopicModal, element, this.topicIndex);
}
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);
}
public toggleCategory(index: number) {
if(this.selectedCategoryIndex !== index) {
this.selectedCategoryIndex = index;
this.toggle = true;
} else {
this.toggle = !this.toggle;
}
}
public editCategoryOpen(element, index:number = -1) {
if(element.className.indexOf('uk-open') !== -1) {
this.hide(element);
} else {
if(index === -1) {
this.copyCategory = new Category(null, null, null, true, true);
} else {
this.copyCategory = HelperFunctions.copy(this.stakeholder.topics[this.topicIndex].categories[index]);
}
this.show(element);
}
}
public saveCategory(element, index = -1) {
if(this.copyCategory.name && this.copyCategory.name !== '') {
if(!this.copyCategory.alias) {
this.copyCategory.alias = this.copyCategory.name.toLowerCase();
}
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.copyCategory, callback);
} else {
this.save('Category has been successfully saved', element, path, this.copyCategory, callback);
}
} else {
this.valid = false;
}
}
public deleteCategoryOpen(name: string, element, index) {
this.deleteOpen(name,'category', this.deleteCategoryModal, element, index);
}
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);
}
public editSubCategoryOpen(element, index:number = -1) {
if(element.className.indexOf('uk-open') !== -1) {
this.hide(element);
} else {
if(index === -1) {
this.copySubCategory = new SubCategory(null, null, null, true, true);
} else {
this.copySubCategory = HelperFunctions.copy(this.stakeholder.topics[this.topicIndex].
categories[this.categoryIndex].subCategories[index]);
}
this.show(element);
}
}
public saveSubCategory(element, index = -1) {
if(this.copySubCategory.name && this.copySubCategory.name !== '') {
if(!this.copySubCategory.alias) {
this.copySubCategory.alias = this.copySubCategory.name.toLowerCase();
}
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.copySubCategory, callback);
} else {
this.save('Subcategory has been successfully saved', element, path, this.copySubCategory, callback);
}
this.hide(element);
} else {
this.valid = false;
}
}
public deleteSubcategoryOpen(name: string, element, index) {
this.deleteOpen(name,'subcategory', this.deleteSubcategoryModal, element, index);
}
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);
}
private navigateToError() {
this.router.navigate(['/error'], {queryParams: {'page': this.router.url}});
}
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();
}
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);
});
}
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);
});
}
back() {
this.router.navigate(['../'], {
relativeTo: this.route
});
}
chooseSubcategory(categoryIndex: number, subcategoryIndex: number) {
this.categoryIndex = categoryIndex;
this.subCategoryIndex = subcategoryIndex;
}
}