Pflichtenhefte: Drag & Drop von Textblöcken
authorMoritz Bunkus <m.bunkus@linet-services.de>
Fri, 8 Mar 2013 14:32:57 +0000 (15:32 +0100)
committerMoritz Bunkus <m.bunkus@linet-services.de>
Tue, 1 Apr 2014 11:02:24 +0000 (13:02 +0200)
SL/Controller/RequirementSpec.pm
SL/Controller/RequirementSpecTextBlock.pm
js/requirement_spec.js
templates/webpages/requirement_spec/show.html
templates/webpages/requirement_spec_text_block/_text_block.html [new file with mode: 0644]
templates/webpages/requirement_spec_text_block/ajax_list.html [new file with mode: 0644]

index 8aac6df..1f6ba7f 100644 (file)
@@ -132,7 +132,7 @@ sub setup {
 
   $::auth->assert('config');
   $::request->{layout}->use_stylesheet("${_}.css") for qw(jquery.contextMenu requirement_spec);
-  $::request->{layout}->use_javascript("${_}.js") for qw(jquery.jstree jquery/jquery.contextMenu requirement_spec);
+  $::request->{layout}->use_javascript("${_}.js") for qw(jquery.jstree jquery/jquery.contextMenu client_js requirement_spec);
   $self->is_template($::form->{is_template} ? 1 : 0);
 
   return 1;
index 4f50740..6027cc5 100644 (file)
@@ -4,9 +4,11 @@ use strict;
 
 use parent qw(SL::Controller::Base);
 
+use SL::ClientJS;
 use SL::DB::RequirementSpec;
 use SL::DB::RequirementSpecTextBlock;
 use SL::Helper::Flash;
+use SL::JSON;
 use SL::Locale::String;
 
 use Rose::Object::MakeMethods::Generic
@@ -14,22 +16,48 @@ use Rose::Object::MakeMethods::Generic
  scalar => [ qw(requirement_spec text_block) ],
 );
 
-# __PACKAGE__->run_before('load_requirement_spec');
 __PACKAGE__->run_before('load_requirement_spec_text_block', only => [qw(dragged_and_dropped)]);
 
 #
 # actions
 #
 
+sub action_ajax_list {
+  my ($self) = @_;
+
+  my $result        = { };
+  my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
+  my $new_where;
+
+  if ($::form->{clicked_type} =~ m/^textblocks-(front|back)/) {
+    $new_where = $1 eq 'front' ? 0 : 1;
+
+  } else {
+    $new_where = $self->output_position_from_id($::form->{clicked_id});
+  }
+
+  # $::lxdebug->message(0, "cur $current_where new $new_where");
+
+  my $js = SL::ClientJS->new;
+
+  if (!defined($current_where) || ($new_where != $current_where)) {
+    my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $new_where ]);
+    my $html        = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $new_where, nownow => DateTime->now_local);
+
+    $js->html('#column-content', $html)
+  }
+
+  $self->render($js);
+}
+
 sub action_dragged_and_dropped {
   my ($self)       = @_;
 
-  $::lxdebug->dump(0, "form", $::form);
-
   my $position           = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position}                                                      : die "Unknown 'position' parameter";
   my $dropped_text_block = $position           =~ m/^ (?: before | after ) $/x        ? SL::DB::RequirementSpecTextBlock->new(id => $::form->{dropped_id})->load : undef;
 
   my $dropped_type       = $position ne 'last' ? undef : $::form->{dropped_type} =~ m/^ textblocks- (?:front|back) $/x ? $::form->{dropped_type} : die "Unknown 'dropped_type' parameter";
