Pflichtenhefte: Drag & Drop von Abschnitten fixen
[kivitendo-erp.git] / SL / Controller / RequirementSpecItem.pm
index 33ff067..0797a3f 100644 (file)
@@ -84,16 +84,17 @@ sub action_dragged_and_dropped {
   my $old_visible_section = $self->visible_section ? $self->visible_section : undef;
   my $old_parent_id       = $self->item->parent_id;
   my $old_type            = $self->item->item_type;
+  my $new_type            = !$dropped_item ? 'section' : $position =~ m/before|after/ ? $dropped_item->item_type : $dropped_item->child_type;
 
   $self->item->db->do_transaction(sub {
     $self->item->remove_from_list;
-    $self->item->parent_id($position =~ m/before|after/ ? $dropped_item->parent_id : $dropped_item->id);
+    $self->item->parent_id($position =~ m/before|after/ ? $dropped_item->parent_id : $dropped_item->id) if $dropped_item;
+    $self->item->item_type($new_type);
     $self->item->add_to_list(position => $position, reference => $::form->{dropped_id} || undef);
   });
 
   $self->item(SL::DB::RequirementSpecItem->new(id => $self->item->id)->load);
   my $new_section         = $self->item->section;
-  my $new_type            = $self->item->item_type;
   my $new_visible_section = SL::DB::RequirementSpecItem->new(id => $self->visible_item->id)->load->section;
 
   return $self->invalidate_version->render($self) if !$old_visible_section || ($new_type eq 'section');
@@ -103,9 +104,13 @@ sub action_dragged_and_dropped {
   my $old_parent  = SL::DB::RequirementSpecItem->new(id => $old_parent_id)->load;
   my $old_section = $old_parent->section;
 
+  # Section debug info:
   # $::lxdebug->message(0, "old sec ID " . $old_section->id . " new " . $new_section->id . " old visible " . $old_visible_section->id . " new visible " . $new_visible_section->id
   #                       . " PARENT: old " . $old_parent->id . " new " . $self->item->parent_id . '/' . $self->item->parent->id);
 
+  # Item debug info:
+  # $::lxdebug->message(0, 'item id: ' . $self->item->id . " new type: $new_type old type: $old_type #old children: " . scalar(@{ $old_parent->children }));
+
   if ($old_visible_section->id != $new_visible_section->id) {
     # The currently visible item has been dragged to a different section.
     $self->invalidate_version;
@@ -187,9 +192,11 @@ sub action_ajax_create {
   if ($type eq 'section') {
     my $node = $self->presenter->requirement_spec_item_jstree_data($self->item);
     $self->invalidate_version;
-    return $self->render_list($self->item)
+    $self->render_list($self->item)
+      ->hide('#section-list-empty')
       ->jstree->create_node('#tree', $insert_after ? ('#fb-' . $insert_after, 'after') : ('#sections', 'last'), $node)
-      ->jstree->select_node('#tree', '#fb-' . $self->item->id)
+      ->jstree->select_node('#tree', '#fb-' . $self->item->id);
+    return $self->add_new_item_form_after_create
       ->render($self);
   }
 
@@ -205,7 +212,7 @@ sub action_ajax_create {
 
   $self->replace_bottom($self->item->parent) if $type eq 'sub-function-block';
 
-  $self->add_new_item_form_after_create if $type =~ m/function-block/;
+  $self->add_new_item_form_after_create;
 
   $self->invalidate_version->render($self);
 }
@@ -219,7 +226,8 @@ sub action_ajax_edit {
     # Show section/item to edit if it is not visible.
 
     my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item->section);
-    $self->js->html('#column-content', $html);
+    $self->set_function_blocks_tab_menu_class(class => 'section-context-menu')
+      ->html('#column-content', $html);
   }
 
   if ($self->item->item_type =~ m/section/) {
@@ -322,7 +330,7 @@ sub action_ajax_delete {
 
     } else {
       my $html = $self->render('requirement_spec_item/_no_section', { output => 0 });
-      $self->js
+      $self->set_function_blocks_tab_menu_class(class => 'section-context-menu')
         ->html('#column-content',      $html)
         ->val('#current_content_type', '')
         ->val('#current_content_id',   '')
@@ -459,8 +467,9 @@ sub action_ajax_paste {
   # Pasting the very first section?
   if (!@{ $self->sections }) {
     my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item);
-    $self->js->html('#column-content', $html)
-             ->jstree->select_node('#tree', '#fb-' . $self->item->id)
+    $self->set_function_blocks_tab_menu_class(class => 'section-context-menu')
+         ->html('#column-content', $html)
+         ->jstree->select_node('#tree', '#fb-' . $self->item->id)
   }
 
   # Update the current view if required.
@@ -525,7 +534,8 @@ sub render_list {
   my ($self, $item, $item_to_select) = @_;
 
   my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $item);
-  $self->js->html('#column-content', $html);
+  $self->set_function_blocks_tab_menu_class(class => 'section-context-menu')
+       ->html('#column-content', $html);
   $self->select_node($item_to_select || $item);
 }
 
@@ -562,7 +572,7 @@ sub ensure_section_is_shown {
   my $new_section = $self->item->section;
   my $html        = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $new_section);
 
-  return $self->js
+  return $self->set_function_blocks_tab_menu_class(class => 'section-context-menu')
     ->html('#column-content', $html)
     ->val('#current_content_type', 'section')
     ->val('#current_content_id',   $new_section->id)
@@ -596,13 +606,19 @@ sub add_new_item_form {
 sub add_new_item_form_after_create {
   my ($self, %params) = @_;
 
-  my $created_item = $self->item;
-  $self->item(SL::DB::RequirementSpecItem->new(requirement_spec_id => $created_item->requirement_spec_id, parent_id => $created_item->parent_id, item_type => $created_item->item_type));
+  my $created_item    = $self->item;
+  my $is_section      = $created_item->item_type eq 'section';
+
+  $self->item(SL::DB::RequirementSpecItem->new(
+    requirement_spec_id => $created_item->requirement_spec_id,
+    parent_id           => $is_section ? $created_item->id : $created_item->parent_id,
+    item_type           => $is_section ? 'function-block'  : $created_item->item_type,
+  ));
 
   $self->add_new_item_form(
-    insert_position   => 'insertAfter',
     insert_reference  => $created_item->id,
-    display_reference => '#' . $created_item->item_type . '-' . $created_item->id,
+    insert_position   => $is_section ? 'appendTo'      : 'insertAfter',
+    display_reference => $is_section ? '#section-list' : '#' . $created_item->item_type . '-' . $created_item->id,
   );
 }
 
@@ -635,9 +651,8 @@ sub add_function_block {
   $self->item(SL::DB::RequirementSpecItem->new(requirement_spec_id => $::form->{requirement_spec_id}, parent_id => $parent_id, item_type => $new_type));
 
   $self->ensure_section_is_shown;
-  $self->add_new_item_form(insert_position => $insert_position, insert_reference => $insert_reference, display_reference => $display_reference);
-
   $self->js->show('#sub-function-block-container-' . $parent_id) if $new_type eq 'sub-function-block';
+  $self->add_new_item_form(insert_position => $insert_position, insert_reference => $insert_reference, display_reference => $display_reference);
 
   $self->js->render($self);
 }