@@ -94,12 +94,12 @@ export class CreateProductSpecComponent implements OnInit, OnDestroy {
9494 stringCharSelected :boolean = true ;
9595 numberCharSelected :boolean = false ;
9696 rangeCharSelected :boolean = false ;
97- booleanCharSelected :boolean = false ;
97+ isOptional :boolean = false ;
98+ optionalDftTrue :boolean = false ;
9899 prodChars :ProductSpecificationCharacteristic [ ] = [ ] ;
99100 finishChars :ProductSpecificationCharacteristic [ ] = [ ] ;
100101 creatingChars :CharacteristicValueSpecification [ ] = [ ] ;
101102 showCreateChar :boolean = false ;
102- nonBooleanChars :string [ ] = [ ] ;
103103
104104 //BUNDLE INFO:
105105 bundleChecked :boolean = false ;
@@ -691,7 +691,6 @@ export class CreateProductSpecComponent implements OnInit, OnDestroy {
691691 this . stringCharSelected = true ;
692692 this . numberCharSelected = false ;
693693 this . rangeCharSelected = false ;
694- this . booleanCharSelected = false ;
695694 this . showPreview = false ;
696695 this . refreshChars ( ) ;
697696 }
@@ -1019,7 +1018,8 @@ export class CreateProductSpecComponent implements OnInit, OnDestroy {
10191018 this . stringCharSelected = true ;
10201019 this . numberCharSelected = false ;
10211020 this . rangeCharSelected = false ;
1022- this . booleanCharSelected = false ;
1021+ this . isOptional = false ;
1022+ this . optionalDftTrue = false ;
10231023 this . creatingChars = [ ] ;
10241024 }
10251025
@@ -1083,37 +1083,23 @@ export class CreateProductSpecComponent implements OnInit, OnDestroy {
10831083 this . stringCharSelected = true ;
10841084 this . numberCharSelected = false ;
10851085 this . rangeCharSelected = false ;
1086- this . booleanCharSelected = false ;
10871086 this . charsForm . reset ( ) ;
10881087 } else if ( event . target . value == 'number' ) {
10891088 this . stringCharSelected = false ;
10901089 this . numberCharSelected = true ;
10911090 this . rangeCharSelected = false ;
1092- this . booleanCharSelected = false ;
10931091 this . charsForm . reset ( ) ;
10941092 } else if ( event . target . value == 'range' ) {
10951093 this . stringCharSelected = false ;
10961094 this . numberCharSelected = false ;
10971095 this . rangeCharSelected = true ;
1098- this . booleanCharSelected = false ;
10991096 this . charsForm . reset ( ) ;
1100- } else {
1101- this . stringCharSelected = false ;
1102- this . numberCharSelected = false ;
1103- this . rangeCharSelected = false ;
1104- this . booleanCharSelected = true ;
1105- // Set default only if not already selected
1106- if ( ! this . charsForm . get ( 'name' ) ?. value && this . nonBooleanChars . length > 0 ) {
1107- this . charsForm . get ( 'name' ) ?. setValue ( this . nonBooleanChars [ 0 ] + ' - enabled' ) ;
1108- }
11091097 }
1098+ this . isOptional = false ;
1099+ this . optionalDftTrue = false ;
11101100 this . creatingChars = [ ] ;
11111101 }
11121102
1113- onSelectBooleanName ( event : any ) {
1114- this . charsForm . get ( 'name' ) ?. setValue ( event . target . value + ' - enabled' ) ;
1115- }
1116-
11171103 addCharValue ( ) {
11181104 if ( this . stringCharSelected ) {
11191105 console . log ( 'string' )
@@ -1196,44 +1182,32 @@ export class CreateProductSpecComponent implements OnInit, OnDestroy {
11961182 }
11971183
11981184 saveChar ( ) {
1199- if ( this . booleanCharSelected ) {
1200- this . creatingChars = [
1201- {
1202- isDefault :false ,
1203- value : true as any
1204- } ,
1205- {
1206- isDefault :true ,
1207- value :false as any
1208- }
1209- ]
1210- }
12111185 if ( this . charsForm . value . name != null ) {
1186+ // Create the main characteristic
12121187 this . prodChars . push ( {
12131188 id : 'urn:ngsi-ld:characteristic:' + uuidv4 ( ) ,
12141189 name : this . charsForm . value . name ,
12151190 description : this . charsForm . value . description != null ? this . charsForm . value . description : '' ,
12161191 productSpecCharacteristicValue : this . creatingChars
12171192 } )
12181193
1219- // Check if it's not a boolean-enabled characteristic
1220- if ( ! this . charsForm . value . name . endsWith ( '- enabled' ) ) {
1221- // Look for a corresponding "enabled" version
1222- const hasEnabledVersion = this . prodChars . some (
1223- ( item ) => item . name === `${ name } - enabled`
1224- ) ;
1225-
1226- // Only push if there's no "- enabled" variant
1227- if ( ! hasEnabledVersion ) {
1228- this . nonBooleanChars . push ( this . charsForm . value . name ) ;
1229- }
1230- } else {
1231- const cleanName = this . charsForm . value . name . replace ( / - e n a b l e d $ / , '' ) . trim ( ) ;
1232- const nonBooleanIndex = this . nonBooleanChars . findIndex ( item => item === cleanName ) ;
1233- if ( nonBooleanIndex !== - 1 ) {
1234- console . log ( 'eliminar boolean' )
1235- this . nonBooleanChars . splice ( nonBooleanIndex , 1 ) ;
1236- }
1194+ // Create the X - enabled characteristic
1195+ if ( this . isOptional ) {
1196+ this . prodChars . push ( {
1197+ id : 'urn:ngsi-ld:characteristic:' + uuidv4 ( ) ,
1198+ name : this . charsForm . value . name + ' - enabled' ,
1199+ description : 'Optional toggle for ' + this . charsForm . value . name ,
1200+ productSpecCharacteristicValue : [
1201+ {
1202+ isDefault : this . optionalDftTrue ,
1203+ value : true as any
1204+ } ,
1205+ {
1206+ isDefault : ! this . optionalDftTrue ,
1207+ value :false as any
1208+ }
1209+ ]
1210+ } )
12371211 }
12381212 }
12391213
@@ -1243,6 +1217,8 @@ export class CreateProductSpecComponent implements OnInit, OnDestroy {
12431217 this . stringCharSelected = true ;
12441218 this . numberCharSelected = false ;
12451219 this . rangeCharSelected = false ;
1220+ this . isOptional = false ;
1221+ this . optionalDftTrue = false ;
12461222 this . refreshChars ( ) ;
12471223 this . cdr . detectChanges ( ) ;
12481224 }
@@ -1254,37 +1230,17 @@ export class CreateProductSpecComponent implements OnInit, OnDestroy {
12541230 this . prodChars . splice ( index , 1 ) ;
12551231 }
12561232
1257- if ( ! char . name . endsWith ( '- enabled' ) ) {
1258- const nonBooleanIndex = this . nonBooleanChars . findIndex ( item => item === char . name ) ;
1259- if ( nonBooleanIndex !== - 1 ) {
1260- console . log ( 'eliminar boolean' )
1261- this . nonBooleanChars . splice ( nonBooleanIndex , 1 ) ;
1262- }
1233+ // If deleting a main characteristic, also delete its "- enabled" variant if it exists
1234+ if ( ! char . name . endsWith ( '- enabled' ) ) {
12631235 const relatedEnabledIndex = this . prodChars . findIndex ( item => item . name === char . name + ' - enabled' ) ;
12641236 if ( relatedEnabledIndex !== - 1 ) {
1265- console . log ( 'eliminar' )
1237+ console . log ( 'eliminar related enabled ' )
12661238 this . prodChars . splice ( relatedEnabledIndex , 1 ) ;
12671239 }
1268- } else {
1269- const cleanName = char . name . replace ( / - e n a b l e d $ / , '' ) . trim ( ) ;
1270- const nonBooleanIndex = this . nonBooleanChars . findIndex ( item => item === cleanName ) ;
1271- if ( nonBooleanIndex == - 1 ) {
1272- console . log ( 'añadir boolean' )
1273- this . nonBooleanChars . push ( cleanName )
1274- }
1275- }
1276-
1277- if ( this . booleanCharSelected ) {
1278- // Set default only if not already selected
1279- if ( this . nonBooleanChars . length > 0 ) {
1280- this . charsForm . get ( 'name' ) ?. setValue ( this . nonBooleanChars [ 0 ] + ' - enabled' ) ;
1281- } else {
1282- this . charsForm . reset ( ) ;
1283- }
12841240 }
12851241
12861242 this . cdr . detectChanges ( ) ;
1287- console . log ( this . prodChars )
1243+ console . log ( this . prodChars )
12881244 }
12891245
12901246 checkInput ( value : string ) : boolean {
0 commit comments