1 package SL::Controller::RequirementSpecItem;
5 use parent qw(SL::Controller::Base);
8 use List::MoreUtils qw(apply);
9 use List::Util qw(first);
13 use SL::Controller::Helper::RequirementSpec;
14 use SL::DB::RequirementSpec;
15 use SL::DB::RequirementSpecComplexity;
16 use SL::DB::RequirementSpecItem;
17 use SL::DB::RequirementSpecRisk;
18 use SL::Helper::Flash;
20 use SL::Locale::String;
22 use Rose::Object::MakeMethods::Generic
24 scalar => [ qw(item visible_item visible_section clicked_item sections) ],
25 'scalar --get_set_init' => [ qw(complexities risks) ],
28 __PACKAGE__->run_before('load_requirement_spec_item', only => [ qw(dragged_and_dropped ajax_update ajax_edit ajax_delete ajax_flag ajax_copy) ]);
29 __PACKAGE__->run_before('init_visible_section');
35 sub action_ajax_list {
38 my $js = SL::ClientJS->new;
40 if (!$::form->{clicked_id}) {
41 # Clicked on "sections" in the tree. Do nothing.
42 return $self->render($js);
45 my $clicked_item = SL::DB::RequirementSpecItem->new(id => $::form->{clicked_id})->load;
46 $self->item($clicked_item->section);
48 if (!$self->visible_section || ($self->visible_section->id != $self->item->id)) {
49 $self->render_list($js, $self->item, $clicked_item);
51 $self->select_node($js, $clicked_item);
57 sub insert_new_item_in_section_view {
60 $js->hide('#section-list-empty');
62 my $new_type = $self->item->item_type;
63 my $id_prefix = $new_type eq 'sub-function-block' ? 'sub-' : '';
64 my $template = 'requirement_spec_item/_' . (apply { s/-/_/g; $_ } $new_type);
65 my $html = "" . $self->render($template, { output => 0 }, requirement_spec_item => $self->item);
66 my $next_item = $self->item->get_next_in_list;
69 $js->insertBefore($html, '#' . $id_prefix . 'function-block-' . $next_item->id);
71 my $parent_is_section = $self->item->parent->item_type eq 'section';
72 $js->appendTo($html, $parent_is_section ? '#section-list' : '#sub-function-block-container-' . $self->item->parent_id);
73 $js->show('#sub-function-block-container-' . $self->item->parent_id) if !$parent_is_section;
76 $self->replace_bottom($js, $self->item->parent) if $new_type eq 'sub-function-block';
79 sub action_dragged_and_dropped {
82 my $position = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position} : die "Unknown 'position' parameter";
83 my $dropped_item = $::form->{dropped_id} ? SL::DB::RequirementSpecItem->new(id => $::form->{dropped_id})->load : undef;
85 my $old_visible_section = $self->visible_section ? $self->visible_section : undef;
86 my $old_parent_id = $self->item->parent_id;
87 my $old_type = $self->item->item_type;
89 $self->item->db->do_transaction(sub {
90 $self->item->remove_from_list;
91 $self->item->parent_id($position =~ m/before|after/ ? $dropped_item->parent_id : $dropped_item->id);
92 $self->item->add_to_list(position => $position, reference => $::form->{dropped_id} || undef);
95 my $js = SL::ClientJS->new;
97 $self->item(SL::DB::RequirementSpecItem->new(id => $self->item->id)->load);
98 my $new_section = $self->item->section;
99 my $new_type = $self->item->item_type;
100 my $new_visible_section = SL::DB::RequirementSpecItem->new(id => $self->visible_item->id)->load->section;
102 return $self->render($js) if !$old_visible_section || ($new_type eq 'section');
104 # From here on $old_visible_section is definitely set.
106 my $old_parent = SL::DB::RequirementSpecItem->new(id => $old_parent_id)->load;
107 my $old_section = $old_parent->section;
109 # $::lxdebug->message(0, "old sec ID " . $old_section->id . " new " . $new_section->id . " old visible " . $old_visible_section->id . " new visible " . $new_visible_section->id
110 # . " PARENT: old " . $old_parent->id . " new " . $self->item->parent_id . '/' . $self->item->parent->id);
112 if ($old_visible_section->id != $new_visible_section->id) {
113 # The currently visible item has been dragged to a different section.
114 return $self->render_list($js, $new_section, $self->item)
118 if ($old_visible_section->id == $old_section->id) {
119 my $id_prefix = $old_type eq 'sub-function-block' ? 'sub-' : '';
120 $js->remove('#' . $id_prefix . 'function-block-' . $self->item->id);
122 if ($old_type eq 'sub-function-block') {
123 $self->replace_bottom($js, $old_parent) ;
124 $js->hide('#sub-function-block-container-' . $old_parent->id) if 0 == scalar(@{ $old_parent->children });
126 } elsif (0 == scalar(@{ $old_section->children })) {
127 $js->show('#section-list-empty');
131 if ($old_visible_section->id == $new_section->id) {
132 $self->insert_new_item_in_section_view($js);
135 # $::lxdebug->dump(0, "js", $js->to_array);
140 sub action_ajax_add_section {
141 my ($self, %params) = @_;
143 die "Missing parameter 'requirement_spec_id'" if !$::form->{requirement_spec_id};
145 $self->item(SL::DB::RequirementSpecItem->new(requirement_spec_id => $::form->{requirement_spec_id}, item_type => 'section'));
147 my $insert_after = $::form->{id} ? SL::DB::RequirementSpecItem->new(id => $::form->{id})->load->section->id : undef;
148 my $html = $self->render('requirement_spec_item/_section_form', { output => 0 }, id_base => 'new_section', insert_after => $insert_after);
151 ->remove('#new_section_form')
152 ->hide('#column-content > *')
153 ->appendTo($html, '#column-content')
154 ->focus('#new_section_title')
158 sub action_ajax_add_function_block {
159 my ($self, %params) = @_;
161 return $self->add_function_block('function-block');
164 sub action_ajax_add_sub_function_block {
165 my ($self, %params) = @_;
167 return $self->add_function_block('sub-function-block');
170 sub action_ajax_create {
171 my ($self, %params) = @_;
173 my $js = SL::ClientJS->new;
174 my $prefix = $::form->{form_prefix} || die "Missing parameter 'form_prefix'";
175 my $attributes = $::form->{$prefix} || die "Missing parameter group '${prefix}'";
176 my $insert_after = delete $attributes->{insert_after};
178 my @errors = $self->item(SL::DB::RequirementSpecItem->new(%{ $attributes }))->validate;
179 return $js->error(@errors)->render($self) if @errors;
182 $self->item->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
184 my $type = $self->item->item_type;
186 if ($type eq 'section') {
187 my $node = $self->presenter->requirement_spec_item_jstree_data($self->item);
188 return $self->render_list($js, $self->item)
189 ->jstree->create_node('#tree', $insert_after ? ('#fb-' . $insert_after, 'after') : ('#sections', 'last'), $node)
190 ->jstree->select_node('#tree', '#fb-' . $self->item->id)
194 my $template = 'requirement_spec_item/_' . (apply { s/-/_/g; $_ } $type);
195 my $html = $self->render($template, { output => 0 }, requirement_spec_item => $self->item, id_prefix => $type eq 'function-block' ? '' : 'sub-');
196 my $node = $self->presenter->requirement_spec_item_jstree_data($self->item);
198 $js->replaceWith('#' . $prefix . '_form', $html)
199 ->hide('#section-list-empty')
200 ->jstree->create_node('#tree', $insert_after ? ('#fb-' . $insert_after, 'after') : ('#fb-' . $self->item->parent_id, 'last'), $node)
201 ->jstree->select_node('#tree', '#fb-' . $self->item->id);
203 $self->replace_bottom($js, $self->item->parent) if $type eq 'sub-function-block';
208 sub action_ajax_edit {
209 my ($self, %params) = @_;
211 $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load);
213 my $js = SL::ClientJS->new;
215 if (!$self->is_item_visible) {
216 # Show section/item to edit if it is not visible.
218 my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item);
219 $js->html('#column-content', $html);
222 if ($self->item->item_type =~ m/section/) {
223 # Edit the section header, not an item.
224 my $html = $self->render('requirement_spec_item/_section_form', { output => 0 });
226 $js->hide('#section-header-' . $self->item->id)
227 ->remove("#edit_section_form")
228 ->insertAfter($html, '#section-header-' . $self->item->id)
229 ->jstree->select_node('#tree', '#fb-' . $self->item->id)
230 ->focus("#edit_section_title")
231 ->val('#current_content_type', 'section')
232 ->val('#current_content_id', $self->item->id)
237 # Edit a function block or a sub function block
238 my @dependencies = $self->create_dependencies;
239 my @selected_dependencies = map { $_->id } @{ $self->item->dependencies };
241 my $html = $self->render('requirement_spec_item/_function_block_form', { output => 0 }, DEPENDENCIES => \@dependencies, SELECTED_DEPENDENCIES => \@selected_dependencies);
242 my $id_base = 'edit_function_block_' . $self->item->id;
243 my $content_top_id = '#' . $self->item->item_type . '-content-top-' . $self->item->id;
245 $js->hide($content_top_id)
246 ->remove("#${id_base}_form")
247 ->insertAfter($html, $content_top_id)
248 ->jstree->select_node('#tree', '#fb-' . $self->item->id)
249 ->focus("#${id_base}_description")
250 ->val('#current_content_type', $self->item->item_type)
251 ->val('#current_content_id', $self->item->id)
255 sub action_ajax_update {
256 my ($self, %params) = @_;
258 my $js = SL::ClientJS->new;
259 my $prefix = $::form->{form_prefix} || die "Missing parameter 'form_prefix'";
260 my $attributes = $::form->{$prefix} || {};
262 foreach (qw(requirement_spec_id parent_id position)) {
263 delete $attributes->{$_} if !defined $attributes->{$_};
266 my @errors = $self->item->assign_attributes(%{ $attributes })->validate;
267 return $js->error(@errors)->render($self) if @errors;
271 my $type = $self->item->item_type;
273 if ($type eq 'section') {
274 # Updated section, now update section header.
276 my $html = $self->render('requirement_spec_item/_section_header', { output => 0 }, requirement_spec_item => $self->item);
278 return SL::ClientJS->new
279 ->remove('#edit_section_form')
280 ->html('#section-header-' . $self->item->id, $html)
281 ->show('#section-header-' . $self->item->id)
282 ->jstree->rename_node('#tree', '#fb-' . $self->item->id, $::request->presenter->requirement_spec_item_tree_node_title($self->item))
286 # Updated function block or sub function block. Update (sub)
287 # function block and potentially the bottom of the parent function
290 my $id_prefix = $type eq 'function-block' ? '' : 'sub-';
291 my $html_top = $self->render('requirement_spec_item/_function_block_content_top', { output => 0 }, requirement_spec_item => $self->item, id_prefix => $id_prefix);
292 $id_prefix .= 'function-block-content-';
294 my $js = SL::ClientJS->new
295 ->remove('#' . $prefix . '_form')
296 ->replaceWith('#' . $id_prefix . 'top-' . $self->item->id, $html_top)
297 ->jstree->rename_node('#tree', '#fb-' . $self->item->id, $::request->presenter->requirement_spec_item_tree_node_title($self->item));
299 $self->replace_bottom($js, $self->item, id_prefix => $id_prefix);
300 $self->replace_bottom($js, $self->item->parent) if $type eq 'sub-function-block';
305 sub action_ajax_delete {
308 my $js = SL::ClientJS->new;
309 my $full_list = $self->item->get_full_list;
313 if ($self->visible_section && ($self->visible_section->id == $self->item->id)) {
314 # Currently visible section is deleted.
316 my $new_section = first { $_->id != $self->item->id } @{ $self->item->requirement_spec->sections };
318 $self->render_list($js, $new_section);
321 my $html = $self->render('requirement_spec_item/_no_section', { output => 0 });
322 $js->html('#column-content', $html)
323 ->val('#current_content_type', '')
324 ->val('#current_content_id', '')
327 } elsif ($self->is_item_visible) {
328 # Item in currently visible section is deleted.
330 my $type = $self->item->item_type;
331 $js->remove('#edit_function_block_' . $self->item->id . '_form')
332 ->remove('#' . $type . '-' . $self->item->id);
334 $self->replace_bottom($js, $self->item->parent_id) if $type eq 'sub-function-block';
336 if (1 == scalar @{ $full_list }) {
337 if ($type eq 'function-block') {
338 $js->show('#section-list-empty');
339 } elsif ($type eq 'sub-function-block') {
340 $js->hide('#sub-function-block-container-' . $self->item->parent_id);
345 $js->jstree->delete_node('#tree', '#fb-' . $self->item->id)
349 sub action_ajax_flag {
352 $self->item->update_attributes(is_flagged => !$self->item->is_flagged);
355 ->action_if($self->is_item_visible, 'toggleClass', '#' . $self->item->item_type . '-' . $self->item->id, 'flagged')
356 ->toggleClass('#fb-' . $self->item->id, 'flagged')
360 sub action_ajax_copy {
361 my ($self, %params) = @_;
363 SL::Clipboard->new->copy($self->item);
364 SL::ClientJS->new->render($self);
367 sub determine_paste_position {
370 if ($self->item->item_type eq 'section') {
371 # Sections are always pasted either directly after the
372 # clicked-upon section or at the very end.
373 return $self->clicked_item ? (undef, $self->clicked_item->section->id) : ();
375 } elsif ($self->item->item_type eq 'function-block') {
377 # - paste on section list: insert into last section as last element
378 # - paste on section: insert into that section as last element
379 # - paste on function block: insert after clicked-upon element
380 # - paste on sub function block: insert after parent function block of clicked-upon element
381 return !$self->clicked_item ? ( $self->sections->[-1]->id, undef )
382 : $self->clicked_item->item_type eq 'section' ? ( $self->clicked_item->id, undef )
383 : $self->clicked_item->item_type eq 'function-block' ? ( $self->clicked_item->parent_id, $self->clicked_item->id )
384 : ( $self->clicked_item->parent->parent_id, $self->clicked_item->parent_id );
386 } else { # sub-function-block
387 # A sub function block:
388 # - paste on section list: promote to function block and insert into last section as last element
389 # - paste on section: promote to function block and insert into that section as last element
390 # - paste on function block: insert as last element in clicked-upon element
391 # - paste on sub function block: insert after clicked-upon element
393 # Promote sub function blocks to function blocks when pasting on a
394 # section or the section list.
395 $self->item->item_type('function-block') if !$self->clicked_item || ($self->clicked_item->item_type eq 'section');
397 return !$self->clicked_item ? ( $self->sections->[-1]->id, undef )
398 : $self->clicked_item->item_type eq 'section' ? ( $self->clicked_item->id, undef )
399 : $self->clicked_item->item_type eq 'function-block' ? ( $self->clicked_item->id, undef )
400 : ( $self->clicked_item->parent_id, $self->clicked_item->id );
404 sub assign_requirement_spec_id_rec {
405 my ($self, $item) = @_;
407 $item->requirement_spec_id($::form->{requirement_spec_id});
408 $self->assign_requirement_spec_id_rec($_) for @{ $item->children || [] };
413 sub create_and_insert_node_rec {
414 my ($self, $js, $item, $new_parent_id, $insert_after) = @_;
416 my $node = $self->presenter->requirement_spec_item_jstree_data($item);
417 $js->jstree->create_node('#tree', $insert_after ? ('#fb-' . $insert_after, 'after') : $new_parent_id ? ('#fb-' . $new_parent_id, 'last') : ('#sections', 'last'), $node);
419 $self->create_and_insert_node_rec($js, $_, $item->id) for @{ $item->children || [] };
421 $js->jstree->open_node('#tree', '#fb-' . $item->id);
424 sub action_ajax_paste {
425 my ($self, %params) = @_;
427 my $js = SL::ClientJS->new;
428 my $copied = SL::Clipboard->new->get_entry(qr/^RequirementSpecItem$/);
431 return $js->error(t8("The clipboard does not contain anything that can be pasted here."))
435 $self->item($self->assign_requirement_spec_id_rec($copied->to_object));
436 my $req_spec = SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load;
437 $self->sections($req_spec->sections);
439 if (($self->item->item_type ne 'section') && !@{ $self->sections }) {
440 return $js->error(t8("You cannot paste function blocks or sub function blocks if there is no section."))
444 $self->clicked_item($::form->{id} ? SL::DB::RequirementSpecItem->new(id => $::form->{id})->load : undef);
446 my ($new_parent_id, $insert_after) = $self->determine_paste_position;
448 # Store result in database.
449 $self->item->update_attributes(requirement_spec_id => $::form->{requirement_spec_id}, parent_id => $new_parent_id);
450 $self->item->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
452 # Update the tree: create the node for all pasted objects.
453 $self->create_and_insert_node_rec($js, $self->item, $new_parent_id, $insert_after);
455 # Pasting the very first section?
456 if (!@{ $self->sections }) {
457 my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item);
458 $js->html('#column-content', $html)
459 ->jstree->select_node('#tree', '#fb-' . $self->item->id)
462 # Update the current view if required.
463 $self->insert_new_item_in_section_view($js) if $self->is_item_visible;
472 sub load_requirement_spec {
474 $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load || die "No such requirement spec");
477 sub load_requirement_spec_item {
479 $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load || die "No such requirement spec item");
486 sub create_random_id {
487 return join '-', Time::HiRes::gettimeofday();
490 sub format_exception {
491 return join "\n", (split m/\n/, $@)[0..4];
494 sub init_complexities {
497 return SL::DB::Manager::RequirementSpecComplexity->get_all_sorted;
503 return SL::DB::Manager::RequirementSpecRisk->get_all_sorted;
507 my ($self, $js, $item_or_id) = @_;
509 my $item = (ref($item_or_id) ? $item_or_id : SL::DB::RequirementSpecItem->new(id => $item_or_id))->load;
510 my $id_prefix = $item->item_type eq 'function-block' ? '' : 'sub-';
511 my $html = $self->render('requirement_spec_item/_function_block_content_bottom', { output => 0 }, requirement_spec_item => $item, id_prefix => $id_prefix);
512 return $js->replaceWith('#' . $id_prefix . 'function-block-content-bottom-' . $item->id, $html);
516 my ($self, $js, $item, $item_to_select) = @_;
518 my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $item);
519 $self->select_node($js->html('#column-content', $html), $item_to_select || $item);
523 my ($self, $js, $item) = @_;
525 $js->val( '#current_content_type', $item->item_type)
526 ->val( '#current_content_id', $item->id)
527 ->jstree->select_node('#tree', '#fb-' . $item->id);
530 sub create_dependency_item {
532 [ $_[0]->id, $self->presenter->truncate(join(' ', grep { $_ } ($_[1], $_[0]->fb_number, $_[0]->description))) ];
535 sub create_dependencies {
538 return map { [ $_->fb_number . ' ' . $_->title,
539 [ map { ( $self->create_dependency_item($_),
540 map { $self->create_dependency_item($_, '->') } @{ $_->sorted_children })
541 } @{ $_->sorted_children } ] ]
542 } @{ $self->item->requirement_spec->sections };
545 sub add_function_block {
546 my ($self, $new_type) = @_;
548 my $clicked_id = $::form->{id} || ($self->visible_item ? $self->visible_item->id : undef);
550 die "Invalid new_type '$new_type'" if $new_type !~ m/^(?:sub-)?function-block$/;
551 die "Missing parameter 'id' and no visible item" if !$clicked_id;
552 die "Missing parameter 'requirement_spec_id'" if !$::form->{requirement_spec_id};
554 my $clicked_item = SL::DB::RequirementSpecItem->new(id => $clicked_id)->load;
555 my $clicked_type = $clicked_item->item_type;
557 die "Invalid clicked_type '$clicked_type'" if $clicked_type !~ m/^(?: section | (?:sub-)? function-block )$/x;
559 my $case = "${clicked_type}:${new_type}";
561 my ($insert_position, $insert_reference, $parent_id, $display_reference)
562 = $case eq 'section:function-block' ? ( 'appendTo', $clicked_item->id, $clicked_item->id, '#section-list' )
563 : $case eq 'function-block:function-block' ? ( 'insertAfter', $clicked_item->id, $clicked_item->parent_id, '#function-block-' )
564 : $case eq 'function-block:sub-function-block' ? ( 'appendTo' , $clicked_item->id, $clicked_item->id, '#sub-function-block-container-' )
565 : $case eq 'sub-function-block:function-block' ? ( 'insertAfter', $clicked_item->parent_id, $clicked_item->parent->parent_id, '#function-block-' )
566 : $case eq 'sub-function-block:sub-function-block' ? ( 'insertAfter', $clicked_item->id, $clicked_item->parent_id, '#sub-function-block-' )
567 : die "Invalid combination of 'clicked_type (section)/new_type ($new_type)'";
569 $self->item(SL::DB::RequirementSpecItem->new(requirement_spec_id => $::form->{requirement_spec_id}, parent_id => $parent_id, item_type => $new_type));
571 $display_reference .= $insert_reference if $display_reference =~ m/-$/;
572 my $id_base = join('_', 'new_function_block', Time::HiRes::gettimeofday(), int rand 1000000000000);
573 my $html = $self->render(
574 'requirement_spec_item/_function_block_form',
576 DEPENDENCIES => [ $self->create_dependencies ],
577 SELECTED_DEPENDENCIES => [],
578 requirement_spec_item => $self->item,
580 insert_after => $insert_position eq 'insertAfter' ? $insert_reference : undef,
583 my $js = SL::ClientJS->new;
585 my $new_section = $self->item->section;
586 if (!$self->is_item_visible) {
587 # Show section/item to edit if it is not visible.
589 $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $new_section);
590 $js->html('#column-content', $html)
591 ->val('#current_content_type', 'section')
592 ->val('#current_content_id', $new_section->id)
593 ->jstree->select_node('#tree', '#fb-' . $new_section->id);
596 # $::lxdebug->message(0, "alright! clicked ID " . $::form->{id} . " type $clicked_type new_type $new_type insert_pos $insert_position ref " . ($insert_reference // '<undef>') . " parent $parent_id display_ref $display_reference");
598 $js->action($insert_position, $html, $display_reference)
599 ->focus("#${id_base}_description");
601 $js->show('#sub-function-block-container-' . $parent_id) if $new_type eq 'sub-function-block';
606 sub is_item_visible {
607 my ($self, $item) = @_;
609 $item ||= $self->item;
610 return $self->visible_section && ($self->visible_section->id == $item->section->id);