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;
23 use Rose::Object::MakeMethods::Generic
25 scalar => [ qw(item visible_item visible_section clicked_item sections) ],
26 'scalar --get_set_init' => [ qw(complexities risks js predefined_texts) ],
29 __PACKAGE__->run_before('check_auth');
30 __PACKAGE__->run_before('load_requirement_spec_item', only => [ qw(dragged_and_dropped ajax_update ajax_edit ajax_delete ajax_flag ajax_copy) ]);
31 __PACKAGE__->run_before('init_visible_section');
37 sub action_ajax_list {
40 if (!$::form->{clicked_id}) {
41 # Clicked on "sections" in the tree. Do nothing.
42 return $self->render($self->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($self->item, $clicked_item);
51 $self->select_node($clicked_item);
54 $self->render($self->js);
57 sub insert_new_item_in_section_view {
60 $self->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 $self->js->insertBefore($html, '#' . $id_prefix . 'function-block-' . $next_item->id);
71 my $parent_is_section = $self->item->parent->item_type eq 'section';
72 $self->js->appendTo($html, $parent_is_section ? '#section-list' : '#sub-function-block-container-' . $self->item->parent_id);
73 $self->js->show('#sub-function-block-container-' . $self->item->parent_id) if !$parent_is_section;
76 $self->replace_bottom($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;
88 my $new_type = !$dropped_item ? 'section' : $position =~ m/before|after/ ? $dropped_item->item_type : $dropped_item->child_type;
90 $self->item->db->do_transaction(sub {
91 $self->item->remove_from_list;
92 $self->item->parent_id($position =~ m/before|after/ ? $dropped_item->parent_id : $dropped_item->id) if $dropped_item;
93 $self->item->item_type($new_type);
94 $self->item->add_to_list(position => $position, reference => $::form->{dropped_id} || undef);
97 $self->item(SL::DB::RequirementSpecItem->new(id => $self->item->id)->load);
98 my $new_section = $self->item->section;
99 my $new_visible_section = SL::DB::RequirementSpecItem->new(id => $self->visible_item->id)->load->section;
101 return $self->invalidate_version->render($self) if !$old_visible_section || ($new_type eq 'section');
103 # From here on $old_visible_section is definitely set.
105 my $old_parent = SL::DB::RequirementSpecItem->new(id => $old_parent_id)->load;
106 my $old_section = $old_parent->section;
108 # Section debug info:
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);
113 # $::lxdebug->message(0, 'item id: ' . $self->item->id . " new type: $new_type old type: $old_type #old children: " . scalar(@{ $old_parent->children }));
115 if ($old_visible_section->id != $new_visible_section->id) {
116 # The currently visible item has been dragged to a different section.
117 $self->invalidate_version;
118 return $self->render_list($new_section, $self->item)
122 if ($old_visible_section->id == $old_section->id) {
123 my $id_prefix = $old_type eq 'sub-function-block' ? 'sub-' : '';
124 $self->js->remove('#' . $id_prefix . 'function-block-' . $self->item->id);
126 if ($old_type eq 'sub-function-block') {
127 $self->replace_bottom($old_parent) ;
128 $self->js->hide('#sub-function-block-container-' . $old_parent->id) if 0 == scalar(@{ $old_parent->children });
130 } elsif (0 == scalar(@{ $old_section->children })) {
131 $self->js->show('#section-list-empty');
135 if ($old_visible_section->id == $new_section->id) {
136 $self->insert_new_item_in_section_view($self->js);
139 # $::lxdebug->dump(0, "js", $self->js->to_array);
141 $self->invalidate_version->render($self);
144 sub action_ajax_add_section {
145 my ($self, %params) = @_;
147 die "Missing parameter 'requirement_spec_id'" if !$::form->{requirement_spec_id};
149 $self->item(SL::DB::RequirementSpecItem->new(requirement_spec_id => $::form->{requirement_spec_id}, item_type => 'section'));
151 my $insert_after = $::form->{id} ? SL::DB::RequirementSpecItem->new(id => $::form->{id})->load->section->id : undef;
152 my $html = $self->render('requirement_spec_item/_section_form', { output => 0 }, id_base => 'new_section', insert_after => $insert_after);
155 ->remove('#new_section_form')
156 ->hide('#column-content > *')
157 ->appendTo($html, '#column-content')
158 ->focus('#new_section_title')
163 sub action_ajax_add_function_block {
164 my ($self, %params) = @_;
166 return $self->add_function_block('function-block');
169 sub action_ajax_add_sub_function_block {
170 my ($self, %params) = @_;
172 return $self->add_function_block('sub-function-block');
175 sub action_ajax_create {
176 my ($self, %params) = @_;
178 my $prefix = $::form->{form_prefix} || die "Missing parameter 'form_prefix'";
179 my $attributes = $::form->{$prefix} || die "Missing parameter group '${prefix}'";
180 my $insert_after = delete $attributes->{insert_after};
182 if (!$attributes->{parent_id}) {
183 $attributes->{order_part_id} ||= $::instance_conf->get_requirement_spec_section_order_part_id;
186 my @errors = $self->item(SL::DB::RequirementSpecItem->new(%{ $attributes }))->validate;
187 return $self->js->error(@errors)->render($self) if @errors;
190 $self->item->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
192 my $type = $self->item->item_type;
194 if ($type eq 'section') {
195 my $node = $self->presenter->requirement_spec_item_jstree_data($self->item);
196 $self->invalidate_version;
197 $self->render_list($self->item)
198 ->hide('#section-list-empty')
199 ->jstree->create_node('#tree', $insert_after ? ('#fb-' . $insert_after, 'after') : ('#sections', 'last'), $node)
200 ->jstree->select_node('#tree', '#fb-' . $self->item->id);
201 return $self->add_new_item_form_after_create
206 my $template = 'requirement_spec_item/_' . (apply { s/-/_/g; $_ } $type);
207 my $html = $self->render($template, { output => 0 }, requirement_spec_item => $self->item, id_prefix => $type eq 'function-block' ? '' : 'sub-');
208 my $node = $self->presenter->requirement_spec_item_jstree_data($self->item);
211 ->replaceWith('#' . $prefix . '_form', $html)
212 ->hide('#section-list-empty')
213 ->jstree->create_node('#tree', $insert_after ? ('#fb-' . $insert_after, 'after') : ('#fb-' . $self->item->parent_id, 'last'), $node)
215 ->jstree->select_node('#tree', '#fb-' . $self->item->id);
217 $self->replace_bottom($self->item->parent) if $type eq 'sub-function-block';
219 $self->add_new_item_form_after_create;
221 $self->invalidate_version->render($self);
224 sub action_ajax_edit {
225 my ($self, %params) = @_;
227 $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load);
229 if (!$self->is_item_visible) {
230 # Show section/item to edit if it is not visible.
232 my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item->section);
233 $self->set_function_blocks_tab_menu_class(class => 'section-context-menu')
234 ->html('#column-content', $html);
237 if ($self->item->item_type =~ m/section/) {
238 # Edit the section header, not an item.
239 my $html = $self->render('requirement_spec_item/_section_form', { output => 0 });
242 ->hide('#section-header-' . $self->item->id)
243 ->remove("#edit_section_form")
244 ->insertAfter($html, '#section-header-' . $self->item->id)
245 ->jstree->select_node('#tree', '#fb-' . $self->item->id)
246 ->val('#current_content_type', 'section')
247 ->val('#current_content_id', $self->item->id)
249 ->focus("#edit_section_title")
254 # Edit a function block or a sub function block
255 my @dependencies = $self->create_dependencies;
256 my @selected_dependencies = map { $_->id } @{ $self->item->dependencies };
258 my $html = $self->render('requirement_spec_item/_function_block_form', { output => 0 }, DEPENDENCIES => \@dependencies, SELECTED_DEPENDENCIES => \@selected_dependencies);
259 my $id_base = 'edit_function_block_' . $self->item->id;
260 my $content_top_id = '#' . $self->item->item_type . '-content-top-' . $self->item->id;
263 ->hide($content_top_id)
264 ->remove("#${id_base}_form")
265 ->insertAfter($html, $content_top_id)
266 ->run('kivi.requirement_spec.init_function_block_keypress_events', "${id_base}_form")
267 ->jstree->select_node('#tree', '#fb-' . $self->item->id)
268 ->val('#current_content_type', $self->item->item_type)
269 ->val('#current_content_id', $self->item->id)
271 ->focus("#${id_base}_description")
275 sub action_ajax_update {
276 my ($self, %params) = @_;
278 my $prefix = $::form->{form_prefix} || die "Missing parameter 'form_prefix'";
279 my $attributes = $::form->{$prefix} || {};
281 foreach (qw(requirement_spec_id parent_id position)) {
282 delete $attributes->{$_} if !defined $attributes->{$_};
285 my @errors = $self->item->assign_attributes(%{ $attributes })->validate;
286 return $self->js->error(@errors)->render($self) if @errors;
290 my $type = $self->item->item_type;
292 if ($type eq 'section') {
293 # Updated section, now update section header.
295 my $html = $self->render('requirement_spec_item/_section_header', { output => 0 }, requirement_spec_item => $self->item);
297 return $self->invalidate_version
298 ->remove('#edit_section_form')
299 ->html('#section-header-' . $self->item->id, $html)
300 ->show('#section-header-' . $self->item->id)
301 ->jstree->rename_node('#tree', '#fb-' . $self->item->id, $::request->presenter->requirement_spec_item_tree_node_title($self->item))
302 ->prop('#fb-' . $self->item->id . ' a', 'title', $self->item->content_excerpt)
303 ->addClass('#fb-' . $self->item->id . ' a', 'tooltip')
308 # Updated function block or sub function block. Update (sub)
309 # function block and potentially the bottom of the parent function
312 my $id_prefix = $type eq 'function-block' ? '' : 'sub-';
313 my $html_top = $self->render('requirement_spec_item/_function_block_content_top', { output => 0 }, requirement_spec_item => $self->item, id_prefix => $id_prefix);
314 $id_prefix .= 'function-block-content-';
317 ->remove('#' . $prefix . '_form')
318 ->replaceWith('#' . $id_prefix . 'top-' . $self->item->id, $html_top)
319 ->prop('#fb-' . $self->item->id . ' a', 'title', $self->item->content_excerpt)
320 ->addClass('#fb-' . $self->item->id . ' a', 'tooltip')
322 ->jstree->rename_node('#tree', '#fb-' . $self->item->id, $::request->presenter->requirement_spec_item_tree_node_title($self->item));
324 $self->replace_bottom($self->item, id_prefix => $id_prefix);
325 $self->replace_bottom($self->item->parent) if $type eq 'sub-function-block';
327 $self->invalidate_version->render($self);
330 sub action_ajax_delete {
333 my $full_list = $self->item->get_full_list;
337 if ($self->visible_section && ($self->visible_section->id == $self->item->id)) {
338 # Currently visible section is deleted.
340 my $new_section = first { $_->id != $self->item->id } @{ $self->item->requirement_spec->sections };
342 $self->render_list($new_section);
345 my $html = $self->render('requirement_spec_item/_no_section', { output => 0 });
346 $self->set_function_blocks_tab_menu_class(class => 'section-context-menu')
347 ->html('#column-content', $html)
348 ->val('#current_content_type', '')
349 ->val('#current_content_id', '')
352 } elsif ($self->is_item_visible) {
353 # Item in currently visible section is deleted.
355 my $type = $self->item->item_type;
356 $self->js->remove('#edit_function_block_' . $self->item->id . '_form')
357 ->remove('#' . $type . '-' . $self->item->id);
359 $self->replace_bottom($self->item->parent_id) if $type eq 'sub-function-block';
361 if (1 == scalar @{ $full_list }) {
362 if ($type eq 'function-block') {
363 $self->js->show('#section-list-empty');
364 } elsif ($type eq 'sub-function-block') {
365 $self->js->hide('#sub-function-block-container-' . $self->item->parent_id);
370 $self->invalidate_version
371 ->jstree->delete_node('#tree', '#fb-' . $self->item->id)
375 sub action_ajax_flag {
378 $self->item->update_attributes(is_flagged => !$self->item->is_flagged);
381 ->action_if($self->is_item_visible, 'toggleClass', '#' . $self->item->item_type . '-' . $self->item->id, 'flagged')
382 ->toggleClass('#fb-' . $self->item->id, 'flagged')
386 sub action_ajax_copy {
387 my ($self, %params) = @_;
389 SL::Clipboard->new->copy($self->item);
390 $self->render($self->js);
393 sub determine_paste_position {
396 if ($self->item->item_type eq 'section') {
397 # Sections are always pasted either directly after the
398 # clicked-upon section or at the very end.
399 return $self->clicked_item ? (undef, $self->clicked_item->section->id) : ();
401 } elsif ($self->item->item_type eq 'function-block') {
403 # - paste on section list: insert into last section as last element
404 # - paste on section: insert into that section as last element
405 # - paste on function block: insert after clicked-upon element
406 # - paste on sub function block: insert after parent function block of clicked-upon element
407 return !$self->clicked_item ? ( $self->sections->[-1]->id, undef )
408 : $self->clicked_item->item_type eq 'section' ? ( $self->clicked_item->id, undef )
409 : $self->clicked_item->item_type eq 'function-block' ? ( $self->clicked_item->parent_id, $self->clicked_item->id )
410 : ( $self->clicked_item->parent->parent_id, $self->clicked_item->parent_id );
412 } else { # sub-function-block
413 # A sub function block:
414 # - paste on section list: promote to function block and insert into last section as last element
415 # - paste on section: promote to function block and insert into that section as last element
416 # - paste on function block: insert as last element in clicked-upon element
417 # - paste on sub function block: insert after clicked-upon element
419 # Promote sub function blocks to function blocks when pasting on a
420 # section or the section list.
421 $self->item->item_type('function-block') if !$self->clicked_item || ($self->clicked_item->item_type eq 'section');
423 return !$self->clicked_item ? ( $self->sections->[-1]->id, undef )
424 : $self->clicked_item->item_type eq 'section' ? ( $self->clicked_item->id, undef )
425 : $self->clicked_item->item_type eq 'function-block' ? ( $self->clicked_item->id, undef )
426 : ( $self->clicked_item->parent_id, $self->clicked_item->id );
430 sub assign_requirement_spec_id_rec {
431 my ($self, $item) = @_;
433 $item->assign_attributes(requirement_spec_id => $::form->{requirement_spec_id}, fb_number => undef);
434 $self->assign_requirement_spec_id_rec($_) for @{ $item->children || [] };
439 sub create_and_insert_node_rec {
440 my ($self, $item, $new_parent_id, $insert_after) = @_;
442 my $node = $self->presenter->requirement_spec_item_jstree_data($item);
443 $self->js->jstree->create_node('#tree', $insert_after ? ('#fb-' . $insert_after, 'after') : $new_parent_id ? ('#fb-' . $new_parent_id, 'last') : ('#sections', 'last'), $node);
445 $self->create_and_insert_node_rec($_, $item->id) for @{ $item->children || [] };
447 $self->js->jstree->open_node('#tree', '#fb-' . $item->id);
450 sub action_ajax_paste {
451 my ($self, %params) = @_;
453 my $copied = SL::Clipboard->new->get_entry(qr/^RequirementSpecItem$/);
456 return $self->js->error(t8("The clipboard does not contain anything that can be pasted here."))
460 $self->item($self->assign_requirement_spec_id_rec($copied->to_object));
461 my $req_spec = SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load;
462 $self->sections($req_spec->sections);
464 if (($self->item->item_type ne 'section') && !@{ $self->sections }) {
465 return $self->js->error(t8("You cannot paste function blocks or sub function blocks if there is no section."))
469 $self->clicked_item($::form->{id} ? SL::DB::RequirementSpecItem->new(id => $::form->{id})->load : undef);
471 my ($new_parent_id, $insert_after) = $self->determine_paste_position;
473 # Store result in database.
474 $self->item->update_attributes(parent_id => $new_parent_id);
475 $self->item->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
477 # Update the tree: create the node for all pasted objects.
478 $self->create_and_insert_node_rec($self->item, $new_parent_id, $insert_after);
480 # Pasting the very first section?
481 if (!@{ $self->sections }) {
482 my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item);
483 $self->set_function_blocks_tab_menu_class(class => 'section-context-menu')
484 ->html('#column-content', $html)
485 ->jstree->select_node('#tree', '#fb-' . $self->item->id)
488 # Update the current view if required.
489 $self->insert_new_item_in_section_view($self->js) if $self->is_item_visible;
491 $self->invalidate_version->render($self);
500 $::auth->assert('requirement_spec_edit');
503 sub load_requirement_spec_item {
505 $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load);
512 sub create_random_id {
513 return join '-', Time::HiRes::gettimeofday();
516 sub format_exception {
517 return join "\n", (split m/\n/, $@)[0..4];
520 sub init_complexities {
523 return SL::DB::Manager::RequirementSpecComplexity->get_all_sorted;
529 return SL::DB::Manager::RequirementSpecRisk->get_all_sorted;
532 sub init_predefined_texts {
533 return SL::DB::Manager::RequirementSpecPredefinedText->get_all_sorted(where => [ useable_for_sections => 1 ]);
538 $self->js(SL::ClientJS->new);
542 my ($self, $item_or_id) = @_;
544 my $item = (ref($item_or_id) ? $item_or_id : SL::DB::RequirementSpecItem->new(id => $item_or_id))->load;
545 my $id_prefix = $item->item_type eq 'function-block' ? '' : 'sub-';
546 my $html = $self->render('requirement_spec_item/_function_block_content_bottom', { output => 0 }, requirement_spec_item => $item, id_prefix => $id_prefix);
547 return $self->js->replaceWith('#' . $id_prefix . 'function-block-content-bottom-' . $item->id, $html);
551 my ($self, $item, $item_to_select) = @_;
553 my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $item);
554 $self->set_function_blocks_tab_menu_class(class => 'section-context-menu')
555 ->html('#column-content', $html);
556 $self->select_node($item_to_select || $item);
560 my ($self, $item) = @_;
563 ->val( '#current_content_type', $item->item_type)
564 ->val( '#current_content_id', $item->id)
565 ->jstree->select_node('#tree', '#fb-' . $item->id);
568 sub create_dependency_item {
570 [ $_[0]->id, $self->presenter->truncate(join(' ', grep { $_ } ($_[1], $_[0]->fb_number, $_[0]->description_as_stripped_html))) ];
573 sub create_dependencies {
576 return map { [ $_->fb_number . ' ' . $_->title,
577 [ map { ( $self->create_dependency_item($_),
578 map { $self->create_dependency_item($_, '->') } @{ $_->children_sorted })
579 } @{ $_->children_sorted } ] ]
580 } @{ $self->item->requirement_spec->sections };
583 sub ensure_section_is_shown {
584 my ($self, %params) = @_;
586 return $self->js if $self->is_item_visible;
588 # Show section/item to edit if it is not visible.
589 my $new_section = $self->item->section;
590 my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $new_section);
592 return $self->set_function_blocks_tab_menu_class(class => 'section-context-menu')
593 ->html('#column-content', $html)
594 ->val('#current_content_type', 'section')
595 ->val('#current_content_id', $new_section->id)
596 ->jstree->select_node('#tree', '#fb-' . $new_section->id);
599 sub add_new_item_form {
600 my ($self, %params) = @_;
602 for (qw(insert_position display_reference)) {
603 croak "Missing parameter $_" if !$params{$_};
605 croak "Missing parameter insert_reference" if ($params{insert_position} eq 'insertAfter') && !$params{insert_reference};
607 my $id_base = join('_', 'new_function_block', Time::HiRes::gettimeofday(), int rand 1000000000000);
608 my $html = $self->render(
609 'requirement_spec_item/_function_block_form',
611 DEPENDENCIES => [ $self->create_dependencies ],
612 SELECTED_DEPENDENCIES => [],
613 requirement_spec_item => $self->item,
615 insert_after => $params{insert_position} eq 'insertAfter' ? $params{insert_reference} : undef,
619 ->action($params{insert_position}, $html, $params{display_reference})
620 ->action_if($self->item->item_type eq 'sub-function-block', 'show', '#sub-function-block-container-' . $self->item->parent_id)
621 ->run('kivi.requirement_spec.init_function_block_keypress_events', "${id_base}_form")
623 ->focus("#${id_base}_description");
626 sub add_new_item_form_after_create {
627 my ($self, %params) = @_;
629 my $created_item = $self->item;
631 my ($new_item_type, $new_parent_id, $insert_position, $insert_reference, $display_reference);
632 if ($created_item->item_type eq 'section') {
633 # $created_item is section, new one will be function-block
634 $new_item_type = 'function-block';
635 $new_parent_id = $created_item->id;
636 $insert_position = 'appendTo';
637 $display_reference = '#section-list';
639 } elsif (!$::form->{shift_in_out}) {
640 # $created_item is function-block or sub-function-block, new one will be the same
641 $new_item_type = $created_item->item_type;
642 $new_parent_id = $created_item->parent_id;
643 $insert_position = 'insertAfter';
644 $insert_reference = $created_item->id;
645 $display_reference = '#' . $created_item->item_type . '-' . $created_item->id;
647 } elsif ($created_item->item_type eq 'function-block') {
648 # $created_item is function-block, new one will be sub-function-block
649 $new_item_type = 'sub-function-block';
650 $new_parent_id = $created_item->id;
651 $insert_position = 'appendTo';
652 $display_reference = '#sub-function-block-container-' . $created_item->id;
655 # $created_item is sub-function-block, new one will be function-block
656 $new_item_type = 'function-block';
657 $new_parent_id = $created_item->parent->parent_id;
658 $insert_position = 'insertAfter';
659 $insert_reference = $created_item->parent_id;
660 $display_reference = '#function-block-' . $created_item->parent_id;
663 $::lxdebug->message(LXDebug::DEBUG1(),
664 "OLD item_type " . $created_item->item_type . " ; NEW item_type $new_item_type ; parent_id $new_parent_id ; " .
665 "insert_position $insert_position ; insert_reference $insert_reference ; display_reference $display_reference");
667 $self->item(SL::DB::RequirementSpecItem->new(
668 requirement_spec_id => $created_item->requirement_spec_id,
669 parent_id => $new_parent_id,
670 item_type => $new_item_type,
673 $self->add_new_item_form(
674 insert_reference => $insert_reference,
675 insert_position => $insert_position,
676 display_reference => $display_reference,
680 sub add_function_block {
681 my ($self, $new_type) = @_;
683 my $clicked_id = $::form->{id} || ($self->visible_item ? $self->visible_item->id : undef);
685 die "Invalid new_type '$new_type'" if $new_type !~ m/^(?:sub-)?function-block$/;
686 die "Missing parameter 'id' and no visible item" if !$clicked_id;
687 die "Missing parameter 'requirement_spec_id'" if !$::form->{requirement_spec_id};
689 my $clicked_item = SL::DB::RequirementSpecItem->new(id => $clicked_id)->load;
690 my $clicked_type = $clicked_item->item_type;
692 die "Invalid clicked_type '$clicked_type'" if $clicked_type !~ m/^(?: section | (?:sub-)? function-block )$/x;
694 my $case = "${clicked_type}:${new_type}";
696 my ($insert_position, $insert_reference, $parent_id, $display_reference)
697 = $case eq 'section:function-block' ? ( 'appendTo', $clicked_item->id, $clicked_item->id, '#section-list' )
698 : $case eq 'function-block:function-block' ? ( 'insertAfter', $clicked_item->id, $clicked_item->parent_id, '#function-block-' )
699 : $case eq 'function-block:sub-function-block' ? ( 'appendTo' , $clicked_item->id, $clicked_item->id, '#sub-function-block-container-' )
700 : $case eq 'sub-function-block:function-block' ? ( 'insertAfter', $clicked_item->parent_id, $clicked_item->parent->parent_id, '#function-block-' )
701 : $case eq 'sub-function-block:sub-function-block' ? ( 'insertAfter', $clicked_item->id, $clicked_item->parent_id, '#sub-function-block-' )
702 : die "Invalid combination of 'clicked_type (section)/new_type ($new_type)'";
704 $display_reference .= $insert_reference if $display_reference =~ m/-$/;
706 $self->item(SL::DB::RequirementSpecItem->new(requirement_spec_id => $::form->{requirement_spec_id}, parent_id => $parent_id, item_type => $new_type));
708 $self->ensure_section_is_shown;
709 $self->js->show('#sub-function-block-container-' . $parent_id) if $new_type eq 'sub-function-block';
710 $self->add_new_item_form(insert_position => $insert_position, insert_reference => $insert_reference, display_reference => $display_reference);
712 $self->js->render($self);
715 sub is_item_visible {
716 my ($self, $item) = @_;
718 $item ||= $self->item;
719 return $self->visible_section && ($self->visible_section->id == $item->section->id);
722 sub invalidate_version {
725 my $html = $self->render('requirement_spec/_version', { output => 0 },
726 requirement_spec => SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id} || $self->item->requirement_spec_id)->load);
727 return $self->js->html('#requirement_spec_version', $html);