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::RequirementSpecPredefinedText;
18 use SL::DB::RequirementSpecRisk;
19 use SL::Helper::Flash;
21 use SL::Locale::String;
22 use SL::Presenter::Text qw(truncate);
24 use Rose::Object::MakeMethods::Generic
26 scalar => [ qw(item visible_item visible_section clicked_item sections) ],
27 'scalar --get_set_init' => [ qw(complexities risks predefined_texts) ],
30 __PACKAGE__->run_before('check_auth');
31 __PACKAGE__->run_before('load_requirement_spec_item', only => [ qw(dragged_and_dropped ajax_update ajax_edit ajax_delete ajax_flag ajax_copy) ]);
32 __PACKAGE__->run_before('init_visible_section');
38 sub action_ajax_list {
41 if (!$::form->{clicked_id}) {
42 # Clicked on "sections" in the tree. Do nothing.
43 return $self->render($self->js);
46 my $clicked_item = SL::DB::RequirementSpecItem->new(id => $::form->{clicked_id})->load;
47 $self->item($clicked_item->section);
49 if (!$self->visible_section || ($self->visible_section->id != $self->item->id)) {
50 $self->render_list($self->item, $clicked_item);
52 $self->select_node($clicked_item);
55 $self->render($self->js);
58 sub insert_new_item_in_section_view {
61 $self->js->hide('#section-list-empty');
63 my $new_type = $self->item->item_type;
64 my $id_prefix = $new_type eq 'sub-function-block' ? 'sub-' : '';
65 my $template = 'requirement_spec_item/_' . (apply { s/-/_/g; $_ } $new_type);
66 my $html = "" . $self->render($template, { output => 0 }, requirement_spec_item => $self->item);
67 my $next_item = $self->item->get_next_in_list;
70 $self->js->insertBefore($html, '#' . $id_prefix . 'function-block-' . $next_item->id);
72 my $parent_is_section = $self->item->parent->item_type eq 'section';
73 $self->js->appendTo($html, $parent_is_section ? '#section-list' : '#sub-function-block-container-' . $self->item->parent_id);
74 $self->js->show('#sub-function-block-container-' . $self->item->parent_id) if !$parent_is_section;
77 $self->replace_bottom($self->item->parent) if $new_type eq 'sub-function-block';
80 sub action_dragged_and_dropped {
83 my $position = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position} : die "Unknown 'position' parameter";
84 my $dropped_item = $::form->{dropped_id} ? SL::DB::RequirementSpecItem->new(id => $::form->{dropped_id})->load : undef;
86 my $old_visible_section = $self->visible_section ? $self->visible_section : undef;
87 my $old_parent_id = $self->item->parent_id;
88 my $old_type = $self->item->item_type;
89 my $new_type = !$dropped_item ? 'section' : $position =~ m/before|after/ ? $dropped_item->item_type : $dropped_item->child_type;
91 $self->item->db->with_transaction(sub {
92 $self->item->remove_from_list;
93 $self->item->parent_id($position =~ m/before|after/ ? $dropped_item->parent_id : $dropped_item->id) if $dropped_item;
94 $self->item->item_type($new_type);
95 $self->item->add_to_list(position => $position, reference => $::form->{dropped_id} || undef);
98 return $self->invalidate_version->render if !$old_visible_section || ($new_type eq 'section');
100 # From here on $old_visible_section is definitely set.
102 $self->item(SL::DB::RequirementSpecItem->new(id => $self->item->id)->load);
103 my $new_section = $self->item->section;
104 my $new_visible_section = SL::DB::RequirementSpecItem->new(id => $self->visible_item->id)->load->section;
106 my $old_parent = SL::DB::RequirementSpecItem->new(id => $old_parent_id)->load;
107 my $old_section = $old_parent->section;
109 # Section debug info:
110 # $::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
111 # . " PARENT: old " . $old_parent->id . " new " . $self->item->parent_id . '/' . $self->item->parent->id);
114 # $::lxdebug->message(0, 'item id: ' . $self->item->id . " new type: $new_type old type: $old_type #old children: " . scalar(@{ $old_parent->children }));
116 if ($old_visible_section->id != $new_visible_section->id) {
117 # The currently visible item has been dragged to a different section.
118 $self->invalidate_version;
119 return $self->render_list($new_section, $self->item)
123 if ($old_visible_section->id == $old_section->id) {
124 my $id_prefix = $old_type eq 'sub-function-block' ? 'sub-' : '';
125 $self->js->remove('#' . $id_prefix . 'function-block-' . $self->item->id);
127 if ($old_type eq 'sub-function-block') {
128 $self->replace_bottom($old_parent) ;
129 $self->js->hide('#sub-function-block-container-' . $old_parent->id) if 0 == scalar(@{ $old_parent->children });
131 } elsif (0 == scalar(@{ $old_section->children })) {
132 $self->js->show('#section-list-empty');
136 if ($old_visible_section->id == $new_section->id) {
137 $self->insert_new_item_in_section_view($self->js);
140 # $::lxdebug->dump(0, "js", $self->js->to_array);
142 $self->invalidate_version->render($self);
145 sub action_ajax_add_section {
146 my ($self, %params) = @_;
148 die "Missing parameter 'requirement_spec_id'" if !$::form->{requirement_spec_id};
150 $self->item(SL::DB::RequirementSpecItem->new(requirement_spec_id => $::form->{requirement_spec_id}, item_type => 'section'));
152 my $insert_after = $::form->{id} ? SL::DB::RequirementSpecItem->new(id => $::form->{id})->load->section->id : undef;
153 my $html = $self->render('requirement_spec_item/_section_form', { output => 0 }, id_base => 'new_section', insert_after => $insert_after);
156 ->remove('#new_section_form')
157 ->hide('#column-content > *')
158 ->appendTo($html, '#column-content')
159 ->focus('#new_section_title')
164 sub action_ajax_add_function_block {
165 my ($self, %params) = @_;
167 return $self->add_function_block('function-block');
170 sub action_ajax_add_sub_function_block {
171 my ($self, %params) = @_;
173 return $self->add_function_block('sub-function-block');
176 sub action_ajax_create {
177 my ($self, %params) = @_;
179 my $prefix = $::form->{form_prefix} || die "Missing parameter 'form_prefix'";
180 my $attributes = $::form->{$prefix} || die "Missing parameter group '${prefix}'";
181 my $insert_after = delete $attributes->{insert_after};
183 if (!$attributes->{parent_id}) {
184 $attributes->{order_part_id} ||= $::instance_conf->get_requirement_spec_section_order_part_id;
187 my @errors = $self->item(SL::DB::RequirementSpecItem->new(%{ $attributes }))->validate;
188 return $self->js->error(@errors)->render($self) if @errors;
191 $self->item->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
193 my $type = $self->item->item_type;
195 if ($type eq 'section') {
196 my $node = $self->item->presenter->jstree_data;
197 $self->invalidate_version;
198 $self->render_list($self->item)
199 ->hide('#section-list-empty')
200 ->jstree->create_node('#tree', $insert_after ? ('#fb-' . $insert_after, 'after') : ('#sections', 'last'), $node)
201 ->jstree->select_node('#tree', '#fb-' . $self->item->id);
202 return $self->add_new_item_form_after_create
207 my $template = 'requirement_spec_item/_' . (apply { s/-/_/g; $_ } $type);
208 my $html = $self->render($template, { output => 0 }, requirement_spec_item => $self->item, id_prefix => $type eq 'function-block' ? '' : 'sub-');
209 my $node = $self->item->presenter->jstree_data;
212 ->replaceWith('#' . $prefix . '_form', $html)
213 ->hide('#section-list-empty')
214 ->jstree->create_node('#tree', $insert_after ? ('#fb-' . $insert_after, 'after') : ('#fb-' . $self->item->parent_id, 'last'), $node)
216 ->jstree->select_node('#tree', '#fb-' . $self->item->id);
218 $self->replace_bottom($self->item->parent) if $type eq 'sub-function-block';
220 $self->add_new_item_form_after_create;
222 $self->invalidate_version->render($self);
225 sub action_ajax_edit {
226 my ($self, %params) = @_;
228 $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load);
230 if (!$self->is_item_visible) {
231 # Show section/item to edit if it is not visible.
233 my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item->section);
234 $self->set_function_blocks_tab_menu_class(class => 'section-context-menu')
235 ->html('#column-content', $html);
238 if ($self->item->item_type =~ m/section/) {
239 # Edit the section header, not an item.
240 my $html = $self->render('requirement_spec_item/_section_form', { output => 0 });
243 ->hide('#section-header-' . $self->item->id)
244 ->remove("#edit_section_form")
245 ->insertAfter($html, '#section-header-' . $self->item->id)
246 ->jstree->select_node('#tree', '#fb-' . $self->item->id)
247 ->val('#current_content_type', 'section')
248 ->val('#current_content_id', $self->item->id)
250 ->focus("#edit_section_title")
255 # Edit a function block or a sub function block
256 my @dependencies = $self->create_dependencies;
257 my @selected_dependencies = map { $_->id } @{ $self->item->dependencies };
259 my $html = $self->render('requirement_spec_item/_function_block_form', { output => 0 }, DEPENDENCIES => \@dependencies, SELECTED_DEPENDENCIES => \@selected_dependencies);
260 my $id_base = 'edit_function_block_' . $self->item->id;
261 my $content_top_id = '#' . $self->item->item_type . '-content-top-' . $self->item->id;
264 ->hide($content_top_id)
265 ->remove("#${id_base}_form")
266 ->insertAfter($html, $content_top_id)
267 ->run('kivi.requirement_spec.init_function_block_keypress_events', "${id_base}_form")
268 ->jstree->select_node('#tree', '#fb-' . $self->item->id)
269 ->val('#current_content_type', $self->item->item_type)
270 ->val('#current_content_id', $self->item->id)
272 ->focus("#${id_base}_description")
276 sub action_ajax_update {
277 my ($self, %params) = @_;
279 my $prefix = $::form->{form_prefix} || die "Missing parameter 'form_prefix'";
280 my $attributes = $::form->{$prefix} || {};
282 foreach (qw(requirement_spec_id parent_id position)) {
283 delete $attributes->{$_} if !defined $attributes->{$_};
286 my @errors = $self->item->assign_attributes(%{ $attributes })->validate;
287 return $self->js->error(@errors)->render($self) if @errors;
291 my $type = $self->item->item_type;
293 if ($type eq 'section') {
294 # Updated section, now update section header.
296 my $html = $self->render('requirement_spec_item/_section_header', { output => 0 }, requirement_spec_item => $self->item);
298 return $self->invalidate_version
299 ->remove('#edit_section_form')
300 ->html('#section-header-' . $self->item->id, $html)
301 ->show('#section-header-' . $self->item->id)
302 ->jstree->rename_node('#tree', '#fb-' . $self->item->id, $self->item->presenter->tree_node_title)
303 ->prop('#fb-' . $self->item->id . ' a', 'title', $self->item->content_excerpt)
304 ->addClass('#fb-' . $self->item->id . ' a', 'tooltip')
309 # Updated function block or sub function block. Update (sub)
310 # function block and potentially the bottom of the parent function
313 my $id_prefix = $type eq 'function-block' ? '' : 'sub-';
314 my $html_top = $self->render('requirement_spec_item/_function_block_content_top', { output => 0 }, requirement_spec_item => $self->item, id_prefix => $id_prefix);
315 $id_prefix .= 'function-block-content-';
318 ->remove('#' . $prefix . '_form')
319 ->replaceWith('#' . $id_prefix . 'top-' . $self->item->id, $html_top)
320 ->prop('#fb-' . $self->item->id . ' a', 'title', $self->item->content_excerpt)
321 ->addClass('#fb-' . $self->item->id . ' a', 'tooltip')
323 ->jstree->rename_node('#tree', '#fb-' . $self->item->id, $self->item->presenter->tree_node_title);
325 $self->replace_bottom($self->item, id_prefix => $id_prefix);
326 $self->replace_bottom($self->item->parent) if $type eq 'sub-function-block';
328 $self->invalidate_version->render($self);
331 sub action_ajax_delete {
334 my $full_list = $self->item->get_full_list;
338 if ($self->visible_section && ($self->visible_section->id == $self->item->id)) {
339 # Currently visible section is deleted.
341 my $new_section = first { $_->id != $self->item->id } @{ $self->item->requirement_spec->sections };
343 $self->render_list($new_section);
346 my $html = $self->render('requirement_spec_item/_no_section', { output => 0 });
347 $self->set_function_blocks_tab_menu_class(class => 'section-context-menu')
348 ->html('#column-content', $html)
349 ->val('#current_content_type', '')
350 ->val('#current_content_id', '')
353 } elsif ($self->is_item_visible) {
354 # Item in currently visible section is deleted.
356 my $type = $self->item->item_type;
357 $self->js->remove('#edit_function_block_' . $self->item->id . '_form')
358 ->remove('#' . $type . '-' . $self->item->id);
360 $self->replace_bottom($self->item->parent_id) if $type eq 'sub-function-block';
362 if (1 == scalar @{ $full_list }) {
363 if ($type eq 'function-block') {
364 $self->js->show('#section-list-empty');
365 } elsif ($type eq 'sub-function-block') {
366 $self->js->hide('#sub-function-block-container-' . $self->item->parent_id);
371 $self->invalidate_version
372 ->jstree->delete_node('#tree', '#fb-' . $self->item->id)
376 sub action_ajax_flag {
379 $self->item->update_attributes(is_flagged => !$self->item->is_flagged);
382 ->action_if($self->is_item_visible, 'toggleClass', '#' . $self->item->item_type . '-' . $self->item->id, 'flagged')
383 ->toggleClass('#fb-' . $self->item->id, 'flagged')
387 sub action_ajax_copy {
388 my ($self, %params) = @_;
390 SL::Clipboard->new->copy($self->item);
391 $self->render($self->js);
394 sub determine_paste_position {
397 if ($self->item->item_type eq 'section') {
398 # Sections are always pasted either directly after the
399 # clicked-upon section or at the very end.
400 return $self->clicked_item ? (undef, $self->clicked_item->section->id) : ();
402 } elsif ($self->item->item_type eq 'function-block') {
404 # - paste on section list: insert into last section as last element
405 # - paste on section: insert into that section as last element
406 # - paste on function block: insert after clicked-upon element
407 # - paste on sub function block: insert after parent function block of clicked-upon element
408 return !$self->clicked_item ? ( $self->sections->[-1]->id, undef )
409 : $self->clicked_item->item_type eq 'section' ? ( $self->clicked_item->id, undef )
410 : $self->clicked_item->item_type eq 'function-block' ? ( $self->clicked_item->parent_id, $self->clicked_item->id )
411 : ( $self->clicked_item->parent->parent_id, $self->clicked_item->parent_id );
413 } else { # sub-function-block
414 # A sub function block:
415 # - paste on section list: promote to function block and insert into last section as last element
416 # - paste on section: promote to function block and insert into that section as last element
417 # - paste on function block: insert as last element in clicked-upon element
418 # - paste on sub function block: insert after clicked-upon element
420 # Promote sub function blocks to function blocks when pasting on a
421 # section or the section list.
422 $self->item->item_type('function-block') if !$self->clicked_item || ($self->clicked_item->item_type eq 'section');
424 return !$self->clicked_item ? ( $self->sections->[-1]->id, undef )
425 : $self->clicked_item->item_type eq 'section' ? ( $self->clicked_item->id, undef )
426 : $self->clicked_item->item_type eq 'function-block' ? ( $self->clicked_item->id, undef )
427 : ( $self->clicked_item->parent_id, $self->clicked_item->id );
431 sub assign_requirement_spec_id_rec {
432 my ($self, $item) = @_;
434 $item->assign_attributes(requirement_spec_id => $::form->{requirement_spec_id}, fb_number => undef);
435 $self->assign_requirement_spec_id_rec($_) for @{ $item->children || [] };
440 sub create_and_insert_node_rec {
441 my ($self, $item, $new_parent_id, $insert_after) = @_;
443 my $node = $item->presenter->jstree_data;
444 $self->js->jstree->create_node('#tree', $insert_after ? ('#fb-' . $insert_after, 'after') : $new_parent_id ? ('#fb-' . $new_parent_id, 'last') : ('#sections', 'last'), $node);
446 $self->create_and_insert_node_rec($_, $item->id) for @{ $item->children || [] };
448 $self->js->jstree->open_node('#tree', '#fb-' . $item->id);
451 sub action_ajax_paste {
452 my ($self, %params) = @_;
454 my $copied = SL::Clipboard->new->get_entry(qr/^RequirementSpecItem$/);
457 return $self->js->error(t8("The clipboard does not contain anything that can be pasted here."))
461 $self->item($self->assign_requirement_spec_id_rec($copied->to_object));
462 my $req_spec = SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load;
463 $self->sections($req_spec->sections);
465 if (($self->item->item_type ne 'section') && !@{ $self->sections }) {
466 return $self->js->error(t8("You cannot paste function blocks or sub function blocks if there is no section."))
470 $self->clicked_item($::form->{id} ? SL::DB::RequirementSpecItem->new(id => $::form->{id})->load : undef);
472 my ($new_parent_id, $insert_after) = $self->determine_paste_position;
474 # Store result in database.
475 $self->item->update_attributes(parent_id => $new_parent_id);
476 $self->item->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
478 # Update the tree: create the node for all pasted objects.
479 $self->create_and_insert_node_rec($self->item, $new_parent_id, $insert_after);
481 # Pasting the very first section?
482 if (!@{ $self->sections }) {
483 my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item);
484 $self->set_function_blocks_tab_menu_class(class => 'section-context-menu')
485 ->html('#column-content', $html)
486 ->jstree->select_node('#tree', '#fb-' . $self->item->id)
489 # Update the current view if required.
490 $self->insert_new_item_in_section_view($self->js) if $self->is_item_visible;
492 $self->invalidate_version->render($self);
501 $::auth->assert('requirement_spec_edit');
504 sub load_requirement_spec_item {
506 $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load);
513 sub create_random_id {
514 return join '-', Time::HiRes::gettimeofday();
517 sub format_exception {
518 return join "\n", (split m/\n/, $@)[0..4];
521 sub init_complexities {
524 return SL::DB::Manager::RequirementSpecComplexity->get_all_sorted;
530 return SL::DB::Manager::RequirementSpecRisk->get_all_sorted;
533 sub init_predefined_texts {
534 return SL::DB::Manager::RequirementSpecPredefinedText->get_all_sorted(where => [ useable_for_sections => 1 ]);
538 my ($self, $item_or_id) = @_;
540 my $item = (ref($item_or_id) ? $item_or_id : SL::DB::RequirementSpecItem->new(id => $item_or_id))->load;
541 my $id_prefix = $item->item_type eq 'function-block' ? '' : 'sub-';
542 my $html = $self->render('requirement_spec_item/_function_block_content_bottom', { output => 0 }, requirement_spec_item => $item, id_prefix => $id_prefix);
543 return $self->js->replaceWith('#' . $id_prefix . 'function-block-content-bottom-' . $item->id, $html);
547 my ($self, $item, $item_to_select) = @_;
549 my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $item);
550 $self->set_function_blocks_tab_menu_class(class => 'section-context-menu')
551 ->html('#column-content', $html);
552 $self->select_node($item_to_select || $item);
556 my ($self, $item) = @_;
559 ->val( '#current_content_type', $item->item_type)
560 ->val( '#current_content_id', $item->id)
561 ->jstree->select_node('#tree', '#fb-' . $item->id);
564 sub create_dependency_item {
566 [ $_[0]->id, truncate(join(' ', grep { $_ } ($_[1], $_[0]->fb_number, $_[0]->description_as_stripped_html))) ];
569 sub create_dependencies {
572 return map { [ $_->fb_number . ' ' . $_->title,
573 [ map { ( $self->create_dependency_item($_),
574 map { $self->create_dependency_item($_, '->') } @{ $_->children_sorted })
575 } @{ $_->children_sorted } ] ]
576 } @{ $self->item->requirement_spec->sections };
579 sub ensure_section_is_shown {
580 my ($self, %params) = @_;
582 return $self->js if $self->is_item_visible;
584 # Show section/item to edit if it is not visible.
585 my $new_section = $self->item->section;
586 my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $new_section);
588 return $self->set_function_blocks_tab_menu_class(class => 'section-context-menu')
589 ->html('#column-content', $html)
590 ->val('#current_content_type', 'section')
591 ->val('#current_content_id', $new_section->id)
592 ->jstree->select_node('#tree', '#fb-' . $new_section->id);
595 sub add_new_item_form {
596 my ($self, %params) = @_;
598 for (qw(insert_position display_reference)) {
599 croak "Missing parameter $_" if !$params{$_};
601 croak "Missing parameter insert_reference" if ($params{insert_position} eq 'insertAfter') && !$params{insert_reference};
603 my $id_base = join('_', 'new_function_block', Time::HiRes::gettimeofday(), int rand 1000000000000);
604 my $html = $self->render(
605 'requirement_spec_item/_function_block_form',
607 DEPENDENCIES => [ $self->create_dependencies ],
608 SELECTED_DEPENDENCIES => [],
609 requirement_spec_item => $self->item,
611 insert_after => $params{insert_position} eq 'insertAfter' ? $params{insert_reference} : undef,
615 ->action($params{insert_position}, $html, $params{display_reference})
616 ->action_if($self->item->item_type eq 'sub-function-block', 'show', '#sub-function-block-container-' . $self->item->parent_id)
617 ->run('kivi.requirement_spec.init_function_block_keypress_events', "${id_base}_form")
619 ->focus("#${id_base}_description");
622 sub add_new_item_form_after_create {
623 my ($self, %params) = @_;
625 my $created_item = $self->item;
627 my ($new_item_type, $new_parent_id, $insert_position, $insert_reference, $display_reference);
628 if ($created_item->item_type eq 'section') {
629 # $created_item is section, new one will be function-block
630 $new_item_type = 'function-block';
631 $new_parent_id = $created_item->id;
632 $insert_position = 'appendTo';
633 $display_reference = '#section-list';
635 } elsif (!$::form->{shift_in_out}) {
636 # $created_item is function-block or sub-function-block, new one will be the same
637 $new_item_type = $created_item->item_type;
638 $new_parent_id = $created_item->parent_id;
639 $insert_position = 'insertAfter';
640 $insert_reference = $created_item->id;
641 $display_reference = '#' . $created_item->item_type . '-' . $created_item->id;
643 } elsif ($created_item->item_type eq 'function-block') {
644 # $created_item is function-block, new one will be sub-function-block
645 $new_item_type = 'sub-function-block';
646 $new_parent_id = $created_item->id;
647 $insert_position = 'appendTo';
648 $display_reference = '#sub-function-block-container-' . $created_item->id;
651 # $created_item is sub-function-block, new one will be function-block
652 $new_item_type = 'function-block';
653 $new_parent_id = $created_item->parent->parent_id;
654 $insert_position = 'insertAfter';
655 $insert_reference = $created_item->parent_id;
656 $display_reference = '#function-block-' . $created_item->parent_id;
659 $::lxdebug->message(LXDebug::DEBUG1(),
660 "OLD item_type " . $created_item->item_type . " ; NEW item_type $new_item_type ; parent_id $new_parent_id ; " .
661 "insert_position $insert_position ; insert_reference $insert_reference ; display_reference $display_reference");
663 $self->item(SL::DB::RequirementSpecItem->new(
664 requirement_spec_id => $created_item->requirement_spec_id,
665 parent_id => $new_parent_id,
666 item_type => $new_item_type,
669 $self->add_new_item_form(
670 insert_reference => $insert_reference,
671 insert_position => $insert_position,
672 display_reference => $display_reference,
676 sub add_function_block {
677 my ($self, $new_type) = @_;
679 my $clicked_id = $::form->{id} || ($self->visible_item ? $self->visible_item->id : undef);
681 die "Invalid new_type '$new_type'" if $new_type !~ m/^(?:sub-)?function-block$/;
682 die "Missing parameter 'id' and no visible item" if !$clicked_id;
683 die "Missing parameter 'requirement_spec_id'" if !$::form->{requirement_spec_id};
685 my $clicked_item = SL::DB::RequirementSpecItem->new(id => $clicked_id)->load;
686 my $clicked_type = $clicked_item->item_type;
688 die "Invalid clicked_type '$clicked_type'" if $clicked_type !~ m/^(?: section | (?:sub-)? function-block )$/x;
690 my $case = "${clicked_type}:${new_type}";
692 my ($insert_position, $insert_reference, $parent_id, $display_reference)
693 = $case eq 'section:function-block' ? ( 'appendTo', $clicked_item->id, $clicked_item->id, '#section-list' )
694 : $case eq 'function-block:function-block' ? ( 'insertAfter', $clicked_item->id, $clicked_item->parent_id, '#function-block-' )
695 : $case eq 'function-block:sub-function-block' ? ( 'appendTo' , $clicked_item->id, $clicked_item->id, '#sub-function-block-container-' )
696 : $case eq 'sub-function-block:function-block' ? ( 'insertAfter', $clicked_item->parent_id, $clicked_item->parent->parent_id, '#function-block-' )
697 : $case eq 'sub-function-block:sub-function-block' ? ( 'insertAfter', $clicked_item->id, $clicked_item->parent_id, '#sub-function-block-' )
698 : die "Invalid combination of 'clicked_type (section)/new_type ($new_type)'";
700 $display_reference .= $insert_reference if $display_reference =~ m/-$/;
702 $self->item(SL::DB::RequirementSpecItem->new(requirement_spec_id => $::form->{requirement_spec_id}, parent_id => $parent_id, item_type => $new_type));
704 $self->ensure_section_is_shown;
705 $self->js->show('#sub-function-block-container-' . $parent_id) if $new_type eq 'sub-function-block';
706 $self->add_new_item_form(insert_position => $insert_position, insert_reference => $insert_reference, display_reference => $display_reference);
711 sub is_item_visible {
712 my ($self, $item) = @_;
714 $item ||= $self->item;
715 return $self->visible_section && ($self->visible_section->id == $item->section->id);
718 sub invalidate_version {
721 my $html = $self->render('requirement_spec/_version', { output => 0 },
722 requirement_spec => SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id} || $self->item->requirement_spec_id)->load);
723 return $self->js->html('#requirement_spec_version', $html);