2019-11-20 11:32:28 +01:00
|
|
|
import {ChangeDetectorRef, Component, Input, OnChanges, OnInit, SimpleChanges, ViewChild} from "@angular/core";
|
2019-11-08 16:47:47 +01:00
|
|
|
import {SideBarService} from "../library/sharedComponents/sidebar/sideBar.service";
|
2019-11-20 11:32:28 +01:00
|
|
|
import {Indicator, IndicatorPath, Stakeholder} from "../utils/entities/stakeholder";
|
2019-11-11 14:28:36 +01:00
|
|
|
import {IndicatorUtils} from "../utils/indicator-utils";
|
2019-11-19 15:25:19 +01:00
|
|
|
import {FormArray, FormBuilder, FormGroup, Validators} from "@angular/forms";
|
|
|
|
import {AlertModal} from "../openaireLibrary/utils/modal/alert";
|
|
|
|
import {StatisticsService} from "../utils/services/statistics.service";
|
|
|
|
import {HelperFunctions} from "../openaireLibrary/utils/HelperFunctions.class";
|
2019-11-20 11:32:28 +01:00
|
|
|
import {DomSanitizer} from "@angular/platform-browser";
|
2019-11-08 16:47:47 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'indicators',
|
|
|
|
templateUrl: './indicators.component.html'
|
|
|
|
})
|
|
|
|
export class IndicatorsComponent implements OnInit, OnChanges {
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
public topicIndex: number = 0;
|
|
|
|
@Input()
|
|
|
|
public categoryIndex: number = 0;
|
|
|
|
@Input()
|
|
|
|
public subcategoryIndex: number = 0;
|
|
|
|
@Input()
|
|
|
|
public stakeholder: Stakeholder = null;
|
|
|
|
public indicatorUtils: IndicatorUtils = new IndicatorUtils();
|
2019-11-19 15:25:19 +01:00
|
|
|
public indicatorFb: FormGroup;
|
|
|
|
public editIndicatorFb: FormGroup;
|
|
|
|
/**
|
|
|
|
* Editable indicator
|
|
|
|
*/
|
|
|
|
public indicator: Indicator;
|
2019-11-13 11:26:50 +01:00
|
|
|
/**
|
|
|
|
* All charts and numbers
|
|
|
|
*/
|
2019-11-08 16:47:47 +01:00
|
|
|
public charts: Indicator[] = [];
|
|
|
|
public numbers: Indicator[] = [];
|
2019-11-13 11:26:50 +01:00
|
|
|
/**
|
|
|
|
* Displayed chart and numbers base on Top filters
|
|
|
|
*/
|
2019-11-08 16:47:47 +01:00
|
|
|
public displayCharts: Indicator[] = [];
|
|
|
|
public displayNumbers: Indicator[] = [];
|
2019-11-13 11:26:50 +01:00
|
|
|
/**
|
|
|
|
* Top filters
|
|
|
|
*/
|
2019-11-08 16:47:47 +01:00
|
|
|
public chartType: string = 'all';
|
2019-11-13 11:26:50 +01:00
|
|
|
public privacy: string = 'all';
|
|
|
|
public status: string = 'all';
|
2019-11-08 16:47:47 +01:00
|
|
|
public keyword: string = null;
|
2019-11-13 11:26:50 +01:00
|
|
|
/**
|
|
|
|
* Grid or List View
|
|
|
|
*/
|
2019-11-08 16:47:47 +01:00
|
|
|
public grid: boolean = true;
|
|
|
|
|
2019-11-19 15:25:19 +01:00
|
|
|
@ViewChild('createIndicatorModal') createIndicatorModal: AlertModal;
|
|
|
|
@ViewChild('editIndicatorModal') editIndicatorModal: AlertModal;
|
|
|
|
|
|
|
|
constructor(private sideBarService: SideBarService,
|
|
|
|
private statisticsService: StatisticsService,
|
2019-11-20 11:32:28 +01:00
|
|
|
private fb: FormBuilder,
|
|
|
|
private sanitizer: DomSanitizer) {
|
2019-11-19 15:25:19 +01:00
|
|
|
}
|
2019-11-08 16:47:47 +01:00
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnChanges(changes: SimpleChanges): void {
|
2019-11-19 15:25:19 +01:00
|
|
|
if (this.canEdit) {
|
|
|
|
this.charts = this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex].charts;
|
2019-11-12 16:55:37 +01:00
|
|
|
this.displayCharts = this.filterChartType(this.filterPrivacy(
|
2019-11-13 11:26:50 +01:00
|
|
|
this.filterStatus(this.filterByKeyword(this.charts, this.keyword), this.status),
|
|
|
|
this.privacy),
|
2019-11-12 16:55:37 +01:00
|
|
|
this.chartType
|
|
|
|
);
|
2019-11-19 15:25:19 +01:00
|
|
|
this.numbers = this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex].numbers;
|
2019-11-12 16:55:37 +01:00
|
|
|
this.displayNumbers = this.filterPrivacy(this.filterStatus(
|
|
|
|
this.filterByKeyword(this.numbers, this.keyword),
|
2019-11-13 11:26:50 +01:00
|
|
|
this.status),
|
|
|
|
this.privacy);
|
2019-11-08 16:47:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 15:25:19 +01:00
|
|
|
public toggleOpen(event = null) {
|
|
|
|
if (!event) {
|
|
|
|
this.sideBarService.setOpen(!this.open);
|
|
|
|
} else if (event && event['value'] === true) {
|
|
|
|
this.sideBarService.setOpen(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public changeGrid(value) {
|
|
|
|
this.grid = value;
|
|
|
|
}
|
|
|
|
|
2019-11-08 16:47:47 +01:00
|
|
|
onChartTypeChange(value) {
|
|
|
|
this.displayCharts = this.filterChartType(this.charts, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
onPrivacyChange(value) {
|
|
|
|
this.displayCharts = this.filterPrivacy(this.charts, value);
|
|
|
|
this.displayNumbers = this.filterPrivacy(this.numbers, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
onStatusChange(value) {
|
|
|
|
this.displayCharts = this.filterStatus(this.charts, value);
|
|
|
|
this.displayNumbers = this.filterStatus(this.numbers, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
onKeywordChange(value) {
|
|
|
|
this.displayCharts = this.filterByKeyword(this.charts, value);
|
|
|
|
this.displayNumbers = this.filterByKeyword(this.numbers, value);
|
|
|
|
}
|
|
|
|
|
2019-11-19 15:25:19 +01:00
|
|
|
private filterChartType(indicators: Indicator[], value): Indicator[] {
|
2019-11-08 16:47:47 +01:00
|
|
|
if (value === 'all') {
|
|
|
|
return indicators;
|
|
|
|
} else {
|
|
|
|
return indicators.filter(indicator =>
|
|
|
|
indicator.indicatorPaths.filter(indicatorPath => indicatorPath.type === value).length > 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 15:25:19 +01:00
|
|
|
private filterPrivacy(indicators: Indicator[], value): Indicator[] {
|
2019-11-08 16:47:47 +01:00
|
|
|
if (value === 'all') {
|
|
|
|
return indicators;
|
|
|
|
} else {
|
|
|
|
return indicators.filter(indicator => indicator.isPublic === (value === 'public'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 15:25:19 +01:00
|
|
|
private filterStatus(indicators: Indicator[], value): Indicator[] {
|
2019-11-08 16:47:47 +01:00
|
|
|
if (value === 'all') {
|
|
|
|
return indicators;
|
|
|
|
} else {
|
|
|
|
return indicators.filter(indicator => indicator.isActive === (value === 'active'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 15:25:19 +01:00
|
|
|
private filterByKeyword(indicators: Indicator[], value): Indicator[] {
|
2019-11-08 16:47:47 +01:00
|
|
|
if (value === null || value === '') {
|
|
|
|
return indicators;
|
|
|
|
} else {
|
|
|
|
return indicators.filter(indicator => indicator.name.includes(value) || indicator.description.includes(value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get open(): boolean {
|
|
|
|
return this.sideBarService.open;
|
|
|
|
}
|
|
|
|
|
2019-11-13 11:26:50 +01:00
|
|
|
get canEdit() {
|
|
|
|
return this.stakeholder &&
|
|
|
|
this.stakeholder.topics[this.topicIndex] &&
|
|
|
|
this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex] &&
|
|
|
|
this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex];
|
|
|
|
}
|
|
|
|
|
2019-11-19 15:25:19 +01:00
|
|
|
public get urls(): FormArray {
|
|
|
|
return this.indicatorFb.get('urls') as FormArray;
|
2019-11-08 16:47:47 +01:00
|
|
|
}
|
|
|
|
|
2019-11-20 11:32:28 +01:00
|
|
|
private getUrlByStakeHolder(indicatorPath: IndicatorPath) {
|
|
|
|
return this.sanitizer.bypassSecurityTrustResourceUrl(
|
|
|
|
this.statisticsService.getChartUrl(indicatorPath.source, this.indicatorUtils.getFullUrl(indicatorPath)));
|
|
|
|
}
|
|
|
|
|
2019-11-19 15:25:19 +01:00
|
|
|
public addUrl() {
|
|
|
|
this.urls.push(this.fb.control('', [Validators.required,
|
|
|
|
Validators.pattern('https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.' +
|
|
|
|
'[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.' +
|
|
|
|
'[a-zA-Z0-9]+\.[^\s]{2,}')])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public createIndicatorOpen() {
|
|
|
|
this.indicatorFb = this.fb.group({
|
|
|
|
name: this.fb.control('', Validators.required),
|
|
|
|
description: this.fb.control(''),
|
|
|
|
width: this.fb.control('small', Validators.required),
|
|
|
|
urls: this.fb.array([]),
|
|
|
|
});
|
|
|
|
this.addUrl();
|
|
|
|
this.createIndicatorModal.alertTitle = 'Create a new chart Indicator';
|
|
|
|
this.createIndicatorModal.cancelButtonText = 'Cancel';
|
|
|
|
this.createIndicatorModal.okButtonText = 'Create';
|
|
|
|
this.createIndicatorModal.okButtonLeft = false;
|
|
|
|
this.createIndicatorModal.alertMessage = false;
|
|
|
|
this.createIndicatorModal.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
public createIndicator() {
|
|
|
|
this.indicator = new Indicator(
|
|
|
|
this.indicatorFb.value.name,
|
|
|
|
this.indicatorFb.value.description,
|
|
|
|
'chart',
|
|
|
|
this.indicatorFb.value.width,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
this.indicatorFb.value.urls.forEach(url => {
|
|
|
|
this.indicator.indicatorPaths.push(
|
|
|
|
this.indicatorUtils.generateIndicatorByChartUrl(this.statisticsService.getChartSource(url), url));
|
|
|
|
});
|
|
|
|
let index = this.charts.push(this.indicator);
|
2019-11-20 11:32:28 +01:00
|
|
|
this.editIndicatorOpen(index - 1);
|
2019-11-19 15:25:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public editIndicatorOpen(index: number) {
|
|
|
|
this.indicator = HelperFunctions.copy(this.charts[index]);
|
|
|
|
this.indicator.indicatorPaths.forEach(indicatorPath => {
|
2019-11-20 11:32:28 +01:00
|
|
|
indicatorPath.safeResourceUrl = this.getUrlByStakeHolder(indicatorPath)
|
|
|
|
console.log(indicatorPath.safeResourceUrl);
|
|
|
|
});
|
|
|
|
let indicatorPaths = this.fb.array([]);
|
|
|
|
this.indicator.indicatorPaths.forEach(indicatorPath => {
|
|
|
|
let parameters = this.fb.array([]);
|
|
|
|
indicatorPath.parameters.forEach((value, key) => {
|
|
|
|
if (this.indicatorUtils.ignoredParameters.indexOf(key) === -1) {
|
|
|
|
// TODO add Validators Map
|
|
|
|
parameters.push(this.fb.group({
|
|
|
|
key: this.fb.control(key),
|
|
|
|
value: this.fb.control(value, Validators.required)
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
});
|
2019-11-19 15:25:19 +01:00
|
|
|
indicatorPaths.push(this.fb.group({
|
2019-11-20 11:32:28 +01:00
|
|
|
parameters: parameters
|
2019-11-19 15:25:19 +01:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
this.editIndicatorFb = this.fb.group({
|
|
|
|
name: this.fb.control(this.indicator.name, Validators.required),
|
|
|
|
description: this.fb.control(this.indicator.description),
|
|
|
|
isPublic: this.fb.control(this.indicator.isPublic),
|
|
|
|
isActive: this.fb.control(this.indicator.isActive),
|
|
|
|
indicatorPaths: indicatorPaths,
|
|
|
|
width: this.fb.control(this.indicator.width, Validators.required),
|
|
|
|
});
|
|
|
|
console.log(this.editIndicatorFb.value);
|
2019-11-20 11:32:28 +01:00
|
|
|
this.editIndicatorModal.alertHeader = false;
|
2019-11-19 15:25:19 +01:00
|
|
|
this.editIndicatorModal.cancelButtonText = 'Cancel';
|
|
|
|
this.editIndicatorModal.okButtonText = 'Save Changes';
|
|
|
|
this.editIndicatorModal.okButtonLeft = false;
|
|
|
|
this.editIndicatorModal.alertMessage = false;
|
2019-11-20 11:32:28 +01:00
|
|
|
this.editIndicatorModal.open();
|
2019-11-19 15:25:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
saveIndicator() {
|
|
|
|
|
2019-11-08 16:47:47 +01:00
|
|
|
}
|
2019-11-20 11:32:28 +01:00
|
|
|
|
|
|
|
focus(event: FocusEvent) {
|
|
|
|
event.srcElement.parentElement.className = event.srcElement.parentElement.className + ' md-input-focus';
|
|
|
|
}
|
2019-11-08 16:47:47 +01:00
|
|
|
}
|