1 package SL::Controller::RequirementSpecItem;
5 use parent qw(SL::Controller::Base);
9 use SL::DB::RequirementSpec;
10 use SL::DB::RequirementSpecComplexity;
11 use SL::DB::RequirementSpecItem;
12 use SL::DB::RequirementSpecRisk;
13 use SL::Helper::Flash;
15 use SL::Locale::String;
17 use Rose::Object::MakeMethods::Generic
19 scalar => [ qw(requirement_spec item visible_item visible_section) ],
20 'scalar --get_set_init' => [ qw(complexities risks) ],
23 # __PACKAGE__->run_before('load_requirement_spec');
24 __PACKAGE__->run_before('load_requirement_spec_item', only => [qw(dragged_and_dropped ajax_update ajax_edit)]);
30 sub action_ajax_list {
33 my $js = SL::ClientJS->new;
35 if (!$::form->{clicked_id}) {
36 # Clicked on "sections" in the tree. Do nothing.
37 return $self->render($js);
40 $self->init_visible_section($::form->{current_content_id}, $::form->{current_content_type});
41 $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{clicked_id})->load->get_section);
43 if (!$self->visible_section || ($self->visible_section->id != $self->item->id)) {
44 my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item);
45 $js->html('#column-content', $html)
46 ->val('#current_content_type', $self->item->get_type)
47 ->val('#current_content_id', $self->item->id);
53 sub action_dragged_and_dropped {
56 my $dropped_item = SL::DB::RequirementSpecItem->new(id => $::form->{dropped_id})->load || die "No such dropped item";
57 my $position = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position} : die "Unknown 'position' parameter";
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 => $dropped_item->id);
65 $self->render(\'', { type => 'json' });
68 sub action_ajax_edit {
69 my ($self, %params) = @_;
71 $::lxdebug->dump(0, "form", $::form);
73 $self->init_visible_section($::form->{current_content_id}, $::form->{current_content_type});
74 $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load);
76 my $js = SL::ClientJS->new;
78 die "TODO: edit section" if $self->item->get_type =~ m/section/;
80 if (!$self->visible_section || ($self->visible_section->id != $self->item->get_section->id)) {
81 my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item);
82 $js->html('#column-content', $html);
85 if ($self->item->get_type =~ m/function-block/) {
86 my $create_item = sub {
87 [ $_[0]->id, $self->presenter->truncate(join(' ', grep { $_ } ($_[1], $_[0]->fb_number, $_[0]->description))) ]
90 map { [ $_->fb_number . ' ' . $_->title,
91 [ map { ( $create_item->($_),
92 map { $create_item->($_, '->') } @{ $_->sorted_children })
93 } @{ $_->sorted_children } ] ]
94 } @{ $self->item->requirement_spec->sections };
96 my @selected_dependencies = map { $_->id } @{ $self->item->dependencies };
98 my $html = $self->render('requirement_spec_item/_function_block_form', { output => 0 }, DEPENDENCIES => \@dependencies, SELECTED_DEPENDENCIES => \@selected_dependencies);
99 my $id_base = $self->item->get_type . '-' . $self->item->id;
100 my $content_top_id = '#' . $self->item->get_type . '-content-top-' . $self->item->id;
102 $js->hide($content_top_id)
103 ->remove("#edit_${id_base}_form")
104 ->insertAfter($html, $content_top_id)
105 ->jstree->select_node('#tree', '#fb-' . $self->item->id)
106 ->focus("#edit_${id_base}_description")
107 ->val('#current_content_type', $self->item->get_type)
108 ->val('#current_content_id', $self->item->id)
113 sub action_ajax_update {
114 my ($self, %params) = @_;
116 my $js = SL::ClientJS->new;
117 my $prefix = $::form->{form_prefix} || 'text_block';
118 my $attributes = $::form->{$prefix} || {};
120 foreach (qw(requirement_spec_id parent_id position)) {
121 delete $attributes->{$_} if !defined $attributes->{$_};
124 my @errors = $self->item->assign_attributes(%{ $attributes })->validate;
125 return $js->error(@errors)->render($self) if @errors;
129 my $id_prefix = $self->item->get_type eq 'function-block' ? '' : 'sub-';
130 my $html_top = $self->render('requirement_spec_item/_function_block_content_top', { output => 0 }, requirement_spec_item => $self->item, id_prefix => $id_prefix);
131 my $html_bottom = $self->render('requirement_spec_item/_function_block_content_bottom', { output => 0 }, requirement_spec_item => $self->item, id_prefix => $id_prefix);
132 $id_prefix .= 'function-block-content-';
135 ->remove('#' . $prefix . '_form')
136 ->replaceWith('#' . $id_prefix . 'top-' . $self->item->id, $html_top)
137 ->replaceWith('#' . $id_prefix . 'bottom-' . $self->item->id, $html_bottom)
138 ->jstree->rename_node('#tree', '#fb-' . $self->item->id, $::request->presenter->requirement_spec_item_tree_node_title($self->item))
146 sub load_requirement_spec {
148 $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load || die "No such requirement spec");
151 sub load_requirement_spec_item {
153 $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load || die "No such requirement spec item");
160 sub create_random_id {
161 return join '-', Time::HiRes::gettimeofday();
164 sub format_exception {
165 return join "\n", (split m/\n/, $@)[0..4];
168 sub init_visible_section {
169 my ($self, $content_id, $content_type) = @_;
171 return undef unless $content_id;
172 return undef unless $content_type =~ m/section|function-block/;
174 $self->visible_item(SL::DB::RequirementSpecItem->new(id => $content_id)->load);
175 return $self->visible_section($self->visible_item->get_section);
178 sub init_complexities {
181 return SL::DB::Manager::RequirementSpecComplexity->get_all_sorted;
187 return SL::DB::Manager::RequirementSpecRisk->get_all_sorted;