1 package SL::Controller::RequirementSpecTextBlock;
 
   5 use parent qw(SL::Controller::Base);
 
   8 use SL::DB::RequirementSpec;
 
   9 use SL::DB::RequirementSpecPredefinedText;
 
  10 use SL::DB::RequirementSpecTextBlock;
 
  11 use SL::Helper::Flash;
 
  13 use SL::Locale::String;
 
  15 use Rose::Object::MakeMethods::Generic
 
  17  scalar => [ qw(requirement_spec text_block) ],
 
  20 __PACKAGE__->run_before('load_requirement_spec_text_block', only => [qw(ajax_edit update dragged_and_dropped)]);
 
  26 sub action_ajax_list {
 
  30   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
 
  33   if ($::form->{clicked_type} =~ m/^textblocks-(front|back)/) {
 
  34     $new_where = $1 eq 'front' ? 0 : 1;
 
  37     $new_where = $self->output_position_from_id($::form->{clicked_id});
 
  40   # $::lxdebug->message(0, "cur $current_where new $new_where");
 
  42   my $js = SL::ClientJS->new;
 
  44   if (!defined($current_where) || ($new_where != $current_where)) {
 
  45     my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $new_where, requirement_spec_id => $::form->{requirement_spec_id} ]);
 
  46     my $html        = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $new_where);
 
  48     $js->html('#column-content', $html)
 
  54 sub action_ajax_edit {
 
  57   my $js = SL::ClientJS->new;
 
  59   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
 
  60   if ($self->text_block->output_position != $current_where) {
 
  61     my $text_blocks = $self->text_block->get_full_list;
 
  62     my $html        = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $self->text_block->output_position);
 
  64     $js->html('#column-content', $html)
 
  65        ->val('#current_content_type', 'text-block')
 
  66        ->val('#current_content_id',   $self->text_block->id);
 
  69   my $predefined_texts = SL::DB::Manager::RequirementSpecPredefinedText->get_all_sorted;
 
  70   my $html             = $self->render('requirement_spec_text_block/_form', { output => 0 }, PREDEFINED_TEXTS => $predefined_texts);
 
  72   $js->hide('#text-block-' . $self->text_block->id)
 
  73      ->insertAfter($html, '#text-block-' . $self->text_block->id)
 
  74      ->jstree->select_node('#tree', '#tb-' . $self->text_block->id)
 
  79   my ($self, %params) = @_;
 
  81   my $prefix     = $::form->{form_prefix} || 'text_block';
 
  82   my $attributes = $::form->{$prefix}     || {};
 
  84   foreach (qw(requirement_spec_id output_position)) {
 
  85     delete $attributes->{$_} if !defined $attributes->{$_};
 
  88   $self->text_block->update_attributes(%{ $attributes });
 
  90   my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
 
  93     ->remove('#' . $prefix . '_form')
 
  94     ->replaceWith('#text-block-' . $self->text_block->id, $html)
 
  95     ->jstree->rename_node('#tree', '#tb-' . $self->text_block->id, $self->text_block->title)
 
 100 sub action_dragged_and_dropped {
 
 103   my $position           = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position}                                                      : die "Unknown 'position' parameter";
 
 104   my $dropped_text_block = $position           =~ m/^ (?: before | after ) $/x        ? SL::DB::RequirementSpecTextBlock->new(id => $::form->{dropped_id})->load : undef;
 
 106   my $dropped_type       = $position ne 'last' ? undef : $::form->{dropped_type} =~ m/^ textblocks- (?:front|back) $/x ? $::form->{dropped_type} : die "Unknown 'dropped_type' parameter";
 
 107   my $old_where          = $self->text_block->output_position;
 
 109   $self->text_block->db->do_transaction(sub {
 
 111     $self->text_block->remove_from_list;
 
 112     $self->text_block->output_position($position =~ m/before|after/ ? $dropped_text_block->output_position : $::form->{dropped_type} eq 'textblocks-front' ? 0 : 1);
 
 113     $self->text_block->add_to_list(position => $position, reference => $dropped_text_block ? $dropped_text_block->id : undef);
 
 116   return $self->render(\'', { type => 'json' }) if $::form->{current_content_type} !~ m/^textblock/;
 
 118   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
 
 119   my $new_where     = $self->text_block->output_position;
 
 120   my $id            = $self->text_block->id;
 
 121   my $js            = SL::ClientJS->new;
 
 123   # $::lxdebug->message(0, "old $old_where current $current_where new $new_where current_CID " . $::form->{current_content_id} . ' selfid ' . $self->text_block->id);
 
 124   if (($old_where != $new_where) && ($::form->{current_content_id} == $self->text_block->id)) {
 
 125     # The currently selected text block is dragged to the opposite
 
 126     # text block location. Re-render the whole content column.
 
 127     my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $new_where ]);
 
 128     my $html        = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $new_where);
 
 130     $js->val('#current_content_type', 'textblocks-' . ($new_where == 0 ? 'front' : 'back'))
 
 131        ->html('#column-content', $html);
 
 134     if ($old_where == $current_where) {
 
 135       $js->remove('#text-block-' . $self->text_block->id);
 
 137       if (0 == scalar(@{ SL::DB::Manager::RequirementSpecTextBlock->get_all(where => [ requirement_spec_id => $self->text_block->requirement_spec_id, output_position => $current_where ]) })) {
 
 138         $js->show('#text-block-list-empty');
 
 142     if ($new_where == $current_where) {
 
 143       $js->hide('#text-block-list-empty');
 
 145       my $html             = "" . $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
 
 147       my $prior_text_block = $self->text_block->get_previous_in_list;
 
 149       if ($prior_text_block) {
 
 150         $js->insertAfter($html, '#text-block-' . $prior_text_block->id);
 
 152         $js->appendTo($html, '#text-block-list');
 
 157   $::lxdebug->message(0, "old $old_where current $current_where new $new_where");
 
 159   $::lxdebug->dump(0, "actions", $js);
 
 168 sub load_requirement_spec {
 
 170   $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load || die "No such requirement spec");
 
 173 sub load_requirement_spec_text_block {
 
 175   $self->text_block(SL::DB::RequirementSpecTextBlock->new(id => $::form->{id})->load || die "No such requirement spec text block");
 
 182 sub output_position_from_id {
 
 183   my ($self, $id, $type, %params) = @_;
 
 186     return $params{default} unless $type =~ m/-(front|back)/;
 
 187     return $1 eq 'front' ? 0 : 1;
 
 190   my $text_block = SL::DB::Manager::RequirementSpecTextBlock->find_by(id => $id);
 
 192   return $params{default}             unless $text_block;
 
 193   return $text_block->output_position unless $params{as} eq 'text';
 
 194   return 1 == $text_block->output_position ? 'front' : 'back';