1 package SL::Controller::RequirementSpecTextBlock;
5 use parent qw(SL::Controller::Base);
8 use Params::Validate ();
13 use SL::Controller::Helper::RequirementSpec;
14 use SL::DB::RequirementSpec;
15 use SL::DB::RequirementSpecPredefinedText;
16 use SL::DB::RequirementSpecTextBlock;
17 use SL::Helper::Flash;
18 use SL::Locale::String;
20 use Rose::Object::MakeMethods::Generic
22 scalar => [ qw(text_block) ],
23 'scalar --get_set_init' => [ qw(predefined_texts js) ],
26 __PACKAGE__->run_before('load_requirement_spec_text_block', only => [qw(ajax_edit ajax_update ajax_delete ajax_flag dragged_and_dropped ajax_copy)]);
32 sub action_ajax_list {
36 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
39 if ($::form->{clicked_type} =~ m/^text-blocks-(front|back)/) {
40 $new_where = $1 eq 'front' ? 0 : 1;
43 $new_where = $self->output_position_from_id($::form->{clicked_id});
46 # $::lxdebug->message(0, "cur $current_where new $new_where");
48 $self->show_list(output_position => $new_where, id => $::form->{clicked_id}, set_type => 1) if ($new_where != ($current_where // -1));
50 $self->render($self->js);
56 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
57 my $new_where = $self->output_position_from_id($::form->{id}) // $::form->{output_position};
59 $self->show_list(output_position => $new_where) if $new_where != $current_where;
61 $self->add_new_text_block_form(output_position => $new_where, insert_after_id => $::form->{id}, requirement_spec_id => $::form->{requirement_spec_id});
63 $self->invalidate_version->render($self);
66 sub action_ajax_edit {
69 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
71 if ($self->text_block->output_position != $current_where) {
72 $self->show_list(output_position => $self->text_block->output_position, id => $self->text_block->id, requirement_spec_id => $self->text_block->requirement_spec_id);
75 my $html = $self->render('requirement_spec_text_block/_form', { output => 0 });
78 ->hide('#text-block-' . $self->text_block->id)
79 ->remove('#edit_text_block_' . $self->text_block->id . '_form')
80 ->insertAfter($html, '#text-block-' . $self->text_block->id)
81 ->jstree->select_node('#tree', '#tb-' . $self->text_block->id)
82 ->focus('#edit_text_block_' . $self->text_block->id . '_title')
86 sub action_ajax_create {
87 my ($self, %params) = @_;
89 my $attributes = $::form->{ $::form->{form_prefix} } || die "Missing attributes";
90 my $insert_after = delete $attributes->{insert_after};
92 my @errors = $self->text_block(SL::DB::RequirementSpecTextBlock->new(%{ $attributes }))->validate;
93 return SL::ClientJS->new->error(@errors)->render($self) if @errors;
95 $self->text_block->save;
96 $self->text_block->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
98 my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
99 my $node = $self->presenter->requirement_spec_text_block_jstree_data($self->text_block);
101 $self->invalidate_version
102 ->hide('#text-block-list-empty')
103 ->replaceWith('#' . $::form->{form_prefix} . '_form', $html)
104 ->jstree->create_node('#tree', $insert_after ? ('#tb-' . $insert_after, 'after') : ('#tb-' . ($attributes->{output_position} == 0 ? 'front' : 'back'), 'last'), $node)
105 ->jstree->select_node('#tree', '#tb-' . $self->text_block->id);
106 $self->add_new_text_block_form(output_position => $self->text_block->output_position, insert_after_id => $self->text_block->id, requirement_spec_id => $self->text_block->requirement_spec_id)
110 sub action_ajax_update {
111 my ($self, %params) = @_;
113 my $prefix = $::form->{form_prefix} || 'text_block';
114 my $attributes = $::form->{$prefix} || {};
116 foreach (qw(requirement_spec_id output_position position)) {
117 delete $attributes->{$_} if !defined $attributes->{$_};
120 my @errors = $self->text_block->assign_attributes(%{ $attributes })->validate;
121 return SL::ClientJS->new->error(@errors)->render($self) if @errors;
123 $self->text_block->save;
125 my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
127 $self->invalidate_version
128 ->remove('#' . $prefix . '_form')
129 ->replaceWith('#text-block-' . $self->text_block->id, $html)
130 ->jstree->rename_node('#tree', '#tb-' . $self->text_block->id, $self->text_block->title)
134 sub action_ajax_delete {
137 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
138 if ($self->text_block->output_position == $current_where) {
140 ->remove('#edit_text_block_' . $self->text_block->id . '_form')
141 ->remove('#text-block-' . $self->text_block->id);
143 $self->js->show('#text-block-list-empty') if 1 == scalar @{ $self->text_block->get_full_list };
146 $self->text_block->delete;
148 $self->invalidate_version
149 ->jstree->delete_node('#tree', '#tb-' . $self->text_block->id)
153 sub action_ajax_flag {
156 $self->text_block->update_attributes(is_flagged => !$self->text_block->is_flagged);
158 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
161 ->action_if($current_where == $self->text_block->output_position, 'toggleClass', '#text-block-' . $self->text_block->id, 'flagged')
162 ->toggleClass('#tb-' . $self->text_block->id, 'flagged')
166 sub action_dragged_and_dropped {
169 my $position = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position} : die "Unknown 'position' parameter";
170 my $dropped_text_block = $position =~ m/^ (?: before | after ) $/x ? SL::DB::RequirementSpecTextBlock->new(id => $::form->{dropped_id})->load : undef;
172 my $dropped_type = $position ne 'last' ? undef : $::form->{dropped_type} =~ m/^ text-blocks- (?:front|back) $/x ? $::form->{dropped_type} : die "Unknown 'dropped_type' parameter";
173 my $old_where = $self->text_block->output_position;
175 $self->text_block->db->do_transaction(sub {
177 $self->text_block->remove_from_list;
178 $self->text_block->output_position($position =~ m/before|after/ ? $dropped_text_block->output_position : $::form->{dropped_type} eq 'text-blocks-front' ? 0 : 1);
179 $self->text_block->add_to_list(position => $position, reference => $dropped_text_block ? $dropped_text_block->id : undef);
182 # $::lxdebug->dump(0, "form", $::form);
184 $self->invalidate_version
185 ->jstree->open_node('#tree', '#tb-' . (!$self->text_block->output_position ? 'front' : 'back'));
187 return $self->js->render($self) if $::form->{current_content_type} !~ m/^text-block/;
189 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
190 my $new_where = $self->text_block->output_position;
191 my $id = $self->text_block->id;
193 # $::lxdebug->message(0, "old $old_where current $current_where new $new_where current_CID " . $::form->{current_content_id} . ' selfid ' . $self->text_block->id);
194 if (($old_where != $new_where) && ($::form->{current_content_id} == $self->text_block->id)) {
195 # The currently selected text block is dragged to the opposite
196 # text block location. Re-render the whole content column.
197 $self->show_list(output_position => $new_where, id => $id);
200 if ($old_where == $current_where) {
201 $self->js->remove('#text-block-' . $self->text_block->id);
203 if (0 == scalar(@{ SL::DB::Manager::RequirementSpecTextBlock->get_all(where => [ requirement_spec_id => $self->text_block->requirement_spec_id, output_position => $current_where ]) })) {
204 $self->js->show('#text-block-list-empty');
208 if ($new_where == $current_where) {
209 $self->js->hide('#text-block-list-empty');
211 my $html = "" . $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
213 my $prior_text_block = $self->text_block->get_previous_in_list;
215 if ($prior_text_block) {
216 $self->js->insertAfter($html, '#text-block-' . $prior_text_block->id);
218 $self->js->prependTo($html, '#text-block-list');
223 $self->js->render($self);
226 sub action_ajax_copy {
227 my ($self, %params) = @_;
229 SL::Clipboard->new->copy($self->text_block);
230 SL::ClientJS->new->render($self);
233 sub action_ajax_paste {
234 my ($self, %params) = @_;
236 my $copied = SL::Clipboard->new->get_entry(qr/^RequirementSpecTextBlock$/);
238 return SL::ClientJS->new
239 ->error(t8("The clipboard does not contain anything that can be pasted here."))
243 my $current_output_position = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
244 my $new_output_position = $::form->{id} ? $self->output_position_from_id($::form->{id}) : $::form->{output_position};
245 my $front_back = 0 == $new_output_position ? 'front' : 'back';
247 $self->text_block($copied->to_object);
248 $self->text_block->update_attributes(requirement_spec_id => $::form->{requirement_spec_id}, output_position => $new_output_position);
249 $self->text_block->add_to_list(position => 'after', reference => $::form->{id}) if $::form->{id};
251 if ($current_output_position == $new_output_position) {
252 my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
253 $self->js->action($::form->{id} ? 'insertAfter' : 'appendTo', $html, '#text-block-' . ($::form->{id} || 'list'));
256 my $node = $self->presenter->requirement_spec_text_block_jstree_data($self->text_block);
257 $self->invalidate_version
258 ->jstree->create_node('#tree', $::form->{id} ? ('#tb-' . $::form->{id}, 'after') : ("#tb-${front_back}", 'last'), $node)
266 sub load_requirement_spec_text_block {
268 $self->text_block(SL::DB::RequirementSpecTextBlock->new(id => $::form->{id})->load || die "No such requirement spec text block");
275 sub output_position_from_id {
276 my ($self, $id, $type, %params) = @_;
279 return $1 eq 'front' ? 0 : 1 if $type =~ m/-(front|back)$/;
280 return undef if $type !~ m/text-block/;
283 my $text_block = $id ? SL::DB::Manager::RequirementSpecTextBlock->find_by(id => $id) : undef;
285 return $text_block ? $text_block->output_position : undef;
288 sub init_predefined_texts {
289 return SL::DB::Manager::RequirementSpecPredefinedText->get_all_sorted;
294 $self->js(SL::ClientJS->new);
297 sub invalidate_version {
300 my $html = $self->render('requirement_spec/_version', { output => 0 },
301 requirement_spec => SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id} || $self->text_block->requirement_spec_id)->load);
302 return $self->js->html('#requirement_spec_version', $html);
305 sub add_new_text_block_form {
306 my ($self, %params) = @_;
308 croak "Missing parameter output_position" unless defined($params{output_position}) && ($params{output_position} ne '');
309 croak "Missing parameter requirement_spec_id" unless $params{requirement_spec_id};
311 $self->text_block(SL::DB::RequirementSpecTextBlock->new(
312 requirement_spec_id => $params{requirement_spec_id},
313 output_position => $params{output_position},
316 my $id_base = join('_', 'new_text_block', Time::HiRes::gettimeofday(), int rand 1000000000000);
317 my $html = $self->render('requirement_spec_text_block/_form', { output => 0 }, id_base => $id_base, insert_after => $params{insert_after_id});
320 ->action($params{insert_after_id} ? 'insertAfter' : 'appendTo', $html, '#text-block-' . ($params{insert_after_id} || 'list'))
321 ->focus('#' . $id_base . '_title');
326 my %params = Params::Validate::validate(@_, { output_position => 1, id => 0, requirement_spec_id => 0, set_type => 0, });
328 $params{requirement_spec_id} ||= $::form->{requirement_spec_id};
329 croak "Unknown requirement_spec_id" if !$params{requirement_spec_id};
331 my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $params{output_position}, requirement_spec_id => $params{requirement_spec_id} ]);
332 my $html = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $params{output_position});
334 $self->js->html('#column-content', $html);
336 $self->js->val('#current_content_type', 'text-blocks-' . (0 == $params{output_position} ? 'front' : 'back')) if $params{id} || $params{set_type};
337 $self->js->val('#current_content_id', $params{id}) if $params{id};
339 return $self->set_function_blocks_tab_menu_class(class => 'text-block-context-menu');