kivitendo/SL/Controller/RequirementSpecTextBlock.pm @ 602e604a
54daa586 | Moritz Bunkus | package SL::Controller::RequirementSpecTextBlock;
|
||
use strict;
|
||||
use parent qw(SL::Controller::Base);
|
||||
ca7c2f91 | Moritz Bunkus | use SL::ClientJS;
|
||
54daa586 | Moritz Bunkus | use SL::DB::RequirementSpec;
|
||
602e604a | Moritz Bunkus | use SL::DB::RequirementSpecPredefinedText;
|
||
54daa586 | Moritz Bunkus | use SL::DB::RequirementSpecTextBlock;
|
||
use SL::Helper::Flash;
|
||||
ca7c2f91 | Moritz Bunkus | use SL::JSON;
|
||
54daa586 | Moritz Bunkus | use SL::Locale::String;
|
||
use Rose::Object::MakeMethods::Generic
|
||||
(
|
||||
scalar => [ qw(requirement_spec text_block) ],
|
||||
);
|
||||
602e604a | Moritz Bunkus | __PACKAGE__->run_before('load_requirement_spec_text_block', only => [qw(ajax_edit update dragged_and_dropped)]);
|
||
54daa586 | Moritz Bunkus | |||
#
|
||||
# actions
|
||||
#
|
||||
ca7c2f91 | Moritz Bunkus | 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)) {
|
||||
602e604a | Moritz Bunkus | my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $new_where, requirement_spec_id => $::form->{requirement_spec_id} ]);
|
||
my $html = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $new_where);
|
||||
ca7c2f91 | Moritz Bunkus | |||
$js->html('#column-content', $html)
|
||||
}
|
||||
$self->render($js);
|
||||
}
|
||||
602e604a | Moritz Bunkus | sub action_ajax_edit {
|
||
my ($self) = @_;
|
||||
my $js = SL::ClientJS->new;
|
||||
my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
|
||||
if ($self->text_block->output_position != $current_where) {
|
||||
my $text_blocks = $self->text_block->get_full_list;
|
||||
my $html = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $self->text_block->output_position);
|
||||
$js->html('#column-content', $html)
|
||||
->val('#current_content_type', 'text-block')
|
||||
->val('#current_content_id', $self->text_block->id);
|
||||
}
|
||||
my $predefined_texts = SL::DB::Manager::RequirementSpecPredefinedText->get_all_sorted;
|
||||
my $html = $self->render('requirement_spec_text_block/_form', { output => 0 }, PREDEFINED_TEXTS => $predefined_texts);
|
||||
$js->hide('#text-block-' . $self->text_block->id)
|
||||
->insertAfter($html, '#text-block-' . $self->text_block->id)
|
||||
->jstree->select_node('#tree', '#tb-' . $self->text_block->id)
|
||||
->render($self);
|
||||
}
|
||||
sub action_update {
|
||||
my ($self, %params) = @_;
|
||||
my $prefix = $::form->{form_prefix} || 'text_block';
|
||||
my $attributes = $::form->{$prefix} || {};
|
||||
foreach (qw(requirement_spec_id output_position)) {
|
||||
delete $attributes->{$_} if !defined $attributes->{$_};
|
||||
}
|
||||
$self->text_block->update_attributes(%{ $attributes });
|
||||
my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
|
||||
SL::ClientJS->new
|
||||
->remove('#' . $prefix . '_form')
|
||||
->replaceWith('#text-block-' . $self->text_block->id, $html)
|
||||
->jstree->rename_node('#tree', '#tb-' . $self->text_block->id, $self->text_block->title)
|
||||
->render($self);
|
||||
}
|
||||
54daa586 | Moritz Bunkus | sub action_dragged_and_dropped {
|
||
my ($self) = @_;
|
||||
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";
|
||||
ca7c2f91 | Moritz Bunkus | my $old_where = $self->text_block->output_position;
|
||
54daa586 | Moritz Bunkus | |||
$self->text_block->db->do_transaction(sub {
|
||||
1;
|
||||
$self->text_block->remove_from_list;
|
||||
$self->text_block->output_position($position =~ m/before|after/ ? $dropped_text_block->output_position : $::form->{dropped_type} eq 'textblocks-front' ? 0 : 1);
|
||||
$self->text_block->add_to_list(position => $position, reference => $dropped_text_block ? $dropped_text_block->id : undef);
|
||||
});
|
||||
ca7c2f91 | Moritz Bunkus | 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);
|
||||
54daa586 | Moritz Bunkus | }
|
||
#
|
||||
# filters
|
||||
#
|
||||
sub load_requirement_spec {
|
||||
my ($self) = @_;
|
||||
$self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load || die "No such requirement spec");
|
||||
}
|
||||
sub load_requirement_spec_text_block {
|
||||
my ($self) = @_;
|
||||
$self->text_block(SL::DB::RequirementSpecTextBlock->new(id => $::form->{id})->load || die "No such requirement spec text block");
|
||||
}
|
||||
#
|
||||
# helpers
|
||||
#
|
||||
ca7c2f91 | Moritz Bunkus | 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';
|
||||
}
|
||||
54daa586 | Moritz Bunkus | 1;
|