1 package SL::Controller::RequirementSpecItem;
5 use parent qw(SL::Controller::Base);
7 use List::MoreUtils qw(apply);
8 use List::Util qw(first);
11 use SL::DB::RequirementSpec;
12 use SL::DB::RequirementSpecComplexity;
13 use SL::DB::RequirementSpecItem;
14 use SL::DB::RequirementSpecRisk;
15 use SL::Helper::Flash;
17 use SL::Locale::String;
19 use Rose::Object::MakeMethods::Generic
21 scalar => [ qw(item visible_item visible_section) ],
22 'scalar --get_set_init' => [ qw(complexities risks) ],
25 __PACKAGE__->run_before('load_requirement_spec_item', only => [ qw(dragged_and_dropped ajax_update ajax_edit ajax_delete) ]);
26 __PACKAGE__->run_before('init_visible_section', only => [ qw(dragged_and_dropped ajax_list ajax_edit ajax_delete) ]);
32 sub action_ajax_list {
35 my $js = SL::ClientJS->new;
37 if (!$::form->{clicked_id}) {
38 # Clicked on "sections" in the tree. Do nothing.
39 return $self->render($js);
42 $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{clicked_id})->load->get_section);
44 $self->render_list($js, $self->item) if !$self->visible_section || ($self->visible_section->id != $self->item->id);
49 sub action_dragged_and_dropped {
52 my $position = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position} : die "Unknown 'position' parameter";
53 my $dropped_item = $::form->{dropped_id} ? SL::DB::RequirementSpecItem->new(id => $::form->{dropped_id})->load : undef;
55 my $visible_section_id = $self->visible_section ? $self->visible_section->id : undef;
56 my $old_parent_id = $self->item->parent_id;
57 my $old_type = $self->item->get_type;
59 $self->item->db->do_transaction(sub {
60 $self->item->remove_from_list;
61 $self->item->parent_id($position =~ m/before|after/ ? $dropped_item->parent_id : $dropped_item->id);
62 $self->item->add_to_list(position => $position, reference => $::form->{dropped_id} || undef);
65 my $js = SL::ClientJS->new;
67 $self->item(SL::DB::RequirementSpecItem->new(id => $self->item->id)->load);
68 my $new_section = $self->item->get_section;
69 my $new_type = $self->item->get_type;
71 return $self->render($js) if !$visible_section_id || ($new_type eq 'section');
73 my $old_parent = SL::DB::RequirementSpecItem->new(id => $old_parent_id)->load;
74 my $old_section = $old_parent->get_section;
76 # $::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);
78 if ($visible_section_id == $old_section->id) {
79 my $id_prefix = $old_type eq 'sub-function-block' ? 'sub-' : '';
80 $js->remove('#' . $id_prefix . 'function-block-' . $self->item->id);
82 if ($old_type eq 'sub-function-block') {
83 $self->replace_bottom($js, $old_parent) ;
84 $js->hide('#sub-function-block-container-' . $old_parent->id) if 0 == scalar(@{ $old_parent->children });
86 } elsif (0 == scalar(@{ $old_section->children })) {
87 $js->show('#section-list-empty');
91 if ($visible_section_id == $new_section->id) {
92 $js->hide('#section-list-empty');
94 my $id_prefix = $new_type eq 'sub-function-block' ? 'sub-' : '';
95 my $template = 'requirement_spec_item/_' . (apply { s/-/_/g; $_ } $new_type);
96 my $html = "" . $self->render($template, { output => 0 }, requirement_spec_item => $self->item);
97 my $next_item = $self->item->get_next_in_list;
100 $js->insertBefore($html, '#' . $id_prefix . 'function-block-' . $next_item->id);
102 my $parent_is_section = $self->item->parent->get_type eq 'section';
103 $js->appendTo($html, $parent_is_section ? '#section-list' : '#sub-function-block-container-' . $self->item->parent_id);
104 $js->show('#sub-function-block-container-' . $self->item->parent_id) if !$parent_is_section;
107 $self->replace_bottom($js, $self->item->parent) if $new_type eq 'sub-function-block';
110 # $::lxdebug->dump(0, "js", $js->to_array);
115 sub action_ajax_add_section {
116 my ($self, %params) = @_;
118 die "Missing parameter 'requirement_spec_id'" if !$::form->{requirement_spec_id};
120 $self->item(SL::DB::RequirementSpecItem->new(requirement_spec_id => $::form->{requirement_spec_id}));
122 my $insert_after = $::form->{id} ? SL::DB::RequirementSpecItem->new(id => $::form->{id})->load->get_section->id : undef;
123 my $html = $self->render('requirement_spec_item/_section_form', { output => 0 }, id_base => 'new_section', insert_after => $insert_after);
126 ->remove('#new_section_form')
127 ->hide('#column-content > *')
128 ->appendTo($html, '#column-content')
129 ->focus('#new_section_title')
133 sub action_ajax_create {
134 my ($self, %params) = @_;
136 my $js = SL::ClientJS->new;
137 my $prefix = $::form->{form_prefix} || die "Missing parameter 'form_prefix'";
138 my $attributes = $::form->{$prefix} || die "Missing parameter group '${prefix}'";
139 my $insert_after = delete $attributes->{insert_after};
141 my @errors = $self->item(SL::DB::RequirementSpecItem->new(%{ $attributes }))->validate;
142 return $js->error(@errors)->render($self) if @errors;
145 $self->item->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
147 my $type = $self->item->get_type;
149 if ($type eq 'section') {
150 my $node = $self->presenter->requirement_spec_item_jstree_data($self->item);
151 return $self->render_list($js, $self->item)
152 ->jstree->create_node('#tree', $insert_after ? ('#fb-' . $insert_after, 'after') : ('#sections', 'last'), $node)
153 ->jstree->select_node('#tree', '#fb-' . $self->item->id)
157 die 'TODO: create item';
160 sub action_ajax_edit {
161 my ($self, %params) = @_;
163 $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load);
165 my $js = SL::ClientJS->new;
167 if (!$self->visible_section || ($self->visible_section->id != $self->item->get_section->id)) {
168 # Show section/item to edit if it is not visible.
170 my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item);
171 $js->html('#column-content', $html);
174 if ($self->item->get_type =~ m/section/) {
175 # Edit the section header, not an item.
176 my $html = $self->render('requirement_spec_item/_section_form', { output => 0 });
178 $js->hide('#section-header-' . $self->item->id)
179 ->remove("#edit_section_form")
180 ->insertAfter($html, '#section-header-' . $self->item->id)
181 ->jstree->select_node('#tree', '#fb-' . $self->item->id)
182 ->focus("#edit_section_title")
183 ->val('#current_content_type', 'section')
184 ->val('#current_content_id', $self->item->id)
189 # Edit a function block or a sub function block
190 my $create_item = sub {
191 [ $_[0]->id, $self->presenter->truncate(join(' ', grep { $_ } ($_[1], $_[0]->fb_number, $_[0]->description))) ]
194 map { [ $_->fb_number . ' ' . $_->title,
195 [ map { ( $create_item->($_),
196 map { $create_item->($_, '->') } @{ $_->sorted_children })
197 } @{ $_->sorted_children } ] ]
198 } @{ $self->item->requirement_spec->sections };
200 my @selected_dependencies = map { $_->id } @{ $self->item->dependencies };
202 my $html = $self->render('requirement_spec_item/_function_block_form', { output => 0 }, DEPENDENCIES => \@dependencies, SELECTED_DEPENDENCIES => \@selected_dependencies);
203 my $id_base = 'edit_function_block_' . $self->item->id;
204 my $content_top_id = '#' . $self->item->get_type . '-content-top-' . $self->item->id;
206 $js->hide($content_top_id)
207 ->remove("#${id_base}_form")
208 ->insertAfter($html, $content_top_id)
209 ->jstree->select_node('#tree', '#fb-' . $self->item->id)
210 ->focus("#${id_base}_description")
211 ->val('#current_content_type', $self->item->get_type)
212 ->val('#current_content_id', $self->item->id)
216 sub action_ajax_update {
217 my ($self, %params) = @_;
219 my $js = SL::ClientJS->new;
220 my $prefix = $::form->{form_prefix} || die "Missing parameter 'form_prefix'";
221 my $attributes = $::form->{$prefix} || {};
223 foreach (qw(requirement_spec_id parent_id position)) {
224 delete $attributes->{$_} if !defined $attributes->{$_};
227 my @errors = $self->item->assign_attributes(%{ $attributes })->validate;
228 return $js->error(@errors)->render($self) if @errors;
232 my $type = $self->item->get_type;
234 if ($type eq 'section') {
235 # Updated section, now update section header.
237 my $html = $self->render('requirement_spec_item/_section_header', { output => 0 }, requirement_spec_item => $self->item);
239 return SL::ClientJS->new
240 ->remove('#edit_section_form')
241 ->html('#section-header-' . $self->item->id, $html)
242 ->show('#section-header-' . $self->item->id)
243 ->jstree->rename_node('#tree', '#fb-' . $self->item->id, $::request->presenter->requirement_spec_item_tree_node_title($self->item))
247 # Updated function block or sub function block. Update (sub)
248 # function block and potentially the bottom of the parent function
251 my $id_prefix = $type eq 'function-block' ? '' : 'sub-';
252 my $html_top = $self->render('requirement_spec_item/_function_block_content_top', { output => 0 }, requirement_spec_item => $self->item, id_prefix => $id_prefix);
253 $id_prefix .= 'function-block-content-';
255 my $js = SL::ClientJS->new
256 ->remove('#' . $prefix . '_form')
257 ->replaceWith('#' . $id_prefix . 'top-' . $self->item->id, $html_top)
258 ->jstree->rename_node('#tree', '#fb-' . $self->item->id, $::request->presenter->requirement_spec_item_tree_node_title($self->item));
260 $self->replace_bottom($js, $self->item, id_prefix => $id_prefix);
261 $self->replace_bottom($js, $self->item->parent) if $type eq 'sub-function-block';
266 sub action_ajax_delete {
269 my $js = SL::ClientJS->new;
273 if ($self->visible_section && ($self->visible_section->id == $self->item->id)) {
274 # Currently visible section is deleted.
276 my $new_section = first { $_->id != $self->item->id } @{ $self->item->requirement_spec->sections };
278 $self->render_list($js, $new_section);
281 my $html = $self->render('requirement_spec_item/_no_section', { output => 0 });
282 $js->html('#column-content', $html)
283 ->val('#current_content_type', '')
284 ->val('#current_content_id', '')
287 } elsif ($self->visible_section && ($self->visible_section->id == $self->item->get_section->id)) {
288 # Item in currently visible section is deleted.
290 my $type = $self->item->get_type;
291 $js->remove('#edit_function_block_' . $self->item->id . '_form')
292 ->remove('#' . $type . '-' . $self->item->id);
294 $self->replace_bottom($js, $self->item->parent_id) if $type eq 'sub-function-block';
296 if (1 == scalar @{ $self->item->get_full_list }) {
297 if ($type eq 'function-block') {
298 $js->show('#section-list-empty');
299 } elsif ($type eq 'sub-function-block') {
300 $js->hide('#sub-function-block-container-' . $self->item->parent_id);
305 $js->jstree->delete_node('#tree', '#fb-' . $self->item->id)
313 sub load_requirement_spec {
315 $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load || die "No such requirement spec");
318 sub load_requirement_spec_item {
320 $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load || die "No such requirement spec item");
327 sub create_random_id {
328 return join '-', Time::HiRes::gettimeofday();
331 sub format_exception {
332 return join "\n", (split m/\n/, $@)[0..4];
335 sub init_visible_section {
338 my $content_id = $::form->{current_content_id};
339 my $content_type = $::form->{current_content_type};
341 return undef unless $content_id;
342 return undef unless $content_type =~ m/section|function-block/;
344 $self->visible_item(SL::DB::Manager::RequirementSpecItem->find_by(id => $content_id));
345 return undef unless $self->visible_item;
347 return $self->visible_section($self->visible_item->get_section);
350 sub init_complexities {
353 return SL::DB::Manager::RequirementSpecComplexity->get_all_sorted;
359 return SL::DB::Manager::RequirementSpecRisk->get_all_sorted;
363 my ($self, $js, $item_or_id) = @_;
365 my $item = (ref($item_or_id) ? $item_or_id : SL::DB::RequirementSpecItem->new(id => $item_or_id))->load;
366 my $id_prefix = $item->get_type eq 'function-block' ? '' : 'sub-';
367 my $html = $self->render('requirement_spec_item/_function_block_content_bottom', { output => 0 }, requirement_spec_item => $item, id_prefix => $id_prefix);
368 return $js->replaceWith('#' . $id_prefix . 'function-block-content-bottom-' . $item->id, $html);
372 my ($self, $js, $item) = @_;
374 my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $item);
375 $js->html('#column-content', $html)
376 ->val( '#current_content_type', $item->get_type)
377 ->val( '#current_content_id', $item->id)
378 ->jstree->select_node('#tree', '#fb-' . $item->id);