+ my $js = SL::ClientJS->new;
+
+ $self->item(SL::DB::RequirementSpecItem->new(id => $self->item->id)->load);
+ my $new_section = $self->item->get_section;
+ my $new_type = $self->item->get_type;
+
+ return $self->render($js) if !$visible_section_id || ($new_type eq 'section');
+
+ my $old_parent = SL::DB::RequirementSpecItem->new(id => $old_parent_id)->load;
+ my $old_section = $old_parent->get_section;
+
+ # $::lxdebug->message(0, "old sec ID " . $old_section->id . " new " . $new_section->id . " visible $visible_section_id PARENT: old " . $old_parent->id . " new " . $self->item->parent_id . '/' . $self->item->parent->id);
+
+ if ($visible_section_id == $old_section->id) {
+ my $id_prefix = $old_type eq 'sub-function-block' ? 'sub-' : '';
+ $js->remove('#' . $id_prefix . 'function-block-' . $self->item->id);
+
+ if ($old_type eq 'sub-function-block') {
+ $self->replace_bottom($js, $old_parent) ;
+ $js->hide('#sub-function-block-container-' . $old_parent->id) if 0 == scalar(@{ $old_parent->children });
+
+ } elsif (0 == scalar(@{ $old_section->children })) {
+ $js->show('#section-list-empty');
+ }
+ }
+
+ if ($visible_section_id == $new_section->id) {
+ $js->hide('#section-list-empty');
+
+ my $id_prefix = $new_type eq 'sub-function-block' ? 'sub-' : '';
+ my $template = apply { s/-/_/g; $_ } $new_type;
+ my $html = "" . $self->render('requirement_spec_item/_' . $template, { output => 0 }, requirement_spec_item => $self->item);
+ my $next_item = $self->item->get_next_in_list;
+
+ if ($next_item) {
+ $js->insertBefore($html, '#' . $id_prefix . 'function-block-' . $next_item->id);
+ } else {
+ my $parent_is_section = $self->item->parent->get_type eq 'section';
+ $js->appendTo($html, $parent_is_section ? '#section-list' : '#sub-function-block-container-' . $self->item->parent_id);
+ $js->show('#sub-function-block-container-' . $self->item->parent_id) if !$parent_is_section;
+ }
+
+ $self->replace_bottom($js, $self->item->parent) if $new_type eq 'sub-function-block';
+ }
+
+ # $::lxdebug->dump(0, "js", $js->to_array);
+
+ $self->render($js);