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

209 lines
6.2 KiB
TypeScript
Raw Normal View History

import {Component, OnDestroy, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {Title} from '@angular/platform-browser';
import {EnvProperties} from '../openaireLibrary/utils/properties/env-properties';
import {ErrorCodes} from '../openaireLibrary/utils/properties/errorCodes';
import {ErrorMessagesComponent} from '../openaireLibrary/utils/errorMessages.component';
import {Category, Stakeholder, SubCategory, Topic} from "../utils/entities/stakeholder";
import {SideBarService} from "../library/sharedComponents/sidebar/sideBar.service";
import {StakeholderService} from "../services/stakeholder.service";
import {Tools} from "../utils/Tools";
declare var UIkit;
@Component({
selector: 'topic',
templateUrl: './topic.component.html',
})
export class TopicComponent implements OnInit, OnDestroy {
public status: number;
public loading: boolean = true;
public errorCodes: ErrorCodes;
public stakeholder: Stakeholder;
private errorMessages: ErrorMessagesComponent;
public topicIndex: number = -1;
public topic: Topic = null;
public categoryIndex: number = -1;
public copyCategory: Category = null;
public subCategoryIndex: number = -1;
public copySubCategory: SubCategory = null;
public valid = true;
public edit: boolean = false;
public toggle: boolean = false;
public unsaved: boolean = false;
public properties: EnvProperties;
public chartType: string = null;
public isPublic: boolean = null;
public isActive: boolean = null;
grid: boolean = true;
constructor(
private route: ActivatedRoute,
private router: Router,
private title: Title,
private sideBarService: SideBarService,
private stakeholderService: StakeholderService) {
this.errorCodes = new ErrorCodes();
this.errorMessages = new ErrorMessagesComponent();
this.status = this.errorCodes.LOADING;
}
public ngOnInit() {
this.route.data
.subscribe((data: { envSpecific: EnvProperties }) => {
this.properties = data.envSpecific;
this.route.params.subscribe( params => {
this.stakeholderService.getStakeholderAsObservable().subscribe(stakeholder => {
if (stakeholder) {
this.sideBarService.setHasSidebar(true);
this.stakeholder = Tools.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_name);
}
}
});
});
});
}
public ngOnDestroy() {
}
public hide(element) {
this.edit = false;
UIkit.drop(element).hide();
}
public show(element) {
this.edit = true;
UIkit.drop(element).show();
}
get open(): boolean {
return this.sideBarService.open;
}
public toggleOpen(event = null) {
if (!event) {
this.sideBarService.setOpen(!this.open);
} else if (event && event['value'] === true) {
this.sideBarService.setOpen(false);
}
}
public toggleCategory(index: number) {
if(this.categoryIndex !== index) {
this.categoryIndex = index;
this.toggle = true;
} else {
this.toggle = !this.toggle;
}
}
public editCategoryOpen(index:number = -1, element = null) {
if(index === -1) {
this.copyCategory = new Category(null, null, null, true, true);
} else {
if(element.className.indexOf('uk-open') !== -1) {
this.hide(element);
} else {
this.copyCategory = Tools.copy(this.stakeholder.topics[this.topicIndex].categories[index]);
this.show(element);
this.valid = true;
}
}
}
public saveCategory(element, index = -1) {
if(this.copyCategory.name && this.copyCategory.name !== '') {
this.copyCategory.alias = this.copyCategory.name.toLowerCase();
if(index === -1) {
this.stakeholder.topics[this.topicIndex].categories.push(this.copyCategory);
} else {
this.stakeholder.topics[this.topicIndex].categories[index] = Tools.copy(this.copyCategory);
}
this.hide(element);
this.unsaved = true;
} else {
this.valid = false;
}
}
public editTopicOpen(element) {
if(element.className.indexOf('uk-open') !== -1) {
this.hide(element);
} else {
this.topic = Tools.copy(this.stakeholder.topics[this.topicIndex]);
this.valid = true;
this.show(element);
}
}
public editSubCategoryOpen(index:number = -1, element = null) {
if(index === -1) {
this.copySubCategory = new SubCategory(null, null, null, true, true);
} else {
if(element.className.indexOf('uk-open') !== -1) {
this.hide(element);
} else {
this.copySubCategory = Tools.copy(this.stakeholder.topics[this.topicIndex].
categories[this.categoryIndex].subCategories[index]);
this.show(element);
this.valid = true;
}
}
}
public saveSubCategory(element, index = -1) {
if(this.copySubCategory.name && this.copySubCategory.name !== '') {
this.copySubCategory.alias = this.copySubCategory.name.toLowerCase();
if(index === -1) {
this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].
subCategories.push(this.copySubCategory);
} else {
this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].
subCategories[index] = Tools.copy(this.copySubCategory);
}
this.hide(element);
this.unsaved = true;
} else {
this.valid = false;
}
}
public saveTopic(element) {
if(this.topic.name && this.topic.name !== '') {
this.topic.alias = this.topic.name.toLowerCase();
this.stakeholder.topics[this.topicIndex] = Tools.copy(this.topic);
this.hide(element);
this.unsaved = true;
} else {
this.valid = false;
}
}
public deleteTopic(element) {
/*this.stakeholder.topics.splice(this.topicIndex, 1);
this.hide(element);
this.back();*/
}
public changeGrid(value) {
this.grid = value;
}
private navigateToError() {
this.router.navigate(['/error'], {queryParams: {'page': this.router.url}});
}
back() {
this.router.navigate(['../'], {
relativeTo: this.route
});
}
}