1 package SL::Controller::RequirementSpecTextBlock;
5 use parent qw(SL::Controller::Base);
12 use SL::DB::RequirementSpec;
13 use SL::DB::RequirementSpecPredefinedText;
14 use SL::DB::RequirementSpecTextBlock;
15 use SL::Helper::Flash;
16 use SL::Locale::String;
18 use Rose::Object::MakeMethods::Generic
20 scalar => [ qw(text_block) ],
21 'scalar --get_set_init' => [ qw(predefined_texts js) ],
24 __PACKAGE__->run_before('load_requirement_spec_text_block', only => [qw(ajax_edit ajax_update ajax_delete ajax_flag dragged_and_dropped ajax_copy)]);
30 sub action_ajax_list {
34 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
37 if ($::form->{clicked_type} =~ m/^text-blocks-(front|back)/) {
38 $new_where = $1 eq 'front' ? 0 : 1;
41 $new_where = $self->output_position_from_id($::form->{clicked_id});
44 # $::lxdebug->message(0, "cur $current_where new $new_where");
46 if (!defined($current_where) || ($new_where != $current_where)) {
47 my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $new_where, requirement_spec_id => $::form->{requirement_spec_id} ]);
48 my $html = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $new_where);
51 ->html('#column-content', $html)
52 ->val('#current_content_type', 'text-blocks-' . (0 == $new_where ? 'front' : 'back'))
53 ->val('#current_content_id', $::form->{clicked_id});
56 $self->render($self->js);
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 $self->js->html('#column-content', $html);
72 $self->add_new_text_block_form(output_position => $new_where, insert_after_id => $::form->{id});
74 $self->invalidate_version->render($self);
77 sub action_ajax_edit {
80 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
81 if ($self->text_block->output_position != $current_where) {
82 my $text_blocks = $self->text_block->get_full_list;
83 my $html = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $self->text_block->output_position);
86 ->html('#column-content', $html)
87 ->val('#current_content_type', 'text-block')
88 ->val('#current_content_id', $self->text_block->id);
91 my $html = $self->render('requirement_spec_text_block/_form', { output => 0 });
94 ->hide('#text-block-' . $self->text_block->id)
95 ->remove('#edit_text_block_' . $self->text_block->id . '_form')
96 ->insertAfter($html, '#text-block-' . $self->text_block->id)
97 ->jstree->select_node('#tree', '#tb-' . $self->text_block->id)
98 ->focus('#edit_text_block_' . $self->text_block->id . '_title')
102 sub action_ajax_create {
103 my ($self, %params) = @_;
105 my $attributes = $::form->{ $::form->{form_prefix} } || die "Missing attributes";
106 my $insert_after = delete $attributes->{insert_after};
108 my @errors = $self->text_block(SL::DB::RequirementSpecTextBlock->new(%{ $attributes }))->validate;
109 return SL::ClientJS->new->error(@errors)->render($self) if @errors;
111 $self->text_block->save;
112 $self->text_block->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
114 my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
115 my $node = $self->presenter->requirement_spec_text_block_jstree_data($self->text_block);
117 $self->invalidate_version
118 ->replaceWith('#' . $::form->{form_prefix} . '_form', $html)
119 ->jstree->create_node('#tree', $insert_after ? ('#tb-' . $insert_after, 'after') : ('#tb-' . ($attributes->{output_position} == 0 ? 'front' : 'back'), 'last'), $node)
120 ->jstree->select_node('#tree', '#tb-' . $self->text_block->id)
124 sub action_ajax_update {
125 my ($self, %params) = @_;
127 my $prefix = $::form->{form_prefix} || 'text_block';
128 my $attributes = $::form->{$prefix} || {};
130 foreach (qw(requirement_spec_id output_position position)) {
131 delete $attributes->{$_} if !defined $attributes->{$_};
134 my @errors = $self->text_block->assign_attributes(%{ $attributes })->validate;
135 return SL::ClientJS->new->error(@errors)->render($self) if @errors;
137 $self->text_block->save;
139 my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
141 $self->invalidate_version
142 ->remove('#' . $prefix . '_form')
143 ->replaceWith('#text-block-' . $self->text_block->id, $html)
144 ->jstree->rename_node('#tree', '#tb-' . $self->text_block->id, $self->text_block->title)
148 sub action_ajax_delete {
151 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
152 if ($self->text_block->output_position == $current_where) {
154 ->remove('#edit_text_block_' . $self->text_block->id . '_form')
155 ->remove('#text-block-' . $self->text_block->id);
157 $self->js->show('#text-block-list-empty') if 1 == scalar @{ $self->text_block->get_full_list };
160 $self->text_block->delete;
162 $self->invalidate_version
163 ->jstree->delete_node('#tree', '#tb-' . $self->text_block->id)
167 sub action_ajax_flag {
170 $self->text_block->update_attributes(is_flagged => !$self->text_block->is_flagged);
172 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
175 ->action_if($current_where == $self->text_block->output_position, 'toggleClass', '#text-block-' . $self->text_block->id, 'flagged')
176 ->toggleClass('#tb-' . $self->text_block->id, 'flagged')
180 sub action_dragged_and_dropped {
183 my $position = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position} : die "Unknown 'position' parameter";
184 my $dropped_text_block = $position =~ m/^ (?: before | after ) $/x ? SL::DB::RequirementSpecTextBlock->new(id => $::form->{dropped_id})->load : undef;
186 my $dropped_type = $position ne 'last' ? undef : $::form->{dropped_type} =~ m/^ text-blocks- (?:front|back) $/x ? $::form->{dropped_type} : die "Unknown 'dropped_type' parameter";
187 my $old_where = $self->text_block->output_position;
189 $self->text_block->db->do_transaction(sub {
191 $self->text_block->remove_from_list;
192 $self->text_block->output_position($position =~ m/before|after/ ? $dropped_text_block->output_position : $::form->{dropped_type} eq 'text-blocks-front' ? 0 : 1);
193 $self->text_block->add_to_list(position => $position, reference => $dropped_text_block ? $dropped_text_block->id : undef);
196 # $::lxdebug->dump(0, "form", $::form);
198 $self->invalidate_version
199 ->jstree->open_node('#tree', '#tb-' . (!$self->text_block->output_position ? 'front' : 'back'));
201 return $self->js->render($self) if $::form->{current_content_type} !~ m/^text-block/;
203 my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
204 my $new_where = $self->text_block->output_position;
205 my $id = $self->text_block->id;
207 # $::lxdebug->message(0, "old $old_where current $current_where new $new_where current_CID " . $::form->{current_content_id} . ' selfid ' . $self->text_block->id);
208 if (($old_where != $new_where) && ($::form->{current_content_id} == $self->text_block->id)) {
209 # The currently selected text block is dragged to the opposite
210 # text block location. Re-render the whole content column.
211 my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $new_where ]);
212 my $html = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $new_where);
215 ->val('#current_content_type', 'text-blocks-' . ($new_where == 0 ? 'front' : 'back'))
216 ->html('#column-content', $html);
219 if ($old_where == $current_where) {
220 $self->js->remove('#text-block-' . $self->text_block->id);
222 if (0 == scalar(@{ SL::DB::Manager::RequirementSpecTextBlock->get_all(where => [ requirement_spec_id => $self->text_block->requirement_spec_id, output_position => $current_where ]) })) {
223 $self->js->show('#text-block-list-empty');
227 if ($new_where == $current_where) {
228 $self->js->hide('#text-block-list-empty');
230 my $html = "" . $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
232 my $prior_text_block = $self->text_block->get_previous_in_list;
234 if ($prior_text_block) {
235 $self->js->insertAfter($html, '#text-block-' . $prior_text_block->id);
237 $self->js->prependTo($html, '#text-block-list');
242 $self->js->render($self);
245 sub action_ajax_copy {
246 my ($self, %params) = @_;
248 SL::Clipboard->new->copy($self->text_block);
249 SL::ClientJS->new->render($self);
252 sub action_ajax_paste {
253 my ($self, %params) = @_;
255 my $copied = SL::Clipboard->new->get_entry(qr/^RequirementSpecTextBlock$/);
257 return SL::ClientJS->new
258 ->error(t8("The clipboard does not contain anything that can be pasted here."))
262 my $current_output_position = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
263 my $new_output_position = $::form->{id} ? $self->output_position_from_id($::form->{id}) : $::form->{output_position};
264 my $front_back = 0 == $new_output_position ? 'front' : 'back';
266 $self->text_block($copied->to_object);
267 $self->text_block->update_attributes(requirement_spec_id => $::form->{requirement_spec_id}, output_position => $new_output_position);
268 $self->text_block->add_to_list(position => 'after', reference => $::form->{id}) if $::form->{id};
270 if ($current_output_position == $new_output_position) {
271 my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
272 $self->js->action($::form->{id} ? 'insertAfter' : 'appendTo', $html, '#text-block-' . ($::form->{id} || 'list'));
275 my $node = $self->presenter->requirement_spec_text_block_jstree_data($self->text_block);
276 $self->invalidate_version
277 ->jstree->create_node('#tree', $::form->{id} ? ('#tb-' . $::form->{id}, 'after') : ("#tb-${front_back}", 'last'), $node)
285 sub load_requirement_spec_text_block {
287 $self->text_block(SL::DB::RequirementSpecTextBlock->new(id => $::form->{id})->load || die "No such requirement spec text block");
294 sub output_position_from_id {
295 my ($self, $id, $type, %params) = @_;
298 return $1 eq 'front' ? 0 : 1 if $type =~ m/-(front|back)$/;
299 return undef if $type !~ m/text-block/;
302 my $text_block = $id ? SL::DB::Manager::RequirementSpecTextBlock->find_by(id => $id) : undef;
304 return $text_block ? $text_block->output_position : undef;
307 sub init_predefined_texts {
308 return SL::DB::Manager::RequirementSpecPredefinedText->get_all_sorted;
313 $self->js(SL::ClientJS->new);
316 sub invalidate_version {
319 my $html = $self->render('requirement_spec/_version', { output => 0 },
320 requirement_spec => SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id} || $self->text_block->requirement_spec_id)->load);
321 return $self->js->html('#requirement_spec_version', $html);
324 sub add_new_text_block_form {
325 my ($self, %params) = @_;
327 croak "Missing parameter output_position" unless defined($params{output_position}) && ($params{output_position} ne '');
329 $self->text_block(SL::DB::RequirementSpecTextBlock->new(
330 requirement_spec_id => $::form->{requirement_spec_id},
331 output_position => $params{output_position},
334 my $id_base = join('_', 'new_text_block', Time::HiRes::gettimeofday(), int rand 1000000000000);
335 my $html = $self->render('requirement_spec_text_block/_form', { output => 0 }, id_base => $id_base, insert_after => $params{insert_after_id});
338 ->action($params{insert_after_id} ? 'insertAfter' : 'appendTo', $html, '#text-block-' . ($params{insert_after_id} || 'list'))
339 ->focus('#' . $id_base . '_title');