2020-05-12 13:05:53 +02:00
import {
AfterViewInit ,
ChangeDetectorRef ,
Component ,
Input ,
OnChanges ,
OnDestroy ,
OnInit ,
SimpleChanges ,
ViewChild
} from "@angular/core" ;
2020-10-23 15:58:16 +02:00
import {
Indicator ,
2020-12-01 16:43:43 +01:00
IndicatorPath , IndicatorSize ,
2020-10-23 15:58:16 +02:00
IndicatorType ,
Section ,
Stakeholder ,
Visibility
} from "../openaireLibrary/monitor/entities/stakeholder" ;
import { IndicatorUtils , StakeholderUtils } from "../utils/indicator-utils" ;
2019-11-24 17:30:04 +01:00
import { FormArray , FormBuilder , FormControl , FormGroup , Validators } from "@angular/forms" ;
2019-11-19 15:25:19 +01:00
import { AlertModal } from "../openaireLibrary/utils/modal/alert" ;
import { StatisticsService } from "../utils/services/statistics.service" ;
import { HelperFunctions } from "../openaireLibrary/utils/HelperFunctions.class" ;
2020-05-13 17:10:44 +02:00
import { DomSanitizer , SafeResourceUrl } from "@angular/platform-browser" ;
2020-12-09 14:24:32 +01:00
import { Reorder , StakeholderService } from "../openaireLibrary/monitor/services/stakeholder.service" ;
2019-11-24 17:30:04 +01:00
import { EnvProperties } from "../openaireLibrary/utils/properties/env-properties" ;
2019-11-26 20:11:24 +01:00
import { Subscriber } from "rxjs" ;
2019-12-23 14:54:37 +01:00
import { LayoutService } from "../openaireLibrary/dashboard/sharedComponents/sidebar/layout.service" ;
2020-01-10 15:20:42 +01:00
import { Router } from "@angular/router" ;
2021-03-04 12:35:10 +01:00
import { Role , Session , User } from "../openaireLibrary/login/utils/helper.class" ;
2020-06-09 16:49:52 +02:00
import { UserManagementService } from "../openaireLibrary/services/user-management.service" ;
2020-12-17 16:39:31 +01:00
import { StringUtils } from "../openaireLibrary/utils/string-utils.class" ;
2021-03-04 12:35:10 +01:00
import { Notification } from "../openaireLibrary/notifications/notifications" ;
import { NotificationUtils } from "../openaireLibrary/notifications/notification-utils" ;
import { NotifyFormComponent } from "../openaireLibrary/notifications/notify-form/notify-form.component" ;
2019-11-24 17:30:04 +01:00
declare var UIkit ;
2019-11-08 16:47:47 +01:00
@Component ( {
2019-12-15 22:25:56 +01:00
selector : 'indicators' ,
2020-11-02 15:46:19 +01:00
templateUrl : './indicators.component.html' ,
styleUrls : [ 'indicators.component.css' ]
2019-11-08 16:47:47 +01:00
} )
2019-11-26 20:11:24 +01:00
export class IndicatorsComponent implements OnInit , OnDestroy , OnChanges , AfterViewInit {
2021-07-16 10:58:27 +02:00
filesToUpload : Array < File > ;
errorMessage = "" ;
2021-07-19 11:50:08 +02:00
import_loading : boolean = false ;
export_loading : boolean = false ;
2021-07-16 10:58:27 +02:00
2019-12-15 22:25:56 +01:00
@Input ( )
public properties : EnvProperties = null ;
@Input ( )
public topicIndex : number = 0 ;
@Input ( )
public categoryIndex : number = 0 ;
@Input ( )
public subcategoryIndex : number = 0 ;
public stakeholder : Stakeholder = null ;
2021-03-04 12:35:10 +01:00
public user : User = null ;
2020-01-10 15:20:42 +01:00
public preview : string ;
2019-12-15 22:25:56 +01:00
public indicatorUtils : IndicatorUtils = new IndicatorUtils ( ) ;
2020-10-23 15:58:16 +02:00
public stakeholderUtils : StakeholderUtils = new StakeholderUtils ( ) ;
2020-06-09 16:49:52 +02:00
public numberIndicatorFb : FormGroup ;
public chartIndicatorFb : FormGroup ;
2020-01-14 20:48:13 +01:00
public chartSections : FormArray ;
public numberSections : FormArray ;
2019-12-15 22:25:56 +01:00
/ * *
* Editable indicator
* /
2020-01-08 12:00:50 +01:00
public section : Section ;
2019-12-15 22:25:56 +01:00
public indicator : Indicator ;
2019-12-19 12:17:32 +01:00
public index : number = - 1 ;
2019-12-15 22:25:56 +01:00
/ * *
* Displayed chart and numbers base on Top filters
* /
2020-01-08 12:00:50 +01:00
public displayCharts : Section [ ] = [ ] ;
public displayNumbers : Section [ ] = [ ] ;
2019-12-15 22:25:56 +01:00
/ * *
* Top filters
* /
2020-11-02 15:46:19 +01:00
@Input ( )
2019-12-15 22:25:56 +01:00
public filters : FormGroup ;
2020-06-17 12:37:04 +02:00
public editing : boolean = false ;
2020-05-13 17:10:44 +02:00
/** Safe Urls*/
public safeUrls : Map < string , SafeResourceUrl > = new Map < string , SafeResourceUrl > ( [ ] ) ;
2020-11-04 16:53:08 +01:00
public numberResults : Map < string , number > = new Map < string , number > ( ) ;
2019-12-15 22:25:56 +01:00
private subscriptions : any [ ] = [ ] ;
private urlSubscriptions : any [ ] = [ ] ;
2021-07-14 18:02:04 +02:00
@ViewChild ( 'editChartModal' , { static : true } ) editChartModal : AlertModal ;
@ViewChild ( 'editNumberModal' , { static : true } ) editNumberModal : AlertModal ;
@ViewChild ( 'deleteModal' , { static : true } ) deleteModal : AlertModal ;
2020-06-23 10:33:13 +02:00
//@ViewChild('deleteAllModal') deleteAllModal: AlertModal;
//@ViewChild('deleteAndDisconnectModal') deleteAndDisconnectModal: AlertModal;
//@ViewChild('deleteChartSectionModal') deleteChartSectionModal: AlertModal;
//@ViewChild('deleteNumberSectionModal') deleteNumberSectionModal: AlertModal;
2021-07-14 18:02:04 +02:00
@ViewChild ( 'deleteSectionModal' , { static : true } ) deleteSectionModal : AlertModal ;
2020-06-23 10:33:13 +02:00
public sectionTypeToDelete : string ;
public sectionChildrenActionOnDelete : string ;
public indicatorChildrenActionOnDelete : string ;
2021-01-14 19:47:03 +01:00
private firstLoad : boolean = true ;
2020-06-09 16:49:52 +02:00
2020-11-11 10:20:54 +01:00
urlParameterizedMessage = null ;
2021-01-15 17:02:30 +01:00
showCheckForSchemaEnhancements :boolean = false ;
2021-03-04 12:35:10 +01:00
private notification : Notification ;
2021-07-14 18:02:04 +02:00
@ViewChild ( 'editNumberNotify' , { static : true } ) editNumberNotify : NotifyFormComponent ;
@ViewChild ( 'editChartNotify' , { static : true } ) editChartNotify : NotifyFormComponent ;
@ViewChild ( 'deleteNotify' , { static : true } ) deleteNotify : NotifyFormComponent ;
2020-11-05 00:11:00 +01:00
constructor ( private layoutService : LayoutService ,
2019-12-15 22:25:56 +01:00
private stakeholderService : StakeholderService ,
private statisticsService : StatisticsService ,
2020-06-09 16:49:52 +02:00
private userManagementService : UserManagementService ,
2019-12-15 22:25:56 +01:00
private fb : FormBuilder ,
2020-01-10 15:20:42 +01:00
private router : Router ,
2020-05-12 13:05:53 +02:00
private cdr : ChangeDetectorRef ,
2019-12-15 22:25:56 +01:00
private sanitizer : DomSanitizer ) {
2021-07-16 10:58:27 +02:00
this . filesToUpload = [ ] ;
2019-12-15 22:25:56 +01:00
}
ngOnInit ( ) : void {
2020-11-13 17:42:12 +01:00
this . subscriptions . push ( this . userManagementService . getUserInfo ( ) . subscribe ( user = > {
2020-06-09 16:49:52 +02:00
this . user = user ;
2020-11-13 17:42:12 +01:00
} ) ) ;
this . subscriptions . push ( this . stakeholderService . getStakeholderAsObservable ( ) . subscribe ( stakeholder = > {
2020-06-24 12:13:29 +02:00
this . stakeholder = stakeholder ;
2021-01-14 19:56:12 +01:00
if ( this . stakeholder && this . firstLoad ) {
2020-06-24 12:13:29 +02:00
this . buildFilters ( ) ;
this . buildSections ( ) ;
2021-01-14 19:56:12 +01:00
this . filterCharts ( ) ;
this . filterNumbers ( ) ;
2020-06-26 12:52:58 +02:00
this . setPreview ( ) ;
2021-01-14 19:56:12 +01:00
this . firstLoad = false ;
2020-06-24 12:13:29 +02:00
}
2020-11-13 17:42:12 +01:00
} ) ) ;
2019-12-15 22:25:56 +01:00
}
ngOnDestroy ( ) : void {
this . subscriptions . forEach ( value = > {
if ( value instanceof Subscriber ) {
value . unsubscribe ( ) ;
} else if ( value instanceof Function ) {
value ( ) ;
}
} ) ;
2020-11-13 17:42:12 +01:00
this . urlSubscriptions . forEach ( value = > {
if ( value instanceof Subscriber ) {
value . unsubscribe ( ) ;
}
} ) ;
2019-12-15 22:25:56 +01:00
}
ngAfterViewInit ( ) : void {
2020-05-12 17:30:49 +02:00
this . initReorder ( ) ;
}
ngOnChanges ( changes : SimpleChanges ) : void {
if ( this . canEdit ) {
if ( changes . topicIndex || changes . categoryIndex || changes . subcategoryIndex ) {
2021-01-14 19:56:12 +01:00
this . buildSections ( ) ;
2020-05-12 17:30:49 +02:00
this . buildFilters ( ) ;
2020-06-17 12:37:04 +02:00
this . initReorder ( ) ;
2020-05-12 17:30:49 +02:00
}
this . filterCharts ( ) ;
this . filterNumbers ( ) ;
}
2020-06-24 12:13:29 +02:00
this . setPreview ( ) ;
}
2020-11-04 16:53:08 +01:00
setNumberIndicators() {
this . numberResults . clear ( ) ;
let urls : Map < string , [ number , number ] [ ] > = new Map < string , [ number , number ] [ ] > ( ) ;
this . numbers . forEach ( ( section , i ) = > {
section . indicators . forEach ( ( number , j ) = > {
2020-11-05 00:11:00 +01:00
let url = this . indicatorUtils . getFullUrlWithFilters ( this . stakeholder , number . indicatorPaths [ 0 ] ) ;
2020-11-04 16:53:08 +01:00
const pair = JSON . stringify ( [ number . indicatorPaths [ 0 ] . source , url ] ) ;
const indexes = urls . get ( pair ) ? urls . get ( pair ) : [ ] ;
indexes . push ( [ i , j ] ) ;
urls . set ( pair , indexes ) ;
} ) ;
} ) ;
urls . forEach ( ( indexes , pair ) = > {
pair = JSON . parse ( pair ) ;
2020-11-13 17:42:12 +01:00
this . subscriptions . push ( this . statisticsService . getNumbers ( this . statisticsService . getSourceType ( pair [ 0 ] ) , pair [ 1 ] ) . subscribe ( response = > {
2020-11-04 16:53:08 +01:00
indexes . forEach ( ( [ i , j ] ) = > {
let result = JSON . parse ( JSON . stringify ( response ) ) ;
this . numbers [ i ] . indicators [ j ] . indicatorPaths [ 0 ] . jsonPath . forEach ( jsonPath = > {
if ( result ) {
result = result [ jsonPath ] ;
}
} ) ;
2020-12-17 16:39:31 +01:00
if ( typeof result === 'string' || typeof result === 'number' ) {
result = Number ( result ) ;
if ( result === Number . NaN ) {
result = 0 ;
}
} else {
result = 0 ;
}
2020-11-11 10:20:54 +01:00
this . numberResults . set ( i + '-' + j , result ) ;
2020-11-04 16:53:08 +01:00
} ) ;
2020-11-13 17:42:12 +01:00
} ) ) ;
2020-11-04 16:53:08 +01:00
} ) ;
}
2020-06-24 12:13:29 +02:00
setPreview() {
2020-11-05 00:11:00 +01:00
if ( this . stakeholder ) {
2020-06-26 12:52:58 +02:00
this . preview = '/' + this . stakeholder . alias ;
2020-11-05 00:11:00 +01:00
if ( this . stakeholder . topics [ this . topicIndex ] ) {
2020-06-26 12:52:58 +02:00
this . preview = '/' + this . stakeholder . alias + '/' + this . stakeholder . topics [ this . topicIndex ] . alias ;
if ( this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] ) {
this . preview += '/' + this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . alias ;
if ( this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . subCategories [ this . subcategoryIndex ] ) {
this . preview += '/' + this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . subCategories [ this . subcategoryIndex ] . alias ;
}
2020-06-24 12:13:29 +02:00
}
2020-06-12 16:46:23 +02:00
}
}
2020-05-12 17:30:49 +02:00
}
initReorder() {
this . subscriptions . forEach ( value = > {
if ( value instanceof Function ) {
value ( ) ;
}
} ) ;
2019-12-15 22:25:56 +01:00
if ( document !== undefined ) {
2020-12-09 14:24:32 +01:00
let callback = ( list , type : IndicatorType , action : 'moved' | 'added' | 'removed' ) : void = > {
2019-12-15 22:25:56 +01:00
let items : HTMLCollection = list . current . children ;
let reordered = [ ] ;
for ( let i = 0 ; i < items . length ; i ++ ) {
if ( items . item ( i ) . id ) {
reordered . push ( items . item ( i ) . id ) ;
}
2019-12-13 16:56:48 +01:00
}
2020-12-09 14:24:32 +01:00
let reorder : Reorder = {
action : action ,
target : list.detail [ 1 ] . id ,
ids : reordered
}
this . reorderIndicators ( list . current . id . toString ( ) . split ( '-' ) [ 1 ] , type , reorder ) ;
2019-12-15 22:25:56 +01:00
} ;
2020-01-08 12:00:50 +01:00
this . numbers . forEach ( ( section ) = > {
2020-06-15 18:58:06 +02:00
this . subscriptions . push ( UIkit . util . on ( document , 'moved' , '#number-' + section . _id , ( list ) : void = > {
2020-12-09 14:24:32 +01:00
callback ( list , "number" , 'moved' ) ;
2020-01-08 12:00:50 +01:00
} ) ) ;
2020-06-15 18:58:06 +02:00
this . subscriptions . push ( UIkit . util . on ( document , 'added' , '#number-' + section . _id , ( list ) : void = > {
2020-12-09 14:24:32 +01:00
callback ( list , "number" , 'added' ) ;
2020-01-08 12:00:50 +01:00
} ) ) ;
2020-06-15 18:58:06 +02:00
this . subscriptions . push ( UIkit . util . on ( document , 'removed' , '#number-' + section . _id , ( list ) : void = > {
2020-12-09 14:24:32 +01:00
callback ( list , "number" , 'removed' ) ;
2020-01-08 12:00:50 +01:00
} ) ) ;
} ) ;
this . charts . forEach ( ( section ) = > {
this . subscriptions . push ( UIkit . util . on ( document , 'moved' , '#chart-' + section . _id , ( list ) : void = > {
2020-12-09 14:24:32 +01:00
callback ( list , "chart" , 'moved' ) ;
2020-01-08 12:00:50 +01:00
} ) ) ;
this . subscriptions . push ( UIkit . util . on ( document , 'added' , '#chart-' + section . _id , ( list ) : void = > {
2020-12-09 14:24:32 +01:00
callback ( list , "chart" , 'added' ) ;
2020-01-08 12:00:50 +01:00
} ) ) ;
this . subscriptions . push ( UIkit . util . on ( document , 'removed' , '#chart-' + section . _id , ( list ) : void = > {
2020-12-09 14:24:32 +01:00
callback ( list , "chart" , 'removed' ) ;
2020-01-08 12:00:50 +01:00
} ) ) ;
} ) ;
2019-12-15 22:25:56 +01:00
}
}
2020-01-10 15:20:42 +01:00
hide ( element : any ) {
UIkit . dropdown ( element ) . hide ( ) ;
2019-12-15 22:25:56 +01:00
}
2020-11-05 00:11:00 +01:00
2019-12-15 22:25:56 +01:00
private buildFilters() {
this . subscriptions . push ( this . filters . get ( 'chartType' ) . valueChanges . subscribe ( value = > {
this . onChartTypeChange ( value ) ;
} ) ) ;
2020-11-05 00:11:00 +01:00
2019-12-15 22:25:56 +01:00
this . subscriptions . push ( this . filters . get ( 'status' ) . valueChanges . subscribe ( value = > {
this . onStatusChange ( value ) ;
} ) ) ;
this . subscriptions . push ( this . filters . get ( 'keyword' ) . valueChanges . subscribe ( value = > {
this . onKeywordChange ( value ) ;
} ) ) ;
}
2020-01-14 20:48:13 +01:00
private buildSections() {
this . numberSections = this . fb . array ( [ ] ) ;
this . numbers . forEach ( section = > {
this . numberSections . push ( this . fb . group ( {
_id : this.fb.control ( section . _id ) ,
title : this.fb.control ( section . title ) ,
2020-11-16 17:48:07 +01:00
creationDate : this.fb.control ( section . creationDate ) ,
2020-01-14 20:48:13 +01:00
stakeholderAlias : this.fb.control ( section . stakeholderAlias ) ,
defaultId : this.fb.control ( section . defaultId ) ,
type : this . fb . control ( section . type ) ,
indicators : this.fb.control ( section . indicators )
} ) ) ;
} ) ;
this . chartSections = this . fb . array ( [ ] ) ;
this . charts . forEach ( section = > {
this . chartSections . push ( this . fb . group ( {
_id : this.fb.control ( section . _id ) ,
title : this.fb.control ( section . title ) ,
2020-11-16 17:48:07 +01:00
creationDate : this.fb.control ( section . creationDate ) ,
2020-01-14 20:48:13 +01:00
stakeholderAlias : this.fb.control ( section . stakeholderAlias ) ,
defaultId : this.fb.control ( section . defaultId ) ,
type : this . fb . control ( section . type ) ,
indicators : this.fb.control ( section . indicators )
} ) ) ;
} ) ;
}
2019-12-15 22:25:56 +01:00
filterCharts() {
2020-10-23 15:58:16 +02:00
this . displayCharts = this . filterChartType (
2020-01-10 15:20:42 +01:00
this . filterStatus ( this . filterByKeyword ( HelperFunctions . copy ( this . charts ) , this . filters . value . keyword ) ,
2019-12-15 22:25:56 +01:00
this . filters . value . status ) ,
this . filters . value . chartType
) ;
2020-05-12 13:05:53 +02:00
this . displayCharts . forEach ( section = > {
section . indicators . forEach ( indicator = > {
indicator . indicatorPaths . forEach ( indicatorPath = > {
2020-06-02 14:45:08 +02:00
let url = this . indicatorUtils . getFullUrl ( this . stakeholder , indicatorPath ) ;
2020-05-13 17:10:44 +02:00
if ( ! this . safeUrls . get ( 'url' ) ) {
indicatorPath . safeResourceUrl = this . getSecureUrlByStakeHolder ( indicatorPath ) ;
this . safeUrls . set ( url , indicatorPath . safeResourceUrl ) ;
}
2020-05-12 13:05:53 +02:00
} ) ;
} )
} ) ;
2020-05-12 17:30:49 +02:00
this . buildSections ( ) ;
2019-12-15 22:25:56 +01:00
}
filterNumbers() {
2020-10-23 15:58:16 +02:00
this . displayNumbers = this . filterStatus (
2020-01-10 15:20:42 +01:00
this . filterByKeyword ( HelperFunctions . copy ( this . numbers ) , this . filters . value . keyword ) ,
2020-10-23 15:58:16 +02:00
this . filters . value . status ) ;
2020-05-12 17:30:49 +02:00
this . buildSections ( ) ;
2020-11-04 16:53:08 +01:00
this . setNumberIndicators ( ) ;
2019-12-15 22:25:56 +01:00
}
onChartTypeChange ( value ) {
2020-01-10 15:20:42 +01:00
this . displayCharts = this . filterChartType ( HelperFunctions . copy ( this . charts ) , value ) ;
2019-12-15 22:25:56 +01:00
}
2020-11-05 00:11:00 +01:00
2019-12-15 22:25:56 +01:00
onStatusChange ( value ) {
2020-01-10 15:20:42 +01:00
this . displayCharts = this . filterStatus ( HelperFunctions . copy ( this . charts ) , value ) ;
this . displayNumbers = this . filterStatus ( HelperFunctions . copy ( this . numbers ) , value ) ;
2019-12-15 22:25:56 +01:00
}
onKeywordChange ( value ) {
2020-01-10 15:20:42 +01:00
this . displayCharts = this . filterByKeyword ( HelperFunctions . copy ( this . charts ) , value ) ;
this . displayNumbers = this . filterByKeyword ( HelperFunctions . copy ( this . numbers ) , value ) ;
2019-12-15 22:25:56 +01:00
}
2020-01-08 12:00:50 +01:00
private filterChartType ( sections : Section [ ] , value ) : Section [ ] {
2020-01-10 15:20:42 +01:00
if ( value !== 'all' ) {
sections . forEach ( section = >
2020-01-08 12:00:50 +01:00
section . indicators = section . indicators . filter ( indicator = >
indicator . indicatorPaths . filter ( indicatorPath = > indicatorPath . type === value ) . length > 0 ) ) ;
2019-12-15 22:25:56 +01:00
}
2020-01-10 15:20:42 +01:00
return sections ;
2019-12-15 22:25:56 +01:00
}
2020-01-08 12:00:50 +01:00
private filterStatus ( sections : Section [ ] , value ) : Section [ ] {
2020-01-10 15:20:42 +01:00
if ( value !== 'all' ) {
sections . forEach ( section = >
2020-10-23 15:58:16 +02:00
section . indicators = section . indicators . filter ( indicator = > indicator . visibility === value ) ) ;
2019-12-15 22:25:56 +01:00
}
2020-01-10 15:20:42 +01:00
return sections ;
2019-12-15 22:25:56 +01:00
}
2020-01-08 12:00:50 +01:00
private filterByKeyword ( sections : Section [ ] , value ) : Section [ ] {
2020-01-10 15:20:42 +01:00
if ( value !== null && value !== '' ) {
sections . forEach ( section = >
2020-01-08 12:00:50 +01:00
section . indicators = section . indicators . filter ( indicator = > ( indicator . name && indicator . name . toLowerCase ( ) . includes ( value . toLowerCase ( ) ) )
2020-06-09 16:49:52 +02:00
|| ( indicator . description && indicator . description . toLowerCase ( ) . includes ( value . toLowerCase ( ) ) )
2020-11-05 00:11:00 +01:00
|| ( indicator . additionalDescription && indicator . additionalDescription . toLowerCase ( ) . includes ( value . toLowerCase ( ) ) )
2020-06-09 16:49:52 +02:00
|| indicator . indicatorPaths . filter ( indicatorPath = > ( indicatorPath . parameters && indicatorPath . parameters . title &&
indicatorPath . parameters . title . includes ( value . toLowerCase ( ) ) ) ) . length > 0 ) ) ;
2019-12-15 22:25:56 +01:00
}
2020-01-10 15:20:42 +01:00
return sections ;
2019-12-15 22:25:56 +01:00
}
2020-01-08 12:00:50 +01:00
get charts ( ) : Section [ ] {
2020-05-13 14:15:25 +02:00
if ( this . stakeholder . topics [ this . topicIndex ] &&
this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] &&
this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . subCategories [ this . subcategoryIndex ] ) {
2020-05-13 10:46:22 +02:00
return this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . subCategories [ this . subcategoryIndex ] . charts ;
} else {
return [ ] ;
}
2019-12-15 22:25:56 +01:00
}
2020-01-08 12:00:50 +01:00
set charts ( sections : Section [ ] ) {
this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . subCategories [ this . subcategoryIndex ] . charts = sections ;
2019-12-15 22:25:56 +01:00
}
2020-01-08 12:00:50 +01:00
get numbers ( ) : Section [ ] {
2020-05-13 14:15:25 +02:00
if ( this . stakeholder . topics [ this . topicIndex ] &&
this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] &&
this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . subCategories [ this . subcategoryIndex ] ) {
2020-05-13 10:46:22 +02:00
return this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . subCategories [ this . subcategoryIndex ] . numbers ;
} else {
return [ ] ;
}
2019-12-15 22:25:56 +01:00
}
2020-01-08 12:00:50 +01:00
set numbers ( sections : Section [ ] ) {
this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . subCategories [ this . subcategoryIndex ] . numbers = sections ;
2019-12-15 22:25:56 +01:00
}
get open ( ) : boolean {
return this . layoutService . open ;
}
2020-01-08 12:00:50 +01:00
get canReorder ( ) : boolean {
2020-10-23 15:58:16 +02:00
return this . filters . value . chartType === 'all' &&
this . filters . value . status === 'all' && this . filters . value . keyword === '' && ! this . editing ;
2019-12-15 22:25:56 +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 ] ;
}
2020-06-09 16:49:52 +02:00
public get numberIndicatorPaths ( ) : FormArray {
return this . numberIndicatorFb . get ( 'indicatorPaths' ) as FormArray ;
}
public get chartIndicatorPaths ( ) : FormArray {
return this . chartIndicatorFb . get ( 'indicatorPaths' ) as FormArray ;
}
2020-12-01 16:43:43 +01:00
public getNumberClassBySize ( size : IndicatorSize ) {
2020-12-09 14:24:32 +01:00
if ( size === 'small' ) {
2020-12-01 16:43:43 +01:00
return 'uk-width-1-4@xl uk-width-1-3@l uk-width-1-2@m uk-width-1-1' ;
2020-12-09 14:24:32 +01:00
} else if ( size === 'medium' ) {
2020-12-01 16:43:43 +01:00
return 'uk-width-1-3@l uk-width-1-2@m uk-width-1-1' ;
} else {
return 'uk-width-1-2@l uk-width-1-1@m uk-width-1-1' ;
}
}
public getChartClassBySize ( size : IndicatorSize ) {
2020-12-09 14:24:32 +01:00
if ( size === 'small' ) {
2020-12-01 16:43:43 +01:00
return 'uk-width-1-3@xl uk-width-1-2@m uk-width-1-1' ;
2020-12-09 14:24:32 +01:00
} else if ( size === 'medium' ) {
2020-12-01 16:43:43 +01:00
return 'uk-width-1-2@l uk-width-1-1' ;
} else {
return 'uk-width-1-1' ;
}
}
2020-06-09 16:49:52 +02:00
public addJsonPath ( index : number ) {
2020-11-05 00:11:00 +01:00
if ( index == 0 && this . getJsonPath ( index ) . getRawValue ( ) [ index ] . indexOf ( "," ) != - 1 ) {
2020-12-17 16:39:31 +01:00
//if in the first path there are more than one paths comma separated, split them and autogenerate the forms
2020-08-06 09:34:41 +02:00
let paths = this . getJsonPath ( index ) . getRawValue ( ) [ index ] . split ( "," ) ;
2020-11-05 00:11:00 +01:00
for ( let i = 0 ; i < paths . length ; i ++ ) {
if ( i != 0 ) {
2020-08-06 09:34:41 +02:00
this . getJsonPath ( index ) . push ( this . fb . control ( '' , Validators . required ) ) ;
}
}
this . getJsonPath ( index ) . setValue ( paths )
2020-12-17 16:39:31 +01:00
} else {
this . getJsonPath ( index ) . push ( this . fb . control ( '' , Validators . required ) ) ;
2020-08-06 09:34:41 +02:00
}
2020-06-09 16:49:52 +02:00
}
2020-11-05 00:11:00 +01:00
public removeJsonPath ( i : number , j : number ) {
2021-01-14 11:02:55 +01:00
if ( this . getJsonPath ( i ) . enabled ) {
2020-12-17 16:39:31 +01:00
this . getJsonPath ( i ) . removeAt ( j ) ;
2020-11-05 00:11:00 +01:00
}
}
2020-12-17 16:39:31 +01:00
public validateJsonPath ( index : number , dirty : boolean = false ) {
2020-11-05 00:11:00 +01:00
let indicatorPath : FormGroup = < FormGroup > this . numberIndicatorPaths . at ( index ) ;
2020-12-09 14:24:32 +01:00
if ( this . indicator . defaultId === null ) {
2020-11-16 17:48:07 +01:00
this . getJsonPath ( index ) . disable ( ) ;
}
2020-11-11 10:20:54 +01:00
indicatorPath . get ( 'result' ) . setErrors ( { validating : true } ) ;
2020-11-13 17:42:12 +01:00
this . subscriptions . push ( this . statisticsService . getNumbers ( null , indicatorPath . get ( 'url' ) . value ) . subscribe ( response = > {
2020-11-05 00:11:00 +01:00
let result = JSON . parse ( JSON . stringify ( response ) ) ;
this . getJsonPath ( index ) . controls . forEach ( jsonPath = > {
if ( result ) {
result = result [ jsonPath . value ] ;
}
} ) ;
2020-11-11 10:20:54 +01:00
setTimeout ( ( ) = > {
2020-12-09 14:24:32 +01:00
if ( this . indicator . defaultId === null ) {
2020-11-16 17:48:07 +01:00
this . getJsonPath ( index ) . enable ( ) ;
2021-01-14 11:02:55 +01:00
if ( dirty ) {
2020-12-17 16:39:31 +01:00
this . getJsonPath ( index ) . markAsDirty ( ) ;
}
2020-11-16 17:48:07 +01:00
}
2020-11-11 10:20:54 +01:00
indicatorPath . get ( 'result' ) . setErrors ( null ) ;
2020-12-09 14:24:32 +01:00
if ( typeof result === 'string' || typeof result === 'number' ) {
2020-11-11 10:20:54 +01:00
result = Number ( result ) ;
if ( result !== Number . NaN ) {
indicatorPath . get ( 'result' ) . setValue ( result ) ;
} else {
2020-12-17 16:39:31 +01:00
indicatorPath . get ( 'result' ) . setValue ( 0 ) ;
2020-11-11 10:20:54 +01:00
}
2020-11-05 00:11:00 +01:00
} else {
2020-12-17 16:39:31 +01:00
indicatorPath . get ( 'result' ) . setValue ( 0 ) ;
}
} , 500 ) ;
} , error = > {
setTimeout ( ( ) = > {
if ( this . indicator . defaultId === null ) {
this . getJsonPath ( index ) . enable ( ) ;
if ( dirty ) {
this . getJsonPath ( index ) . markAsDirty ( ) ;
}
2020-11-05 00:11:00 +01:00
}
2020-12-17 16:39:31 +01:00
indicatorPath . get ( 'result' ) . setErrors ( null ) ;
indicatorPath . get ( 'result' ) . setValue ( 0 ) ;
2020-11-11 10:20:54 +01:00
} , 500 ) ;
2020-11-13 17:42:12 +01:00
} ) ) ;
2020-06-09 16:49:52 +02:00
}
public getJsonPath ( index : number ) : FormArray {
return this . numberIndicatorPaths . at ( index ) . get ( 'jsonPath' ) as FormArray ;
2019-12-15 22:25:56 +01:00
}
2020-11-05 00:11:00 +01:00
public getCurrentJsonPath ( index : number ) : string [ ] {
return this . section . indicators [ this . index ] . indicatorPaths [ index ] . jsonPath ;
}
2019-12-15 22:25:56 +01:00
public getParameters ( index : number ) : FormArray {
2020-06-09 16:49:52 +02:00
return this . chartIndicatorPaths . at ( index ) . get ( 'parameters' ) as FormArray ;
2019-12-15 22:25:56 +01:00
}
public getParameter ( index : number , key : string ) : FormControl {
return this . getParameters ( index ) . controls . filter ( control = > control . value . key === key ) [ 0 ] as FormControl ;
}
private getSecureUrlByStakeHolder ( indicatorPath : IndicatorPath ) {
return this . sanitizer . bypassSecurityTrustResourceUrl (
2020-06-02 14:45:08 +02:00
this . statisticsService . getChartUrl ( indicatorPath . source , this . indicatorUtils . getFullUrl ( this . stakeholder , indicatorPath ) ) ) ;
2019-12-15 22:25:56 +01:00
}
private getUrlByStakeHolder ( indicatorPath : IndicatorPath ) {
2020-06-02 14:45:08 +02:00
return this . statisticsService . getChartUrl ( indicatorPath . source , this . indicatorUtils . getFullUrl ( this . stakeholder , indicatorPath ) ) ;
2019-12-15 22:25:56 +01:00
}
2020-11-19 17:16:36 +01:00
public addNumberIndicatorPath ( url : string = '' , parameters : FormArray = new FormArray ( [ ] ) , source : string = 'stats-tool' , jsonPath : FormArray = new FormArray ( [ ] ) ) {
2020-11-05 00:11:00 +01:00
if ( jsonPath . length === 0 ) {
2020-06-09 16:49:52 +02:00
jsonPath . push ( this . fb . control ( '' , Validators . required ) ) ;
}
this . numberIndicatorPaths . push ( this . fb . group ( {
2020-12-17 16:39:31 +01:00
url : this.fb.control ( url , [ Validators . required , StringUtils . urlValidator ( ) ] ) ,
2020-06-09 16:49:52 +02:00
jsonPath : jsonPath ,
2020-11-11 10:20:54 +01:00
result : this.fb.control ( 0 , Validators . required ) ,
2020-07-07 16:08:47 +02:00
// parameters: parameters,
2020-06-09 16:49:52 +02:00
source : this.fb.control ( source , Validators . required )
}
) ) ;
2021-01-14 11:02:55 +01:00
let index = this . numberIndicatorPaths . length - 1 ;
if ( this . numberIndicatorPaths . at ( index ) . get ( 'url' ) . valid ) {
this . validateJsonPath ( index ) ;
2021-01-15 17:02:30 +01:00
this . checkForSchemaEnhancements ( this . numberIndicatorPaths . at ( index ) . get ( 'url' ) . value ) ;
2021-01-14 11:02:55 +01:00
}
if ( this . indicator . defaultId === null ) {
this . subscriptions . push ( this . numberIndicatorPaths . at ( index ) . get ( 'url' ) . valueChanges . subscribe ( value = > {
this . numberIndicatorPaths . at ( index ) . get ( 'result' ) . setValue ( null ) ;
if ( this . numberIndicatorPaths . at ( index ) . get ( 'url' ) . valid ) {
let indicatorPath : IndicatorPath = this . indicatorUtils . generateIndicatorByNumberUrl ( this . statisticsService . getNumberSource ( value ) , value , this . stakeholder , this . numberIndicatorPaths . at ( index ) . get ( 'jsonPath' ) . value , this . statisticsService . numberSources . get ( this . statisticsService . getNumberSource ( value ) ) ) ;
2021-07-16 13:31:31 +02:00
if ( ! this . isStakeholderParametersValid ( indicatorPath ) ) {
2021-01-14 11:02:55 +01:00
// default profile
if ( this . stakeholder . defaultId == null ) {
this . urlParameterizedMessage = "This indicator couldn't be generated properly. Stakeholders based on this profile may not inherit the data correctly."
2020-11-16 17:48:07 +01:00
} else {
2021-01-14 11:02:55 +01:00
this . urlParameterizedMessage = "This indicator couldn't be generated properly. Please make sure chart data is for the current stakeholder."
2020-11-19 17:16:36 +01:00
}
2020-07-07 16:08:47 +02:00
} else {
2020-11-11 10:20:54 +01:00
this . urlParameterizedMessage = null ;
2020-07-07 16:08:47 +02:00
}
2021-01-15 17:02:30 +01:00
this . checkForSchemaEnhancements ( this . numberIndicatorPaths . at ( index ) . get ( 'url' ) . value ) ;
2020-11-11 10:20:54 +01:00
if ( this . indicator . indicatorPaths [ index ] ) {
2021-01-14 11:02:55 +01:00
this . indicator . indicatorPaths [ index ] = indicatorPath ;
} else {
this . indicator . indicatorPaths . push ( indicatorPath ) ;
2020-11-11 10:20:54 +01:00
}
2021-01-14 11:02:55 +01:00
if ( indicatorPath . source ) {
this . numberIndicatorPaths . at ( index ) . get ( 'source' ) . setValue ( indicatorPath . source ) ;
2020-11-11 10:20:54 +01:00
}
2021-01-14 11:02:55 +01:00
if ( indicatorPath . jsonPath . length > 1 && this . getJsonPath ( index ) . length == 1 ) {
let paths = indicatorPath . jsonPath ;
for ( let i = 0 ; i < paths . length ; i ++ ) {
if ( i == this . getJsonPath ( index ) . length ) {
this . getJsonPath ( index ) . push ( this . fb . control ( '' , Validators . required ) ) ;
}
}
this . getJsonPath ( index ) . setValue ( paths )
}
} else {
this . urlParameterizedMessage = null ;
}
} )
) ;
this . subscriptions . push ( this . numberIndicatorPaths . at ( index ) . get ( 'jsonPath' ) . valueChanges . subscribe ( value = > {
if ( this . indicator . indicatorPaths [ index ] ) {
this . indicator . indicatorPaths [ index ] . jsonPath = value ;
}
this . numberIndicatorPaths . at ( index ) . get ( 'result' ) . setValue ( null ) ;
} )
) ;
this . subscriptions . push ( this . numberIndicatorPaths . at ( index ) . get ( 'source' ) . valueChanges . subscribe ( value = > {
if ( this . indicator . indicatorPaths [ index ] ) {
this . indicator . indicatorPaths [ index ] . source = value ;
}
} )
) ;
} else {
this . numberIndicatorPaths . at ( index ) . get ( 'url' ) . disable ( ) ;
this . numberIndicatorPaths . at ( index ) . get ( 'jsonPath' ) . disable ( ) ;
this . numberIndicatorPaths . at ( index ) . get ( 'source' ) . disable ( ) ;
2020-06-16 09:30:45 +02:00
}
2020-06-09 16:49:52 +02:00
}
2020-11-05 00:11:00 +01:00
public addChartIndicatorPath ( value : string = '' , parameters : FormArray = new FormArray ( [ ] ) , disableUrl : boolean = false , type : string = null ) {
2020-06-09 16:49:52 +02:00
this . chartIndicatorPaths . push ( this . fb . group ( {
2020-12-17 16:39:31 +01:00
url : this.fb.control ( value , [ Validators . required , StringUtils . urlValidator ( ) ] ) ,
2020-06-02 17:44:45 +02:00
parameters : parameters ,
type : this . fb . control ( type )
2019-12-15 22:25:56 +01:00
}
) ) ;
2021-01-14 11:02:55 +01:00
let index = this . chartIndicatorPaths . length - 1 ;
2020-11-05 00:11:00 +01:00
if ( disableUrl ) {
2021-01-14 11:02:55 +01:00
this . chartIndicatorPaths . at ( index ) . get ( 'url' ) . disable ( ) ;
2020-05-15 14:26:59 +02:00
} else {
2021-01-15 17:02:30 +01:00
this . checkForSchemaEnhancements ( this . chartIndicatorPaths . at ( index ) . get ( 'url' ) . value ) ;
2021-01-14 11:02:55 +01:00
this . urlSubscriptions . push ( this . chartIndicatorPaths . at ( index ) . get ( 'url' ) . valueChanges . subscribe ( value = > {
if ( this . chartIndicatorPaths . at ( index ) . get ( 'url' ) . valid ) {
let indicatorPath : IndicatorPath = this . indicatorUtils . generateIndicatorByChartUrl ( this . statisticsService . getChartSource ( value ) , value , this . chartIndicatorPaths . at ( index ) . get ( 'type' ) . value , this . stakeholder ) ;
2021-07-16 13:31:31 +02:00
if ( ! this . isStakeholderParametersValid ( indicatorPath ) ) {
2021-01-14 11:02:55 +01:00
// default profile
if ( this . stakeholder . defaultId == null ) {
this . urlParameterizedMessage = "This chart couldn't be generated properly. Stakeholders based on this profile may not inherit the data correctly."
2020-11-11 10:20:54 +01:00
} else {
2021-01-14 11:02:55 +01:00
this . urlParameterizedMessage = "This chart couldn't be generated properly. Please make sure chart data is for the current stakeholder."
2020-05-15 14:26:59 +02:00
}
2021-01-14 11:02:55 +01:00
} else {
this . urlParameterizedMessage = null ;
}
2021-01-15 17:02:30 +01:00
this . checkForSchemaEnhancements ( this . chartIndicatorPaths . at ( index ) . get ( 'url' ) . value ) ;
2021-01-14 11:02:55 +01:00
( this . chartIndicatorPaths . at ( index ) as FormGroup ) . get ( 'type' ) . setValue ( indicatorPath . type ) ;
let parameters = this . getParametersAsFormArray ( indicatorPath ) ;
( this . chartIndicatorPaths . at ( index ) as FormGroup ) . setControl ( 'parameters' , parameters ) ;
if ( ! this . indicator . indicatorPaths [ index ] ) {
this . indicator . indicatorPaths [ index ] = indicatorPath ;
this . indicator . indicatorPaths [ index ] . safeResourceUrl = this . getSecureUrlByStakeHolder ( indicatorPath ) ;
} else {
indicatorPath . safeResourceUrl = this . indicator . indicatorPaths [ index ] . safeResourceUrl ;
this . indicator . indicatorPaths [ index ] = indicatorPath ;
}
} else {
this . urlParameterizedMessage = null ;
}
} ) ) ;
2020-05-15 14:26:59 +02:00
}
2019-12-15 22:25:56 +01:00
}
2021-07-16 13:31:31 +02:00
private isStakeholderParametersValid ( indicatorPath :IndicatorPath ) {
return ! ( ( indicatorPath . chartObject && Object . keys ( indicatorPath . parameters ) . indexOf ( "index_id" ) == - 1 && Object . keys ( indicatorPath . parameters ) . indexOf ( "index_name" ) == - 1 && Object . keys ( indicatorPath . parameters ) . indexOf ( "index_shortName" ) == - 1 )
|| ( ! indicatorPath . chartObject && indicatorPath . url . indexOf ( "index_id" ) == - 1 && indicatorPath . url . indexOf ( "index_name" ) == - 1 && ( indicatorPath . url ) . indexOf ( "index_shortName" ) == - 1 ) ) ;
}
2020-06-09 16:49:52 +02:00
private getJsonPathAsFormArray ( indicatorPath : IndicatorPath ) : FormArray {
let jsonPath = this . fb . array ( [ ] ) ;
if ( indicatorPath . jsonPath ) {
indicatorPath . jsonPath . forEach ( path = > {
jsonPath . push ( this . fb . control ( path , Validators . required ) ) ;
} ) ;
}
return jsonPath ;
}
2019-12-15 22:25:56 +01:00
private getParametersAsFormArray ( indicatorPath : IndicatorPath ) : FormArray {
let parameters = this . fb . array ( [ ] ) ;
if ( indicatorPath . parameters ) {
Object . keys ( indicatorPath . parameters ) . forEach ( key = > {
if ( this . indicatorUtils . ignoredParameters . indexOf ( key ) === - 1 ) {
if ( this . indicatorUtils . parametersValidators . has ( key ) ) {
parameters . push ( this . fb . group ( {
key : this.fb.control ( key ) ,
value : this.fb.control ( indicatorPath . parameters [ key ] , this . indicatorUtils . parametersValidators . get ( key ) )
} ) ) ;
} else {
parameters . push ( this . fb . group ( {
key : this.fb.control ( key ) ,
value : this.fb.control ( indicatorPath . parameters [ key ] )
} ) ) ;
}
2019-11-27 11:09:14 +01:00
}
2019-12-15 22:25:56 +01:00
} ) ;
}
return parameters ;
}
2020-06-09 16:49:52 +02:00
public editNumberIndicatorOpen ( section : Section , id = null ) {
2020-11-11 10:20:54 +01:00
this . urlParameterizedMessage = null ;
2020-06-09 16:49:52 +02:00
this . section = section ;
this . index = ( id ) ? section . indicators . findIndex ( value = > value . _id === id ) : - 1 ;
if ( this . index !== - 1 ) {
this . indicator = HelperFunctions . copy ( this . section . indicators [ this . index ] ) ;
this . numberIndicatorFb = this . fb . group ( {
_id : this.fb.control ( this . indicator . _id ) ,
2020-06-24 12:13:29 +02:00
name : this.fb.control ( this . indicator . name , Validators . required ) ,
2020-06-09 16:49:52 +02:00
description : this.fb.control ( this . indicator . description ) ,
2020-11-16 17:48:07 +01:00
creationDate : this.fb.control ( this . indicator . creationDate ) ,
2020-10-29 10:17:39 +01:00
additionalDescription : this.fb.control ( this . indicator . additionalDescription ) ,
2020-10-23 15:58:16 +02:00
visibility : this.fb.control ( this . indicator . visibility ) ,
2020-06-09 16:49:52 +02:00
indicatorPaths : this.fb.array ( [ ] , Validators . required ) ,
type : this . fb . control ( this . indicator . type ) ,
width : this.fb.control ( this . indicator . width ) ,
2020-11-27 11:46:58 +01:00
height : this.fb.control ( this . indicator . height ) ,
2020-06-09 16:49:52 +02:00
defaultId : this.fb.control ( this . indicator . defaultId )
} ) ;
this . indicator . indicatorPaths . forEach ( indicatorPath = > {
2020-11-05 00:11:00 +01:00
this . addNumberIndicatorPath ( this . statisticsService . getNumberUrl ( indicatorPath . source , this . indicatorUtils . getFullUrl ( this . stakeholder , indicatorPath ) ) , indicatorPath . parameters , indicatorPath . source , this . getJsonPathAsFormArray ( indicatorPath ) ) ;
2020-06-09 16:49:52 +02:00
} ) ;
} else {
2020-11-27 11:46:58 +01:00
this . indicator = new Indicator ( '' , '' , '' , 'number' , 'small' , 'small' , "PUBLIC" , [ ] ) ;
2020-06-09 16:49:52 +02:00
this . numberIndicatorFb = this . fb . group ( {
_id : this.fb.control ( this . indicator . _id ) ,
2020-06-24 12:13:29 +02:00
name : this.fb.control ( this . indicator . name , Validators . required ) ,
2020-06-09 16:49:52 +02:00
description : this.fb.control ( this . indicator . description ) ,
2020-10-29 10:17:39 +01:00
additionalDescription : this.fb.control ( this . indicator . additionalDescription ) ,
2020-10-23 15:58:16 +02:00
visibility : this.fb.control ( this . indicator . visibility ) ,
2020-06-09 16:49:52 +02:00
indicatorPaths : this.fb.array ( [ ] , Validators . required ) ,
type : this . fb . control ( this . indicator . type ) ,
width : this.fb.control ( this . indicator . width ) ,
2020-11-27 11:46:58 +01:00
height : this.fb.control ( this . indicator . height ) ,
2020-06-09 16:49:52 +02:00
defaultId : this.fb.control ( this . indicator . defaultId )
} ) ;
this . addNumberIndicatorPath ( ) ;
}
2020-11-11 10:20:54 +01:00
if ( this . indicator . defaultId ) {
2020-11-05 09:20:18 +01:00
setTimeout ( ( ) = > {
2020-11-20 11:52:17 +01:00
this . numberIndicatorFb . get ( 'description' ) . disable ( ) ;
2020-11-05 09:20:18 +01:00
} , 0 ) ;
}
2020-06-09 16:49:52 +02:00
this . editNumberModal . cancelButtonText = 'Cancel' ;
this . editNumberModal . okButtonLeft = false ;
this . editNumberModal . alertMessage = false ;
if ( this . index === - 1 ) {
this . editNumberModal . alertTitle = 'Create a new number indicator' ;
this . editNumberModal . okButtonText = 'Save' ;
2021-03-04 12:35:10 +01:00
this . notification = NotificationUtils . createIndicator ( this . user . firstname + ' ' + this . user . lastname , this . stakeholder . name ) ;
this . editNumberNotify . reset ( this . notification . message ) ;
2020-06-09 16:49:52 +02:00
} else {
this . editNumberModal . alertTitle = 'Edit number indicator\'s information' ;
this . editNumberModal . okButtonText = 'Save Changes' ;
2021-03-04 12:35:10 +01:00
this . notification = NotificationUtils . editIndicator ( this . user . firstname + ' ' + this . user . lastname , this . stakeholder . name ) ;
this . editNumberNotify . reset ( this . notification . message ) ;
2020-06-09 16:49:52 +02:00
}
this . editNumberModal . open ( ) ;
}
2021-07-16 13:31:31 +02:00
2020-01-08 12:00:50 +01:00
public editChartIndicatorOpen ( section : Section , id = null ) {
2020-11-11 10:20:54 +01:00
this . urlParameterizedMessage = null ;
2019-12-15 22:25:56 +01:00
this . urlSubscriptions . forEach ( value = > {
if ( value instanceof Subscriber ) {
value . unsubscribe ( ) ;
}
} ) ;
2020-01-08 12:00:50 +01:00
this . section = section ;
this . index = ( id ) ? section . indicators . findIndex ( value = > value . _id === id ) : - 1 ;
2019-12-15 22:25:56 +01:00
if ( this . index !== - 1 ) {
2020-01-08 12:00:50 +01:00
this . indicator = HelperFunctions . copy ( this . section . indicators [ this . index ] ) ;
2020-06-09 16:49:52 +02:00
this . chartIndicatorFb = this . fb . group ( {
_id : this.fb.control ( this . indicator . _id ) ,
name : this.fb.control ( this . indicator . name ) ,
2020-11-16 17:48:07 +01:00
creationDate : this.fb.control ( this . indicator . creationDate ) ,
2019-12-15 22:25:56 +01:00
description : this.fb.control ( this . indicator . description ) ,
2020-10-29 10:17:39 +01:00
additionalDescription : this.fb.control ( this . indicator . additionalDescription ) ,
2020-10-23 15:58:16 +02:00
visibility : this.fb.control ( this . indicator . visibility ) ,
2019-12-15 22:25:56 +01:00
indicatorPaths : this.fb.array ( [ ] ) ,
width : this.fb.control ( this . indicator . width ) ,
2020-11-27 11:46:58 +01:00
height : this.fb.control ( this . indicator . height ) ,
2020-05-15 14:26:59 +02:00
defaultId : this.fb.control ( this . indicator . defaultId )
2019-12-15 22:25:56 +01:00
} ) ;
this . indicator . indicatorPaths . forEach ( indicatorPath = > {
2020-06-09 16:49:52 +02:00
this . addChartIndicatorPath ( this . getUrlByStakeHolder ( indicatorPath ) ,
2020-06-02 17:44:45 +02:00
this . getParametersAsFormArray ( indicatorPath ) , this . indicator . defaultId !== null , indicatorPath . type ) ;
2020-05-12 13:05:53 +02:00
indicatorPath . safeResourceUrl = this . getSecureUrlByStakeHolder ( indicatorPath ) ;
2019-12-15 22:25:56 +01:00
} ) ;
} else {
2020-11-27 11:46:58 +01:00
this . indicator = new Indicator ( '' , '' , '' , 'chart' , 'medium' , 'medium' , "PUBLIC" , [ ] ) ;
2020-06-09 16:49:52 +02:00
this . chartIndicatorFb = this . fb . group ( {
2020-06-10 17:30:43 +02:00
_id : this.fb.control ( this . indicator . _id ) ,
name : this.fb.control ( this . indicator . name ) ,
description : this.fb.control ( this . indicator . description ) ,
2020-10-29 10:17:39 +01:00
additionalDescription : this.fb.control ( this . indicator . additionalDescription ) ,
2020-10-23 15:58:16 +02:00
visibility : this.fb.control ( this . indicator . visibility ) ,
2019-12-15 22:25:56 +01:00
indicatorPaths : this.fb.array ( [ ] ) ,
2020-06-10 17:30:43 +02:00
width : this.fb.control ( this . indicator . width , Validators . required ) ,
2020-11-27 11:46:58 +01:00
height : this.fb.control ( this . indicator . height , Validators . required ) ,
2020-06-10 17:30:43 +02:00
defaultId : this.fb.control ( this . indicator . defaultId )
2019-12-15 22:25:56 +01:00
} ) ;
2020-06-09 16:49:52 +02:00
this . addChartIndicatorPath ( ) ;
2019-12-15 22:25:56 +01:00
}
2020-11-11 10:20:54 +01:00
if ( this . indicator . defaultId ) {
2020-11-05 09:20:18 +01:00
setTimeout ( ( ) = > {
2020-11-20 11:52:17 +01:00
this . chartIndicatorFb . get ( 'description' ) . disable ( ) ;
2020-11-05 09:20:18 +01:00
} , 0 ) ;
}
2020-06-09 16:49:52 +02:00
this . editChartModal . cancelButtonText = 'Cancel' ;
this . editChartModal . okButtonLeft = false ;
this . editChartModal . alertMessage = false ;
2019-12-15 22:25:56 +01:00
if ( this . index === - 1 ) {
2020-06-09 16:49:52 +02:00
this . editChartModal . alertTitle = 'Create a new chart indicator' ;
this . editChartModal . okButtonText = 'Save' ;
2021-03-04 12:35:10 +01:00
this . notification = NotificationUtils . createIndicator ( this . user . firstname + ' ' + this . user . lastname , this . stakeholder . name ) ;
this . editChartNotify . reset ( this . notification . message ) ;
2019-12-15 22:25:56 +01:00
} else {
2020-06-09 16:49:52 +02:00
this . editChartModal . alertTitle = 'Edit chart indicator\'s information' ;
this . editChartModal . okButtonText = 'Save Changes' ;
2021-03-04 12:35:10 +01:00
this . notification = NotificationUtils . editIndicator ( this . user . firstname + ' ' + this . user . lastname , this . stakeholder . name ) ; ;
this . editChartNotify . reset ( this . notification . message ) ;
2019-12-15 22:25:56 +01:00
}
2020-06-09 16:49:52 +02:00
this . editChartModal . open ( ) ;
2019-12-15 22:25:56 +01:00
}
saveIndicator() {
2020-06-16 10:23:51 +02:00
this . editing = true ;
2019-12-15 22:25:56 +01:00
if ( this . indicator . type === 'chart' ) {
2020-11-20 11:52:17 +01:00
this . chartIndicatorFb . get ( 'description' ) . enable ( ) ;
2020-07-07 16:08:47 +02:00
this . indicator = this . indicatorUtils . generateIndicatorByForm ( this . chartIndicatorFb . value , this . indicator . indicatorPaths , this . indicator . type , true ) ;
2020-05-12 13:05:53 +02:00
this . section = this . charts . find ( section = > section . _id === this . section . _id ) ;
2020-06-09 16:49:52 +02:00
} else {
2020-11-20 11:52:17 +01:00
this . numberIndicatorFb . get ( 'description' ) . enable ( ) ;
2020-07-07 16:08:47 +02:00
this . indicator = this . indicatorUtils . generateIndicatorByForm ( this . numberIndicatorFb . value , this . indicator . indicatorPaths , this . indicator . type , false ) ;
2020-06-24 12:13:29 +02:00
this . section = this . numbers . find ( section = > section . _id === this . section . _id ) ;
2019-12-15 22:25:56 +01:00
}
let path = [
this . stakeholder . _id ,
this . stakeholder . topics [ this . topicIndex ] . _id ,
this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . _id ,
2020-01-08 12:00:50 +01:00
this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . subCategories [ this . subcategoryIndex ] . _id ,
this . section . _id
2019-12-15 22:25:56 +01:00
] ;
2020-11-13 17:42:12 +01:00
this . subscriptions . push ( this . stakeholderService . saveElement ( this . properties . monitorServiceAPIURL , this . indicator , path ) . subscribe ( indicator = > {
2019-12-15 22:25:56 +01:00
if ( this . index !== - 1 ) {
2020-01-08 12:00:50 +01:00
this . section . indicators [ this . index ] = indicator ;
2019-12-15 22:25:56 +01:00
} else {
2020-01-08 12:00:50 +01:00
this . section . indicators . push ( indicator ) ;
2019-12-15 22:25:56 +01:00
}
2021-03-04 12:35:10 +01:00
this . notification . entity = indicator . _id ;
this . notification . stakeholder = this . stakeholder . alias ;
this . notification . stakeholderType = this . stakeholder . type ;
this . notification . groups = [ Role . curator ( this . stakeholder . type ) ] ;
if ( this . stakeholder . defaultId ) {
this . notification . groups . push ( Role . manager ( this . stakeholder . type , this . stakeholder . alias ) ) ;
if ( this . indicator . type === "chart" ) {
this . filterCharts ( ) ;
this . chartIndicatorFb = null ;
this . editChartNotify . sendNotification ( this . notification ) ;
} else {
this . filterNumbers ( ) ;
this . numberIndicatorFb = null ;
this . editNumberNotify . sendNotification ( this . notification ) ;
}
2020-06-24 12:13:29 +02:00
} else {
2021-03-04 12:35:10 +01:00
this . stakeholderService . getStakeholders ( this . properties . monitorServiceAPIURL , null , this . stakeholder . _id ) . subscribe ( stakeholders = > {
stakeholders . forEach ( value = > {
this . notification . groups . push ( Role . manager ( value . type , value . alias ) )
} ) ;
if ( this . indicator . type === "chart" ) {
this . filterCharts ( ) ;
this . chartIndicatorFb = null ;
this . editChartNotify . sendNotification ( this . notification ) ;
} else {
this . filterNumbers ( ) ;
this . numberIndicatorFb = null ;
this . editNumberNotify . sendNotification ( this . notification ) ;
}
} ) ;
2020-06-24 12:13:29 +02:00
}
2020-12-01 16:43:43 +01:00
UIkit . notification ( 'Indicator has been <b>successfully saved</b>' , {
2020-05-13 17:10:44 +02:00
status : 'success' ,
2020-12-01 16:43:43 +01:00
timeout : 6000 ,
pos : 'bottom-right'
2020-05-13 17:10:44 +02:00
} ) ;
2020-06-16 10:23:51 +02:00
this . editing = false ;
2019-12-15 22:25:56 +01:00
} , error = > {
2020-06-09 16:49:52 +02:00
this . chartIndicatorFb = null ;
2020-05-13 17:10:44 +02:00
UIkit . notification ( error . error . message , {
status : 'danger' ,
2020-12-01 16:43:43 +01:00
timeout : 6000 ,
pos : 'bottom-right'
2020-05-13 17:10:44 +02:00
} ) ;
2020-06-16 10:23:51 +02:00
this . editing = false ;
2020-11-13 17:42:12 +01:00
} ) ) ;
2019-12-15 22:25:56 +01:00
}
2021-07-16 13:31:31 +02:00
saveIndicators ( indicators :Indicator [ ] ) {
this . editing = true ;
let path = [
this . stakeholder . _id ,
this . stakeholder . topics [ this . topicIndex ] . _id ,
this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . _id ,
this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . subCategories [ this . subcategoryIndex ] . _id
] ;
this . subscriptions . push ( this . stakeholderService . saveBulkElements ( this . properties . monitorServiceAPIURL , indicators , path ) . subscribe ( stakeholder = > {
this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . subCategories [ this . subcategoryIndex ] . charts = stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . subCategories [ this . subcategoryIndex ] . charts ;
this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . subCategories [ this . subcategoryIndex ] . numbers = stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . subCategories [ this . subcategoryIndex ] . numbers ;
this . buildSections ( ) ;
this . filterCharts ( ) ;
this . filterNumbers ( ) ;
this . editing = false ;
2021-07-19 11:50:08 +02:00
this . import_loading = false ;
2021-07-16 13:31:31 +02:00
UIkit . notification ( "Success! Indicated were imported" , {
status : 'success' ,
timeout : 6000 ,
pos : 'bottom-right'
} ) ;
} , error = > {
this . chartIndicatorFb = null ;
UIkit . notification ( error . error . message , {
status : 'danger' ,
timeout : 6000 ,
pos : 'bottom-right'
} ) ;
this . editing = false ;
2021-07-19 11:50:08 +02:00
this . import_loading = false ;
2021-07-16 13:31:31 +02:00
} ) ) ;
}
2020-12-09 14:24:32 +01:00
reorderIndicators ( sectionId : string , type : IndicatorType , reorder : Reorder ) {
2020-06-16 10:23:51 +02:00
this . editing = true ;
2019-12-15 22:25:56 +01:00
let path = [
this . stakeholder . _id ,
this . stakeholder . topics [ this . topicIndex ] . _id ,
this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . _id ,
2020-01-08 12:00:50 +01:00
this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . subCategories [ this . subcategoryIndex ] . _id ,
sectionId
2019-12-15 22:25:56 +01:00
] ;
2020-12-09 14:24:32 +01:00
this . subscriptions . push ( this . stakeholderService . reorderIndicators ( this . properties . monitorServiceAPIURL , path , reorder , type ) . subscribe ( indicators = > {
2019-12-15 22:25:56 +01:00
if ( type === 'chart' ) {
2020-01-08 12:00:50 +01:00
this . charts . find ( section = > section . _id === sectionId ) . indicators = indicators ;
2019-12-15 22:25:56 +01:00
this . filterCharts ( ) ;
} else {
2020-01-08 12:00:50 +01:00
this . numbers . find ( section = > section . _id === sectionId ) . indicators = indicators ;
2019-12-15 22:25:56 +01:00
this . filterNumbers ( ) ;
}
2020-06-16 10:23:51 +02:00
this . editing = false ;
2020-11-13 17:42:12 +01:00
} ) ) ;
2019-12-15 22:25:56 +01:00
}
hasDifference ( index : number ) : boolean {
2019-12-15 23:02:39 +01:00
let hasDifference = false ;
2020-06-09 16:49:52 +02:00
this . chartIndicatorPaths . at ( index ) . value . parameters . forEach ( ( parameter ) = > {
2020-01-08 12:00:50 +01:00
if ( parameter . value !== this . indicator . indicatorPaths [ index ] . parameters [ parameter . key ] ) {
2019-12-15 23:02:39 +01:00
hasDifference = true ;
return ;
}
} ) ;
return hasDifference || this . indicator . indicatorPaths [ index ] . safeResourceUrl . toString ( ) !==
2019-12-15 22:25:56 +01:00
this . getSecureUrlByStakeHolder ( this . indicator . indicatorPaths [ index ] ) . toString ( ) ;
}
2020-06-09 16:49:52 +02:00
public get isAdministrator ( ) : boolean {
2020-11-19 17:16:36 +01:00
return Session . isPortalAdministrator ( this . user ) ;
}
2020-12-09 14:24:32 +01:00
2020-11-19 17:16:36 +01:00
public get isCurator ( ) : boolean {
return this . isAdministrator || Session . isMonitorCurator ( this . user ) ;
2020-06-09 16:49:52 +02:00
}
2020-12-09 14:24:32 +01:00
2019-12-15 22:25:56 +01:00
refreshIndicator() {
2020-11-05 00:11:00 +01:00
this . indicator = this . indicatorUtils . generateIndicatorByForm ( this . chartIndicatorFb . value , this . indicator . indicatorPaths , 'chart' , true ) ;
2019-12-15 22:25:56 +01:00
this . indicator . indicatorPaths . forEach ( indicatorPath = > {
indicatorPath . safeResourceUrl = this . getSecureUrlByStakeHolder ( indicatorPath ) ;
} ) ;
}
2020-06-23 10:33:13 +02:00
deleteIndicatorOpen ( section : Section , indicatorId : string , type : string , childrenAction : string = null ) {
this . indicatorChildrenActionOnDelete = null ;
2020-11-05 00:11:00 +01:00
if ( childrenAction == "delete" ) {
2020-06-23 10:33:13 +02:00
this . indicatorChildrenActionOnDelete = childrenAction ;
2020-11-05 00:11:00 +01:00
} else if ( childrenAction == "disconnect" ) {
2020-06-23 10:33:13 +02:00
this . indicatorChildrenActionOnDelete = childrenAction ;
}
2020-11-05 00:11:00 +01:00
2020-01-08 12:00:50 +01:00
this . section = section ;
2020-05-13 10:46:22 +02:00
if ( type === 'chart' ) {
2020-05-12 13:05:53 +02:00
this . index = this . charts . find ( value = > value . _id == section . _id ) . indicators . findIndex ( value = > value . _id == indicatorId ) ;
} else {
this . index = this . numbers . find ( value = > value . _id == section . _id ) . indicators . findIndex ( value = > value . _id == indicatorId ) ;
}
2020-01-08 12:00:50 +01:00
this . indicator = section . indicators . find ( value = > value . _id == indicatorId ) ;
2019-12-19 12:17:32 +01:00
this . deleteModal . alertTitle = 'Delete indicator' ;
this . deleteModal . cancelButtonText = 'No' ;
this . deleteModal . okButtonText = 'Yes' ;
2021-03-04 12:35:10 +01:00
this . notification = NotificationUtils . deleteIndicator ( this . user . firstname + ' ' + this . user . lastname , this . stakeholder . name ) ;
this . deleteNotify . reset ( this . notification . message ) ;
2019-12-19 12:17:32 +01:00
this . deleteModal . open ( ) ;
2019-12-15 22:25:56 +01:00
}
deleteIndicator() {
2020-06-16 10:23:51 +02:00
this . editing = true ;
2019-12-15 22:25:56 +01:00
let path = [
this . stakeholder . _id ,
this . stakeholder . topics [ this . topicIndex ] . _id ,
this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . _id ,
this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . subCategories [ this . subcategoryIndex ] . _id ,
2020-01-08 12:00:50 +01:00
this . section . _id ,
2019-12-15 22:25:56 +01:00
this . indicator . _id
] ;
2020-11-13 17:42:12 +01:00
this . subscriptions . push ( this . stakeholderService . deleteElement ( this . properties . monitorServiceAPIURL , path , this . indicatorChildrenActionOnDelete ) . subscribe ( ( ) = > {
2019-12-15 22:25:56 +01:00
if ( this . indicator . type === 'chart' ) {
2020-01-08 12:00:50 +01:00
this . charts . find ( section = > section . _id === this . section . _id ) . indicators . splice ( this . index , 1 ) ;
2020-05-12 13:05:53 +02:00
this . filterCharts ( ) ;
2019-12-15 22:25:56 +01:00
} else {
2020-01-08 12:00:50 +01:00
this . numbers . find ( section = > section . _id === this . section . _id ) . indicators . splice ( this . index , 1 ) ;
2020-05-12 13:05:53 +02:00
this . filterNumbers ( ) ;
2019-12-15 22:25:56 +01:00
}
2020-12-01 16:43:43 +01:00
UIkit . notification ( 'Indicator has been <b>successfully deleted</b>' , {
2020-05-13 17:10:44 +02:00
status : 'success' ,
2020-12-01 16:43:43 +01:00
timeout : 6000 ,
pos : 'bottom-right'
2020-05-13 17:10:44 +02:00
} ) ;
2021-03-04 12:35:10 +01:00
this . notification . entity = this . indicator . _id ;
this . notification . stakeholder = this . stakeholder . alias ;
this . notification . stakeholderType = this . stakeholder . type ;
this . notification . groups = [ Role . curator ( this . stakeholder . type ) ] ;
if ( this . stakeholder . defaultId ) {
this . notification . groups . push ( Role . manager ( this . stakeholder . type , this . stakeholder . alias ) ) ;
this . deleteNotify . sendNotification ( this . notification ) ;
} else {
this . stakeholderService . getStakeholders ( this . properties . monitorServiceAPIURL , null , this . stakeholder . _id ) . subscribe ( stakeholders = > {
stakeholders . forEach ( value = > {
this . notification . groups . push ( Role . manager ( value . type , value . alias ) )
} ) ;
this . deleteNotify . sendNotification ( this . notification ) ;
} ) ;
}
2020-06-16 10:23:51 +02:00
this . editing = false ;
2020-05-13 17:10:44 +02:00
} , error = > {
UIkit . notification ( error . error . message , {
status : 'danger' ,
2020-12-01 16:43:43 +01:00
timeout : 6000 ,
pos : 'bottom-right'
2020-05-13 17:10:44 +02:00
} ) ;
2020-06-16 10:23:51 +02:00
this . editing = false ;
2020-11-13 17:42:12 +01:00
} ) ) ;
2019-12-15 22:25:56 +01:00
}
2020-11-05 00:11:00 +01:00
changeIndicatorStatus ( sectionId : string , indicator : Indicator , visibility : Visibility ) {
2020-06-16 10:23:51 +02:00
this . editing = true ;
2019-12-15 22:25:56 +01:00
let path = [
this . stakeholder . _id ,
this . stakeholder . topics [ this . topicIndex ] . _id ,
this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . _id ,
this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . subCategories [ this . subcategoryIndex ] . _id ,
2020-01-08 12:00:50 +01:00
sectionId ,
2019-12-15 22:25:56 +01:00
indicator . _id
] ;
2020-11-13 17:42:12 +01:00
this . subscriptions . push ( this . stakeholderService . changeVisibility ( this . properties . monitorServiceAPIURL , path , visibility ) . subscribe ( visibility = > {
2020-10-23 15:58:16 +02:00
indicator . visibility = visibility ;
2020-12-01 16:43:43 +01:00
UIkit . notification ( 'Indicator has been <b>successfully changed</b> to ' + indicator . visibility . toLowerCase ( ) , {
2020-05-13 17:10:44 +02:00
status : 'success' ,
2020-12-01 16:43:43 +01:00
timeout : 6000 ,
pos : 'bottom-right'
2020-05-13 17:10:44 +02:00
} ) ;
2020-06-16 10:23:51 +02:00
this . editing = false ;
2020-05-13 17:10:44 +02:00
} , error = > {
2020-11-03 11:04:45 +01:00
UIkit . notification ( 'An error has been occurred. Try again later' , {
2020-05-13 17:10:44 +02:00
status : 'danger' ,
2020-12-01 16:43:43 +01:00
timeout : 6000 ,
pos : 'bottom-right'
2020-05-13 17:10:44 +02:00
} ) ;
2020-06-16 10:23:51 +02:00
this . editing = false ;
2020-11-13 17:42:12 +01:00
} ) ) ;
2019-12-15 22:25:56 +01:00
}
2020-05-13 10:46:22 +02:00
saveSection ( section : Section , index : number , type : IndicatorType = "chart" ) {
2020-06-16 10:23:51 +02:00
this . editing = true ;
2020-05-12 17:30:49 +02:00
let path = [
this . stakeholder . _id ,
this . stakeholder . topics [ this . topicIndex ] . _id ,
this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . _id ,
this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . subCategories [ this . subcategoryIndex ] . _id
] ;
2020-11-13 17:42:12 +01:00
this . subscriptions . push ( this . stakeholderService . saveSection ( this . properties . monitorServiceAPIURL , section , path , index ) . subscribe ( section = > {
2020-05-13 10:46:22 +02:00
if ( type === 'chart' ) {
2020-05-12 17:30:49 +02:00
this . charts [ index ] = section ;
this . filterCharts ( ) ;
} else {
this . numbers [ index ] = section ;
this . filterNumbers ( ) ;
}
this . initReorder ( ) ;
2020-12-01 16:43:43 +01:00
UIkit . notification ( 'Section has been <b>successfully saved</b>' , {
2020-05-12 17:30:49 +02:00
status : 'success' ,
2020-12-01 16:43:43 +01:00
timeout : 6000 ,
pos : 'bottom-right'
2020-05-12 17:30:49 +02:00
} ) ;
2020-06-16 10:23:51 +02:00
this . editing = false ;
2020-05-12 17:30:49 +02:00
} , error = > {
UIkit . notification ( error . error . message , {
status : 'danger' ,
2020-12-01 16:43:43 +01:00
timeout : 6000 ,
pos : 'bottom-right'
2020-05-12 17:30:49 +02:00
} ) ;
2020-06-16 10:23:51 +02:00
this . editing = false ;
2020-11-13 17:42:12 +01:00
} ) ) ;
2020-05-12 17:30:49 +02:00
}
2020-01-14 20:48:13 +01:00
2020-05-12 17:30:49 +02:00
createSection ( index = - 1 , type : IndicatorType = 'chart' ) {
2020-06-16 10:23:51 +02:00
this . editing = true ;
2020-05-12 17:30:49 +02:00
this . section = new Section ( type , null , null , this . stakeholder . alias ) ;
let path = [
this . stakeholder . _id ,
this . stakeholder . topics [ this . topicIndex ] . _id ,
this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . _id ,
this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . subCategories [ this . subcategoryIndex ] . _id
] ;
2020-11-13 17:42:12 +01:00
this . subscriptions . push ( this . stakeholderService . saveSection ( this . properties . monitorServiceAPIURL , this . section , path , index ) . subscribe ( section = > {
2020-05-13 10:46:22 +02:00
if ( type === 'chart' ) {
if ( index !== - 1 ) {
2020-05-12 17:30:49 +02:00
this . charts . splice ( index , 0 , section ) ;
} else {
this . charts . push ( section ) ;
}
this . filterCharts ( ) ;
} else {
2020-05-13 10:46:22 +02:00
if ( index !== - 1 ) {
2020-05-12 17:30:49 +02:00
this . numbers . splice ( index , 0 , section ) ;
} else {
this . numbers . push ( section ) ;
}
this . filterNumbers ( ) ;
}
this . initReorder ( ) ;
2020-12-01 16:43:43 +01:00
UIkit . notification ( 'Section has been <b>successfully created</b>' , {
2020-05-12 17:30:49 +02:00
status : 'success' ,
2020-12-01 16:43:43 +01:00
timeout : 6000 ,
pos : 'bottom-right'
2020-05-12 17:30:49 +02:00
} ) ;
2020-06-16 10:23:51 +02:00
this . editing = false ;
2020-05-12 17:30:49 +02:00
} , error = > {
UIkit . notification ( error . error . message , {
status : 'danger' ,
2020-12-01 16:43:43 +01:00
timeout : 6000 ,
pos : 'bottom-right'
2020-05-12 17:30:49 +02:00
} ) ;
2020-06-16 10:23:51 +02:00
this . editing = false ;
2020-11-13 17:42:12 +01:00
} ) ) ;
2020-05-12 17:30:49 +02:00
}
2020-06-23 10:33:13 +02:00
// deleteNumberSectionOpen(section: Section, index: number) {
// this.section = section;
// this.index = index;
// this.deleteNumberSectionModal.alertTitle = 'Delete Section';
// this.deleteNumberSectionModal.cancelButtonText = 'No';
// this.deleteNumberSectionModal.okButtonText = 'Yes';
// this.deleteNumberSectionModal.open();
// }
//
// deleteChartSectionOpen(section: Section, index: number) {
// this.section = section;
// this.index = index;
// this.deleteChartSectionModal.alertTitle = 'Delete Section';
// this.deleteChartSectionModal.cancelButtonText = 'No';
// this.deleteChartSectionModal.okButtonText = 'Yes';
// this.deleteChartSectionModal.open();
// }
2020-11-05 00:11:00 +01:00
2020-06-23 10:33:13 +02:00
deleteSectionOpen ( section : Section , index : number , type : IndicatorType , childrenAction : string = null ) {
2020-11-05 00:11:00 +01:00
if ( ! this . editing && ! section . defaultId ) {
2020-11-02 15:46:19 +01:00
this . sectionTypeToDelete = type ;
this . sectionChildrenActionOnDelete = null ;
if ( childrenAction == "delete" ) {
this . sectionChildrenActionOnDelete = childrenAction ;
} else if ( childrenAction == "disconnect" ) {
this . sectionChildrenActionOnDelete = childrenAction ;
}
2020-11-05 00:11:00 +01:00
2020-11-02 15:46:19 +01:00
this . section = section ;
this . index = index ;
this . deleteSectionModal . alertTitle = 'Delete Section' ;
this . deleteSectionModal . cancelButtonText = 'No' ;
this . deleteSectionModal . okButtonText = 'Yes' ;
this . deleteSectionModal . open ( ) ;
2020-06-23 10:33:13 +02:00
}
2020-05-12 17:30:49 +02:00
}
2020-06-23 10:33:13 +02:00
deleteSection() {
2020-06-16 10:23:51 +02:00
this . editing = true ;
2020-05-12 17:30:49 +02:00
let path = [
this . stakeholder . _id ,
this . stakeholder . topics [ this . topicIndex ] . _id ,
this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . _id ,
this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . subCategories [ this . subcategoryIndex ] . _id ,
this . section . _id
] ;
2020-11-13 17:42:12 +01:00
this . subscriptions . push ( this . stakeholderService . deleteElement ( this . properties . monitorServiceAPIURL , path , this . sectionChildrenActionOnDelete ) . subscribe ( ( ) = > {
2020-06-23 10:33:13 +02:00
if ( this . sectionTypeToDelete === "chart" ) {
2020-05-12 17:30:49 +02:00
this . charts . splice ( this . index , 1 ) ;
this . filterCharts ( ) ;
} else {
this . numbers . splice ( this . index , 1 ) ;
this . filterNumbers ( ) ;
}
this . initReorder ( ) ;
2020-12-01 16:43:43 +01:00
UIkit . notification ( 'Section has been <b>successfully deleted</b>' , {
2020-05-12 17:30:49 +02:00
status : 'success' ,
2020-12-01 16:43:43 +01:00
timeout : 6000 ,
pos : 'bottom-right'
2020-05-12 17:30:49 +02:00
} ) ;
2020-06-16 10:23:51 +02:00
this . editing = false ;
2020-05-12 17:30:49 +02:00
} , error = > {
UIkit . notification ( error . error . message , {
status : 'danger' ,
2020-12-01 16:43:43 +01:00
timeout : 6000 ,
pos : 'bottom-right'
2020-05-12 17:30:49 +02:00
} ) ;
2020-06-16 10:23:51 +02:00
this . editing = false ;
2020-11-13 17:42:12 +01:00
} ) ) ;
2020-01-14 20:48:13 +01:00
}
2021-01-15 17:02:30 +01:00
private checkForSchemaEnhancements ( url :string ) {
//new schema
this . showCheckForSchemaEnhancements = this . isAdministrator && url && ! this . properties . useOldStatisticsSchema && this . indicatorUtils . checkForSchemaEnhancements ( url ) ;
}
2021-07-16 13:31:31 +02:00
importIndicatorsAndSave ( charts :any [ ] ) {
let indicators :Indicator [ ] = [ ] ;
// name description additionalDescription, height, width, visibility
let noValidParams = 0 ;
let duplicates = 0 ;
for ( let chart of charts ) {
let exists = false ;
let indicatorPath ;
2021-07-19 11:50:08 +02:00
// validate indicators' schema from file
let invalid_file_message = "" ;
if ( ! chart . type ) {
invalid_file_message = "No indicator type is specified. Type should be chart or number." ;
} else if ( chart . type != "chart" || chart . type != "number" ) {
invalid_file_message = "Invalid indicator type. Type should be chart or number." ;
} else if ( chart . type == "number" && ! chart . jsonPath ) {
invalid_file_message = "No jsonPath is specified for number indicator."
} else if ( ! chart . url ) {
invalid_file_message = "No indicator url is specified." ;
}
if ( invalid_file_message ) {
UIkit . notification ( "Error on indicators' schema. Type should be chart or number, url is needed and for number indicators, jsonPath is needed as well." , {
status : 'danger' ,
timeout : 6000 ,
pos : 'bottom-right'
} ) ;
this . editing = false ;
this . import_loading = false ;
break ;
}
2021-07-16 13:31:31 +02:00
if ( chart . type == "chart" ) {
indicatorPath = this . indicatorUtils . generateIndicatorByChartUrl ( this . statisticsService . getChartSource ( chart . url ) , chart . url , chart . type , this . stakeholder ) ;
for ( let section of this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . subCategories [ this . subcategoryIndex ] . charts ) {
for ( let chart of section . indicators ) {
if ( JSON . stringify ( chart . indicatorPaths [ 0 ] . chartObject ) == JSON . stringify ( indicatorPath . chartObject ) ) {
duplicates ++ ;
exists = true ;
}
}
}
} else if ( chart . type == "number" ) {
indicatorPath = this . indicatorUtils . generateIndicatorByNumberUrl ( this . statisticsService . getNumberSource ( chart . url ) , chart . url , this . stakeholder ,
chart . jsonPath , this . statisticsService . numberSources . get ( this . statisticsService . getNumberSource ( chart . url ) ) ) ;
for ( let section of this . stakeholder . topics [ this . topicIndex ] . categories [ this . categoryIndex ] . subCategories [ this . subcategoryIndex ] . numbers ) {
for ( let chart of section . indicators ) {
if ( JSON . stringify ( chart . indicatorPaths [ 0 ] . chartObject ) == JSON . stringify ( indicatorPath . chartObject ) ) {
duplicates ++ ;
exists = true ;
}
}
}
}
if ( ! this . isStakeholderParametersValid ( indicatorPath ) ) {
noValidParams ++ ;
}
if ( ! exists ) {
let i : Indicator = new Indicator ( chart . name , chart . description , chart . additionalDescription , chart . type , chart . width , chart . height , chart . visibility , [ indicatorPath ] ) ;
indicators . push ( i ) ;
}
}
if ( duplicates > 0 ) {
UIkit . notification ( duplicates + " urls already exist and will not be imported!" , {
status : 'warning' ,
timeout : 6000 ,
pos : 'bottom-right'
} ) ;
}
if ( noValidParams > 0 ) {
let noValidMesseage = "Some indicators couldn't be generated properly. Please make sure chart data is for the current stakeholder." ;
if ( this . stakeholder . defaultId == null ) {
noValidMesseage = "Some indicators couldn't be generated properly. Stakeholders based on this profile may not inherit the data correctly." ;
}
UIkit . notification ( noValidMesseage , {
status : 'danger' ,
timeout : 6000 ,
pos : 'bottom-right'
} ) ;
2021-07-19 11:50:08 +02:00
this . editing = false ;
this . import_loading = false ;
2021-07-16 13:31:31 +02:00
} else if ( indicators . length > 0 ) {
console . log ( indicators ) ;
this . saveIndicators ( indicators )
}
if ( indicators . length == 0 ) {
UIkit . notification ( " No urls imported!" , {
status : 'warning' ,
timeout : 6000 ,
pos : 'bottom-right'
} ) ;
2021-07-19 11:50:08 +02:00
this . editing = false ;
this . import_loading = false ;
2021-07-16 13:31:31 +02:00
}
}
2021-01-15 17:02:30 +01:00
2021-07-16 10:58:27 +02:00
public export_indicators() {
console . debug ( "Export indicators" ) ;
2021-07-19 11:50:08 +02:00
this . editing = true ;
this . export_loading = true ;
2021-07-16 10:58:27 +02:00
let indicators = [ ] ;
let index : number = 0 ;
this . displayNumbers . forEach ( section = > {
section . indicators . forEach ( indicator = > {
indicator . indicatorPaths . forEach ( indicatorPath = > {
console . debug ( "export number: " , this . statisticsService . getNumberUrl ( indicatorPath . source , this . indicatorUtils . getFullUrl ( this . stakeholder , indicatorPath ) ) ) ;
indicators [ index ] = {
2021-07-19 11:50:08 +02:00
"type" : indicator . type , "name" : indicator . name , "jsonPath" : indicatorPath . jsonPath ,
2021-07-16 10:58:27 +02:00
"description" : indicator . description , "additionalDescription" : indicator . additionalDescription ,
"visibility" : indicator . visibility , "width" : indicator . width , "height" : indicator . height ,
2021-07-19 11:50:08 +02:00
"url" : this . statisticsService . getNumberUrl ( indicatorPath . source , this . indicatorUtils . getFullUrl ( this . stakeholder , indicatorPath ) )
} ;
2021-07-16 10:58:27 +02:00
index ++ ;
} ) ;
} ) ;
} ) ;
this . displayCharts . forEach ( section = > {
section . indicators . forEach ( indicator = > {
indicator . indicatorPaths . forEach ( indicatorPath = > {
2021-07-19 11:50:08 +02:00
console . debug ( "export chart: " + this . getUrlByStakeHolder ( indicatorPath ) ) ;
2021-07-16 10:58:27 +02:00
indicators [ index ] = {
"type" : indicator . type , "name" : indicator . name ,
"description" : indicator . description , "additionalDescription" : indicator . additionalDescription ,
"visibility" : indicator . visibility , "width" : indicator . width , "height" : indicator . height ,
2021-07-19 11:50:08 +02:00
"url" : this . getUrlByStakeHolder ( indicatorPath )
} ;
2021-07-16 10:58:27 +02:00
index ++ ;
} ) ;
} ) ;
} ) ;
let topic = this . stakeholder ? this . stakeholder . topics [ this . topicIndex ] : null ;
let category = topic ? topic . categories [ this . categoryIndex ] : null ;
let subCategory = category ? category . subCategories [ this . subcategoryIndex ] : null ;
var jsonFileUrl = window . URL . createObjectURL ( new Blob ( [ JSON . stringify ( indicators ) ] , { type : 'application/json' } ) ) ;
var a = window . document . createElement ( 'a' ) ;
window . document . body . appendChild ( a ) ;
a . setAttribute ( 'style' , 'display: none' ) ;
a . href = jsonFileUrl ;
2021-07-19 11:50:08 +02:00
a . download = this . stakeholder . alias + "_" + topic . alias + "_" + category . alias + "_" + subCategory . alias + ".json" ;
2021-07-16 10:58:27 +02:00
a . click ( ) ;
window . URL . revokeObjectURL ( jsonFileUrl ) ;
a . remove ( ) ; // remove the element
2021-07-19 11:50:08 +02:00
this . editing = false ;
this . export_loading = false ;
2021-07-16 10:58:27 +02:00
}
fileChangeEvent ( fileInput : any ) {
2021-07-19 11:50:08 +02:00
this . editing = true ;
this . import_loading = true ;
2021-07-16 10:58:27 +02:00
this . filesToUpload = < Array < File > > fileInput . target . files ;
this . upload ( ) ;
}
upload() {
console . debug ( this . filesToUpload ) ;
if ( this . filesToUpload . length == 0 ) {
console . error ( "There is no selected file to upload." ) ;
2021-07-19 11:50:08 +02:00
UIkit . notification ( "There is no selected file to upload." , {
status : 'danger' ,
timeout : 6000 ,
pos : 'bottom-right'
} ) ;
this . editing = false ;
this . import_loading = false ;
2021-07-16 10:58:27 +02:00
return ;
} else {
if ( this . filesToUpload [ 0 ] . name . indexOf ( ".json" ) == - 1 || ( this . filesToUpload [ 0 ] . type != "application/json" ) ) {
console . error ( "No valid file type. The required type is JSON" ) ;
2021-07-19 11:50:08 +02:00
UIkit . notification ( "No valid file type. The required type is JSON" , {
status : 'danger' ,
timeout : 6000 ,
pos : 'bottom-right'
} ) ;
this . editing = false ;
this . import_loading = false ;
2021-07-16 10:58:27 +02:00
return ;
}
}
// this.loading.open();
this . makeFileRequest ( this . properties . utilsService + '/upload?type=json' , [ ] , this . filesToUpload ) . then ( async ( result : string ) = > {
let json_result = JSON . parse ( result ) ;
2021-07-19 11:50:08 +02:00
// validate file
if ( ! json_result || json_result . length == 0 ) {
UIkit . notification ( "Importing file is empty" , {
status : 'danger' ,
timeout : 6000 ,
pos : 'bottom-right'
} ) ;
this . editing = false ;
this . import_loading = false ;
} else {
this . importIndicatorsAndSave ( json_result ) ;
}
2021-07-16 10:58:27 +02:00
} , ( error ) = > {
2021-07-19 11:50:08 +02:00
console . error ( "Error importing files" , error ) ;
UIkit . notification ( "Error importing files" , {
status : 'danger' ,
timeout : 6000 ,
pos : 'bottom-right'
} ) ;
this . editing = false ;
this . import_loading = false ;
2021-07-16 10:58:27 +02:00
} ) ;
}
makeFileRequest ( url : string , params : Array < string > , files : Array < File > ) {
return new Promise ( ( resolve , reject ) = > {
const formData : any = new FormData ( ) ;
const xhr = new XMLHttpRequest ( ) ;
for ( let i = 0 ; i < files . length ; i ++ ) {
formData . append ( "uploads[]" , files [ i ] , files [ i ] . name ) ;
}
xhr . onreadystatechange = function ( ) {
if ( xhr . readyState == 4 ) {
if ( xhr . status == 200 ) {
resolve ( xhr . response ) ;
} else {
reject ( xhr . response ) ;
}
}
} ;
xhr . open ( "POST" , url , true ) ;
xhr . send ( formData ) ;
} ) ;
}
2019-11-08 16:47:47 +01:00
}