X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/b42a86ead32ac2a4cefcc972f35b3e723f21d6bc..b48939fe80e819f27898d0782e802440170ac0bb:/SL/Controller/RequirementSpecItem.pm diff --git a/SL/Controller/RequirementSpecItem.pm b/SL/Controller/RequirementSpecItem.pm index 53708fd25..b1709ce09 100644 --- a/SL/Controller/RequirementSpecItem.pm +++ b/SL/Controller/RequirementSpecItem.pm @@ -4,7 +4,9 @@ use strict; use parent qw(SL::Controller::Base); +use Carp; use List::MoreUtils qw(apply); +use List::Util qw(first); use Time::HiRes (); use SL::DB::RequirementSpec; @@ -17,20 +19,19 @@ use SL::Locale::String; use Rose::Object::MakeMethods::Generic ( - scalar => [ qw(requirement_spec item visible_item visible_section) ], + scalar => [ qw(item visible_item visible_section) ], 'scalar --get_set_init' => [ qw(complexities risks) ], ); -# __PACKAGE__->run_before('load_requirement_spec'); -__PACKAGE__->run_before('load_requirement_spec_item', only => [ qw(dragged_and_dropped ajax_update ajax_edit) ]); -__PACKAGE__->run_before('init_visible_section', only => [ qw(dragged_and_dropped ajax_list ajax_edit) ]); +__PACKAGE__->run_before('load_requirement_spec_item', only => [ qw(dragged_and_dropped ajax_update ajax_edit ajax_delete ajax_flag) ]); +__PACKAGE__->run_before('init_visible_section'); # # actions # sub action_ajax_list { - my ($self) = @_; + my ($self, $js) = @_; my $js = SL::ClientJS->new; @@ -41,12 +42,7 @@ sub action_ajax_list { $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{clicked_id})->load->get_section); - if (!$self->visible_section || ($self->visible_section->id != $self->item->id)) { - my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item); - $js->html('#column-content', $html) - ->val('#current_content_type', $self->item->get_type) - ->val('#current_content_id', $self->item->id); - } + $self->render_list($js, $self->item) if !$self->visible_section || ($self->visible_section->id != $self->item->id); $self->render($js); } @@ -97,8 +93,8 @@ sub action_dragged_and_dropped { $js->hide('#section-list-empty'); my $id_prefix = $new_type eq 'sub-function-block' ? 'sub-' : ''; - my $template = apply { s/-/_/g; $_ } $new_type; - my $html = "" . $self->render('requirement_spec_item/_' . $template, { output => 0 }, requirement_spec_item => $self->item); + my $template = 'requirement_spec_item/_' . (apply { s/-/_/g; $_ } $new_type); + my $html = "" . $self->render($template, { output => 0 }, requirement_spec_item => $self->item); my $next_item = $self->item->get_next_in_list; if ($next_item) { @@ -117,6 +113,74 @@ sub action_dragged_and_dropped { $self->render($js); } +sub action_ajax_add_section { + my ($self, %params) = @_; + + die "Missing parameter 'requirement_spec_id'" if !$::form->{requirement_spec_id}; + + $self->item(SL::DB::RequirementSpecItem->new(requirement_spec_id => $::form->{requirement_spec_id})); + + my $insert_after = $::form->{id} ? SL::DB::RequirementSpecItem->new(id => $::form->{id})->load->get_section->id : undef; + my $html = $self->render('requirement_spec_item/_section_form', { output => 0 }, id_base => 'new_section', insert_after => $insert_after); + + SL::ClientJS->new + ->remove('#new_section_form') + ->hide('#column-content > *') + ->appendTo($html, '#column-content') + ->focus('#new_section_title') + ->render($self); +} + +sub action_ajax_add_function_block { + my ($self, %params) = @_; + + return $self->add_function_block('function-block'); +} + +sub action_ajax_add_sub_function_block { + my ($self, %params) = @_; + + return $self->add_function_block('sub-function-block'); +} + +sub action_ajax_create { + my ($self, %params) = @_; + + my $js = SL::ClientJS->new; + my $prefix = $::form->{form_prefix} || die "Missing parameter 'form_prefix'"; + my $attributes = $::form->{$prefix} || die "Missing parameter group '${prefix}'"; + my $insert_after = delete $attributes->{insert_after}; + + my @errors = $self->item(SL::DB::RequirementSpecItem->new(%{ $attributes }))->validate; + return $js->error(@errors)->render($self) if @errors; + + $self->item->save; + $self->item->add_to_list(position => 'after', reference => $insert_after) if $insert_after; + + my $type = $self->item->get_type; + + if ($type eq 'section') { + my $node = $self->presenter->requirement_spec_item_jstree_data($self->item); + return $self->render_list($js, $self->item) + ->jstree->create_node('#tree', $insert_after ? ('#fb-' . $insert_after, 'after') : ('#sections', 'last'), $node) + ->jstree->select_node('#tree', '#fb-' . $self->item->id) + ->render($self); + } + + my $template = 'requirement_spec_item/_' . (apply { s/-/_/g; $_ } $type); + my $html = $self->render($template, { output => 0 }, requirement_spec_item => $self->item, id_prefix => $type eq 'function-block' ? '' : 'sub-'); + my $node = $self->presenter->requirement_spec_item_jstree_data($self->item); + + $js->replaceWith('#' . $prefix . '_form', $html) + ->hide('#section-list-empty') + ->jstree->create_node('#tree', $insert_after ? ('#fb-' . $insert_after, 'after') : ('#fb-' . $self->item->parent_id, 'last'), $node) + ->jstree->select_node('#tree', '#fb-' . $self->item->id); + + $self->replace_bottom($js, $self->item->parent) if $type eq 'sub-function-block'; + + $js->render($self); +} + sub action_ajax_edit { my ($self, %params) = @_; @@ -124,46 +188,51 @@ sub action_ajax_edit { my $js = SL::ClientJS->new; - die "TODO: edit section" if $self->item->get_type =~ m/section/; - if (!$self->visible_section || ($self->visible_section->id != $self->item->get_section->id)) { + # Show section/item to edit if it is not visible. + my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item); $js->html('#column-content', $html); } - if ($self->item->get_type =~ m/function-block/) { - my $create_item = sub { - [ $_[0]->id, $self->presenter->truncate(join(' ', grep { $_ } ($_[1], $_[0]->fb_number, $_[0]->description))) ] - }; - my @dependencies = - map { [ $_->fb_number . ' ' . $_->title, - [ map { ( $create_item->($_), - map { $create_item->($_, '->') } @{ $_->sorted_children }) - } @{ $_->sorted_children } ] ] - } @{ $self->item->requirement_spec->sections }; - - my @selected_dependencies = map { $_->id } @{ $self->item->dependencies }; - - my $html = $self->render('requirement_spec_item/_function_block_form', { output => 0 }, DEPENDENCIES => \@dependencies, SELECTED_DEPENDENCIES => \@selected_dependencies); - my $id_base = $self->item->get_type . '-' . $self->item->id; - my $content_top_id = '#' . $self->item->get_type . '-content-top-' . $self->item->id; - - $js->hide($content_top_id) - ->remove("#edit_${id_base}_form") - ->insertAfter($html, $content_top_id) + if ($self->item->get_type =~ m/section/) { + # Edit the section header, not an item. + my $html = $self->render('requirement_spec_item/_section_form', { output => 0 }); + + $js->hide('#section-header-' . $self->item->id) + ->remove("#edit_section_form") + ->insertAfter($html, '#section-header-' . $self->item->id) ->jstree->select_node('#tree', '#fb-' . $self->item->id) - ->focus("#edit_${id_base}_description") - ->val('#current_content_type', $self->item->get_type) - ->val('#current_content_id', $self->item->id) + ->focus("#edit_section_title") + ->val('#current_content_type', 'section') + ->val('#current_content_id', $self->item->id) ->render($self); + return; } + + # Edit a function block or a sub function block + my @dependencies = $self->create_dependencies; + my @selected_dependencies = map { $_->id } @{ $self->item->dependencies }; + + my $html = $self->render('requirement_spec_item/_function_block_form', { output => 0 }, DEPENDENCIES => \@dependencies, SELECTED_DEPENDENCIES => \@selected_dependencies); + my $id_base = 'edit_function_block_' . $self->item->id; + my $content_top_id = '#' . $self->item->get_type . '-content-top-' . $self->item->id; + + $js->hide($content_top_id) + ->remove("#${id_base}_form") + ->insertAfter($html, $content_top_id) + ->jstree->select_node('#tree', '#fb-' . $self->item->id) + ->focus("#${id_base}_description") + ->val('#current_content_type', $self->item->get_type) + ->val('#current_content_id', $self->item->id) + ->render($self); } sub action_ajax_update { my ($self, %params) = @_; my $js = SL::ClientJS->new; - my $prefix = $::form->{form_prefix} || 'function_block'; + my $prefix = $::form->{form_prefix} || die "Missing parameter 'form_prefix'"; my $attributes = $::form->{$prefix} || {}; foreach (qw(requirement_spec_id parent_id position)) { @@ -175,7 +244,26 @@ sub action_ajax_update { $self->item->save; - my $id_prefix = $self->item->get_type eq 'function-block' ? '' : 'sub-'; + my $type = $self->item->get_type; + + if ($type eq 'section') { + # Updated section, now update section header. + + my $html = $self->render('requirement_spec_item/_section_header', { output => 0 }, requirement_spec_item => $self->item); + + return SL::ClientJS->new + ->remove('#edit_section_form') + ->html('#section-header-' . $self->item->id, $html) + ->show('#section-header-' . $self->item->id) + ->jstree->rename_node('#tree', '#fb-' . $self->item->id, $::request->presenter->requirement_spec_item_tree_node_title($self->item)) + ->render($self); + } + + # Updated function block or sub function block. Update (sub) + # function block and potentially the bottom of the parent function + # block. + + my $id_prefix = $type eq 'function-block' ? '' : 'sub-'; my $html_top = $self->render('requirement_spec_item/_function_block_content_top', { output => 0 }, requirement_spec_item => $self->item, id_prefix => $id_prefix); $id_prefix .= 'function-block-content-'; @@ -185,11 +273,68 @@ sub action_ajax_update { ->jstree->rename_node('#tree', '#fb-' . $self->item->id, $::request->presenter->requirement_spec_item_tree_node_title($self->item)); $self->replace_bottom($js, $self->item, id_prefix => $id_prefix); - $self->replace_bottom($js, $self->item->parent) if $self->item->get_type eq 'sub-function-block'; + $self->replace_bottom($js, $self->item->parent) if $type eq 'sub-function-block'; $js->render($self); } +sub action_ajax_delete { + my ($self) = @_; + + my $js = SL::ClientJS->new; + my $full_list = $self->item->get_full_list; + + $self->item->delete; + + if ($self->visible_section && ($self->visible_section->id == $self->item->id)) { + # Currently visible section is deleted. + + my $new_section = first { $_->id != $self->item->id } @{ $self->item->requirement_spec->sections }; + if ($new_section) { + $self->render_list($js, $new_section); + + } else { + my $html = $self->render('requirement_spec_item/_no_section', { output => 0 }); + $js->html('#column-content', $html) + ->val('#current_content_type', '') + ->val('#current_content_id', '') + } + + } elsif ($self->visible_section && ($self->visible_section->id == $self->item->get_section->id)) { + # Item in currently visible section is deleted. + + my $type = $self->item->get_type; + $js->remove('#edit_function_block_' . $self->item->id . '_form') + ->remove('#' . $type . '-' . $self->item->id); + + $self->replace_bottom($js, $self->item->parent_id) if $type eq 'sub-function-block'; + + if (1 == scalar @{ $full_list }) { + if ($type eq 'function-block') { + $js->show('#section-list-empty'); + } elsif ($type eq 'sub-function-block') { + $js->hide('#sub-function-block-container-' . $self->item->parent_id); + } + } + } + + $js->jstree->delete_node('#tree', '#fb-' . $self->item->id) + ->render($self); +} + +sub action_ajax_flag { + my ($self) = @_; + + $self->item->update_attributes(is_flagged => !$self->item->is_flagged); + + my $is_visible = $self->visible_section && ($self->visible_section->id == $self->item->get_section->id); + + SL::ClientJS->new + ->action_if($is_visible, 'toggleClass', '#' . $self->item->get_type . '-' . $self->item->id, 'flagged') + ->toggleClass('#fb-' . $self->item->id, 'flagged') + ->render($self); +} + # # filters # @@ -225,7 +370,9 @@ sub init_visible_section { return undef unless $content_id; return undef unless $content_type =~ m/section|function-block/; - $self->visible_item(SL::DB::RequirementSpecItem->new(id => $content_id)->load); + $self->visible_item(SL::DB::Manager::RequirementSpecItem->find_by(id => $content_id)); + return undef unless $self->visible_item; + return $self->visible_section($self->visible_item->get_section); } @@ -250,4 +397,90 @@ sub replace_bottom { return $js->replaceWith('#' . $id_prefix . 'function-block-content-bottom-' . $item->id, $html); } +sub render_list { + my ($self, $js, $item) = @_; + + my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $item); + $js->html('#column-content', $html) + ->val( '#current_content_type', $item->get_type) + ->val( '#current_content_id', $item->id) + ->jstree->select_node('#tree', '#fb-' . $item->id); +} + +sub create_dependency_item { + my $self = shift; + [ $_[0]->id, $self->presenter->truncate(join(' ', grep { $_ } ($_[1], $_[0]->fb_number, $_[0]->description))) ]; +} + +sub create_dependencies { + my ($self) = @_; + + return map { [ $_->fb_number . ' ' . $_->title, + [ map { ( $self->create_dependency_item($_), + map { $self->create_dependency_item($_, '->') } @{ $_->sorted_children }) + } @{ $_->sorted_children } ] ] + } @{ $self->item->requirement_spec->sections }; +} + +sub add_function_block { + my ($self, $new_type) = @_; + + my $clicked_id = $::form->{id} || ($self->visible_item ? $self->visible_item->id : undef); + + die "Invalid new_type '$new_type'" if $new_type !~ m/^(?:sub-)?function-block$/; + die "Missing parameter 'id' and no visible item" if !$clicked_id; + die "Missing parameter 'requirement_spec_id'" if !$::form->{requirement_spec_id}; + + my $clicked_item = SL::DB::RequirementSpecItem->new(id => $clicked_id)->load; + my $clicked_type = $clicked_item->get_type; + + die "Invalid clicked_type '$clicked_type'" if $clicked_type !~ m/^(?: section | (?:sub-)? function-block )$/x; + + my $case = "${clicked_type}:${new_type}"; + + my ($insert_position, $insert_reference, $parent_id, $display_reference) + = $case eq 'section:function-block' ? ( 'appendTo', $clicked_item->id, $clicked_item->id, '#section-list' ) + : $case eq 'function-block:function-block' ? ( 'insertAfter', $clicked_item->id, $clicked_item->parent_id, '#function-block-' ) + : $case eq 'function-block:sub-function-block' ? ( 'appendTo' , $clicked_item->id, $clicked_item->id, '#sub-function-block-container-' ) + : $case eq 'sub-function-block:function-block' ? ( 'insertAfter', $clicked_item->parent_id, $clicked_item->parent->parent_id, '#function-block-' ) + : $case eq 'sub-function-block:sub-function-block' ? ( 'insertAfter', $clicked_item->id, $clicked_item->parent_id, '#sub-function-block-' ) + : die "Invalid combination of 'clicked_type (section)/new_type ($new_type)'"; + + $self->item(SL::DB::RequirementSpecItem->new(requirement_spec_id => $::form->{requirement_spec_id}, parent_id => $parent_id)); + + $display_reference .= $insert_reference if $display_reference =~ m/-$/; + my $id_base = join('_', 'new_function_block', Time::HiRes::gettimeofday(), int rand 1000000000000); + my $html = $self->render( + 'requirement_spec_item/_function_block_form', + { output => 0 }, + DEPENDENCIES => [ $self->create_dependencies ], + SELECTED_DEPENDENCIES => [], + requirement_spec_item => $self->item, + id_base => $id_base, + insert_after => $insert_position eq 'insertAfter' ? $insert_reference : undef, + ); + + my $js = SL::ClientJS->new; + + my $new_section = $self->item->get_section; + if (!$self->visible_section || ($self->visible_section->id != $new_section->id)) { + # Show section/item to edit if it is not visible. + + $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $new_section); + $js->html('#column-content', $html) + ->val('#current_content_type', 'section') + ->val('#current_content_id', $new_section->id) + ->jstree->select_node('#tree', '#fb-' . $new_section->id); + } + + # $::lxdebug->message(0, "alright! clicked ID " . $::form->{id} . " type $clicked_type new_type $new_type insert_pos $insert_position ref " . ($insert_reference // '') . " parent $parent_id display_ref $display_reference"); + + $js->action($insert_position, $html, $display_reference) + ->focus("#${id_base}_description"); + + $js->show('#sub-function-block-container-' . $parent_id) if $new_type eq 'sub-function-block'; + + $js->render($self); +} + 1;