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 ajax_update ajax_delete 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 ->remove('#edit_text_block_' . $self->text_block->id . '_form')
74 ->insertAfter($html, '#text-block-' . $self->text_block->id)
75 ->jstree->select_node('#tree', '#tb-' . $self->text_block->id)
79 sub action_ajax_update {
80 my ($self, %params) = @_;
82 my $prefix = $::form->{form_prefix} || 'text_block';
83 my $attributes = $::form->{$prefix} || {};
85 foreach (qw(requirement_spec_id output_position)) {
86 delete $attributes->{$_} if !defined $attributes->{$_};
89 $self->text_block->update_attributes(%{ $attributes });
91 my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
94 ->remove('#' . $prefix . '_form')
95 ->replaceWith('#text-block-' . $self->text_block->id, $html)
96 ->jstree->rename_node('#tree', '#tb-' . $self->text_block->id, $self->text_block->title)
100 sub action_ajax_delete {
103 my $js = SL::ClientJS->new;
105 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
106 if ($self->text_block->output_position == $current_where) {
107 $js->remove('#edit_text_block_' . $self->text_block->id . '_form')
108 ->remove('#text-block-' . $self->text_block->id);
110 $js->show('#text-block-list-empty') if 1 == scalar @{ $self->text_block->get_full_list };
113 $self->text_block->delete;
115 $js->jstree->delete_node('#tree', '#tb-' . $self->text_block->id)
119 sub action_dragged_and_dropped {
122 my $position = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position} : die "Unknown 'position' parameter";
123 my $dropped_text_block = $position =~ m/^ (?: before | after ) $/x ? SL::DB::RequirementSpecTextBlock->new(id => $::form->{dropped_id})->load : undef;
125 my $dropped_type = $position ne 'last' ? undef : $::form->{dropped_type} =~ m/^ textblocks- (?:front|back) $/x ? $::form->{dropped_type} : die "Unknown 'dropped_type' parameter";
126 my $old_where = $self->text_block->output_position;
128 $self->text_block->db->do_transaction(sub {
130 $self->text_block->remove_from_list;
131 $self->text_block->output_position($position =~ m/before|after/ ? $dropped_text_block->output_position : $::form->{dropped_type} eq 'textblocks-front' ? 0 : 1);
132 $self->text_block->add_to_list(position => $position, reference => $dropped_text_block ? $dropped_text_block->id : undef);
135 return $self->render(\'', { type => 'json' }) if $::form->{current_content_type} !~ m/^textblock/;
137 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
138 my $new_where = $self->text_block->output_position;
139 my $id = $self->text_block->id;
140 my $js = SL::ClientJS->new;
142 # $::lxdebug->message(0, "old $old_where current $current_where new $new_where current_CID " . $::form->{current_content_id} . ' selfid ' . $self->text_block->id);
143 if (($old_where != $new_where) && ($::form->{current_content_id} == $self->text_block->id)) {
144 # The currently selected text block is dragged to the opposite
145 # text block location. Re-render the whole content column.
146 my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $new_where ]);
147 my $html = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $new_where);
149 $js->val('#current_content_type', 'textblocks-' . ($new_where == 0 ? 'front' : 'back'))
150 ->html('#column-content', $html);
153 if ($old_where == $current_where) {
154 $js->remove('#text-block-' . $self->text_block->id);
156 if (0 == scalar(@{ SL::DB::Manager::RequirementSpecTextBlock->get_all(where => [ requirement_spec_id => $self->text_block->requirement_spec_id, output_position => $current_where ]) })) {
157 $js->show('#text-block-list-empty');
161 if ($new_where == $current_where) {
162 $js->hide('#text-block-list-empty');
164 my $html = "" . $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
166 my $prior_text_block = $self->text_block->get_previous_in_list;
168 if ($prior_text_block) {
169 $js->insertAfter($html, '#text-block-' . $prior_text_block->id);
171 $js->appendTo($html, '#text-block-list');
183 sub load_requirement_spec {
185 $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load || die "No such requirement spec");
188 sub load_requirement_spec_text_block {
190 $self->text_block(SL::DB::RequirementSpecTextBlock->new(id => $::form->{id})->load || die "No such requirement spec text block");
197 sub output_position_from_id {
198 my ($self, $id, $type, %params) = @_;
201 return $params{default} unless $type =~ m/-(front|back)/;
202 return $1 eq 'front' ? 0 : 1;
205 my $text_block = SL::DB::Manager::RequirementSpecTextBlock->find_by(id => $id);
207 return $params{default} unless $text_block;
208 return $text_block->output_position unless $params{as} eq 'text';
209 return 1 == $text_block->output_position ? 'front' : 'back';