Jifty/Web/Form/Clickable.pm

filter: Statementstmt 8.8% 17 / 193 Branchbran 0.0% 0 / 84 Conditioncond 0.0% 0 / 59 MC/DCmcdc 0.0% 0 / 33 Subroutinesub 22.7% 5 / 22 Podpod 100% 14 / 14 totaltotal 8.8% 36 / 405 CC 74 SCAR 52.0
CC
74
Cov
5%
generate
506.0
new
272.0
region_argument
72.0
CRAP 182.1
« Jifty/Request.pm ↑ Summary Jifty/Object.pm »
1 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Jifty::Web::Form::Clickable;
5 1 ▶use Class::Trigger;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Jifty::Web::Form::Clickable - Some item that can be clicked on --
11 either a button or a link.
12  
13 =head1 DESCRIPTION
14  
15 =cut
16  
17 1 ▶use base 'Jifty::Web::Form::Element';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =head1 METHODS
20  
21 =head2 accessors
22  
23 Clickable adds C<url>, C<escape_label>, C<continuation>, C<call>,
24 C<returns>, C<submit>, and C<preserve_state> to the list of accessors
25 and mutators, in addition to those offered by
26 L<Jifty::Web::Form::Element/accessors>.
27  
28 =cut
29  
30 0 ▶sub accessors {
Subroutine accessors: not called CC=1 SCAR=6.9
Pod documented
31 shift->SUPER::accessors,
32 0 qw(url escape_label tooltip continuation call returns submit target preserve_state render_as_button render_as_link);
33 }
34 1 __PACKAGE__->mk_accessors(
35 qw(url escape_label tooltip continuation call returns submit target preserve_state render_as_button render_as_link)
36 );
37  
38 =head2 new PARAMHASH
39  
40 Creates a new L<Jifty::Web::Form::Clickable> object. Depending on the
41 requirements, it may render as a link or as a button. Possible
42 parameters in the I<PARAMHASH> are:
43  
44 =over 4
45  
46 =item url
47  
48 Sets the page that the user will end up on after they click the
49 button. Defaults to the current page.
50  
51 =item label
52  
53 The text on the clickable object.
54  
55 =item tooltip
56  
57 Additional information about the link target.
58  
59 =item escape_label
60  
61 If set to true, HTML escapes the content of the label and tooltip before
62 displaying them. This is only relevant for objects that are rendered as
63 HTML links. The default is true.
64  
65 =item continuation
66  
67 The current continuation for the link. Defaults to the current
68 continuation now, if there is one. This may be either a
69 L<Jifty::Continuation> object, or the C<id> of such.
70  
71 =item call
72  
73 The continuation to call when the link is clicked. This will happen
74 after actions have run, if any. Like C<continuation>, this may be a
75 L<Jifty::Continuation> object or the C<id> of such.
76  
77 =item returns
78  
79 Passing this parameter implies the creation of a continuation when the
80 link is clicked. It takes an anonymous hash of return location to
81 where the return value is pulled from -- that is, the same structure
82 the C<parameters> method takes.
83  
84 See L<Jifty::Request::Mapper/query_parameters> for details.
85  
86 =item submit
87  
88 A list of actions to run when the object is clicked. This may be an
89 array reference or a single element; each element may either be a
90 moniker or, a L<Jifty::Action> or a hashref with the keys 'action' and 'arguments'.
91 An undefined value submits B<all> actions in the form, an empty list
92 reference (the default) submits none.
93  
94 In the most complex case, you have something like this:
95  
96 submit => [
97 { action => $my_action,
98 arguments => {
99 name => 'Default McName',
100 age => '23'
101 },
102 },
103 $my_other_action,
104 'some-other-action-moniker'
105 ]
106  
107 If you specify arguments in the submit block for a button, they will override
108 any values from form fields submitted by the user.
109  
110  
111 =item preserve_state
112  
113 A boolean; whether state variables are preserved across the link.
114 Defaults to true if there are any AJAX actions on the link, false
115 otherwise.
116  
117 =item parameters
118  
119 A hash reference of query parameters that go on the link or button.
120 These will end up being submitted exactly like normal query
121 parameters.
122  
123 =item as_button
124  
125 By default, Jifty will attempt to make the clickable into a link
126 rather than a button, if there are no actions to run on submit.
127 Providing a true value for C<as_button> forces L<generate> to produce
128 a L<Jifty::Web::Form::Clickable::InlineButton> instead of a
129 L<Jifty::Web::Form::Link>.
130  
131 =item as_link
132  
133 Attempt to rework a button into displaying as a link -- note that this
134 only works in javascript browsers. Supplying B<both> C<as_button> and
135 C<as_link> will work, and not as perverse as it might sound at first
136 -- it allows you to make any simple GET request into a POST request,
137 while still appearing as a link (a GET request).
138  
139 =item target
140  
141 For things that start off as links, give them an html C<target> attribute.
142  
143 =cut
144  
145 =item Anything from L<Jifty::Web::Form::Element>
146  
147 Note that this includes the C<onclick> parameter, which allows
148 you to attach javascript to your Clickable object, but be careful
149 that your Javascript looks like C<return someFunction();>, or you may
150 get an unexpected error from your browser.
151  
152 =back
153  
154 =cut
155  
156 0 ▶sub new {
Subroutine new: not called CC=16 SCAR=56.1
Pod documented
157 0 my $class = shift;
158  
159 0 my %args = (
160 parameters => {},
161 @_,
162 );
163  
164 0 $class->call_trigger( 'before_new', \%args );
165  
166 0 $args{render_as_button} = delete $args{as_button};
167 0 $args{render_as_link} = delete $args{as_link};
168  
169 0 my $self = $class->SUPER::new(
170 { class => '',
171 label => 'Click me!',
172 url => Jifty->web->request->top_request->path,
173 escape_label => 1,
174 tooltip => '',
175 continuation => Jifty->web->request->continuation,
176 submit => [],
177 preserve_state => 0,
178 parameters => {},
179 render_as_button => 0,
180 render_as_link => 0,
181 %args,
182 },
183 );
184  
185 0 for (qw/continuation call/) {
186 0 ▶ $self->{$_} = $self->{$_}->id if $self->{$_} and ref $self->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{$_} and ref $self->{$_} 0 0 0
Condition0%0/3
$self->{$_} and ref $self->{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{$_} and ref $self->{$_}
$self->{$_} ref $self->{$_}
Truth table0%0/3
A and B
A= $self->{$_}B= ref $self->{$_}
ABresult
0X0
100
111
187 }
188  
189 0 ▶ if ( $self->{submit} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"submit"}) 0 0 0
190 $self->{submit} = [ $self->{submit} ]
191 0 ▶ unless ref $self->{submit} eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"submit"} eq "ARRAY" 0 0 0
192  
193 0 my @submit_temp = ();
194 0 foreach my $submit ( @{ $self->{submit} } ) {
195  
196 # If we have been handed an action moniker to submit, just submit that.
197 0 ▶ if ( !ref($submit) ) { push @submit_temp, $submit }
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $submit) { } 0 0 0
elsif (builtin::blessed($submit)) { } 0 0 0
198  
199 # We've been handed a Jifty::Action to submit
200 elsif ( blessed($submit) ) {
201 0 push @submit_temp, $submit->moniker;
202 0 $self->register_action($submit);
203 }
204  
205 # We've been handed a hashref which contains an action and arguments
206 else {
207  
208 # Add whatever additional arguments they've requested to the button
209 $args{parameters}{ $submit->{'action'}->form_field_name($_) }
210 = $submit->{arguments}{$_}
211 0 for keys %{ $submit->{arguments} };
212  
213 # Add the action's moniker to the submit
214 0 push @submit_temp, $submit->{'action'}->moniker;
215 0 $self->register_action($submit->{'action'});
216 }
217 }
218  
219 0 @{ $self->{submit} } = @submit_temp;
220 }
221  
222 # Anything doing fragment replacement needs to preserve the
223 # current state as well
224 0 ▶ if ( grep { $self->$_ } $self->handlers_used or $self->preserve_state ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$self->$_;} $self->handlers_used or $self->preserve_state) 0 0 0
Condition0%0/3
grep {$self->$_;} $self->handlers_used or $self->preserve_state
l!l&&r!l&&!r
000
MC/DC0%0/2
grep {$self->$_;} $self->handlers_used or $self->preserve_state
grep {$self->$_;} $self->handlers_used $self->preserve_state
Truth table0%0/3
A or B
A= grep {$self->$_;} $self->handlers_usedB= $self->preserve_state
ABresult
1X1
011
000
225 0 my %state_vars = Jifty->web->state_variables;
226 0 ▶ while ( my ( $key, $val ) = each %state_vars ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %state_vars) 0 0 0
227 0 ▶ if ( $key =~ /^region-(.*?)\.(.*)$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key =~ /^region-(.*?)\.(.*)$/) { } 0 0 0
elsif ($key =~ /^region-(.*)$/) { } 0 0 0
228 0 $self->region_argument( $1, $2 => $val );
229 } elsif ( $key =~ /^region-(.*)$/ ) {
230 0 $self->region_fragment( $1, $val );
231 } else {
232 0 $self->state_variable( $key => $val );
233 }
234 }
235 }
236  
237 $self->parameter( $_ => $args{parameters}{$_} )
238 0 for keys %{ $args{parameters} };
239  
240 0 return $self;
241 }
242  
243 =head2 url [VALUE]
244  
245 Gets or sets the page that the user will end up on after they click
246 the button. Defaults to the current page.
247  
248 =head2 label [VALUE]
249  
250 Gets or sets the text on the clickable object.
251  
252 =head2 escape_label [VALUE]
253  
254 Gets or sets if the label is escaped. This is only relevant for
255 objects that are rendered as HTML links. The default is true.
256  
257 =head2 continuation [VALUE]
258  
259 Gets or sets the current continuation for the link. Defaults to the
260 current continuation now, if there is one. This may be either a
261 L<Jifty::Continuation> object, or the C<id> of such.
262  
263 =head2 call [VALUE]
264  
265 Gets or sets the continuation to call when the link is clicked. This
266 will happen after actions have run, if any. Like C<continuation>,
267 this may be a L<Jifty::Continuation> object or the C<id> of such.
268  
269 =head2 returns [VALUE]
270  
271 Gets or sets the return value mapping from the continuation. See
272 L<Jifty::Request::Mapper> for details.
273  
274 =head2 submit [VALUE]
275  
276 Gets or sets the list of actions to run when the object is clicked.
277 This may be an array reference or a single element; each element may
278 either be a moniker or a L<Jifty::Action>. An undefined value submits
279 B<all> actions in the form, an empty list reference (the default)
280 submits none.
281  
282 =head2 preserve_state [VALUE]
283  
284 Gets or sets whether state variables are preserved across the link.
285 Defaults to true if there are any AJAX actions on the link, false
286 otherwise.
287  
288 =head2 parameter KEY VALUE
289  
290 Sets the given HTTP parameter named C<KEY> to the given C<VALUE>.
291  
292 =cut
293  
294 0 ▶sub parameter {
Subroutine parameter: not called CC=1 SCAR=6.9
Pod documented
295 0 my $self = shift;
296 0 my ( $key, $value ) = @_;
297 0 $self->{parameters}{$key} = $value;
298 }
299  
300 =head2 state_variable KEY VALUE
301  
302 Sets the state variable named C<KEY> to C<VALUE>.
303  
304 =cut
305  
306 0 ▶sub state_variable {
Subroutine state_variable: not called CC=4 SCAR=30.0
Pod documented
307 0 my $self = shift;
308 0 ▶ defined $self->call_trigger( 'before_state_variable', @_ )
Branch: 1 branch
BranchTrueFalseTotal
defined $self->call_trigger("before_state_variable", @_) or return 0 0 0
309 or return; # if aborted by trigger
310  
311 0 my ( $key, $value, $fallback ) = @_;
312 0 ▶ if ( defined $value and length $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and length $value) { } 0 0 0
Condition0%0/3
defined $value and length $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and length $value
defined $value length $value
Truth table0%0/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
313 0 $self->{state_variable}{"J:V-$key"} = $value;
314 } else {
315 0 delete $self->{state_variable}{"J:V-$key"};
316 0 $self->{fallback}{"J:V-$key"} = $fallback;
317 }
318 }
319  
320 =head2 region_fragment NAME PATH
321  
322 Sets the path of the fragment named C<NAME> to be C<PATH>.
323  
324 =cut
325  
326 0 ▶sub region_fragment {
Subroutine region_fragment: not called CC=4 SCAR=30.0
Pod documented
327 0 my $self = shift;
328 0 my ( $region, $fragment ) = @_;
329  
330 0 ▶ my $name = ref $region ? $region->qualified_name : $region;
Branch: 1 branch
BranchTrueFalseTotal
ref $region ? : 0 0 0
331 0 my $defaults = Jifty->web->get_region($name);
332  
333 0 ▶ if ( $defaults and $fragment eq $defaults->default_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defaults and $fragment eq $defaults->default_path) { } 0 0 0
Condition0%0/3
$defaults and $fragment eq $defaults->default_path
!ll&&!rl&&r
000
MC/DC0%0/2
$defaults and $fragment eq $defaults->default_path
$defaults $fragment eq $defaults->default_path
Truth table0%0/3
A and B
A= $defaultsB= $fragment eq $defaults->default_path
ABresult
0X0
100
111
334 0 $self->state_variable( "region-$name" => undef, $fragment );
335 } else {
336 0 $self->state_variable( "region-$name" => $fragment );
337 }
338 }
339  
340 =head2 region_argument NAME ARG VALUE
341  
342 Sets the value of the C<ARG> argument on the fragment named C<NAME> to
343 C<VALUE>.
344  
345 =cut
346  
347 0 ▶sub region_argument {
Subroutine region_argument: not called CC=8 SCAR=42.8
Pod documented
348 0 my $self = shift;
349 0 my ( $region, $argument, $value ) = @_;
350  
351 0 ▶ my $name = ref $region ? $region->qualified_name : $region;
Branch: 1 branch
BranchTrueFalseTotal
ref $region ? : 0 0 0
352 0 my $defaults = Jifty->web->get_region($name);
353 0 ▶ my $default = $defaults ? $defaults->default_argument($argument) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$defaults ? : 0 0 0
354  
355 0 ▶ if ( ( not defined $default and not defined $value )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $default || defined $value or defined $default and defined $value and $default eq $value) { } 0 0 0
Condition0%0/12
not defined $default || defined $value or defined $default and defined $value and $default eq $value
l!l&&r!l&&!r
000
defined $default || defined $value
l!l&&r!l&&!r
000
defined $default and defined $value and $default eq $value
!ll&&!rl&&r
000
defined $default and defined $value
!ll&&!rl&&r
000
MC/DC0%0/5
not defined $default || defined $value or defined $default and defined $value and $default eq $value
defined $default defined $value defined $default defined $value $default eq $value
Truth table0%0/9
not(A || B) or C and D and E
A= defined $defaultB= defined $valueC= defined $defaultD= defined $valueE= $default eq $value
ABCDEresult
00XXX1
1X1111
011111
1X0XX0
1X10X0
1X1100
010XX0
0110X0
011100
356 or ( defined $default and defined $value and $default eq $value ) )
357 {
358 0 $self->state_variable( "region-$name.$argument" => undef, $value );
359 } else {
360 0 $self->state_variable( "region-$name.$argument" => $value );
361 }
362  
363 }
364  
365 # Query-map any complex structures
366 0 ▶sub _map {
Subroutine _map: not called CC=2 SCAR=17.9
367 0 my %old_args = @_;
368 0 my %new_args;
369  
370 0 ▶ while ( my ( $key, $val ) = each %old_args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %old_args) 0 0 0
371 0 my ( $new_key, $new_val )
372 = Jifty::Request::Mapper->query_parameters( $key => $val );
373 0 $new_args{$new_key} = $new_val;
374 }
375  
376 0 return %new_args;
377 }
378  
379 =head2 parameters
380  
381 Returns the generic list of HTTP form parameters attached to the link as a hash.
382 Use of this is discouraged in favor or L</post_parameters> and
383 L</get_parameters>.
384  
385 =cut
386  
387 0 ▶sub parameters {
Subroutine parameters: not called CC=6 SCAR=37.4
Pod documented
388 0 my $self = shift;
389  
390 0 my %parameters;
391  
392 0 ▶ if ( $self->returns ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->returns) { } 0 0 0
393 %parameters
394 0 = Jifty::Request::Mapper->query_parameters( %{ $self->returns } );
395 0 $parameters{"J:CREATE"} = 1;
396 $parameters{"J:PATH"} = Jifty::Web::Form::Clickable->new(
397 url => $self->url,
398 parameters => $self->{parameters},
399 0 continuation => undef,
400 )->complete_url;
401 } else {
402 0 %parameters = %{ $self->{parameters} };
403 }
404  
405 0 ▶ %parameters = _map( %{ $self->{state_variable} || {} }, %parameters );
Condition0%0/2
$self->{'state_variable'} || {}
l!l
00
MC/DC0%0/1
$self->{'state_variable'} || {}
$self->{'state_variable'}
Truth table0%0/2
A
A= $self->{'state_variable'}
Aresult
11
00
406  
407 0 ▶ $parameters{"J:CALL"} = $self->call
Branch: 1 branch
BranchTrueFalseTotal
if $self->call 0 0 0
408 if $self->call;
409  
410 0 ▶ $parameters{"J:C"} = $self->continuation
Branch: 1 branch
BranchTrueFalseTotal
if $self->continuation and not $self->call 0 0 0
Condition0%0/3
$self->continuation and not $self->call
!ll&&!rl&&r
000
MC/DC0%0/2
$self->continuation and not $self->call
$self->continuation not $self->call
Truth table0%0/3
A and not(B)
A= $self->continuationB= not $self->call
ABresult
0X0
100
111
411 if $self->continuation
412 and not $self->call;
413  
414 0 return %parameters;
415 }
416  
417 =head2 post_parameters
418  
419 Returns the hash of parameters as they would be needed on a POST
420 request.
421  
422 =cut
423  
424 0 ▶sub post_parameters {
Subroutine post_parameters: not called CC=7 SCAR=40.3
Pod documented
425 0 my $self = shift;
426  
427 my %parameters
428 0 ▶ = ( _map( %{ $self->{fallback} || {} } ), $self->parameters );
Condition0%0/2
$self->{'fallback'} || {}
l!l
00
MC/DC0%0/1
$self->{'fallback'} || {}
$self->{'fallback'}
Truth table0%0/2
A
A= $self->{'fallback'}
Aresult
11
00
429  
430 # Submit actions should only show up once
431 0 my %uniq;
432 0 ▶ $self->submit( [ grep { not $uniq{$_}++ } @{ $self->submit } ] )
Branch: 1 branch
BranchTrueFalseTotal
if $self->submit 0 0 0
433 if $self->submit;
434  
435 # Add a redirect, if this isn't to the right page
436 0 ▶ if ( $self->url ne Jifty->web->request->top_request->path and not $self->returns ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->url ne 'Jifty'->web->request->top_request->path and not $self->returns) { } 0 0 0
Condition0%0/3
$self->url ne "Jifty"->web->request->top_request->path and not $self->returns
!ll&&!rl&&r
000
MC/DC0%0/2
$self->url ne "Jifty"->web->request->top_request->path and not $self->returns
$self->url ne "Jifty"->web->request->top_request->path not $self->returns
Truth table0%0/3
A and not(B)
A= $self->url ne "Jifty"->web->request->top_request->pathB= not $self->returns
ABresult
0X0
100
111
437 0 Jifty::Util->require('Jifty::Action::Redirect');
438 0 my $redirect = Jifty::Action::Redirect->new(
439 arguments => { url => $self->url } );
440 0 $parameters{ $redirect->register_name } = ref $redirect;
441 0 $parameters{ $redirect->form_field_name('url') } = $self->url;
442 $parameters{"J:ACTIONS"}
443 0 ▶ = join( '!', @{ $self->submit }, $redirect->moniker )
Branch: 1 branch
BranchTrueFalseTotal
if $self->submit 0 0 0
444 if $self->submit;
445 } else {
446 0 ▶ $parameters{"J:ACTIONS"} = join( '!', @{ $self->submit } )
Branch: 1 branch
BranchTrueFalseTotal
if $self->submit 0 0 0
447 if $self->submit;
448 }
449  
450 0 return %parameters;
451 }
452  
453 =head2 get_parameters
454  
455 Returns the hash of parameters as they would be needed on a GET
456 request.
457  
458 =cut
459  
460 0 ▶sub get_parameters {
Subroutine get_parameters: not called CC=1 SCAR=6.9
Pod documented
461 0 my $self = shift;
462  
463 0 my %parameters = $self->parameters;
464  
465 0 return %parameters;
466 }
467  
468 =head2 complete_url
469  
470 Returns the complete GET URL, as it would appear on a link.
471  
472 =cut
473  
474 0 ▶sub complete_url {
Subroutine complete_url: not called CC=6 SCAR=37.4
Pod documented
475 0 my $self = shift;
476  
477 0 my %parameters = $self->get_parameters;
478  
479 0 ▶ my $url = $self->returns ? URI->new(Jifty->web->request->request_uri)->path : $self->url;
Branch: 1 branch
BranchTrueFalseTotal
$self->returns ? : 0 0 0
480  
481 # XXX TODO: This should really construct a URI object and call some
482 # setters, but what's the perf hit there? This method is used all over the
483 # place.
484 0 ▶ if (%parameters) {
Branch: 1 branch
BranchTrueFalseTotal
if (%parameters) 0 0 0
485 # The query string can't come after the hash fragment.
486 0 ▶ my $hash = $url =~ s/(#.*)$// ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$url =~ s/(#.*)$// ? : 0 0 0
487 0 ▶ $url .= ( $url =~ /\?/ ) ? ";" : "?";
Branch: 1 branch
BranchTrueFalseTotal
$url =~ /\?/ ? : 0 0 0
488 0 $url .= Jifty->web->query_string(%parameters);
489 0 ▶ $url .= $hash if $hash;
Branch: 1 branch
BranchTrueFalseTotal
if $hash 0 0 0
490 }
491  
492 0 return $url;
493 }
494  
495 0 ▶sub _defined_accessor_values {
Subroutine _defined_accessor_values: not called CC=3 SCAR=24.8
496 0 my $self = shift;
497 # Note we're walking around Class::Accessor here
498 0 ▶ return { map { my $val = $self->{"_$_"} || $self->{$_}; defined $val ? ( $_ => $val ) : () }
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 0 0 0
Condition0%0/3
$$self{"_$_"} || $self->{$_}
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{"_$_"} || $self->{$_}
$$self{"_$_"} $self->{$_}
Truth table0%0/3
A || B
A= $$self{"_$_"}B= $self->{$_}
ABresult
1X1
011
000
499 $self->SUPER::accessors };
500 }
501  
502 =head2 as_link
503  
504 Returns the clickable as a L<Jifty::Web::Form::Link>, if possible.
505 Use of this method is discouraged in favor of L</generate>, which can
506 better determine if a link or a button is more appropriate.
507  
508 =cut
509  
510 0 ▶sub as_link {
Subroutine as_link: not called CC=1 SCAR=6.9
Pod documented
511 0 my $self = shift;
512  
513 0 my $args = $self->_defined_accessor_values;
514 0 my $link = Jifty::Web::Form::Link->new(
515 { %$args,
516 escape_label => $self->escape_label,
517 url => $self->complete_url,
518 target => $self->target,
519 continuation => $self->_continuation,
520 @_
521 }
522 );
523 0 return $link;
524 }
525  
526 0 ▶sub _continuation {
Subroutine _continuation: not called CC=3 SCAR=24.8
527  
528 # continuation info used by the update() call on client side
529 0 my $self = shift;
530 0 ▶ if ( $self->call ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->call) 0 0 0
531 0 return { 'type' => 'call', id => $self->call };
532 }
533 0 ▶ if ( $self->returns ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->returns) 0 0 0
534 0 return { 'create' => $self->url };
535 }
536  
537 0 return {};
538 }
539  
540 =head2 as_button
541  
542 Returns the clickable as a L<Jifty::Web::Form::Field::InlineButton>,
543 if possible. Use of this method is discouraged in favor of
544 L</generate>, which can better determine if a link or a button is more
545 appropriate.
546  
547 =cut
548  
549 0 ▶sub as_button {
Subroutine as_button: not called CC=1 SCAR=6.9
Pod documented
550 0 my $self = shift;
551  
552 0 my $args = $self->_defined_accessor_values;
553 0 my $field = Jifty::Web::Form::Field->new(
554 { %$args,
555 type => 'InlineButton',
556 continuation => $self->_continuation,
557 title => $self->tooltip,
558 @_
559 }
560 );
561 0 my %parameters = $self->post_parameters;
562  
563 $field->input_name(
564 join "|",
565 0 map { $_ . "=" . $parameters{$_} }
566 0 grep { defined $parameters{$_} } keys %parameters
567 );
568 0 $field->name( join '|', keys %{ $args->{parameters} } );
569 0 $field->button_as_link( $self->render_as_link );
570  
571 0 return $field;
572 }
573  
574 =head2 generate
575  
576 Returns a L<Jifty::Web::Form::Field::InlineButton> or
577 I<Jifty::Web::Form::Link>, whichever is more appropriate given the
578 parameters.
579  
580 =cut
581  
582 ## XXX TODO: This code somewhat duplicates hook-handling logic in
583 ## Element.pm, in terms of handling shortcuts like
584 ## 'refresh_self'. Some of the logic should probably be unified.
585  
586 0 ▶sub generate {
Subroutine generate: not called CC=22 SCAR=62.3
Pod documented
587 0 my $self = shift;
588 0 my $web = Jifty->web;
589 0 for my $trigger ( $self->handlers_used ) {
590 0 my $value = $self->$trigger;
591 0 ▶ next unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 0 0
592 0 my @hooks = @{$value};
593 0 for my $hook (@hooks) {
594 0 ▶ next unless ref $hook eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hook eq "HASH" 0 0 0
595 $hook->{region} ||= $hook->{refresh}
596 0 ▶ || $web->qualified_region;
Condition0%0/6
$hook->{"region"} ||= $hook->{"refresh"} || $web->qualified_region
l!l&&r!l&&!r
000
$hook->{'refresh'} || $web->qualified_region
l!l&&r!l&&!r
000
MC/DC0%0/3
$hook->{"region"} ||= $hook->{"refresh"} || $web->qualified_region
$hook->{"region"} $hook->{'refresh'} $web->qualified_region
Truth table0%0/4
A ||= B || C
A= $hook->{"region"}B= $hook->{'refresh'}C= $web->qualified_region
ABCresult
1XX1
01X1
0011
0000
597  
598 my $region
599 = ref $hook->{region}
600 ? $hook->{region}
601 0 ▶ : $web->get_region( $hook->{region} );
Branch: 1 branch
BranchTrueFalseTotal
ref $hook->{'region'} ? : 0 0 0
602  
603 0 ▶ if ( $hook->{replace_with} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hook->{"replace_with"}) 0 0 0
604 0 my $currently_shown = '';
605 0 ▶ if ($region) {
Branch: 1 branch
BranchTrueFalseTotal
if ($region) 0 0 0
606  
607 0 my $state_var = $web->request->state_variable(
608 "region-" . $region->qualified_name );
609 0 ▶ $currently_shown = $state_var->value if ($state_var);
Branch: 1 branch
BranchTrueFalseTotal
if $state_var 0 0 0
610 }
611  
612 # Toggle region if the toggle flag is set, and clicking wouldn't change path
613 0 ▶ if ( $hook->{toggle}
Branch: 1 branch
BranchTrueFalseTotal
if ($hook->{'toggle'} and $hook->{'replace_with'} eq $currently_shown) { } 0 0 0
Condition0%0/3
$hook->{"toggle"} and $hook->{"replace_with"} eq $currently_shown
!ll&&!rl&&r
000
MC/DC0%0/2
$hook->{"toggle"} and $hook->{"replace_with"} eq $currently_shown
$hook->{"toggle"} $hook->{"replace_with"} eq $currently_shown
Truth table0%0/3
A and B
A= $hook->{"toggle"}B= $hook->{"replace_with"} eq $currently_shown
ABresult
0X0
100
111
614 and $hook->{replace_with} eq $currently_shown )
615 {
616 $self->region_fragment( $hook->{region},
617 0 "/__jifty/empty" );
618  
619 } else {
620 $self->region_fragment( $hook->{region},
621 0 $hook->{replace_with} );
622 }
623  
624 }
625 $self->region_argument( $hook->{region}, $_ => $hook->{args}{$_} )
626 0 for keys %{ $hook->{args} };
627 0 ▶ if ( $hook->{submit} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hook->{"submit"}) 0 0 0
628 0 ▶ $self->{submit} ||= [];
Condition0%0/2
$self->{"submit"} ||= []
l!l
00
MC/DC0%0/1
$self->{"submit"} ||= []
$self->{"submit"}
Truth table0%0/2
A
A= $self->{"submit"}
Aresult
11
00
629 0 for my $moniker ( @{ $hook->{submit} } ) {
630 0 my $action = $web->{'actions'}{$moniker};
631 0 $self->register_action($action);
632 $self->parameter( $action->form_field_name($_),
633 $hook->{action_arguments}{$moniker}{$_} )
634 0 for
635 0 ▶ keys %{ $hook->{action_arguments}{$moniker} || {} };
Condition0%0/2
$hook->{'action_arguments'}{$moniker} || {}
l!l
00
MC/DC0%0/1
$hook->{'action_arguments'}{$moniker} || {}
$hook->{'action_arguments'}{$moniker}
Truth table0%0/2
A
A= $hook->{'action_arguments'}{$moniker}
Aresult
11
00
636 }
637 0 push @{ $self->{submit} }, @{ $hook->{submit} };
638 }
639 }
640 }
641  
642 return (
643 ( not( $self->submit )
644 0 ▶ || @{ $self->submit }
Branch: 1 branch
BranchTrueFalseTotal
!$self->submit || @{$self->submit;} || $self->render_as_button ? : 0 0 0
Condition0%0/6
not $self->submit or @{$self->submit;} or $self->render_as_button
l!l&&r!l&&!r
000
not $self->submit or @{$self->submit;}
l!l&&r!l&&!r
000
MC/DC0%0/3
not $self->submit or @{$self->submit;} or $self->render_as_button
not $self->submit @{$self->submit;} $self->render_as_button
Truth table0%0/4
not(A) or B or C
A= not $self->submitB= @{$self->submit;}C= $self->render_as_button
ABCresult
1XX1
01X1
0011
0000
645 || $self->render_as_button
646 )
647 ? $self->as_button(@_)
648 : $self->as_link(@_)
649 );
650 }
651  
652 =head2 register_action ACTION
653  
654 Registers the action if it isn't registered already, but only on the
655 link. That is, the registration will not be seen by any other buttons
656 in the form.
657  
658 =cut
659  
660 0 ▶sub register_action {
Subroutine register_action: not called CC=4 SCAR=30.0
Pod documented
661 0 my $self = shift;
662 0 my ($action) = @_;
663 0 ▶ return if Jifty->web->form->actions->{ $action->moniker };
Branch: 1 branch
BranchTrueFalseTotal
if "Jifty"->web->form->actions->{$action->moniker} 0 0 0
664  
665 0 my $arguments = $action->arguments;
666 0 $self->parameter( $action->register_name, ref $action );
667 $self->parameter( $action->fallback_form_field_name($_),
668 $action->argument_value($_) || $arguments->{$_}->{'default_value'} )
669 0 ▶ for grep { $arguments->{$_}{constructor} } keys %{$arguments};
Condition0%0/3
$action->argument_value($_) || $arguments->{$_}{'default_value'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$action->argument_value($_) || $arguments->{$_}{'default_value'}
$action->argument_value($_) $arguments->{$_}{'default_value'}
Truth table0%0/3
A || B
A= $action->argument_value($_)B= $arguments->{$_}{'default_value'}
ABresult
1X1
011
000
670 }
671  
672 1 1;
« Jifty/Request.pm ↑ Summary Jifty/Object.pm »