@@ -324,6 +324,7 @@ def init_general_sections_state_and_server_vars(self):
324324
325325 # loop through input file sections
326326 self .state .general_sections = {}
327+ self .state .add_section = ""
327328 for section_name , section_data in self ._server_vars [
328329 "fourc_yaml_content"
329330 ].sections .items ():
@@ -942,6 +943,97 @@ def change_export_fourc_yaml_path(self, export_fourc_yaml_path, **kwargs):
942943 #################################################
943944 # SELECTION CHANGES #################################
944945 ################################################
946+ @controller .set ("click_delete_section_button" )
947+ def click_delete_section_button (self , ** kwargs ):
948+ """Deletes the currently selected section; if it was the last
949+ subsection, delete the main too."""
950+ if self .state .selected_section_name in self .state .json_schema .get (
951+ "required" , []
952+ ):
953+ return
954+
955+ cur_main = self .state .selected_main_section_name
956+ cur_section = self .state .selected_section_name
957+ if not cur_main or not cur_section :
958+ return
959+
960+ general_sections = copy .deepcopy (self .state .general_sections ) or {}
961+ sections_names = copy .deepcopy (self .state .section_names ) or {}
962+
963+ # delete the subsection's data
964+ del general_sections [cur_main ][cur_section ]
965+ self .state .general_sections = general_sections
966+
967+ # rebuild subsections list (new list ref -> reactive)
968+ subs_before = sections_names [cur_main ]["subsections" ]
969+ new_subs = [s for s in subs_before if s != cur_section ]
970+ sections_names [cur_main ] = {** sections_names [cur_main ], "subsections" : new_subs }
971+
972+ if cur_section == cur_main :
973+ # last one -> delete the main group immediately
974+ sections_names .pop (cur_main , None )
975+ general_sections .pop (cur_main , None )
976+ self .state .section_names = sections_names
977+ self .state .general_sections = general_sections
978+
979+ # choose a new valid selection
980+ new_main = next (iter (sections_names .keys ()), "" )
981+ self .state .selected_main_section_name = new_main
982+ self .state .selected_section_name = (
983+ sections_names [new_main ]["subsections" ][0 ]
984+ if new_main and sections_names [new_main ]["subsections" ]
985+ else ""
986+ )
987+ return
988+
989+ self .state .section_names = sections_names
990+ self .state .selected_main_section_name = cur_main
991+ self .state .selected_section_name = new_subs [0 ] if new_subs else ""
992+
993+ @change ("add_section" )
994+ def change_add_section (self , ** kwargs ):
995+ """Reaction to section selection."""
996+ add_section = self .state .add_section
997+ main_section_name = add_section .split ("/" )[0 ] or ""
998+
999+ if add_section not in self .state .json_schema .get ("properties" , {}):
1000+ return
1001+
1002+ general_sections = copy .deepcopy (self .state .general_sections ) or {}
1003+ section_names = copy .deepcopy (self .state .section_names ) or {}
1004+
1005+ # Ensure main buckets exist
1006+ if main_section_name not in section_names :
1007+ section_names [main_section_name ] = {
1008+ "subsections" : [main_section_name ],
1009+ "content_mode" : self .state .all_content_modes ["general_section" ],
1010+ }
1011+ if main_section_name not in general_sections :
1012+ general_sections [main_section_name ] = {main_section_name : {}}
1013+
1014+ # Store data under main -> sub
1015+ if add_section not in general_sections [main_section_name ]:
1016+ general_sections [main_section_name ][add_section ] = {}
1017+
1018+ # Replace subsections list with a NEW list object
1019+ subs = section_names [main_section_name ]["subsections" ]
1020+ if add_section not in subs :
1021+ subs = subs + [add_section ] # new list ref
1022+ section_names [main_section_name ] = {
1023+ ** section_names [main_section_name ], # keep content_mode
1024+ "subsections" : subs , # new list ref
1025+ }
1026+
1027+ # Commit (new references -> reactive)
1028+ self .state .general_sections = general_sections
1029+ self .state .section_names = section_names
1030+
1031+ # Set a valid selection so VSelect updates
1032+ self .state .selected_main_section_name = main_section_name
1033+ self .state .selected_section_name = add_section
1034+
1035+ self .state .add_section = ""
1036+
9451037 @change ("selected_main_section_name" )
9461038 def change_selected_main_section_name (self , selected_main_section_name , ** kwargs ):
9471039 """Reaction to change of state.selected_main_section_name."""
0 commit comments