1 package SL::Controller::RequirementSpecTextBlock;
5 use parent qw(SL::Controller::Base);
11 use SL::DB::RequirementSpec;
12 use SL::DB::RequirementSpecPredefinedText;
13 use SL::DB::RequirementSpecTextBlock;
14 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 ajax_flag dragged_and_dropped ajax_copy)]);
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)
52 ->val('#current_content_type', 'text-blocks-' . (0 == $new_where ? 'front' : 'back'))
53 ->val('#current_content_id', $::form->{clicked_id});
62 my $js = SL::ClientJS->new;
64 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
65 my $new_where = $self->output_position_from_id($::form->{id}) // $::form->{output_position};
67 if ($new_where != $current_where) {
68 my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $new_where, requirement_spec_id => $::form->{requirement_spec_id} ]);
69 my $html = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $new_where);
71 $js->html('#column-content', $html);
74 $self->text_block(SL::DB::RequirementSpecTextBlock->new(
75 requirement_spec_id => $::form->{requirement_spec_id},
76 output_position => $::form->{output_position},
79 my $id_base = join('_', 'new_text_block', Time::HiRes::gettimeofday(), int rand 1000000000000);
80 my $html = $self->render('requirement_spec_text_block/_form', { output => 0 }, id_base => $id_base, insert_after => $::form->{id});
82 $js->action($::form->{id} ? 'insertAfter' : 'appendTo', $html, '#text-block-' . ($::form->{id} || 'list'))
83 ->focus('#' . $id_base . '_title')
87 sub action_ajax_edit {
90 my $js = SL::ClientJS->new;
92 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
93 if ($self->text_block->output_position != $current_where) {
94 my $text_blocks = $self->text_block->get_full_list;
95 my $html = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $self->text_block->output_position);
97 $js->html('#column-content', $html)
98 ->val('#current_content_type', 'text-block')
99 ->val('#current_content_id', $self->text_block->id);
102 my $html = $self->render('requirement_spec_text_block/_form', { output => 0 });
104 $js->hide('#text-block-' . $self->text_block->id)
105 ->remove('#edit_text_block_' . $self->text_block->id . '_form')
106 ->insertAfter($html, '#text-block-' . $self->text_block->id)
107 ->jstree->select_node('#tree', '#tb-' . $self->text_block->id)
108 ->focus('#edit_text_block_' . $self->text_block->id . '_title')
112 sub action_ajax_create {
113 my ($self, %params) = @_;
115 my $attributes = $::form->{ $::form->{form_prefix} } || die "Missing attributes";
116 my $insert_after = delete $attributes->{insert_after};
118 my @errors = $self->text_block(SL::DB::RequirementSpecTextBlock->new(%{ $attributes }))->validate;
119 return SL::ClientJS->new->error(@errors)->render($self) if @errors;
121 $self->text_block->save;
122 $self->text_block->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
124 my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
125 my $node = $self->presenter->requirement_spec_text_block_jstree_data($self->text_block);
128 ->replaceWith('#' . $::form->{form_prefix} . '_form', $html)
129 ->jstree->create_node('#tree', $insert_after ? ('#tb-' . $insert_after, 'after') : ('#tb-' . ($attributes->{output_position} == 0 ? 'front' : 'back'), 'last'), $node)
130 ->jstree->select_node('#tree', '#tb-' . $self->text_block->id)
134 sub action_ajax_update {
135 my ($self, %params) = @_;
137 my $prefix = $::form->{form_prefix} || 'text_block';
138 my $attributes = $::form->{$prefix} || {};
140 foreach (qw(requirement_spec_id output_position position)) {
141 delete $attributes->{$_} if !defined $attributes->{$_};
144 my @errors = $self->text_block->assign_attributes(%{ $attributes })->validate;
145 return SL::ClientJS->new->error(@errors)->render($self) if @errors;
147 $self->text_block->save;
149 my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
152 ->remove('#' . $prefix . '_form')
153 ->replaceWith('#text-block-' . $self->text_block->id, $html)
154 ->jstree->rename_node('#tree', '#tb-' . $self->text_block->id, $self->text_block->title)
158 sub action_ajax_delete {
161 my $js = SL::ClientJS->new;
163 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
164 if ($self->text_block->output_position == $current_where) {
165 $js->remove('#edit_text_block_' . $self->text_block->id . '_form')
166 ->remove('#text-block-' . $self->text_block->id);
168 $js->show('#text-block-list-empty') if 1 == scalar @{ $self->text_block->get_full_list };
171 $self->text_block->delete;
173 $js->jstree->delete_node('#tree', '#tb-' . $self->text_block->id)
177 sub action_ajax_flag {
180 $self->text_block->update_attributes(is_flagged => !$self->text_block->is_flagged);
182 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
185 ->action_if($current_where == $self->text_block->output_position, 'toggleClass', '#text-block-' . $self->text_block->id, 'flagged')
186 ->toggleClass('#tb-' . $self->text_block->id, 'flagged')
190 sub action_dragged_and_dropped {
193 my $position = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position} : die "Unknown 'position' parameter";
194 my $dropped_text_block = $position =~ m/^ (?: before | after ) $/x ? SL::DB::RequirementSpecTextBlock->new(id => $::form->{dropped_id})->load : undef;
196 my $dropped_type = $position ne 'last' ? undef : $::form->{dropped_type} =~ m/^ text-blocks- (?:front|back) $/x ? $::form->{dropped_type} : die "Unknown 'dropped_type' parameter";
197 my $old_where = $self->text_block->output_position;
199 $self->text_block->db->do_transaction(sub {
201 $self->text_block->remove_from_list;
202 $self->text_block->output_position($position =~ m/before|after/ ? $dropped_text_block->output_position : $::form->{dropped_type} eq 'text-blocks-front' ? 0 : 1);
203 $self->text_block->add_to_list(position => $position, reference => $dropped_text_block ? $dropped_text_block->id : undef);
206 # $::lxdebug->dump(0, "form", $::form);
208 return $self->render(\'', { type => 'json' }) if $::form->{current_content_type} !~ m/^text-block/;
210 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
211 my $new_where = $self->text_block->output_position;
212 my $id = $self->text_block->id;
213 my $js = SL::ClientJS->new;
215 # $::lxdebug->message(0, "old $old_where current $current_where new $new_where current_CID " . $::form->{current_content_id} . ' selfid ' . $self->text_block->id);
216 if (($old_where != $new_where) && ($::form->{current_content_id} == $self->text_block->id)) {
217 # The currently selected text block is dragged to the opposite
218 # text block location. Re-render the whole content column.
219 my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $new_where ]);
220 my $html = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $new_where);
222 $js->val('#current_content_type', 'text-blocks-' . ($new_where == 0 ? 'front' : 'back'))
223 ->html('#column-content', $html);
226 if ($old_where == $current_where) {
227 $js->remove('#text-block-' . $self->text_block->id);
229 if (0 == scalar(@{ SL::DB::Manager::RequirementSpecTextBlock->get_all(where => [ requirement_spec_id => $self->text_block->requirement_spec_id, output_position => $current_where ]) })) {
230 $js->show('#text-block-list-empty');
234 if ($new_where == $current_where) {
235 $js->hide('#text-block-list-empty');
237 my $html = "" . $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
239 my $prior_text_block = $self->text_block->get_previous_in_list;
241 if ($prior_text_block) {
242 $js->insertAfter($html, '#text-block-' . $prior_text_block->id);
244 $js->prependTo($html, '#text-block-list');
252 sub action_ajax_copy {
253 my ($self, %params) = @_;
255 SL::Clipboard->new->copy($self->text_block);
256 SL::ClientJS->new->render($self);
259 sub action_ajax_paste {
260 my ($self, %params) = @_;
262 my $copied = SL::Clipboard->new->get_entry(qr/^RequirementSpecTextBlock$/);
264 return SL::ClientJS->new
265 ->error(t8("The clipboard does not contain anything that can be pasted here."))
269 my $js = SL::ClientJS->new;
270 my $current_output_position = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
271 my $new_output_position = $::form->{id} ? $self->output_position_from_id($::form->{id}) : $::form->{output_position};
272 my $front_back = 0 == $new_output_position ? 'front' : 'back';
274 $self->text_block($copied->to_object);
275 $self->text_block->update_attributes(requirement_spec_id => $::form->{requirement_spec_id}, output_position => $new_output_position);
276 $self->text_block->add_to_list(position => 'after', reference => $::form->{id}) if $::form->{id};
278 if ($current_output_position == $new_output_position) {
279 my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
280 $js->action($::form->{id} ? 'insertAfter' : 'appendTo', $html, '#text-block-' . ($::form->{id} || 'list'));
283 my $node = $self->presenter->requirement_spec_text_block_jstree_data($self->text_block);
284 $js->jstree->create_node('#tree', $::form->{id} ? ('#tb-' . $::form->{id}, 'after') : ("#tb-${front_back}", 'last'), $node)
292 sub load_requirement_spec {
294 $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load || die "No such requirement spec");
297 sub load_requirement_spec_text_block {
299 $self->text_block(SL::DB::RequirementSpecTextBlock->new(id => $::form->{id})->load || die "No such requirement spec text block");
306 sub output_position_from_id {
307 my ($self, $id, $type, %params) = @_;
310 return $1 eq 'front' ? 0 : 1 if $type =~ m/-(front|back)$/;
311 return undef if $type !~ m/text-block/;
314 my $text_block = $id ? SL::DB::Manager::RequirementSpecTextBlock->find_by(id => $id) : undef;
316 return $text_block ? $text_block->output_position : undef;
319 sub init_predefined_texts {
320 return SL::DB::Manager::RequirementSpecPredefinedText->get_all_sorted;