Pflichtenheftversionen anlegen und auflisten
[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('check_auth');
29 __PACKAGE__->run_before('load_requirement_spec_item', only => [ qw(dragged_and_dropped ajax_update ajax_edit ajax_delete ajax_flag ajax_copy) ]);
30 __PACKAGE__->run_before('init_visible_section');
31
32 #
33 # actions
34 #
35
36 sub action_ajax_list {
37   my ($self, $js) = @_;
38
39   my $js = SL::ClientJS->new;
40
41   if (!$::form->{clicked_id}) {
42     # Clicked on "sections" in the tree. Do nothing.
43     return $self->render($js);
44   }
45
46   my $clicked_item = SL::DB::RequirementSpecItem->new(id => $::form->{clicked_id})->load;
47   $self->item($clicked_item->section);
48
49   if (!$self->visible_section || ($self->visible_section->id != $self->item->id)) {
50     $self->render_list($js, $self->item, $clicked_item);
51   } else {
52     $self->select_node($js, $clicked_item);
53   }
54
55   $self->render($js);
56 }
57
58 sub insert_new_item_in_section_view {
59   my ($self, $js) = @_;
60
61   $js->hide('#section-list-empty');
62
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;
68
69   if ($next_item) {
70     $js->insertBefore($html, '#' . $id_prefix . 'function-block-' . $next_item->id);
71   } else {
72     my $parent_is_section = $self->item->parent->item_type eq 'section';
73     $js->appendTo($html, $parent_is_section ? '#section-list' : '#sub-function-block-container-' . $self->item->parent_id);
74     $js->show('#sub-function-block-container-' . $self->item->parent_id) if !$parent_is_section;
75   }
76
77   $self->replace_bottom($js, $self->item->parent) if $new_type eq 'sub-function-block';
78 }
79
80 sub action_dragged_and_dropped {
81   my ($self)              = @_;
82
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;
85
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
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);
93     $self->item->add_to_list(position => $position, reference => $::form->{dropped_id} || undef);
94   });
95
96   my $js = SL::ClientJS->new;
97
98   $self->item(SL::DB::RequirementSpecItem->new(id => $self->item->id)->load);
99   my $new_section         = $self->item->section;
100   my $new_type            = $self->item->item_type;
101   my $new_visible_section = SL::DB::RequirementSpecItem->new(id => $self->visible_item->id)->load->section;
102
103   return $self->render($js) if !$old_visible_section || ($new_type eq 'section');
104
105   # From here on $old_visible_section is definitely set.
106
107   my $old_parent  = SL::DB::RequirementSpecItem->new(id => $old_parent_id)->load;
108   my $old_section = $old_parent->section;
109
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);
112
113   if ($old_visible_section->id != $new_visible_section->id) {
114     # The currently visible item has been dragged to a different section.
115     return $self->render_list($js, $new_section, $self->item)
116       ->render($self);
117   }
118
119   if ($old_visible_section->id == $old_section->id) {
120     my $id_prefix = $old_type eq 'sub-function-block' ? 'sub-' : '';
121     $js->remove('#' . $id_prefix . 'function-block-' . $self->item->id);
122
123     if ($old_type eq 'sub-function-block') {
124       $self->replace_bottom($js, $old_parent) ;
125       $js->hide('#sub-function-block-container-' . $old_parent->id) if 0 == scalar(@{ $old_parent->children });
126
127     } elsif (0 == scalar(@{ $old_section->children })) {
128       $js->show('#section-list-empty');
129     }
130   }
131
132   if ($old_visible_section->id == $new_section->id) {
133     $self->insert_new_item_in_section_view($js);
134   }
135
136   # $::lxdebug->dump(0, "js", $js->to_array);
137
138   $self->render($js);
139 }
140
141 sub action_ajax_add_section {
142   my ($self, %params) = @_;
143
144   die "Missing parameter 'requirement_spec_id'" if !$::form->{requirement_spec_id};
145
146   $self->item(SL::DB::RequirementSpecItem->new(requirement_spec_id => $::form->{requirement_spec_id}, item_type => 'section'));
147
148   my $insert_after = $::form->{id} ? SL::DB::RequirementSpecItem->new(id => $::form->{id})->load->section->id : undef;
149   my $html         = $self->render('requirement_spec_item/_section_form', { output => 0 }, id_base => 'new_section', insert_after => $insert_after);
150
151   SL::ClientJS->new
152     ->remove('#new_section_form')
153     ->hide('#column-content > *')
154     ->appendTo($html, '#column-content')
155     ->focus('#new_section_title')
156     ->render($self);
157 }
158
159 sub action_ajax_add_function_block {
160   my ($self, %params) = @_;
161
162   return $self->add_function_block('function-block');
163 }
164
165 sub action_ajax_add_sub_function_block {
166   my ($self, %params) = @_;
167
168   return $self->add_function_block('sub-function-block');
169 }
170
171 sub action_ajax_create {
172   my ($self, %params) = @_;
173
174   my $js              = SL::ClientJS->new;
175   my $prefix          = $::form->{form_prefix} || die "Missing parameter 'form_prefix'";
176   my $attributes      = $::form->{$prefix}     || die "Missing parameter group '${prefix}'";
177   my $insert_after    = delete $attributes->{insert_after};
178
179   my @errors = $self->item(SL::DB::RequirementSpecItem->new(%{ $attributes }))->validate;
180   return $js->error(@errors)->render($self) if @errors;
181
182   $self->item->save;
183   $self->item->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
184
185   my $type = $self->item->item_type;
186
187   if ($type eq 'section') {
188     my $node = $self->presenter->requirement_spec_item_jstree_data($self->item);
189     return $self->render_list($js, $self->item)
190       ->jstree->create_node('#tree', $insert_after ? ('#fb-' . $insert_after, 'after') : ('#sections', 'last'), $node)
191       ->jstree->select_node('#tree', '#fb-' . $self->item->id)
192       ->render($self);
193   }
194
195   my $template = 'requirement_spec_item/_' . (apply { s/-/_/g; $_ } $type);
196   my $html     = $self->render($template, { output => 0 }, requirement_spec_item => $self->item, id_prefix => $type eq 'function-block' ? '' : 'sub-');
197   my $node     = $self->presenter->requirement_spec_item_jstree_data($self->item);
198
199   $js->replaceWith('#' . $prefix . '_form', $html)
200      ->hide('#section-list-empty')
201      ->jstree->create_node('#tree', $insert_after ? ('#fb-' . $insert_after, 'after') : ('#fb-' . $self->item->parent_id, 'last'), $node)
202      ->jstree->select_node('#tree', '#fb-' . $self->item->id);
203
204   $self->replace_bottom($js, $self->item->parent) if $type eq 'sub-function-block';
205
206   $js->render($self);
207 }
208
209 sub action_ajax_edit {
210   my ($self, %params) = @_;
211
212   $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load);
213
214   my $js = SL::ClientJS->new;
215
216   if (!$self->is_item_visible) {
217     # Show section/item to edit if it is not visible.
218
219     my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item);
220     $js->html('#column-content', $html);
221   }
222
223   if ($self->item->item_type =~ m/section/) {
224     # Edit the section header, not an item.
225     my $html = $self->render('requirement_spec_item/_section_form', { output => 0 });
226
227     $js->hide('#section-header-' . $self->item->id)
228        ->remove("#edit_section_form")
229        ->insertAfter($html, '#section-header-' . $self->item->id)
230        ->jstree->select_node('#tree', '#fb-' . $self->item->id)
231        ->focus("#edit_section_title")
232        ->val('#current_content_type', 'section')
233        ->val('#current_content_id',   $self->item->id)
234        ->render($self);
235     return;
236   }
237
238   # Edit a function block or a sub function block
239   my @dependencies          = $self->create_dependencies;
240   my @selected_dependencies = map { $_->id } @{ $self->item->dependencies };
241
242   my $html                  = $self->render('requirement_spec_item/_function_block_form', { output => 0 }, DEPENDENCIES => \@dependencies, SELECTED_DEPENDENCIES => \@selected_dependencies);
243   my $id_base               = 'edit_function_block_' . $self->item->id;
244   my $content_top_id        = '#' . $self->item->item_type . '-content-top-' . $self->item->id;
245
246   $js->hide($content_top_id)
247      ->remove("#${id_base}_form")
248      ->insertAfter($html, $content_top_id)
249      ->jstree->select_node('#tree', '#fb-' . $self->item->id)
250      ->focus("#${id_base}_description")
251      ->val('#current_content_type', $self->item->item_type)
252      ->val('#current_content_id', $self->item->id)
253      ->render($self);
254 }
255
256 sub action_ajax_update {
257   my ($self, %params) = @_;
258
259   my $js         = SL::ClientJS->new;
260   my $prefix     = $::form->{form_prefix} || die "Missing parameter 'form_prefix'";
261   my $attributes = $::form->{$prefix}     || {};
262
263   foreach (qw(requirement_spec_id parent_id position)) {
264     delete $attributes->{$_} if !defined $attributes->{$_};
265   }
266
267   my @errors = $self->item->assign_attributes(%{ $attributes })->validate;
268   return $js->error(@errors)->render($self) if @errors;
269
270   $self->item->save;
271
272   my $type = $self->item->item_type;
273
274   if ($type eq 'section') {
275     # Updated section, now update section header.
276
277     my $html = $self->render('requirement_spec_item/_section_header', { output => 0 }, requirement_spec_item => $self->item);
278
279     return SL::ClientJS->new
280       ->remove('#edit_section_form')
281       ->html('#section-header-' . $self->item->id, $html)
282       ->show('#section-header-' . $self->item->id)
283       ->jstree->rename_node('#tree', '#fb-' . $self->item->id, $::request->presenter->requirement_spec_item_tree_node_title($self->item))
284       ->render($self);
285   }
286
287   # Updated function block or sub function block. Update (sub)
288   # function block and potentially the bottom of the parent function
289   # block.
290
291   my $id_prefix    = $type eq 'function-block' ? '' : 'sub-';
292   my $html_top     = $self->render('requirement_spec_item/_function_block_content_top',    { output => 0 }, requirement_spec_item => $self->item, id_prefix => $id_prefix);
293   $id_prefix      .= 'function-block-content-';
294
295   my $js = SL::ClientJS->new
296     ->remove('#' . $prefix . '_form')
297     ->replaceWith('#' . $id_prefix . 'top-' . $self->item->id, $html_top)
298     ->jstree->rename_node('#tree', '#fb-' . $self->item->id, $::request->presenter->requirement_spec_item_tree_node_title($self->item));
299
300   $self->replace_bottom($js, $self->item, id_prefix => $id_prefix);
301   $self->replace_bottom($js, $self->item->parent) if $type eq 'sub-function-block';
302
303   $js->render($self);
304 }
305
306 sub action_ajax_delete {
307   my ($self) = @_;
308
309   my $js        = SL::ClientJS->new;
310   my $full_list = $self->item->get_full_list;
311
312   $self->item->delete;
313
314   if ($self->visible_section && ($self->visible_section->id == $self->item->id)) {
315     # Currently visible section is deleted.
316
317     my $new_section = first { $_->id != $self->item->id } @{ $self->item->requirement_spec->sections };
318     if ($new_section) {
319       $self->render_list($js, $new_section);
320
321     } else {
322       my $html = $self->render('requirement_spec_item/_no_section', { output => 0 });
323       $js->html('#column-content', $html)
324          ->val('#current_content_type', '')
325          ->val('#current_content_id', '')
326     }
327
328   } elsif ($self->is_item_visible) {
329     # Item in currently visible section is deleted.
330
331     my $type = $self->item->item_type;
332     $js->remove('#edit_function_block_' . $self->item->id . '_form')
333        ->remove('#' . $type . '-' . $self->item->id);
334
335     $self->replace_bottom($js, $self->item->parent_id) if $type eq 'sub-function-block';
336
337     if (1 == scalar @{ $full_list }) {
338       if ($type eq 'function-block') {
339         $js->show('#section-list-empty');
340       } elsif ($type eq 'sub-function-block') {
341         $js->hide('#sub-function-block-container-' . $self->item->parent_id);
342       }
343     }
344   }
345
346   $js->jstree->delete_node('#tree', '#fb-' . $self->item->id)
347      ->render($self);
348 }
349
350 sub action_ajax_flag {
351   my ($self) = @_;
352
353   $self->item->update_attributes(is_flagged => !$self->item->is_flagged);
354
355   SL::ClientJS->new
356    ->action_if($self->is_item_visible, 'toggleClass', '#' . $self->item->item_type . '-' . $self->item->id, 'flagged')
357    ->toggleClass('#fb-' . $self->item->id, 'flagged')
358    ->render($self);
359 }
360
361 sub action_ajax_copy {
362   my ($self, %params) = @_;
363
364   SL::Clipboard->new->copy($self->item);
365   SL::ClientJS->new->render($self);
366 }
367
368 sub determine_paste_position {
369   my ($self) = @_;
370
371   if ($self->item->item_type eq 'section') {
372     # Sections are always pasted either directly after the
373     # clicked-upon section or at the very end.
374     return $self->clicked_item ? (undef, $self->clicked_item->section->id) : ();
375
376   } elsif ($self->item->item_type eq 'function-block') {
377     # A function block:
378     # - paste on section list: insert into last section as last element
379     # - paste on section: insert into that section as last element
380     # - paste on function block: insert after clicked-upon element
381     # - paste on sub function block: insert after parent function block of clicked-upon element
382     return !$self->clicked_item                                ? ( $self->sections->[-1]->id,              undef                          )
383          :  $self->clicked_item->item_type eq 'section'        ? ( $self->clicked_item->id,                undef                          )
384          :  $self->clicked_item->item_type eq 'function-block' ? ( $self->clicked_item->parent_id,         $self->clicked_item->id        )
385          :                                                       ( $self->clicked_item->parent->parent_id, $self->clicked_item->parent_id );
386
387   } else {                      # sub-function-block
388     # A sub function block:
389     # - paste on section list: promote to function block and insert into last section as last element
390     # - paste on section: promote to function block and insert into that section as last element
391     # - paste on function block: insert as last element in clicked-upon element
392     # - paste on sub function block: insert after clicked-upon element
393
394     # Promote sub function blocks to function blocks when pasting on a
395     # section or the section list.
396     $self->item->item_type('function-block') if !$self->clicked_item || ($self->clicked_item->item_type eq 'section');
397
398     return !$self->clicked_item                                ? ( $self->sections->[-1]->id,      undef                   )
399          :  $self->clicked_item->item_type eq 'section'        ? ( $self->clicked_item->id,        undef                   )
400          :  $self->clicked_item->item_type eq 'function-block' ? ( $self->clicked_item->id,        undef                   )
401          :                                                       ( $self->clicked_item->parent_id, $self->clicked_item->id );
402   }
403 }
404
405 sub assign_requirement_spec_id_rec {
406   my ($self, $item) = @_;
407
408   $item->requirement_spec_id($::form->{requirement_spec_id});
409   $self->assign_requirement_spec_id_rec($_) for @{ $item->children || [] };
410
411   return $item;
412 }
413
414 sub create_and_insert_node_rec {
415   my ($self, $js, $item, $new_parent_id, $insert_after) = @_;
416
417   my $node = $self->presenter->requirement_spec_item_jstree_data($item);
418   $js->jstree->create_node('#tree', $insert_after ? ('#fb-' . $insert_after, 'after') : $new_parent_id ? ('#fb-' . $new_parent_id, 'last') : ('#sections', 'last'), $node);
419
420   $self->create_and_insert_node_rec($js, $_, $item->id) for @{ $item->children || [] };
421
422   $js->jstree->open_node('#tree', '#fb-' . $item->id);
423 }
424
425 sub action_ajax_paste {
426   my ($self, %params) = @_;
427
428   my $js     = SL::ClientJS->new;
429   my $copied = SL::Clipboard->new->get_entry(qr/^RequirementSpecItem$/);
430
431   if (!$copied) {
432     return $js->error(t8("The clipboard does not contain anything that can be pasted here."))
433               ->render($self);
434   }
435
436   $self->item($self->assign_requirement_spec_id_rec($copied->to_object));
437   my $req_spec = SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load;
438   $self->sections($req_spec->sections);
439
440   if (($self->item->item_type ne 'section') && !@{ $self->sections }) {
441     return $js->error(t8("You cannot paste function blocks or sub function blocks if there is no section."))
442               ->render($self);
443   }
444
445   $self->clicked_item($::form->{id} ? SL::DB::RequirementSpecItem->new(id => $::form->{id})->load : undef);
446
447   my ($new_parent_id, $insert_after) = $self->determine_paste_position;
448
449   # Store result in database.
450   $self->item->update_attributes(requirement_spec_id => $::form->{requirement_spec_id}, parent_id => $new_parent_id);
451   $self->item->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
452
453   # Update the tree: create the node for all pasted objects.
454   $self->create_and_insert_node_rec($js, $self->item, $new_parent_id, $insert_after);
455
456   # Pasting the very first section?
457   if (!@{ $self->sections }) {
458     my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item);
459     $js->html('#column-content', $html)
460        ->jstree->select_node('#tree', '#fb-' . $self->item->id)
461   }
462
463   # Update the current view if required.
464   $self->insert_new_item_in_section_view($js) if $self->is_item_visible;
465
466   $js->render($self);
467 }
468
469 #
470 # filters
471 #
472
473 sub check_auth {
474   my ($self) = @_;
475   $::auth->assert('sales_quotation_edit');
476 }
477
478 sub load_requirement_spec {
479   my ($self) = @_;
480   $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load || die "No such requirement spec");
481 }
482
483 sub load_requirement_spec_item {
484   my ($self) = @_;
485   $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load || die "No such requirement spec item");
486 }
487
488 #
489 # helpers
490 #
491
492 sub create_random_id {
493   return join '-', Time::HiRes::gettimeofday();
494 }
495
496 sub format_exception {
497   return join "\n", (split m/\n/, $@)[0..4];
498 }
499
500 sub init_complexities {
501   my ($self) = @_;
502
503   return SL::DB::Manager::RequirementSpecComplexity->get_all_sorted;
504 }
505
506 sub init_risks {
507   my ($self) = @_;
508
509   return SL::DB::Manager::RequirementSpecRisk->get_all_sorted;
510 }
511
512 sub replace_bottom {
513   my ($self, $js, $item_or_id) = @_;
514
515   my $item      = (ref($item_or_id) ? $item_or_id : SL::DB::RequirementSpecItem->new(id => $item_or_id))->load;
516   my $id_prefix = $item->item_type eq 'function-block' ? '' : 'sub-';
517   my $html      = $self->render('requirement_spec_item/_function_block_content_bottom', { output => 0 }, requirement_spec_item => $item, id_prefix => $id_prefix);
518   return $js->replaceWith('#' . $id_prefix . 'function-block-content-bottom-' . $item->id, $html);
519 }
520
521 sub render_list {
522   my ($self, $js, $item, $item_to_select) = @_;
523
524   my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $item);
525   $self->select_node($js->html('#column-content', $html), $item_to_select || $item);
526 }
527
528 sub select_node {
529   my ($self, $js, $item) = @_;
530
531   $js->val( '#current_content_type', $item->item_type)
532      ->val( '#current_content_id',   $item->id)
533      ->jstree->select_node('#tree', '#fb-' . $item->id);
534 }
535
536 sub create_dependency_item {
537   my $self = shift;
538   [ $_[0]->id, $self->presenter->truncate(join(' ', grep { $_ } ($_[1], $_[0]->fb_number, $_[0]->description))) ];
539 }
540
541 sub create_dependencies {
542   my ($self) = @_;
543
544   return map { [ $_->fb_number . ' ' . $_->title,
545                  [ map { ( $self->create_dependency_item($_),
546                            map { $self->create_dependency_item($_, '->') } @{ $_->sorted_children })
547                        } @{ $_->sorted_children } ] ]
548              } @{ $self->item->requirement_spec->sections };
549 }
550
551 sub add_function_block {
552   my ($self, $new_type) = @_;
553
554   my $clicked_id = $::form->{id} || ($self->visible_item ? $self->visible_item->id : undef);
555
556   die "Invalid new_type '$new_type'"               if $new_type !~ m/^(?:sub-)?function-block$/;
557   die "Missing parameter 'id' and no visible item" if !$clicked_id;
558   die "Missing parameter 'requirement_spec_id'"    if !$::form->{requirement_spec_id};
559
560   my $clicked_item = SL::DB::RequirementSpecItem->new(id => $clicked_id)->load;
561   my $clicked_type = $clicked_item->item_type;
562
563   die "Invalid clicked_type '$clicked_type'" if $clicked_type !~ m/^(?: section | (?:sub-)? function-block )$/x;
564
565   my $case = "${clicked_type}:${new_type}";
566
567   my ($insert_position, $insert_reference, $parent_id, $display_reference)
568     = $case eq 'section:function-block'                ? ( 'appendTo',    $clicked_item->id,        $clicked_item->id,                '#section-list'                  )
569     : $case eq 'function-block:function-block'         ? ( 'insertAfter', $clicked_item->id,        $clicked_item->parent_id,         '#function-block-'               )
570     : $case eq 'function-block:sub-function-block'     ? ( 'appendTo'  ,  $clicked_item->id,        $clicked_item->id,                '#sub-function-block-container-' )
571     : $case eq 'sub-function-block:function-block'     ? ( 'insertAfter', $clicked_item->parent_id, $clicked_item->parent->parent_id, '#function-block-'               )
572     : $case eq 'sub-function-block:sub-function-block' ? ( 'insertAfter', $clicked_item->id,        $clicked_item->parent_id,         '#sub-function-block-'           )
573     :                                                    die "Invalid combination of 'clicked_type (section)/new_type ($new_type)'";
574
575   $self->item(SL::DB::RequirementSpecItem->new(requirement_spec_id => $::form->{requirement_spec_id}, parent_id => $parent_id, item_type => $new_type));
576
577   $display_reference .= $insert_reference if $display_reference =~ m/-$/;
578   my $id_base         = join('_', 'new_function_block', Time::HiRes::gettimeofday(), int rand 1000000000000);
579   my $html            = $self->render(
580     'requirement_spec_item/_function_block_form',
581     { output => 0 },
582     DEPENDENCIES          => [ $self->create_dependencies ],
583     SELECTED_DEPENDENCIES => [],
584     requirement_spec_item => $self->item,
585     id_base               => $id_base,
586     insert_after          => $insert_position eq 'insertAfter' ? $insert_reference : undef,
587   );
588
589   my $js = SL::ClientJS->new;
590
591   my $new_section = $self->item->section;
592   if (!$self->is_item_visible) {
593     # Show section/item to edit if it is not visible.
594
595     $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $new_section);
596     $js->html('#column-content', $html)
597        ->val('#current_content_type', 'section')
598        ->val('#current_content_id',   $new_section->id)
599        ->jstree->select_node('#tree', '#fb-' . $new_section->id);
600   }
601
602   # $::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");
603
604   $js->action($insert_position, $html, $display_reference)
605      ->focus("#${id_base}_description");
606
607   $js->show('#sub-function-block-container-' . $parent_id) if $new_type eq 'sub-function-block';
608
609   $js->render($self);
610 }
611
612 sub is_item_visible {
613   my ($self, $item) = @_;
614
615   $item ||= $self->item;
616   return $self->visible_section && ($self->visible_section->id == $item->section->id);
617 }
618
619 1;