1 package SL::Controller::RequirementSpecItem;
5 use parent qw(SL::Controller::Base);
8 use List::MoreUtils qw(apply);
9 use List::Util qw(first);
12 use SL::DB::RequirementSpec;
13 use SL::DB::RequirementSpecComplexity;
14 use SL::DB::RequirementSpecItem;
15 use SL::DB::RequirementSpecRisk;
16 use SL::Helper::Flash;
18 use SL::Locale::String;
20 use Rose::Object::MakeMethods::Generic
22 scalar => [ qw(item visible_item visible_section) ],
23 'scalar --get_set_init' => [ qw(complexities risks) ],
26 __PACKAGE__->run_before('load_requirement_spec_item', only => [ qw(dragged_and_dropped ajax_update ajax_edit ajax_delete) ]);
27 __PACKAGE__->run_before('init_visible_section');
33 sub action_ajax_list {
36 my $js = SL::ClientJS->new;
38 if (!$::form->{clicked_id}) {
39 # Clicked on "sections" in the tree. Do nothing.
40 return $self->render($js);
43 $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{clicked_id})->load->get_section);
45 $self->render_list($js, $self->item) if !$self->visible_section || ($self->visible_section->id != $self->item->id);
50 sub action_dragged_and_dropped {
53 my $position = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position} : die "Unknown 'position' parameter";
54 my $dropped_item = $::form->{dropped_id} ? SL::DB::RequirementSpecItem->new(id => $::form->{dropped_id})->load : undef;
56 my $visible_section_id = $self->visible_section ? $self->visible_section->id : undef;
57 my $old_parent_id = $self->item->parent_id;
58 my $old_type = $self->item->get_type;
60 $self->item->db->do_transaction(sub {
61 $self->item->remove_from_list;
62 $self->item->parent_id($position =~ m/before|after/ ? $dropped_item->parent_id : $dropped_item->id);
63 $self->item->add_to_list(position => $position, reference => $::form->{dropped_id} || undef);
66 my $js = SL::ClientJS->new;
68 $self->item(SL::DB::RequirementSpecItem->new(id => $self->item->id)->load);
69 my $new_section = $self->item->get_section;
70 my $new_type = $self->item->get_type;
72 return $self->render($js) if !$visible_section_id || ($new_type eq 'section');
74 my $old_parent = SL::DB::RequirementSpecItem->new(id => $old_parent_id)->load;
75 my $old_section = $old_parent->get_section;
77 # $::lxdebug->message(0, "old sec ID " . $old_section->id . " new " . $new_section->id . " visible $visible_section_id PARENT: old " . $old_parent->id . " new " . $self->item->parent_id . '/' . $self->item->parent->id);
79 if ($visible_section_id == $old_section->id) {
80 my $id_prefix = $old_type eq 'sub-function-block' ? 'sub-' : '';
81 $js->remove('#' . $id_prefix . 'function-block-' . $self->item->id);
83 if ($old_type eq 'sub-function-block') {
84 $self->replace_bottom($js, $old_parent) ;
85 $js->hide('#sub-function-block-container-' . $old_parent->id) if 0 == scalar(@{ $old_parent->children });
87 } elsif (0 == scalar(@{ $old_section->children })) {
88 $js->show('#section-list-empty');
92 if ($visible_section_id == $new_section->id) {
93 $js->hide('#section-list-empty');
95 my $id_prefix = $new_type eq 'sub-function-block' ? 'sub-' : '';
96 my $template = 'requirement_spec_item/_' . (apply { s/-/_/g; $_ } $new_type);
97 my $html = "" . $self->render($template, { output => 0 }, requirement_spec_item => $self->item);
98 my $next_item = $self->item->get_next_in_list;
101 $js->insertBefore($html, '#' . $id_prefix . 'function-block-' . $next_item->id);
103 my $parent_is_section = $self->item->parent->get_type eq 'section';
104 $js->appendTo($html, $parent_is_section ? '#section-list' : '#sub-function-block-container-' . $self->item->parent_id);
105 $js->show('#sub-function-block-container-' . $self->item->parent_id) if !$parent_is_section;
108 $self->replace_bottom($js, $self->item->parent) if $new_type eq 'sub-function-block';
111 # $::lxdebug->dump(0, "js", $js->to_array);
116 sub action_ajax_add_section {
117 my ($self, %params) = @_;
119 die "Missing parameter 'requirement_spec_id'" if !$::form->{requirement_spec_id};
121 $self->item(SL::DB::RequirementSpecItem->new(requirement_spec_id => $::form->{requirement_spec_id}));
123 my $insert_after = $::form->{id} ? SL::DB::RequirementSpecItem->new(id => $::form->{id})->load->get_section->id : undef;
124 my $html = $self->render('requirement_spec_item/_section_form', { output => 0 }, id_base => 'new_section', insert_after => $insert_after);
127 ->remove('#new_section_form')
128 ->hide('#column-content > *')
129 ->appendTo($html, '#column-content')
130 ->focus('#new_section_title')
134 sub action_ajax_add_function_block {
135 my ($self, %params) = @_;
137 return $self->add_function_block('function-block');
140 sub action_ajax_add_sub_function_block {
141 my ($self, %params) = @_;
143 return $self->add_function_block('sub-function-block');
146 sub action_ajax_create {
147 my ($self, %params) = @_;
149 my $js = SL::ClientJS->new;
150 my $prefix = $::form->{form_prefix} || die "Missing parameter 'form_prefix'";
151 my $attributes = $::form->{$prefix} || die "Missing parameter group '${prefix}'";
152 my $insert_after = delete $attributes->{insert_after};
154 my @errors = $self->item(SL::DB::RequirementSpecItem->new(%{ $attributes }))->validate;
155 return $js->error(@errors)->render($self) if @errors;
158 $self->item->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
160 my $type = $self->item->get_type;
162 if ($type eq 'section') {
163 my $node = $self->presenter->requirement_spec_item_jstree_data($self->item);
164 return $self->render_list($js, $self->item)
165 ->jstree->create_node('#tree', $insert_after ? ('#fb-' . $insert_after, 'after') : ('#sections', 'last'), $node)
166 ->jstree->select_node('#tree', '#fb-' . $self->item->id)
170 die 'TODO: create item';
173 sub action_ajax_edit {
174 my ($self, %params) = @_;
176 $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load);
178 my $js = SL::ClientJS->new;
180 if (!$self->visible_section || ($self->visible_section->id != $self->item->get_section->id)) {
181 # Show section/item to edit if it is not visible.
183 my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item);
184 $js->html('#column-content', $html);
187 if ($self->item->get_type =~ m/section/) {
188 # Edit the section header, not an item.
189 my $html = $self->render('requirement_spec_item/_section_form', { output => 0 });
191 $js->hide('#section-header-' . $self->item->id)
192 ->remove("#edit_section_form")
193 ->insertAfter($html, '#section-header-' . $self->item->id)
194 ->jstree->select_node('#tree', '#fb-' . $self->item->id)
195 ->focus("#edit_section_title")
196 ->val('#current_content_type', 'section')
197 ->val('#current_content_id', $self->item->id)
202 # Edit a function block or a sub function block
203 my @dependencies = $self->create_dependencies;
204 my @selected_dependencies = map { $_->id } @{ $self->item->dependencies };
206 my $html = $self->render('requirement_spec_item/_function_block_form', { output => 0 }, DEPENDENCIES => \@dependencies, SELECTED_DEPENDENCIES => \@selected_dependencies);
207 my $id_base = 'edit_function_block_' . $self->item->id;
208 my $content_top_id = '#' . $self->item->get_type . '-content-top-' . $self->item->id;
210 $js->hide($content_top_id)
211 ->remove("#${id_base}_form")
212 ->insertAfter($html, $content_top_id)
213 ->jstree->select_node('#tree', '#fb-' . $self->item->id)
214 ->focus("#${id_base}_description")
215 ->val('#current_content_type', $self->item->get_type)
216 ->val('#current_content_id', $self->item->id)
220 sub action_ajax_update {
221 my ($self, %params) = @_;
223 my $js = SL::ClientJS->new;
224 my $prefix = $::form->{form_prefix} || die "Missing parameter 'form_prefix'";
225 my $attributes = $::form->{$prefix} || {};
227 foreach (qw(requirement_spec_id parent_id position)) {
228 delete $attributes->{$_} if !defined $attributes->{$_};
231 my @errors = $self->item->assign_attributes(%{ $attributes })->validate;
232 return $js->error(@errors)->render($self) if @errors;
236 my $type = $self->item->get_type;
238 if ($type eq 'section') {
239 # Updated section, now update section header.
241 my $html = $self->render('requirement_spec_item/_section_header', { output => 0 }, requirement_spec_item => $self->item);
243 return SL::ClientJS->new
244 ->remove('#edit_section_form')
245 ->html('#section-header-' . $self->item->id, $html)
246 ->show('#section-header-' . $self->item->id)
247 ->jstree->rename_node('#tree', '#fb-' . $self->item->id, $::request->presenter->requirement_spec_item_tree_node_title($self->item))
251 # Updated function block or sub function block. Update (sub)
252 # function block and potentially the bottom of the parent function
255 my $id_prefix = $type eq 'function-block' ? '' : 'sub-';
256 my $html_top = $self->render('requirement_spec_item/_function_block_content_top', { output => 0 }, requirement_spec_item => $self->item, id_prefix => $id_prefix);
257 $id_prefix .= 'function-block-content-';
259 my $js = SL::ClientJS->new
260 ->remove('#' . $prefix . '_form')
261 ->replaceWith('#' . $id_prefix . 'top-' . $self->item->id, $html_top)
262 ->jstree->rename_node('#tree', '#fb-' . $self->item->id, $::request->presenter->requirement_spec_item_tree_node_title($self->item));
264 $self->replace_bottom($js, $self->item, id_prefix => $id_prefix);
265 $self->replace_bottom($js, $self->item->parent) if $type eq 'sub-function-block';
270 sub action_ajax_delete {
273 my $js = SL::ClientJS->new;
277 if ($self->visible_section && ($self->visible_section->id == $self->item->id)) {
278 # Currently visible section is deleted.
280 my $new_section = first { $_->id != $self->item->id } @{ $self->item->requirement_spec->sections };
282 $self->render_list($js, $new_section);
285 my $html = $self->render('requirement_spec_item/_no_section', { output => 0 });
286 $js->html('#column-content', $html)
287 ->val('#current_content_type', '')
288 ->val('#current_content_id', '')
291 } elsif ($self->visible_section && ($self->visible_section->id == $self->item->get_section->id)) {
292 # Item in currently visible section is deleted.
294 my $type = $self->item->get_type;
295 $js->remove('#edit_function_block_' . $self->item->id . '_form')
296 ->remove('#' . $type . '-' . $self->item->id);
298 $self->replace_bottom($js, $self->item->parent_id) if $type eq 'sub-function-block';
300 if (1 == scalar @{ $self->item->get_full_list }) {
301 if ($type eq 'function-block') {
302 $js->show('#section-list-empty');
303 } elsif ($type eq 'sub-function-block') {
304 $js->hide('#sub-function-block-container-' . $self->item->parent_id);
309 $js->jstree->delete_node('#tree', '#fb-' . $self->item->id)
317 sub load_requirement_spec {
319 $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load || die "No such requirement spec");
322 sub load_requirement_spec_item {
324 $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load || die "No such requirement spec item");
331 sub create_random_id {
332 return join '-', Time::HiRes::gettimeofday();
335 sub format_exception {
336 return join "\n", (split m/\n/, $@)[0..4];
339 sub init_visible_section {
342 my $content_id = $::form->{current_content_id};
343 my $content_type = $::form->{current_content_type};
345 return undef unless $content_id;
346 return undef unless $content_type =~ m/section|function-block/;
348 $self->visible_item(SL::DB::Manager::RequirementSpecItem->find_by(id => $content_id));
349 return undef unless $self->visible_item;
351 return $self->visible_section($self->visible_item->get_section);
354 sub init_complexities {
357 return SL::DB::Manager::RequirementSpecComplexity->get_all_sorted;
363 return SL::DB::Manager::RequirementSpecRisk->get_all_sorted;
367 my ($self, $js, $item_or_id) = @_;
369 my $item = (ref($item_or_id) ? $item_or_id : SL::DB::RequirementSpecItem->new(id => $item_or_id))->load;
370 my $id_prefix = $item->get_type eq 'function-block' ? '' : 'sub-';
371 my $html = $self->render('requirement_spec_item/_function_block_content_bottom', { output => 0 }, requirement_spec_item => $item, id_prefix => $id_prefix);
372 return $js->replaceWith('#' . $id_prefix . 'function-block-content-bottom-' . $item->id, $html);
376 my ($self, $js, $item) = @_;
378 my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $item);
379 $js->html('#column-content', $html)
380 ->val( '#current_content_type', $item->get_type)
381 ->val( '#current_content_id', $item->id)
382 ->jstree->select_node('#tree', '#fb-' . $item->id);
385 sub create_dependency_item {
387 [ $_[0]->id, $self->presenter->truncate(join(' ', grep { $_ } ($_[1], $_[0]->fb_number, $_[0]->description))) ];
390 sub create_dependencies {
393 return map { [ $_->fb_number . ' ' . $_->title,
394 [ map { ( $self->create_dependency_item($_),
395 map { $self->create_dependency_item($_, '->') } @{ $_->sorted_children })
396 } @{ $_->sorted_children } ] ]
397 } @{ $self->item->requirement_spec->sections };
400 sub add_function_block {
401 my ($self, $new_type) = @_;
403 die "Invalid new_type '$new_type'" if $new_type !~ m/^(?:sub-)?function-block$/;
404 die "Missing parameter 'id'" if !$::form->{id};
405 die "Missing parameter 'requirement_spec_id'" if !$::form->{requirement_spec_id};
407 my $clicked_item = SL::DB::RequirementSpecItem->new(id => $::form->{id})->load;
408 my $clicked_type = $clicked_item->get_type;
410 die "Invalid clicked_type '$clicked_type'" if $clicked_type !~ m/^(?: section | (?:sub-)? function-block )$/x;
412 my $case = "${clicked_type}:${new_type}";
414 my ($insert_position, $insert_reference, $parent_id, $display_reference)
415 = $case eq 'section:function-block' ? ( 'appendTo', $clicked_item->id, $clicked_item->id, '#section-list' )
416 : $case eq 'function-block:function-block' ? ( 'insertAfter', $clicked_item->id, $clicked_item->parent_id, '#function-block-' )
417 : $case eq 'function-block:sub-function-block' ? ( 'appendTo' , $clicked_item->id, $clicked_item->id, '#sub-function-block-container-' )
418 : $case eq 'sub-function-block:function-block' ? ( 'insertAfter', $clicked_item->parent_id, $clicked_item->parent->parent_id, '#function-block-' )
419 : $case eq 'sub-function-block:sub-function-block' ? ( 'insertAfter', $clicked_item->id, $clicked_item->parent_id, '#sub-function-block-' )
420 : die "Invalid combination of 'clicked_type (section)/new_type ($new_type)'";
422 $self->item(SL::DB::RequirementSpecItem->new(requirement_spec_id => $::form->{requirement_spec_id}, parent_id => $parent_id));
424 $display_reference .= $insert_reference if $display_reference =~ m/-$/;
425 my $id_base = join('_', 'new_function_block', Time::HiRes::gettimeofday(), int rand 1000000000000);
426 my $html = $self->render(
427 'requirement_spec_item/_function_block_form',
429 DEPENDENCIES => [ $self->create_dependencies ],
430 SELECTED_DEPENDENCIES => [],
431 requirement_spec_item => $self->item,
433 insert_after => $insert_reference,
436 my $js = SL::ClientJS->new;
438 my $new_section = $self->item->get_section;
439 if (!$self->visible_section || ($self->visible_section->id != $new_section->id)) {
440 # Show section/item to edit if it is not visible.
442 $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $new_section);
443 $js->html('#column-content', $html)
444 ->val('#current_content_type', 'section')
445 ->val('#current_content_id', $new_section->id)
446 ->jstree->select_node('#tree', '#fb-' . $new_section->id);
449 # $::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");
451 $js->action($insert_position, $html, $display_reference)
452 ->focus("#${id_base}_description")