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