1 package SL::Controller::RequirementSpecTextBlock;
5 use parent qw(SL::Controller::Base);
10 use SL::DB::RequirementSpec;
11 use SL::DB::RequirementSpecPredefinedText;
12 use SL::DB::RequirementSpecTextBlock;
13 use SL::Helper::Flash;
15 use SL::Locale::String;
17 use Rose::Object::MakeMethods::Generic
19 scalar => [ qw(requirement_spec text_block) ],
20 'scalar --get_set_init' => [ qw(predefined_texts) ],
23 __PACKAGE__->run_before('load_requirement_spec_text_block', only => [qw(ajax_edit ajax_update ajax_delete dragged_and_dropped)]);
29 sub action_ajax_list {
33 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
36 if ($::form->{clicked_type} =~ m/^text-blocks-(front|back)/) {
37 $new_where = $1 eq 'front' ? 0 : 1;
40 $new_where = $self->output_position_from_id($::form->{clicked_id});
43 # $::lxdebug->message(0, "cur $current_where new $new_where");
45 my $js = SL::ClientJS->new;
47 if (!defined($current_where) || ($new_where != $current_where)) {
48 my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $new_where, requirement_spec_id => $::form->{requirement_spec_id} ]);
49 my $html = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $new_where);
51 $js->html('#column-content', $html)
60 my $js = SL::ClientJS->new;
62 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
63 my $new_where = $self->output_position_from_id($::form->{id}) // $::form->{output_position};
65 if ($new_where != $current_where) {
66 my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $new_where, requirement_spec_id => $::form->{requirement_spec_id} ]);
67 my $html = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $new_where);
69 $js->html('#column-content', $html);
72 $self->text_block(SL::DB::RequirementSpecTextBlock->new(
73 requirement_spec_id => $::form->{requirement_spec_id},
74 output_position => $::form->{output_position},
77 my $id_base = join('_', 'new_text_block', Time::HiRes::gettimeofday(), int rand 1000000000000);
78 my $html = $self->render('requirement_spec_text_block/_form', { output => 0 }, id_base => $id_base, insert_after => $::form->{id});
80 $js->action($::form->{id} ? 'insertAfter' : 'appendTo', $html, '#text-block-' . ($::form->{id} || 'list'))
81 ->focus('#' . $id_base . '_title')
85 sub action_ajax_edit {
88 my $js = SL::ClientJS->new;
90 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
91 if ($self->text_block->output_position != $current_where) {
92 my $text_blocks = $self->text_block->get_full_list;
93 my $html = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $self->text_block->output_position);
95 $js->html('#column-content', $html)
96 ->val('#current_content_type', 'text-block')
97 ->val('#current_content_id', $self->text_block->id);
100 my $html = $self->render('requirement_spec_text_block/_form', { output => 0 });
102 $js->hide('#text-block-' . $self->text_block->id)
103 ->remove('#edit_text_block_' . $self->text_block->id . '_form')
104 ->insertAfter($html, '#text-block-' . $self->text_block->id)
105 ->jstree->select_node('#tree', '#tb-' . $self->text_block->id)
106 ->focus('#edit_text_block_' . $self->text_block->id . '_title')
110 sub action_ajax_create {
111 my ($self, %params) = @_;
113 my $attributes = $::form->{ $::form->{form_prefix} } || die "Missing attributes";
114 my $insert_after = delete $attributes->{insert_after};
116 my @errors = $self->text_block(SL::DB::RequirementSpecTextBlock->new(%{ $attributes }))->validate;
117 return SL::ClientJS->new->error(@errors)->render($self) if @errors;
119 $self->text_block->save;
120 $self->text_block->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
122 my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
123 my $node = $self->presenter->requirement_spec_text_block_jstree_data($self->text_block);
126 ->replaceWith('#' . $::form->{form_prefix} . '_form', $html)
127 ->jstree->create_node('#tree', $insert_after ? ('#tb-' . $insert_after, 'after') : ('#tb-' . ($attributes->{output_position} == 0 ? 'front' : 'back'), 'last'), $node)
131 sub action_ajax_update {
132 my ($self, %params) = @_;
134 my $prefix = $::form->{form_prefix} || 'text_block';
135 my $attributes = $::form->{$prefix} || {};
137 foreach (qw(requirement_spec_id output_position position)) {
138 delete $attributes->{$_} if !defined $attributes->{$_};
141 my @errors = $self->text_block->assign_attributes(%{ $attributes })->validate;
142 return SL::ClientJS->new->error(@errors)->render($self) if @errors;
144 $self->text_block->save;
146 my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
149 ->remove('#' . $prefix . '_form')
150 ->replaceWith('#text-block-' . $self->text_block->id, $html)
151 ->jstree->rename_node('#tree', '#tb-' . $self->text_block->id, $self->text_block->title)
155 sub action_ajax_delete {
158 my $js = SL::ClientJS->new;
160 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
161 if ($self->text_block->output_position == $current_where) {
162 $js->remove('#edit_text_block_' . $self->text_block->id . '_form')
163 ->remove('#text-block-' . $self->text_block->id);
165 $js->show('#text-block-list-empty') if 1 == scalar @{ $self->text_block->get_full_list };
168 $self->text_block->delete;
170 $js->jstree->delete_node('#tree', '#tb-' . $self->text_block->id)
174 sub action_dragged_and_dropped {
177 my $position = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position} : die "Unknown 'position' parameter";
178 my $dropped_text_block = $position =~ m/^ (?: before | after ) $/x ? SL::DB::RequirementSpecTextBlock->new(id => $::form->{dropped_id})->load : undef;
180 my $dropped_type = $position ne 'last' ? undef : $::form->{dropped_type} =~ m/^ text-blocks- (?:front|back) $/x ? $::form->{dropped_type} : die "Unknown 'dropped_type' parameter";
181 my $old_where = $self->text_block->output_position;
183 $self->text_block->db->do_transaction(sub {
185 $self->text_block->remove_from_list;
186 $self->text_block->output_position($position =~ m/before|after/ ? $dropped_text_block->output_position : $::form->{dropped_type} eq 'text-blocks-front' ? 0 : 1);
187 $self->text_block->add_to_list(position => $position, reference => $dropped_text_block ? $dropped_text_block->id : undef);
190 # $::lxdebug->dump(0, "form", $::form);
192 return $self->render(\'', { type => 'json' }) if $::form->{current_content_type} !~ m/^text-block/;
194 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
195 my $new_where = $self->text_block->output_position;
196 my $id = $self->text_block->id;
197 my $js = SL::ClientJS->new;
199 # $::lxdebug->message(0, "old $old_where current $current_where new $new_where current_CID " . $::form->{current_content_id} . ' selfid ' . $self->text_block->id);
200 if (($old_where != $new_where) && ($::form->{current_content_id} == $self->text_block->id)) {
201 # The currently selected text block is dragged to the opposite
202 # text block location. Re-render the whole content column.
203 my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $new_where ]);
204 my $html = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $new_where);
206 $js->val('#current_content_type', 'text-blocks-' . ($new_where == 0 ? 'front' : 'back'))
207 ->html('#column-content', $html);
210 if ($old_where == $current_where) {
211 $js->remove('#text-block-' . $self->text_block->id);
213 if (0 == scalar(@{ SL::DB::Manager::RequirementSpecTextBlock->get_all(where => [ requirement_spec_id => $self->text_block->requirement_spec_id, output_position => $current_where ]) })) {
214 $js->show('#text-block-list-empty');
218 if ($new_where == $current_where) {
219 $js->hide('#text-block-list-empty');
221 my $html = "" . $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
223 my $prior_text_block = $self->text_block->get_previous_in_list;
225 if ($prior_text_block) {
226 $js->insertAfter($html, '#text-block-' . $prior_text_block->id);
228 $js->appendTo($html, '#text-block-list');
240 sub load_requirement_spec {
242 $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load || die "No such requirement spec");
245 sub load_requirement_spec_text_block {
247 $self->text_block(SL::DB::RequirementSpecTextBlock->new(id => $::form->{id})->load || die "No such requirement spec text block");
254 sub output_position_from_id {
255 my ($self, $id, $type, %params) = @_;
258 return $1 eq 'front' ? 0 : 1 if $type =~ m/-(front|back)$/;
259 return undef if $type !~ m/text-block/;
262 my $text_block = SL::DB::Manager::RequirementSpecTextBlock->find_by(id => $id);
264 return $text_block ? $text_block->output_position : undef;
267 sub init_predefined_texts {
268 return SL::DB::Manager::RequirementSpecPredefinedText->get_all_sorted;