@ -45,7 +45,8 @@ declare var UIkit;
export class IndicatorsComponent implements OnInit , OnDestroy , OnChanges , AfterViewInit {
filesToUpload : Array < File > ;
errorMessage = "" ;
enableUpload : boolean = true ;
import_loading : boolean = false ;
export_loading : boolean = false ;
@Input ( )
public properties : EnvProperties = null ;
@ -910,6 +911,7 @@ export class IndicatorsComponent implements OnInit, OnDestroy, OnChanges, AfterV
this . filterCharts ( ) ;
this . filterNumbers ( ) ;
this . editing = false ;
this . import_loading = false ;
UIkit . notification ( "Success! Indicated were imported" , {
status : 'success' ,
timeout : 6000 ,
@ -923,6 +925,7 @@ export class IndicatorsComponent implements OnInit, OnDestroy, OnChanges, AfterV
pos : 'bottom-right'
} ) ;
this . editing = false ;
this . import_loading = false ;
} ) ) ;
@ -1233,6 +1236,30 @@ export class IndicatorsComponent implements OnInit, OnDestroy, OnChanges, AfterV
for ( let chart of charts ) {
let exists = false ;
let indicatorPath ;
// 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 ;
}
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 ) {
@ -1283,6 +1310,8 @@ export class IndicatorsComponent implements OnInit, OnDestroy, OnChanges, AfterV
timeout : 6000 ,
pos : 'bottom-right'
} ) ;
this . editing = false ;
this . import_loading = false ;
} else if ( indicators . length > 0 ) {
console . log ( indicators ) ;
this . saveIndicators ( indicators )
@ -1293,12 +1322,17 @@ export class IndicatorsComponent implements OnInit, OnDestroy, OnChanges, AfterV
timeout : 6000 ,
pos : 'bottom-right'
} ) ;
this . editing = false ;
this . import_loading = false ;
}
}
public export_indicators() {
console . debug ( "Export indicators" ) ;
this . editing = true ;
this . export_loading = true ;
let indicators = [ ] ;
let index : number = 0 ;
@ -1307,10 +1341,11 @@ export class IndicatorsComponent implements OnInit, OnDestroy, OnChanges, AfterV
indicator . indicatorPaths . forEach ( indicatorPath = > {
console . debug ( "export number: " , this . statisticsService . getNumberUrl ( indicatorPath . source , this . indicatorUtils . getFullUrl ( this . stakeholder , indicatorPath ) ) ) ;
indicators [ index ] = {
"type" : indicator . type , "name" : indicator . name , "jsonPath" : indicatorPath . jsonPath ,
"type" : indicator . type , "name" : indicator . name , "jsonPath" : indicatorPath . jsonPath ,
"description" : indicator . description , "additionalDescription" : indicator . additionalDescription ,
"visibility" : indicator . visibility , "width" : indicator . width , "height" : indicator . height ,
"url" : this . statisticsService . getNumberUrl ( indicatorPath . source , this . indicatorUtils . getFullUrl ( this . stakeholder , indicatorPath ) ) } ;
"url" : this . statisticsService . getNumberUrl ( indicatorPath . source , this . indicatorUtils . getFullUrl ( this . stakeholder , indicatorPath ) )
} ;
index ++ ;
} ) ;
} ) ;
@ -1319,12 +1354,13 @@ export class IndicatorsComponent implements OnInit, OnDestroy, OnChanges, AfterV
this . displayCharts . forEach ( section = > {
section . indicators . forEach ( indicator = > {
indicator . indicatorPaths . forEach ( indicatorPath = > {
console . debug ( "export chart: " + this . getUrlByStakeHolder ( indicatorPath ) ) ;
console . debug ( "export chart: " + this . getUrlByStakeHolder ( indicatorPath ) ) ;
indicators [ index ] = {
"type" : indicator . type , "name" : indicator . name ,
"description" : indicator . description , "additionalDescription" : indicator . additionalDescription ,
"visibility" : indicator . visibility , "width" : indicator . width , "height" : indicator . height ,
"url" : this . getUrlByStakeHolder ( indicatorPath ) } ;
"url" : this . getUrlByStakeHolder ( indicatorPath )
} ;
index ++ ;
} ) ;
} ) ;
@ -1339,29 +1375,45 @@ export class IndicatorsComponent implements OnInit, OnDestroy, OnChanges, AfterV
window . document . body . appendChild ( a ) ;
a . setAttribute ( 'style' , 'display: none' ) ;
a . href = jsonFileUrl ;
a . download = this . stakeholder . alias + "_" + topic . alias + "_" + category . alias + "_" + subCategory . alias + ".json" ;
a . download = this . stakeholder . alias + "_" + topic . alias + "_" + category . alias + "_" + subCategory . alias + ".json" ;
a . click ( ) ;
window . URL . revokeObjectURL ( jsonFileUrl ) ;
a . remove ( ) ; // remove the element
this . editing = false ;
this . export_loading = false ;
}
fileChangeEvent ( fileInput : any ) {
this . editing = true ;
this . import_loading = true ;
this . filesToUpload = < Array < File > > fileInput . target . files ;
this . upload ( ) ;
}
upload() {
this . enableUpload = false ;
// this.errorMessage = "";
console . debug ( this . filesToUpload ) ;
if ( this . filesToUpload . length == 0 ) {
console . error ( "There is no selected file to upload." ) ;
// this.errorMessage = "There is no selected file to upload.";
UIkit . notification ( "There is no selected file to upload." , {
status : 'danger' ,
timeout : 6000 ,
pos : 'bottom-right'
} ) ;
this . editing = false ;
this . import_loading = false ;
return ;
} else {
if ( this . filesToUpload [ 0 ] . name . indexOf ( ".json" ) == - 1 || ( this . filesToUpload [ 0 ] . type != "application/json" ) ) {
// this.errorMessage = "No valid file type. The required type is JSON";
console . error ( "No valid file type. The required type is JSON" ) ;
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 ;
return ;
}
}
@ -1370,11 +1422,28 @@ export class IndicatorsComponent implements OnInit, OnDestroy, OnChanges, AfterV
this . makeFileRequest ( this . properties . utilsService + '/upload?type=json' , [ ] , this . filesToUpload ) . then ( async ( result : string ) = > {
let json_result = JSON . parse ( result ) ;
this . importIndicatorsAndSave ( json_result ) ;
this . endOfFetching ( ) ;
// 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 ) ;
}
} , ( error ) = > {
this . enableUpload = true ;
console . error ( "An error occurred" ) ;
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 ;
} ) ;
}
@ -1398,10 +1467,4 @@ export class IndicatorsComponent implements OnInit, OnDestroy, OnChanges, AfterV
xhr . send ( formData ) ;
} ) ;
}
endOfFetching() {
// this.showReport = true;
this . enableUpload = true ;
}
}