@@ -439,6 +439,7 @@ def init_general_sections_state_and_server_vars(self):
439439
440440 # loop through input file sections
441441 self .state .general_sections = {}
442+ self .state .add_section = ""
442443 self .state .add_key = "" # key for the add property row
443444 self .state .add_value = "" # value for the add property row
444445 for section_name , section_data in self ._server_vars [
@@ -1058,6 +1059,97 @@ def change_export_fourc_yaml_path(self, export_fourc_yaml_path, **kwargs):
10581059 #################################################
10591060 # SELECTION CHANGES #################################
10601061 ################################################
1062+ @controller .set ("click_delete_section_button" )
1063+ def click_delete_section_button (self , ** kwargs ):
1064+ """Deletes the currently selected section; if it was the last
1065+ subsection, delete the main too."""
1066+ if self .state .selected_section_name in self .state .json_schema .get (
1067+ "required" , []
1068+ ):
1069+ return
1070+
1071+ cur_main = self .state .selected_main_section_name
1072+ cur_section = self .state .selected_section_name
1073+ if not cur_main or not cur_section :
1074+ return
1075+
1076+ general_sections = copy .deepcopy (self .state .general_sections ) or {}
1077+ sections_names = copy .deepcopy (self .state .section_names ) or {}
1078+
1079+ # delete the subsection's data
1080+ del general_sections [cur_main ][cur_section ]
1081+ self .state .general_sections = general_sections
1082+
1083+ # rebuild subsections list (new list ref -> reactive)
1084+ subs_before = sections_names [cur_main ]["subsections" ]
1085+ new_subs = [s for s in subs_before if s != cur_section ]
1086+ sections_names [cur_main ] = {** sections_names [cur_main ], "subsections" : new_subs }
1087+
1088+ if cur_section == cur_main :
1089+ # last one -> delete the main group immediately
1090+ sections_names .pop (cur_main , None )
1091+ general_sections .pop (cur_main , None )
1092+ self .state .section_names = sections_names
1093+ self .state .general_sections = general_sections
1094+
1095+ # choose a new valid selection
1096+ new_main = next (iter (sections_names .keys ()), "" )
1097+ self .state .selected_main_section_name = new_main
1098+ self .state .selected_section_name = (
1099+ sections_names [new_main ]["subsections" ][0 ]
1100+ if new_main and sections_names [new_main ]["subsections" ]
1101+ else ""
1102+ )
1103+ return
1104+
1105+ self .state .section_names = sections_names
1106+ self .state .selected_main_section_name = cur_main
1107+ self .state .selected_section_name = new_subs [0 ] if new_subs else ""
1108+
1109+ @change ("add_section" )
1110+ def change_add_section (self , ** kwargs ):
1111+ """Reaction to section selection."""
1112+ add_section = self .state .add_section
1113+ main_section_name = add_section .split ("/" )[0 ] or ""
1114+
1115+ if add_section not in self .state .json_schema .get ("properties" , {}):
1116+ return
1117+
1118+ general_sections = copy .deepcopy (self .state .general_sections ) or {}
1119+ section_names = copy .deepcopy (self .state .section_names ) or {}
1120+
1121+ # Ensure main buckets exist
1122+ if main_section_name not in section_names :
1123+ section_names [main_section_name ] = {
1124+ "subsections" : [main_section_name ],
1125+ "content_mode" : self .state .all_content_modes ["general_section" ],
1126+ }
1127+ if main_section_name not in general_sections :
1128+ general_sections [main_section_name ] = {main_section_name : {}}
1129+
1130+ # Store data under main -> sub
1131+ if add_section not in general_sections [main_section_name ]:
1132+ general_sections [main_section_name ][add_section ] = {}
1133+
1134+ # Replace subsections list with a NEW list object
1135+ subs = section_names [main_section_name ]["subsections" ]
1136+ if add_section not in subs :
1137+ subs = subs + [add_section ] # new list ref
1138+ section_names [main_section_name ] = {
1139+ ** section_names [main_section_name ], # keep content_mode
1140+ "subsections" : subs , # new list ref
1141+ }
1142+
1143+ # Commit (new references -> reactive)
1144+ self .state .general_sections = general_sections
1145+ self .state .section_names = section_names
1146+
1147+ # Set a valid selection so VSelect updates
1148+ self .state .selected_main_section_name = main_section_name
1149+ self .state .selected_section_name = add_section
1150+
1151+ self .state .add_section = ""
1152+
10611153 @change ("selected_main_section_name" )
10621154 def change_selected_main_section_name (self , selected_main_section_name , ** kwargs ):
10631155 """Reaction to change of state.selected_main_section_name."""
0 commit comments