Pflichtenhefte: Zeit- und Kostenschätzungsmaske
[kivitendo-erp.git] / SL / Controller / RequirementSpecItem.pm
1 package SL::Controller::RequirementSpecItem;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use Carp;
8 use List::MoreUtils qw(apply);
9 use List::Util qw(first);
10 use Time::HiRes ();
11
12 use SL::Clipboard;
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;
19 use SL::JSON;
20 use SL::Locale::String;
21
22 use Rose::Object::MakeMethods::Generic
23 (
24   scalar                  => [ qw(item visible_item visible_section clicked_item sections) ],
25   'scalar --get_set_init' => [ qw(complexities risks) ],
26 );
27
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');
30
31 #
32 # actions
33 #
34
35 sub action_ajax_list {
36   my ($self, $js) = @_;
37
38   my $js = SL::ClientJS->new;
39
40   if (!$::form->{clicked_id}) {
41     # Clicked on "sections" in the tree. Do nothing.
42     return $self->render($js);
43   }
44
45   my $clicked_item = SL::DB::RequirementSpecItem->new(id => $::form->{clicked_id})->load;
46   $self->item($clicked_item->section);
47
48   if (!$self->visible_section || ($self->visible_section->id != $self->item->id)) {
49     $self->render_list($js, $self->item, $clicked_item);
50   } else {
51     $self->select_node($js, $clicked_item);
52   }
53
54   $self->render($js);
55 }
56
57 sub insert_new_item_in_section_view {
58   my ($self, $js) = @_;
59
60   $js->hide('#section-list-empty');
61
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;
67
68   if ($next_item) {
69     $js->insertBefore($html, '#' . $id_prefix . 'function-block-' . $next_item->id);
70   } else {
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;
74   }
75
76   $self->replace_bottom($js, $self->item->parent) if $new_type eq 'sub-function-block';
77 }
78
79 sub action_dragged_and_dropped {
80   my ($self)              = @_;
81
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;
84
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
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);
93   });
94
95   my $js = SL::ClientJS->new;
96
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;
101
102   return $self->render($js) if !$old_visible_section || ($new_type eq 'section');
103
104   # From here on $old_visible_section is definitely set.
105
106   my $old_parent  = SL::DB::RequirementSpecItem->new(id => $old_parent_id)->load;
107   my $old_section = $old_parent->section;
108
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);
111
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)
115       ->render($self);
116   }
117
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);
121
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 });
125
126     } elsif (0 == scalar(@{ $old_section->children })) {
127       $js->show('#section-list-empty');
128     }
129   }
130
131   if ($old_visible_section->id == $new_section->id) {
132     $self->insert_new_item_in_section_view($js);
133   }
134
135   # $::lxdebug->dump(0, "js", $js->to_array);
136
137   $self->render($js);
138 }
139
140 sub action_ajax_add_section {
141   my ($self, %params) = @_;
142
143   die "Missing parameter 'requirement_spec_id'" if !$::form->{requirement_spec_id};
144
145   $self->item(SL::DB::RequirementSpecItem->new(requirement_spec_id => $::form->{requirement_spec_id}, item_type => 'section'));
146
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);
149
150   SL::ClientJS->new
151     ->remove('#new_section_form')
152     ->hide('#column-content > *')
153     ->appendTo($html, '#column-content')
154     ->focus('#new_section_title')
155     ->render($self);
156 }
157
158 sub action_ajax_add_function_block {
159   my ($self, %params) = @_;
160
161   return $self->add_function_block('function-block');
162 }
163
164 sub action_ajax_add_sub_function_block {
165   my ($self, %params) = @_;
166
167   return $self->add_function_block('sub-function-block');
168 }
169
170 sub action_ajax_create {
171   my ($self, %params) = @_;
172
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};
177
178   my @errors = $self->item(SL::DB::RequirementSpecItem->new(%{ $attributes }))->validate;
179   return $js->error(@errors)->render($self) if @errors;
180
181   $self->item->save;
182   $self->item->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
183
184   my $type = $self->item->item_type;
185
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)
191       ->render($self);
192   }
193
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);
197
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);
202
203   $self->replace_bottom($js, $self->item->parent) if $type eq 'sub-function-block';
204
205   $js->render($self);
206 }
207
208 sub action_ajax_edit {
209   my ($self, %params) = @_;
210
211   $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load);
212
213   my $js = SL::ClientJS->new;
214
215   if (!$self->is_item_visible) {
216     # Show section/item to edit if it is not visible.
217
218     my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item);
219     $js->html('#column-content', $html);
220   }
221
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 });
225
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)
233        ->render($self);
234     return;
235   }
236
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 };
240
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;
244
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)
252      ->render($self);
253 }
254
255 sub action_ajax_update {
256   my ($self, %params) = @_;
257
258   my $js         = SL::ClientJS->new;
259   my $prefix     = $::form->{form_prefix} || die "Missing parameter 'form_prefix'";
260   my $attributes = $::form->{$prefix}     || {};
261
262   foreach (qw(requirement_spec_id parent_id position)) {
263     delete $attributes->{$_} if !defined $attributes->{$_};
264   }
265
266   my @errors = $self->item->assign_attributes(%{ $attributes })->validate;
267   return $js->error(@errors)->render($self) if @errors;
268
269   $self->item->save;
270
271   my $type = $self->item->item_type;
272
273   if ($type eq 'section') {
274     # Updated section, now update section header.
275
276     my $html = $self->render('requirement_spec_item/_section_header', { output => 0 }, requirement_spec_item => $self->item);
277
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))
283       ->render($self);
284   }
285
286   # Updated function block or sub function block. Update (sub)
287   # function block and potentially the bottom of the parent function
288   # block.
289
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-';
293
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));
298
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';
301
302   $js->render($self);
303 }
304
305 sub action_ajax_delete {
306   my ($self) = @_;
307
308   my $js        = SL::ClientJS->new;
309   my $full_list = $self->item->get_full_list;
310
311   $self->item->delete;
312
313   if ($self->visible_section && ($self->visible_section->id == $self->item->id)) {
314     # Currently visible section is deleted.
315
316     my $new_section = first { $_->id != $self->item->id } @{ $self->item->requirement_spec->sections };
317     if ($new_section) {
318       $self->render_list($js, $new_section);
319
320     } else {
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', '')
325     }
326
327   } elsif ($self->is_item_visible) {
328     # Item in currently visible section is deleted.
329
330     my $type = $self->item->item_type;
331     $js->remove('#edit_function_block_' . $self->item->id . '_form')
332        ->remove('#' . $type . '-' . $self->item->id);
333
334     $self->replace_bottom($js, $self->item->parent_id) if $type eq 'sub-function-block';
335
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);
341       }
342     }
343   }
344
345   $js->jstree->delete_node('#tree', '#fb-' . $self->item->id)
346      ->render($self);
347 }
348
349 sub action_ajax_flag {
350   my ($self) = @_;
351
352   $self->item->update_attributes(is_flagged => !$self->item->is_flagged);
353
354   SL::ClientJS->new
355    ->action_if($self->is_item_visible, 'toggleClass', '#' . $self->item->item_type . '-' . $self->item->id, 'flagged')
356    ->toggleClass('#fb-' . $self->item->id, 'flagged')
357    ->render($self);
358 }
359
360 sub action_ajax_copy {
361   my ($self, %params) = @_;
362
363   SL::Clipboard->new->copy($self->item);
364   SL::ClientJS->new->render($self);
365 }
366
367 sub determine_paste_position {
368   my ($self) = @_;
369
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) : ();
374
375   } elsif ($self->item->item_type eq 'function-block') {
376     # A 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 );
385
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
392
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');
396
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 );
401   }
402 }
403
404 sub assign_requirement_spec_id_rec {
405   my ($self, $item) = @_;
406
407   $item->requirement_spec_id($::form->{requirement_spec_id});
408   $self->assign_requirement_spec_id_rec($_) for @{ $item->children || [] };
409
410   return $item;
411 }
412
413 sub create_and_insert_node_rec {
414   my ($self, $js, $item, $new_parent_id, $insert_after) = @_;
415
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);
418
419   $self->create_and_insert_node_rec($js, $_, $item->id) for @{ $item->children || [] };
420
421   $js->jstree->open_node('#tree', '#fb-' . $item->id);
422 }
423
424 sub action_ajax_paste {
425   my ($self, %params) = @_;
426
427   my $js     = SL::ClientJS->new;
428   my $copied = SL::Clipboard->new->get_entry(qr/^RequirementSpecItem$/);
429
430   if (!$copied) {
431     return $js->error(t8("The clipboard does not contain anything that can be pasted here."))
432               ->render($self);
433   }
434
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);
438
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."))
441               ->render($self);
442   }
443
444   $self->clicked_item($::form->{id} ? SL::DB::RequirementSpecItem->new(id => $::form->{id})->load : undef);
445
446   my ($new_parent_id, $insert_after) = $self->determine_paste_position;
447
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;
451
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);
454
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)
460   }
461
462   # Update the current view if required.
463   $self->insert_new_item_in_section_view($js) if $self->is_item_visible;
464
465   $js->render($self);
466 }
467
468 #
469 # filters
470 #
471
472 sub load_requirement_spec {
473   my ($self) = @_;
474   $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load || die "No such requirement spec");
475 }
476
477 sub load_requirement_spec_item {
478   my ($self) = @_;
479   $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load || die "No such requirement spec item");
480 }
481
482 #
483 # helpers
484 #
485
486 sub create_random_id {
487   return join '-', Time::HiRes::gettimeofday();
488 }
489
490 sub format_exception {
491   return join "\n", (split m/\n/, $@)[0..4];
492 }
493
494 sub init_complexities {
495   my ($self) = @_;
496
497   return SL::DB::Manager::RequirementSpecComplexity->get_all_sorted;
498 }
499
500 sub init_risks {
501   my ($self) = @_;
502
503   return SL::DB::Manager::RequirementSpecRisk->get_all_sorted;
504 }
505
506 sub replace_bottom {
507   my ($self, $js, $item_or_id) = @_;
508
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);
513 }
514
515 sub render_list {
516   my ($self, $js, $item, $item_to_select) = @_;
517
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);
520 }
521
522 sub select_node {
523   my ($self, $js, $item) = @_;
524
525   $js->val( '#current_content_type', $item->item_type)
526      ->val( '#current_content_id',   $item->id)
527      ->jstree->select_node('#tree', '#fb-' . $item->id);
528 }
529
530 sub create_dependency_item {
531   my $self = shift;
532   [ $_[0]->id, $self->presenter->truncate(join(' ', grep { $_ } ($_[1], $_[0]->fb_number, $_[0]->description))) ];
533 }
534
535 sub create_dependencies {
536   my ($self) = @_;
537
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 };
543 }
544
545 sub add_function_block {
546   my ($self, $new_type) = @_;
547
548   my $clicked_id = $::form->{id} || ($self->visible_item ? $self->visible_item->id : undef);
549
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};
553
554   my $clicked_item = SL::DB::RequirementSpecItem->new(id => $clicked_id)->load;
555   my $clicked_type = $clicked_item->item_type;
556
557   die "Invalid clicked_type '$clicked_type'" if $clicked_type !~ m/^(?: section | (?:sub-)? function-block )$/x;
558
559   my $case = "${clicked_type}:${new_type}";
560
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)'";
568
569   $self->item(SL::DB::RequirementSpecItem->new(requirement_spec_id => $::form->{requirement_spec_id}, parent_id => $parent_id, item_type => $new_type));
570
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',
575     { output => 0 },
576     DEPENDENCIES          => [ $self->create_dependencies ],
577     SELECTED_DEPENDENCIES => [],
578     requirement_spec_item => $self->item,
579     id_base               => $id_base,
580     insert_after          => $insert_position eq 'insertAfter' ? $insert_reference : undef,
581   );
582
583   my $js = SL::ClientJS->new;
584
585   my $new_section = $self->item->section;
586   if (!$self->is_item_visible) {
587     # Show section/item to edit if it is not visible.
588
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);
594   }
595
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");
597
598   $js->action($insert_position, $html, $display_reference)
599      ->focus("#${id_base}_description");
600
601   $js->show('#sub-function-block-container-' . $parent_id) if $new_type eq 'sub-function-block';
602
603   $js->render($self);
604 }
605
606 sub is_item_visible {
607   my ($self, $item) = @_;
608
609   $item ||= $self->item;
610   return $self->visible_section && ($self->visible_section->id == $item->section->id);
611 }
612
613 1;