1 package SL::Controller::RequirementSpecTextBlock;
5 use parent qw(SL::Controller::Base);
10 use SL::DB::RequirementSpec;
11 use SL::DB::RequirementSpecPredefinedText;
12 use SL::DB::RequirementSpecTextBlock;
13 use SL::Helper::Flash;
15 use SL::Locale::String;
17 use Rose::Object::MakeMethods::Generic
19 scalar => [ qw(requirement_spec text_block) ],
20 'scalar --get_set_init' => [ qw(predefined_texts) ],
23 __PACKAGE__->run_before('load_requirement_spec_text_block', only => [qw(ajax_edit ajax_update ajax_delete dragged_and_dropped)]);
29 sub action_ajax_list {
33 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
36 if ($::form->{clicked_type} =~ m/^text-blocks-(front|back)/) {
37 $new_where = $1 eq 'front' ? 0 : 1;
40 $new_where = $self->output_position_from_id($::form->{clicked_id});
43 # $::lxdebug->message(0, "cur $current_where new $new_where");
45 my $js = SL::ClientJS->new;
47 if (!defined($current_where) || ($new_where != $current_where)) {
48 my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $new_where, requirement_spec_id => $::form->{requirement_spec_id} ]);
49 my $html = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $new_where);
51 $js->html('#column-content', $html)
57 sub action_ajax_edit {
60 my $js = SL::ClientJS->new;
62 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
63 if ($self->text_block->output_position != $current_where) {
64 my $text_blocks = $self->text_block->get_full_list;
65 my $html = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $self->text_block->output_position);
67 $js->html('#column-content', $html)
68 ->val('#current_content_type', 'text-block')
69 ->val('#current_content_id', $self->text_block->id);
72 my $html = $self->render('requirement_spec_text_block/_form', { output => 0 });
74 $js->hide('#text-block-' . $self->text_block->id)
75 ->remove('#edit_text_block_' . $self->text_block->id . '_form')
76 ->insertAfter($html, '#text-block-' . $self->text_block->id)
77 ->jstree->select_node('#tree', '#tb-' . $self->text_block->id)
78 ->focus('#edit_text_block_' . $self->text_block->id . '_title')
82 sub action_ajax_update {
83 my ($self, %params) = @_;
85 my $prefix = $::form->{form_prefix} || 'text_block';
86 my $attributes = $::form->{$prefix} || {};
88 foreach (qw(requirement_spec_id output_position)) {
89 delete $attributes->{$_} if !defined $attributes->{$_};
92 $self->text_block->update_attributes(%{ $attributes });
94 my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
97 ->remove('#' . $prefix . '_form')
98 ->replaceWith('#text-block-' . $self->text_block->id, $html)
99 ->jstree->rename_node('#tree', '#tb-' . $self->text_block->id, $self->text_block->title)
103 sub action_ajax_create {
105 my ($self, %params) = @_;
107 my $prefix = $::form->{form_prefix} || 'text_block';
108 my $attributes = $::form->{$prefix} || {};
110 foreach (qw(requirement_spec_id output_position)) {
111 delete $attributes->{$_} if !defined $attributes->{$_};
114 $self->text_block->update_attributes(%{ $attributes });
116 my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
119 ->remove('#' . $prefix . '_form')
120 ->replaceWith('#text-block-' . $self->text_block->id, $html)
121 ->jstree->rename_node('#tree', '#tb-' . $self->text_block->id, $self->text_block->title)
125 sub action_ajax_delete {
128 my $js = SL::ClientJS->new;
130 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
131 if ($self->text_block->output_position == $current_where) {
132 $js->remove('#edit_text_block_' . $self->text_block->id . '_form')
133 ->remove('#text-block-' . $self->text_block->id);
135 $js->show('#text-block-list-empty') if 1 == scalar @{ $self->text_block->get_full_list };
138 $self->text_block->delete;
140 $js->jstree->delete_node('#tree', '#tb-' . $self->text_block->id)
144 sub action_ajax_add {
147 my $js = SL::ClientJS->new;
149 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
150 my $new_where = $self->output_position_from_id($::form->{id}) // $::form->{output_position};
152 if ($new_where != $current_where) {
153 my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $new_where, requirement_spec_id => $::form->{requirement_spec_id} ]);
154 my $html = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $new_where);
156 $js->html('#column-content', $html);
159 $self->text_block(SL::DB::RequirementSpecTextBlock->new(
160 requirement_spec_id => $::form->{requirement_spec_id},
161 output_position => $::form->{output_position},
164 my $id_base = join('_', 'new_text_block', Time::HiRes::gettimeofday(), int rand 1000000000000);
165 my $html = $self->render('requirement_spec_text_block/_form', { output => 0 }, id_base => $id_base);
167 $js->action($::form->{id} ? 'insertAfter' : 'appendTo', $html, '#text-block-' . ($::form->{id} || 'list'))
168 ->focus('#' . $id_base . '_title')
172 sub action_dragged_and_dropped {
175 my $position = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position} : die "Unknown 'position' parameter";
176 my $dropped_text_block = $position =~ m/^ (?: before | after ) $/x ? SL::DB::RequirementSpecTextBlock->new(id => $::form->{dropped_id})->load : undef;
178 my $dropped_type = $position ne 'last' ? undef : $::form->{dropped_type} =~ m/^ text-blocks- (?:front|back) $/x ? $::form->{dropped_type} : die "Unknown 'dropped_type' parameter";
179 my $old_where = $self->text_block->output_position;
181 $self->text_block->db->do_transaction(sub {
183 $self->text_block->remove_from_list;
184 $self->text_block->output_position($position =~ m/before|after/ ? $dropped_text_block->output_position : $::form->{dropped_type} eq 'text-blocks-front' ? 0 : 1);
185 $self->text_block->add_to_list(position => $position, reference => $dropped_text_block ? $dropped_text_block->id : undef);
188 # $::lxdebug->dump(0, "form", $::form);
190 return $self->render(\'', { type => 'json' }) if $::form->{current_content_type} !~ m/^text-block/;
192 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
193 my $new_where = $self->text_block->output_position;
194 my $id = $self->text_block->id;
195 my $js = SL::ClientJS->new;
197 # $::lxdebug->message(0, "old $old_where current $current_where new $new_where current_CID " . $::form->{current_content_id} . ' selfid ' . $self->text_block->id);
198 if (($old_where != $new_where) && ($::form->{current_content_id} == $self->text_block->id)) {
199 # The currently selected text block is dragged to the opposite
200 # text block location. Re-render the whole content column.
201 my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $new_where ]);
202 my $html = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $new_where);
204 $js->val('#current_content_type', 'text-blocks-' . ($new_where == 0 ? 'front' : 'back'))
205 ->html('#column-content', $html);
208 if ($old_where == $current_where) {
209 $js->remove('#text-block-' . $self->text_block->id);
211 if (0 == scalar(@{ SL::DB::Manager::RequirementSpecTextBlock->get_all(where => [ requirement_spec_id => $self->text_block->requirement_spec_id, output_position => $current_where ]) })) {
212 $js->show('#text-block-list-empty');
216 if ($new_where == $current_where) {
217 $js->hide('#text-block-list-empty');
219 my $html = "" . $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
221 my $prior_text_block = $self->text_block->get_previous_in_list;
223 if ($prior_text_block) {
224 $js->insertAfter($html, '#text-block-' . $prior_text_block->id);
226 $js->appendTo($html, '#text-block-list');
238 sub load_requirement_spec {
240 $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load || die "No such requirement spec");
243 sub load_requirement_spec_text_block {
245 $self->text_block(SL::DB::RequirementSpecTextBlock->new(id => $::form->{id})->load || die "No such requirement spec text block");
252 sub output_position_from_id {
253 my ($self, $id, $type, %params) = @_;
256 return $1 eq 'front' ? 0 : 1 if $type =~ m/-(front|back)$/;
257 return undef if $type !~ m/text-block/;
260 my $text_block = SL::DB::Manager::RequirementSpecTextBlock->find_by(id => $id);
262 return $text_block ? $text_block->output_position : undef;
265 sub init_predefined_texts {
266 return SL::DB::Manager::RequirementSpecPredefinedText->get_all_sorted;