+  my $old_where          = $self->text_block->output_position;
 
   $self->text_block->db->do_transaction(sub {
     1;
@@ -38,7 +66,52 @@ sub action_dragged_and_dropped {
     $self->text_block->add_to_list(position => $position, reference => $dropped_text_block ? $dropped_text_block->id : undef);
   });
 
-  $self->render(\'', { type => 'json' });
+  return $self->render(\'', { type => 'json' }) if $::form->{current_content_type} !~ m/^textblock/;
+
+  my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
+  my $new_where     = $self->text_block->output_position;
+  my $id            = $self->text_block->id;
+  my $js            = SL::ClientJS->new;
+
+  # $::lxdebug->message(0, "old $old_where current $current_where new $new_where current_CID " . $::form->{current_content_id} . ' selfid ' . $self->text_block->id);
+  if (($old_where != $new_where) && ($::form->{current_content_id} == $self->text_block->id)) {
+    # The currently selected text block is dragged to the opposite
+    # text block location. Re-render the whole content column.
+    my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $new_where ]);
+    my $html        = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $new_where);
+
+    $js->val('#current_content_type', 'textblocks-' . ($new_where == 0 ? 'front' : 'back'))
+       ->html('#column-content', $html);
+
+  } else {
+    if ($old_where == $current_where) {
+      $js->remove('#text-block-' . $self->text_block->id);
+
+      if (0 == scalar(@{ SL::DB::Manager::RequirementSpecTextBlock->get_all(where => [ requirement_spec_id => $self->text_block->requirement_spec_id, output_position => $current_where ]) })) {
+        $js->show('#text-block-list-empty');
+      }
+    }
+
+    if ($new_where == $current_where) {
+      $js->hide('#text-block-list-empty');
+
+      my $html             = "" . $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
+      $html                =~ s/^\s+//;
+      my $prior_text_block = $self->text_block->get_previous_in_list;
+
+      if ($prior_text_block) {
+        $js->insertAfter($html, '#text-block-' . $prior_text_block->id);
+      } else {
+        $js->appendTo($html, '#text-block-list');
+      }
+    }
+  }
+
+  $::lxdebug->message(0, "old $old_where current $current_where new $new_where");
+
+  $::lxdebug->dump(0, "actions", $js);
+
+  $self->render($js);
 }
 
 #
@@ -59,4 +132,19 @@ sub load_requirement_spec_text_block {
 # helpers
 #
 
+sub output_position_from_id {
+  my ($self, $id, $type, %params) = @_;
+
+  if (!$id) {
+    return $params{default} unless $type =~ m/-(front|back)/;
+    return $1 eq 'front' ? 0 : 1;
+  }
+
+  my $text_block = SL::DB::Manager::RequirementSpecTextBlock->find_by(id => $id);
+
+  return $params{default}             unless $text_block;
+  return $text_block->output_position unless $params{as} eq 'text';
+  return 1 == $text_block->output_position ? 'front' : 'back';
+}
+
 1;
index 2b0d200..fdd949e 100644 (file)
@@ -49,17 +49,48 @@ function node_moved(event) {
   var dropped    = move_obj.r;
   var controller = dragged.data("type") == "textblock" ? "RequirementSpecTextBlock" : "RequirementSpecItem";
   var data       = {
-    action:              controller + "/dragged_and_dropped",
-    requirement_spec_id: $('#requirement_spec_id').val(),
-    id:                  dragged.data("id"),
-    dropped_id:          dropped.data("id"),
-    dropped_type:        dropped.data("type"),
-    position:            move_obj.p
+    action:               controller + "/dragged_and_dropped",
+    requirement_spec_id:  $('#requirement_spec_id').val(),
+    id:                   dragged.data("id"),
+    dropped_id:           dropped.data("id"),
+    dropped_type:         dropped.data("type"),
+    position:             move_obj.p,
+    current_content_type: $('#current_content_type').val(),
+    current_content_id:   $('#current_content_id').val()
   };
   // console.debug("controller: " + controller);
   // console.debug(data);
 
-  $.ajax({ url: "controller.pl", data: data });
+  $.post("controller.pl", data, eval_json_result);
+
+  return true;
+}
+
+function node_clicked(event) {
+  var node = $.jstree._reference('#tree')._get_node(event.target);
+  var type = node ? node.data('type') : undefined;
+
+  if (!type)
+    return;
+
+  if ('sections' ==  type) {
+    $('#current_content_type').val('sections');
+    $('#current_content_id').val('');
+    return;
+  }
+
+  var url = 'controller.pl?action='
+  $.get('controller.pl', {
+    action:               (/^textblock/ ? 'RequirementSpecTextBlock' : 'RequirementSpecItem') + '/ajax_list.js',
+    current_content_type: $('#current_content_type').val(),
+    current_content_id:   $('#current_content_id').val(),
+    clicked_type:         type,
+    clicked_id:           node.data('id')
+  }, function(new_data) {
+    $('#current_content_type').val(type);
+    $('#current_content_id').val(node.data('id'));
+    eval_json_result(new_data);
+  });
 }
 
 function section_form_requested(data) {
index f6c15c2..5c73a51 100644 (file)
  </div>
 
  <div id="content-column" class="clearfix">
-  <div id="section-container" class="section-container">
+  [% L.hidden_tag('current_content_type', SELF.requirement_spec_item.id ? 'section' : '') %]
+  [% L.hidden_tag('current_content_id',   SELF.requirement_spec_item.id) %]
 
-   <div id="section_content" class="section-content">
-    [%- IF SELF.requirement_spec_item && SELF.requirement_spec_item.id -%]
-     [%- INCLUDE 'requirement_spec_item/_single_section.html' requirement_spec_item=SELF.requirement_spec_item -%]
-    [%- ELSE -%]
-     no section
-     [%#- render :partial => 'requirement_spec_items/no_section' -%]
-    [%- END -%]
-   </div>
+  <div id="column-content">
+   [%- IF SELF.requirement_spec_item && SELF.requirement_spec_item.id -%]
+    [%- INCLUDE 'requirement_spec_item/_single_section.html' requirement_spec_item=SELF.requirement_spec_item -%]
+   [%- ELSE -%]
+    no section
+    [%#- render :partial => 'requirement_spec_items/no_section' -%]
+   [%- END -%]
   </div>
  </div>
 </div>
          },
          "plugins": [ "themes", "json_data", "ui", "crrm", "dnd" ]
        })
-       .bind("move_node.jstree", node_moved);
-
-       $(document).ajaxSend(function() {
-         $('#spinner').show();
-       }).ajaxStop(function() {
-         $('#spinner').hide();
-       });
+       .bind("move_node.jstree", node_moved)
+       .bind("click.jstree",     node_clicked)
      });
   -->
 </script>
