@@ -3,25 +3,26 @@ import { FormBuilder, FormGroup, FormArray, AbstractControl } from '@angular/for
33import { SettingsService } from '../../service/settings/settings.service' ;
44import { LoaderService } from 'src/app/service/loader/data-loader.service' ;
55import { DataStore } from 'src/app/model/data-store' ;
6- import { MetaStore } from 'src/app/model/meta-store' ;
76import { ProgressDefinitions } from 'src/app/model/types' ;
87import {
98 DialogInfo ,
109 ModalMessageComponent ,
1110} from 'src/app/component/modal-message/modal-message.component' ;
12- import { dateStr } from 'src/app/util/util' ;
11+ import { dateStr , deepCopy } from 'src/app/util/util' ;
12+ import { MetaStore } from 'src/app/model/meta-store' ;
1313
1414@Component ( {
1515 selector : 'app-settings' ,
1616 templateUrl : './settings.component.html' ,
1717 styleUrls : [ './settings.component.css' ] ,
1818} )
1919export class SettingsComponent implements OnInit {
20+ meta ! : MetaStore ;
2021 dataStoreMaxLevel ! : number ;
2122 selectedMaxLevel ! : number ;
2223 selectedMaxLevelCaption : String = '' ;
2324 progressDefinitionsForm ! : FormGroup ;
24- progressDefinitions : ProgressDefinitions = { } ;
25+ tempProgressDefinitions : ProgressDefinitions = { } ;
2526 editingProgressDefinitions : boolean = false ;
2627
2728 private BROWSER_LOCALE = 'BROWSER' ;
@@ -42,36 +43,16 @@ export class SettingsComponent implements OnInit {
4243 private settingsService : SettingsService ,
4344 private formBuilder : FormBuilder ,
4445 public modal : ModalMessageComponent
45- ) {
46- this . initProgressDefinitionsForm ( ) ;
47- }
46+ ) { }
4847
4948 ngOnInit ( ) : void {
49+ this . initialize ( ) ;
50+ this . initProgressDefinitionsForm ( ) ;
5051 this . loader
5152 . load ( )
5253 . then ( ( dataStore : DataStore ) => {
53- this . dataStoreMaxLevel = dataStore . getMaxLevel ( ) ;
54- this . selectedMaxLevel = this . settingsService . getMaxLevel ( ) || this . dataStoreMaxLevel ;
55- this . updateMaxLevelCaption ( ) ;
56-
57- // Load progress definitions
58- if ( dataStore . meta ) {
59- this . progressDefinitions = dataStore . meta . progressDefinition ;
60- this . updateProgressDefinitionsForm ( ) ;
61- }
62-
63- this . selectedDateFormat = this . settingsService . getDateFormat ( ) || this . BROWSER_LOCALE ;
64-
65- // Init dates
66- let date : Date = new Date ( ) ;
67- date = new Date ( date . getFullYear ( ) , 0 , 31 ) ; // 31 Jan current year
68- for ( let format of this . dateFormats ) {
69- if ( format . value === this . BROWSER_LOCALE ) {
70- format . label += ` (${ dateStr ( date ) } )` ;
71- } else {
72- if ( ! format . label ) format . label = dateStr ( date , format . value ) ;
73- }
74- }
54+ this . setYamlData ( dataStore ) ;
55+ this . updateProgressDefinitionsForm ( ) ;
7556 } )
7657 . catch ( err => {
7758 this . modal . openDialog ( new DialogInfo ( err . message , 'An error occurred' ) ) ;
@@ -81,6 +62,33 @@ export class SettingsComponent implements OnInit {
8162 } ) ;
8263 }
8364
65+ initialize ( ) : void {
66+ this . selectedDateFormat = this . settingsService . getDateFormat ( ) || this . BROWSER_LOCALE ;
67+
68+ // Init dates
69+ let date : Date = new Date ( ) ;
70+ date = new Date ( date . getFullYear ( ) , 0 , 31 ) ; // 31 Jan current year
71+ for ( let format of this . dateFormats ) {
72+ if ( format . value === this . BROWSER_LOCALE ) {
73+ format . label += ` (${ dateStr ( date ) } )` ;
74+ } else {
75+ if ( ! format . label ) format . label = dateStr ( date , format . value ) ;
76+ }
77+ }
78+ }
79+
80+ setYamlData ( dataStore : DataStore ) : void {
81+ this . dataStoreMaxLevel = dataStore . getMaxLevel ( ) ;
82+ this . selectedMaxLevel = this . settingsService . getMaxLevel ( ) || this . dataStoreMaxLevel ;
83+ this . updateMaxLevelCaption ( ) ;
84+
85+ // Load progress definitions
86+ if ( dataStore . meta ) {
87+ this . meta = dataStore . meta ;
88+ this . tempProgressDefinitions = deepCopy ( this . meta . progressDefinition ) ;
89+ }
90+ }
91+
8492 onDateFormatChange ( ) : void {
8593 let value : any = this . selectedDateFormat == 'null' ? null : this . selectedDateFormat ;
8694 this . settingsService . setDateFormat ( value ) ;
@@ -97,6 +105,7 @@ export class SettingsComponent implements OnInit {
97105 this . updateMaxLevelCaption ( ) ;
98106 }
99107
108+ // === Max Level ===
100109 updateMaxLevelCaption ( ) : void {
101110 if ( this . selectedMaxLevel == this . dataStoreMaxLevel ) {
102111 this . selectedMaxLevelCaption = 'All maturity levels' ;
@@ -106,20 +115,26 @@ export class SettingsComponent implements OnInit {
106115 }
107116 }
108117
118+ // === Progress Definitions ===
109119 private initProgressDefinitionsForm ( ) : void {
110120 this . progressDefinitionsForm = this . formBuilder . group ( {
111121 definitions : this . formBuilder . array ( [ ] ) ,
112122 } ) ;
113123 }
114124
115- get definitionsFormArray ( ) {
125+ get definitionsFormArray ( ) : FormArray {
116126 return this . progressDefinitionsForm . get ( 'definitions' ) as FormArray ;
117127 }
118128
129+ // Return the FormGroup for a specific index in the definitions FormArray.
130+ getDefinitionGroup ( index : number ) : FormGroup {
131+ return this . definitionsFormArray . at ( index ) as FormGroup ;
132+ }
133+
119134 private updateProgressDefinitionsForm ( ) : void {
120135 this . definitionsFormArray . clear ( ) ;
121136
122- Object . entries ( this . progressDefinitions ) . forEach ( ( [ key , progDef ] ) => {
137+ Object . entries ( this . tempProgressDefinitions ) . forEach ( ( [ key , progDef ] ) => {
123138 this . definitionsFormArray . push (
124139 this . formBuilder . group ( {
125140 key : [ key ] ,
@@ -132,18 +147,22 @@ export class SettingsComponent implements OnInit {
132147 }
133148
134149 addProgressDefinition ( ) : void {
135- this . definitionsFormArray . push (
150+ let index : number = this . definitionsFormArray . length - 1 ;
151+ let score : number = this . getFormGroupValue ( this . definitionsFormArray . at ( index - 1 ) , 'score' ) ;
152+ score = Math . trunc ( ( score + 100 ) / 2 ) ;
153+
154+ this . definitionsFormArray . insert (
155+ index ,
136156 this . formBuilder . group ( {
137157 key : [ '' ] ,
138- score : [ 0 ] ,
158+ score : [ score ] ,
139159 definition : [ '' ] ,
140160 } )
141161 ) ;
142162 }
143163
144164 removeProgressDefinition ( index : number ) : void {
145165 this . definitionsFormArray . removeAt ( index ) ;
146- this . saveProgressDefinitions ( ) ;
147166 }
148167
149168 saveProgressDefinitions ( ) : void {
@@ -176,7 +195,9 @@ export class SettingsComponent implements OnInit {
176195 // this.updateProgressDefinitionsForm();
177196 // }
178197 // });
198+ this . tempProgressDefinitions = deepCopy ( this . meta . progressDefinition ) ;
179199 this . editingProgressDefinitions = false ;
200+ this . updateProgressDefinitionsForm ( ) ;
180201 }
181202
182203 toggleProgressDefinitionsEdit ( ) : void {
0 commit comments