diff --git a/templates/webpages/requirement_spec_text_block/_text_block.html b/templates/webpages/requirement_spec_text_block/_text_block.html
new file mode 100644 (file)
index 0000000..ae371f3
--- /dev/null
@@ -0,0 +1,14 @@
+[%- USE HTML -%][%- USE L -%][%- USE LxERP -%]
+<div id="text-block-[% HTML.escape(text_block.id) %]" class="requirement-spec-text-block">
+ <h2 class="requirement-spec-text-block-title">[%- HTML.escape(text_block.title) %]</h2>
+
+ [% IF !text_block.description %]
+  <div class="requirement-spec-text-block-empty-description">
+   [%- LxERP.t8("No description has been entered yet.") %]
+  </div>
+ [% ELSE %]
+  <div class="requirement-spec-text-block-description">
+   [% L.simple_format(text_block.description) %]
+  </div>
+ [% END %]
+</div>
diff --git a/templates/webpages/requirement_spec_text_block/ajax_list.html b/templates/webpages/requirement_spec_text_block/ajax_list.html
new file mode 100644 (file)
index 0000000..dacf154
--- /dev/null
@@ -0,0 +1,14 @@
+[%- USE LxERP -%][% nownow %]
+<h1>
+ [%- IF output_position == 0 %][%- LxERP.t8("Text blocks front") %][%- ELSE %][%- LxERP.t8("Text blocks back") %][%- END %]
+</h1>
+
+<div id="text-block-list-empty"[% IF TEXT_BLOCKS.size %] style="display: none"[% END %]>
+ [%- LxERP.t8("No text blocks have been created for this position.") %]
+</div>
+
+<div id="text-block-list">
+ [%- FOREACH text_block = TEXT_BLOCKS %]
+  [%- INCLUDE 'requirement_spec_text_block/_text_block.html' %]
+ [%- END %]
+</div>