Jifty/Web/Form/Element.pm

filter: Statementstmt 17.7% 34 / 192 Branchbran 0.0% 0 / 90 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 47.6% 10 / 21 Podpod 100% 9 / 9 totaltotal 14.1% 53 / 376 CC 73 SCAR 67.0
CC
73
Cov
10%
_javascript_attrs_structure
1722.0
_handler_setup
240.0
javascript_attrs
90.0
CRAP 810.2
↑ Summary Jifty/Action/Record/Update.pm »
1 113 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 113 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Jifty::Web::Form::Element;
5 113 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Jifty::Web::Form::Element - Some item that can be rendered in a form
10  
11 =head1 DESCRIPTION
12  
13 Describes any HTML element that might live in a form, and thus might
14 have javascript on it.
15  
16 Handlers are placed on L<Jifty::Web::Form::Element> objects by calling
17 the name of the javascript event handler, such as C<onclick> or C<onchange>,
18 with a set of arguments.
19  
20 The format of the arguments passed to C<onclick> (or any similar
21 method) is a string, a hash reference, or a reference to an array of
22 multiple hash references. Strings are inserted verbatim.
23  
24 Hash references can take a number of possible keys. The most
25 important is the mode of the fragment replacement, if any; it is
26 specified by providing at most one of the following keys:
27  
28 =over
29  
30 =item append => PATH
31  
32 Add the given C<PATH> as a new fragment at the end within the element
33 specified by the CSS selector in L</element>.
34  
35 =item prepend => PATH
36  
37 Add the given C<PATH> as a new fragment at the start within the element
38 specified by the CSS selector in L</element>.
39  
40 =item before => PATH
41  
42 Add the given C<PATH> as a new fragment immediately before the element
43 specified by the CSS selector in L</element>.
44  
45 =item after => PATH
46  
47 Add the given C<PATH> as a new fragment immediately after the element
48 specified by the CSS selector in L</element>.
49  
50 =item popout => PATH
51  
52 Displays the given C<PATH> as a new fragment in a lightbox-style popout.
53  
54 =item replace_with => PATH
55  
56 Replaces the region specified by the C<region> parameter (which
57 defaults to the current region) with the fragment located at the given
58 C<PATH>. If C<undef> is passed as the C<PATH>, acts like a L</delete>.
59  
60 =item refresh => REGION
61  
62 Refreshes the given C<REGION>, which should be a
63 L<Jifty::Web::PageRegion> object, or the fully qualified name of such.
64  
65 =item refresh_self => 1
66  
67 Refreshes the current region; this is the default action, if a
68 non-empty C<args> is supplied, but no other mode is given.
69  
70 =item delete => REGION
71  
72 Removes the given C<REGION> from the page, permanently.
73  
74 =back
75  
76 The following options are also supported:
77  
78 =over
79  
80 =item toggle => BOOLEAN
81  
82 If set to true, then the link will possibly toggle the region to
83 empty, if the region's current path is the same as the path the region
84 is trying to be set to.
85  
86 =item region => REGION
87  
88 The region that should be updated. This defaults to the current
89 region.
90  
91 =item element => CSS SELECTOR
92  
93 A css selector specifying where the new region should be placed; used
94 with L</append>, L</prepend>, L</before>, and L</after> above. The
95 L<Jifty::Web::PageRegion/get_element> method may be useful in
96 specifying elements of parent page regions.
97  
98 Defaults to the current region.
99  
100 =item submit => MONIKER
101  
102 A Jifty::Action, Jifty::Action moniker, hashref of
103 { action => Jifty::Action::Subclass,
104 arguments => { argument => value, argument2 => value2 }
105  
106 or an arrayref of them.
107  
108 These actions are submitted when the event is fired. Any arguments
109 specified will override arguments submitted by form field.
110  
111 If you explicitly pass C<undef>, then all actions will be submitted.
112 This can be useful in conjunction with an C<onclick> handler, since
113 declaring an C<onclick> handler intentionally turns off action submission.
114  
115 =item disable => BOOLEAN
116  
117 If true, disable all form fields associated with the actions in
118 C<submit> when this Element is clicked. This serves to give immediate
119 visual indication that the request is being processed, as well as to
120 prevent double-submits.
121  
122 Defaults to true.
123  
124 =item args => HASHREF
125  
126 Arguments to the region. These will override the arguments to the
127 region that the region was given when it was last rendered.
128  
129 =item effect => STRING
130  
131 The Scriptaculous or jQuery visual effect to use when updating or
132 creating the fragment.
133  
134 =item effect_args => HASHREF
135  
136 A hashref of arguments to pass to the effect when it is created. These
137 can be used to change the duration of the effect, for instance.
138  
139 =item remove_effect => STRING
140  
141 As C<effect>, but for when the previous version of the region is
142 removed.
143  
144 =item remove_effect_args => HASHREF
145  
146 As C<effect_args>, but for C<remove_effect>.
147  
148 =item beforeclick => STRING
149  
150 String contains some Javascript code to be used before a click.
151  
152 =item confirm => STRING
153  
154 Prompt the user with a Javascript confirm dialog with the given text
155 before carrying out the rest of the handlers. If the user cancels, do
156 nothing, otherwise proceed as normal.
157  
158 TODO: This does not have a non-Javascript fallback method yet.
159  
160 =back
161  
162 =cut
163  
164 113 ▶use base qw/Jifty::Object Class::Accessor::Fast/;
Subroutine BEGIN: called CC=1 SCAR=0.0
165 113 ▶use Jifty::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
166  
167 =head2 handlers
168  
169 The following handlers are supported:
170  
171 onclick onchange ondblclick onmousedown onmouseup onmouseover
172 onmousemove onmouseout onfocus onblur onkeypress onkeydown
173 onkeyup onselect
174  
175 NOTE: onload, onunload, onsubmit and onreset are not yet supported
176  
177 WARNING: if you use the onclick handler, make sure that your javascript
178 is "return (function name);", or you may well get a very strange-looking
179 error from your browser.
180  
181 =cut
182  
183 113 ▶use constant handlers => qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
184 onclick onchange ondblclick onmousedown onmouseup onmouseover
185 onmousemove onmouseout onfocus onblur onkeypress onkeydown
186 onkeyup onselect
187 113 );
188  
189 113 ▶use constant possible_handlers => { map {($_ => 1)} handlers};
Subroutine BEGIN: called CC=1 SCAR=0.0
190  
191  
192 =head2 accessors
193  
194 Any descendant of L<Jifty::Web::Form::Element> should be able to
195 accept any of the event handlers (above) as one of the keys to its
196 C<new> parameter hash.
197  
198 =cut
199  
200 113 ▶sub accessors { handlers, qw(class title key_binding key_binding_label id label tooltip continuation rel) }
Subroutine accessors: called CC=1 SCAR=0.0
Pod documented
201  
202 113 __PACKAGE__->mk_accessors(map "_$_", handlers);
203 113 __PACKAGE__->mk_accessors(qw(class title key_binding key_binding_label id label tooltip continuation rel) );
204  
205 =head2 new PARAMHASH OVERRIDE
206  
207 Create a new C<Jifty::Web::Form::Element> object blessed with
208 PARAMHASH, and set with accessors for the hash values in OVERRIDE.
209  
210 =cut
211  
212 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
213 0 my ($class, $args, $other) = @_;
214 0 ▶ $args = {%{$args}, %{$other || {}}};
Condition0%0/2
$other || {}
l!l
00
MC/DC0%0/1
$other || {}
$other
Truth table0%0/2
A
A= $other
Aresult
11
00
215 # force using accessor for onclick init
216 0 my $override = {};
217 $override->{$_} = delete $args->{$_}
218 0 ▶ for grep {possible_handlers->{$_} and defined $args->{$_}} keys %{$args};
Condition0%0/3
{onblur => 1, onchange => 1, onclick => 1, ondblclick => 1, onfocus => 1, onkeydown => 1, onkeypress => 1, onkeyup => 1, onmousedown => 1, onmousemove => 1, onmouseout => 1, onmouseover => 1, onmouseup => 1, onselect => 1}->{$_} && defined $args->{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
{onblur => 1, onchange => 1, onclick => 1, ondblclick => 1, onfocus => 1, onkeydown => 1, onkeypress => 1, onkeyup => 1, onmousedown => 1, onmousemove => 1, onmouseout => 1, onmouseover => 1, onmouseup => 1, onselect => 1}->{$_} && defined $args->{$_}
{onblur => 1, onchange => 1, onclick => 1, ondblclick => 1, onfocus => 1, onkeydown => 1, onkeypress => 1, onkeyup => 1, onmousedown => 1, onmousemove => 1, onmouseout => 1, onmouseover => 1, onmouseup => 1, onselect => 1}->{$_} defined $args->{$_}
Truth table0%0/3
A && B
A= {onblur => 1, onchange => 1, onclick => 1, ondblclick => 1, onfocus => 1, onkeydown => 1, onkeypress => 1, onkeyup => 1, onmousedown => 1, onmousemove => 1, onmouseout => 1, onmouseover => 1, onmouseup => 1, onselect => 1}->{$_}B= defined $args->{$_}
ABresult
0X0
100
111
219  
220 0 my $self = $class->SUPER::new($args);
221  
222 0 $self->{handlers_used} = {};
223 0 $self->$_( $override->{$_} ) for keys %{$override};
224  
225 0 return $self;
226 }
227  
228 113 __PACKAGE__->_mk_normalising_accessor($_) for __PACKAGE__->handlers;
229  
230 1582 ▶sub _mk_normalising_accessor {
Subroutine _mk_normalising_accessor: called CC=1 SCAR=0.7
231 1582 my ($class, $accessor) = @_;
232 1582 my $internal_method = "_$accessor";
233 113 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
234 1582 ▶ *{$accessor} = sub {
Subroutine __ANON__: not called CC=2 SCAR=14.6
235 0 my $self = shift;
236 0 ▶ return $self->{$internal_method} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
237  
238 0 $self->$internal_method($self->_handler_setup($internal_method, @_));
239 1582 };
240 }
241  
242 =head2 onclick
243  
244 The onclick event occurs when the pointing device button is clicked
245 over an element. This attribute may be used with most elements.
246  
247 =head2 onchange
248  
249 The onchange event occurs when a control loses the input focus
250 and its value has been modified since gaining focus. This handler
251 can be used with all form elements.
252  
253 =head2 ondblclick
254  
255 The ondblclick event occurs when the pointing device button is
256 double clicked over an element. This handler
257 can be used with all form elements.
258  
259 =head2 onmousedown
260  
261 The onmousedown event occurs when the pointing device button is
262 pressed over an element. This handler
263 can be used with all form elements.
264  
265 =head2 onmouseup
266  
267 The onmouseup event occurs when the pointing device button is released
268 over an element. This handler can be used with all form elements.
269  
270 =head2 onmouseover
271  
272 The onmouseover event occurs when the pointing device is moved onto an
273 element. This handler can be used with all form elements.
274  
275 =head2 onmousemove
276  
277 The onmousemove event occurs when the pointing device is moved while it
278 is over an element. This handler can be used with all form elements.
279  
280 =head2 onmouseout
281  
282 The onmouseout event occurs when the pointing device is moved away from
283 an element. This handler can be used with all form elements.
284  
285 =head2 onfocus
286  
287 The onfocus event occurs when an element receives focus either by the
288 pointing device or by tabbing navigation. This handler
289 can be used with all form elements.
290  
291 =head2 onblur
292  
293 The onblur event occurs when an element loses focus either by the pointing
294 device or by tabbing navigation. This handler can be used with all
295 form elements.
296  
297 =head2 onkeypress
298  
299 The onkeypress event occurs when a key is pressed and released over an
300 element. This handler can be used with all form elements.
301  
302 =head2 onkeydown
303  
304 The onkeydown event occurs when a key is pressed down over an element.
305 This handler can be used with all form elements.
306  
307 =head2 onkeyup
308  
309 The onkeyup event occurs when a key is released over an element.
310 This handler can be used with all form elements.
311  
312 =head2 onselect
313  
314 The onselect event occurs when a user selects some text in a text field.
315 This attribute may be used with the text and textarea fields.
316  
317 =head2 _handler_setup
318  
319 This method is used by all handlers to normalize all arguments.
320  
321 =cut
322  
323 0 ▶sub _handler_setup {
Subroutine _handler_setup: not called CC=15 SCAR=54.8
324 0 my $self = shift;
325 0 my $trigger = shift;
326  
327 0 ▶ return $self->$trigger unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
328 0 $trigger =~ s/^_//;
329 0 $self->{handlers_used}{$trigger} = 1;
330  
331 0 my ($arg) = @_;
332  
333 0 ▶ $arg = [$arg] unless ref $arg eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arg eq "ARRAY" 0 0 0
334  
335 #Normalize arguments as much as possible here, to simplify later
336 #processing of them here and in Clickable.
337 0 for my $hook ( @$arg ) {
338 0 ▶ next unless ref $hook eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hook eq "HASH" 0 0 0
339  
340 # Normalize actions to monikers to prevent circular references,
341 # since Jifty::Action caches instances of Jifty::Web::Form::Clickable.
342 0 ▶ if ( $hook->{submit} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hook->{"submit"}) 0 0 0
343 0 ▶ $hook->{submit} = [ $hook->{submit} ] unless ref $hook->{submit} eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hook->{"submit"} eq "ARRAY" 0 0 0
344  
345 0 my @submit_tmp;
346 0 foreach my $submit ( @{$hook->{submit}}) {
347 0 ▶ if (!ref($submit)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $submit) { } 0 0 0
elsif (builtin::blessed($submit)) { } 0 0 0
348 0 push @submit_tmp, $submit;
349 } elsif(blessed($submit)) {
350 0 push @submit_tmp, $submit->moniker;
351 } else { # it's a hashref
352 0 push @submit_tmp, $submit->{'action'}->moniker;
353 0 $hook->{'action_arguments'}->{ $submit->{'action'}->moniker } = $submit->{'arguments'};
354 }
355  
356 }
357  
358 0 @{$hook->{submit}} = @submit_tmp;
359 }
360  
361 0 ▶ $hook->{args} ||= $hook->{arguments}; # should be able to use 'arguments' and not lose.
Condition0%0/3
$hook->{"args"} ||= $hook->{"arguments"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$hook->{"args"} ||= $hook->{"arguments"}
$hook->{"args"} $hook->{"arguments"}
Truth table0%0/3
A ||= B
A= $hook->{"args"}B= $hook->{"arguments"}
ABresult
1X1
011
000
362  
363 0 ▶ if ( $hook->{args} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hook->{'args'}) { } 0 0 0
364 # We're going to pass complex query mapping structures
365 # as-is to the server, but we need to make sure we're not
366 # trying to pass around Actions, merely their monikers.
367 0 for my $key ( keys %{ $hook->{args} } ) {
368 0 ▶ next unless ref $hook->{args}{$key} eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hook->{"args"}{$key} eq "HASH" 0 0 0
369 0 $hook->{args}{$key}{$_} = $hook->{args}{$key}{$_}->moniker for grep { ref $hook->{args}{$key}{$_} } keys %{ $hook->{args}{$key} };
370 }
371 } else {
372 0 $hook->{args} = {};
373 }
374  
375 }
376  
377 0 return $arg;
378  
379 }
380  
381 =head2 handlers_used
382  
383 Returns the names of javascript handlers which exist for this element.
384  
385 =cut
386  
387 0 ▶sub handlers_used {
Subroutine handlers_used: not called CC=1 SCAR=6.9
Pod documented
388 0 my $self = shift;
389 0 return keys %{$self->{handlers_used}};
390 }
391  
392 =head2 javascript
393  
394 Returns the javascript necessary to make the events happen, as a
395 string of HTML attributes.
396  
397 =cut
398  
399 0 ▶sub javascript {
Subroutine javascript: not called CC=1 SCAR=6.9
Pod documented
400 0 my $self = shift;
401 0 my %response = $self->javascript_attrs;
402  
403 return join "",
404 0 map { qq| $_="| . Jifty->web->escape($response{$_}) . qq|"| }
405 sort keys %response;
406 }
407  
408 0 ▶sub _javascript_attrs_structure {
Subroutine _javascript_attrs_structure: not called CC=41 SCAR=74.5
409 0 my $self = shift;
410 0 my %structure;
411  
412 0 my $current_region = Jifty->web->current_region;
413  
414 0 for my $trigger ( $self->handlers_used ) {
415 # Walk around the Class::Accessor, for speed
416 0 my $value = $self->{"_$trigger"};
417 0 ▶ next unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 0 0
418  
419 0 ▶ if ( !( $self->handler_allowed($trigger) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->handler_allowed($trigger)) 0 0 0
420 0 $self->log->error(
421 "Handler '$trigger' is not supported for field '"
422 . $self->label
423 . "' with class "
424 . ref $self );
425 0 next;
426 }
427  
428 0 my @fragments;
429 # if $actions is undef, that means we're submitting _all_ actions in the clickable
430 # if $actions is defined but empty, that means we're submitting no actions
431 # if $actions is not empty, we're submitting those actions
432 0 my $actions = {}; # Maps actions => disable?
433 0 my $confirm;
434 my $beforeclick;
435 0 my $action_arguments = {};
436 0 my $preload_key;
437  
438 0 for my $hook (grep {ref $_ eq "HASH"} (@{$value})) {
439 0 my %args;
440  
441 # Submit action
442 0 ▶ if ( exists $hook->{submit} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hook->{"submit"}) 0 0 0
443 0 $actions = undef;
444 0 ▶ my $disable_form_on_click = exists $hook->{disable} ? $hook->{disable} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $hook->{'disable'} ? : 0 0 0
445 # Normalize to 1/0 to pass to JS
446 0 ▶ $disable_form_on_click = $disable_form_on_click ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$disable_form_on_click ? : 0 0 0
447 0 ▶ for (@{ $hook->{submit} || [] }) {
Condition0%0/2
$hook->{'submit'} || []
l!l
00
MC/DC0%0/1
$hook->{'submit'} || []
$hook->{'submit'}
Truth table0%0/2
A
A= $hook->{'submit'}
Aresult
11
00
448 0 $actions->{$_} = $disable_form_on_click;
449 0 $action_arguments->{$_} = $hook->{'action_arguments'}->{$_};
450 }
451  
452 }
453  
454 0 ▶ $hook->{region} ||= Jifty->web->qualified_region;
Condition0%0/3
$hook->{"region"} ||= "Jifty"->web->qualified_region
l!l&&r!l&&!r
000
MC/DC0%0/2
$hook->{"region"} ||= "Jifty"->web->qualified_region
$hook->{"region"} "Jifty"->web->qualified_region
Truth table0%0/3
A ||= B
A= $hook->{"region"}B= "Jifty"->web->qualified_region
ABresult
1X1
011
000
455  
456 # Should we show a javascript confirm message?
457 0 ▶ if ($hook->{confirm}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hook->{"confirm"}) 0 0 0
458 0 $confirm = $hook->{confirm};
459 }
460  
461 # Some code usable before onclick
462 0 ▶ if ($hook->{beforeclick}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hook->{"beforeclick"}) 0 0 0
463 0 $beforeclick = $hook->{beforeclick};
464 }
465  
466 # Placement
467 0 ▶ if (exists $hook->{append}) {
Branch: 9 branches
BranchTrueFalseTotal
if (exists $hook->{'append'}) { } 0 0 0
elsif (exists $hook->{'prepend'}) { } 0 0 0
elsif (exists $hook->{'before'}) { } 0 0 0
elsif (exists $hook->{'after'}) { } 0 0 0
elsif (exists $hook->{'popout'}) { } 0 0 0
elsif (exists $hook->{'replace_with'}) { } 0 0 0
elsif (exists $hook->{'refresh'}) { } 0 0 0
elsif (exists $hook->{'refresh_self'} and $current_region or $current_region and $hook->{'args'} and %{$hook->{'args'};}) { } 0 0 0
elsif (exists $hook->{'delete'}) { } 0 0 0
468 0 @args{qw/mode path/} = ('Bottom', $hook->{append});
469 0 ▶ $hook->{element} ||= "#region-".$hook->{region};
Condition0%0/2
$hook->{"element"} ||= '#region-' . $hook->{"region"}
l!l
00
MC/DC0%0/1
$hook->{"element"} ||= '#region-' . $hook->{"region"}
$hook->{"element"}
Truth table0%0/2
A
A= $hook->{"element"}
Aresult
11
00
470 } elsif (exists $hook->{prepend}) {
471 0 @args{qw/mode path/} = ('Top', $hook->{prepend});
472 0 ▶ $hook->{element} ||= "#region-".$hook->{region};
Condition0%0/2
$hook->{"element"} ||= '#region-' . $hook->{"region"}
l!l
00
MC/DC0%0/1
$hook->{"element"} ||= '#region-' . $hook->{"region"}
$hook->{"element"}
Truth table0%0/2
A
A= $hook->{"element"}
Aresult
11
00
473 } elsif (exists $hook->{before}) {
474 0 @args{qw/mode path/} = ('Before', $hook->{before});
475 0 ▶ $hook->{element} ||= "#region-".$hook->{region};
Condition0%0/2
$hook->{"element"} ||= '#region-' . $hook->{"region"}
l!l
00
MC/DC0%0/1
$hook->{"element"} ||= '#region-' . $hook->{"region"}
$hook->{"element"}
Truth table0%0/2
A
A= $hook->{"element"}
Aresult
11
00
476 } elsif (exists $hook->{after}) {
477 0 @args{qw/mode path/} = ('After', $hook->{after});
478 0 ▶ $hook->{element} ||= "#region-".$hook->{region};
Condition0%0/2
$hook->{"element"} ||= '#region-' . $hook->{"region"}
l!l
00
MC/DC0%0/1
$hook->{"element"} ||= '#region-' . $hook->{"region"}
$hook->{"element"}
Truth table0%0/2
A
A= $hook->{"element"}
Aresult
11
00
479 } elsif (exists $hook->{popout}) {
480 0 @args{qw/mode path/} = ('Popout', $hook->{popout});
481 # Facebox always appends to its #facebox element, but that
482 # doesn't exist before the first use so we need something
483 # that's ever present. The value doesn't matter as long as it
484 # exists. Setting a value here means you can use Popout
485 # outside a region without specifying a value yourself.
486 0 $hook->{element} = "body";
487 } elsif (exists $hook->{replace_with}) {
488 0 ▶ if (defined $hook->{replace_with}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hook->{'replace_with'}) { } 0 0 0
489 0 @args{qw/mode path region/} = ('Replace', $hook->{replace_with}, $hook->{region});
490 } else {
491 0 @args{qw/mode region/} = ('Delete', $hook->{region});
492 }
493 } elsif (exists $hook->{refresh}) {
494 0 ▶ my $region = ref $hook->{refresh} ? $hook->{refresh} : Jifty->web->get_region($hook->{refresh});
Branch: 1 branch
BranchTrueFalseTotal
ref $hook->{'refresh'} ? : 0 0 0
495 0 ▶ if ($region) {
Branch: 1 branch
BranchTrueFalseTotal
if ($region) { } 0 0 0
496 0 @args{qw/mode path region/} = ('Replace', $region->path, $region->qualified_name);
497 } else {
498 0 $self->log->debug("Can't find region ".$hook->{refresh});
499 0 @args{qw/mode path region/} = ('Replace', undef, $hook->{refresh});
500 }
501 0 ▶ } elsif ((exists $hook->{refresh_self} and $current_region) or ($current_region and $hook->{args} and %{$hook->{args}})) {
Condition0%0/12
exists $hook->{"refresh_self"} and $current_region or $current_region and $hook->{"args"} and %{$hook->{"args"};}
l!l&&r!l&&!r
000
exists $hook->{"refresh_self"} and $current_region
!ll&&!rl&&r
000
$current_region and $hook->{"args"} and %{$hook->{"args"};}
!ll&&!rl&&r
000
$current_region and $hook->{"args"}
!ll&&!rl&&r
000
MC/DC0%0/5
exists $hook->{"refresh_self"} and $current_region or $current_region and $hook->{"args"} and %{$hook->{"args"};}
exists $hook->{"refresh_self"} $current_region $current_region $hook->{"args"} %{$hook->{"args"};}
Truth table0%0/9
A and B or C and D and E
A= exists $hook->{"refresh_self"}B= $current_regionC= $current_regionD= $hook->{"args"}E= %{$hook->{"args"};}
ABCDEresult
11XXX1
0X1111
101111
0X0XX0
0X10X0
0X1100
100XX0
1010X0
101100
502 # If we just pass arguments, treat as a refresh_self
503 0 @args{qw/mode path region/} = ('Replace', $current_region->path, $current_region);
504 } elsif (exists $hook->{delete}) {
505 0 @args{qw/mode region/} = ('Delete', $hook->{delete});
506 } else {
507 # If we're not doing any of the above, skip this one
508 0 next;
509 }
510  
511 # Qualified name if we have a ref
512 0 ▶ $args{region} = $args{region}->qualified_name if ref $args{region};
Branch: 1 branch
BranchTrueFalseTotal
if ref $args{"region"} 0 0 0
513  
514 # What element we're replacing.
515 0 ▶ if ($hook->{element}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hook->{"element"}) 0 0 0
516 0 ▶ $args{element} = ref $hook->{element} ? "#region-".$hook->{element}->qualified_name : $hook->{element};
Branch: 1 branch
BranchTrueFalseTotal
ref $hook->{'element'} ? : 0 0 0
517 0 ▶ $args{region} = $args{element} =~ /^#region-(\S+)/ ? "$1-".Jifty->web->serial : Jifty->web->serial;
Branch: 1 branch
BranchTrueFalseTotal
$args{'element'} =~ /^#region-(\S+)/ ? : 0 0 0
518 }
519  
520 # Arguments
521 0 $args{args} = $hook->{args};
522  
523 # Toggle functionality
524 0 ▶ $args{toggle} = 1 if $hook->{toggle};
Branch: 1 branch
BranchTrueFalseTotal
if $hook->{"toggle"} 0 0 0
525  
526 # Preloading functionality
527 0 ▶ if ($hook->{preload}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hook->{"preload"}) 0 0 0
528 $preload_key = $hook->{preload} eq 1
529 ? Jifty->web->serial
530 0 ▶ : $hook->{preload};
Branch: 1 branch
BranchTrueFalseTotal
$hook->{'preload'} eq 1 ? : 0 0 0
531 }
532  
533 # Effects
534 0 $args{$_} = $hook->{$_} for grep {exists $hook->{$_}} qw/effect effect_args remove_effect remove_effect_args/;
535  
536 0 push @fragments, \%args;
537 }
538  
539 0 $structure{$trigger} = {
540 value => $value,
541 fragments => \@fragments,
542 actions => $actions,
543 action_arguments => $action_arguments,
544 confirm => $confirm,
545 beforeclick => $beforeclick,
546 preload_key => $preload_key,
547 };
548 }
549  
550 0 return \%structure;
551 }
552  
553 =head2 javascript_attrs
554  
555 Returns the javascript necessary to make the events happen, as a
556 hash of attribute-name and value.
557  
558 =cut
559  
560 0 ▶sub javascript_attrs {
Subroutine javascript_attrs: not called CC=9 SCAR=45.0
Pod documented
561 0 my $self = shift;
562  
563 0 my $structure = $self->_javascript_attrs_structure(@_);
564 0 my %response;
565  
566 0 for my $trigger (keys %$structure) {
567 0 my $trigger_structure = $structure->{$trigger};
568 0 my $fragments = $trigger_structure->{fragments};
569 0 my $actions = $trigger_structure->{actions};
570  
571 my $string = join ";",
572 0 grep { not ref $_ }
573 0 @{ $trigger_structure->{value} };
574  
575 0 ▶ if ( @$fragments or ( !$actions || %$actions ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$fragments or (not $actions or %$actions)) 0 0 0
Condition0%0/6
@$fragments or (not $actions or %$actions)
l!l&&r!l&&!r
000
not $actions or %$actions
l!l&&r!l&&!r
000
MC/DC0%0/3
@$fragments or (not $actions or %$actions)
@$fragments not $actions %$actions
Truth table0%0/4
A or not(B) or C
A= @$fragmentsB= not $actionsC= %$actions
ABCresult
1XX1
01X1
0011
0000
576 my $update_json = Jifty::JSON::encode_json({
577 actions => $actions,
578 action_arguments => $trigger_structure->{action_arguments},
579 fragments => $fragments,
580 continuation => $self->continuation,
581 preload_key => $trigger_structure->{preload_key},
582 0 }, { convert_blessed => 1 });
583  
584 0 ▶ my $update = $trigger eq "onclick" ? "Jifty.c(event,$update_json,this)"
Branch: 1 branch
BranchTrueFalseTotal
$trigger eq 'onclick' ? : 0 0 0
585 : "Jifty.update($update_json,this);";
586 0 $string .= "return $update";
587 0 ▶ $string .= " || true" unless $self->javascript_preempt;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->javascript_preempt 0 0 0
588 }
589  
590 0 ▶ if ($trigger_structure->{confirm}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trigger_structure->{"confirm"}) 0 0 0
591 0 my $text = Jifty::JSON::encode_json( $trigger_structure->{confirm} );
592 0 $string = "if(!confirm($text)){ Jifty.stopEvent(event); return false; }" . $string;
593 }
594  
595 0 ▶ if ($trigger_structure->{beforeclick}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trigger_structure->{"beforeclick"}) 0 0 0
596 0 $string = $trigger_structure->{beforeclick} . $string;
597 }
598  
599 0 $response{$trigger} = $string;
600 }
601  
602 0 return %response;
603 }
604  
605 =head2 javascript_preempt
606  
607 Returns true if the the javascript's handlers should prevent the web
608 browser's standard effects from happening; that is, for C<onclick>, it
609 prevents buttons from submitting and the like. The default is to
610 return true, but this can be overridden.
611  
612 =cut
613  
614 0 ▶sub javascript_preempt { return 1 };
Subroutine javascript_preempt: not called CC=1 SCAR=6.9
Pod documented
615  
616 =head2 class
617  
618 Sets the CSS class that the element will display as
619  
620 =head2 title
621  
622 Sets the title that the element will display, e.g. for tooltips
623  
624 =head2 key_binding
625  
626 Sets the key binding associated with this element
627  
628 =head2 key_binding_label
629  
630 Sets the key binding label associated with this element (if none is specified,
631 the normal label is used instead)
632  
633 =head2 id
634  
635 Subclasses must override this to provide each element with a unique id.
636  
637 =head2 label
638  
639 Sets the label of the element. This will be used for the key binding
640 legend if key_binding_label is not set.
641  
642 =head2 key_binding_javascript
643  
644 Returns the javascript fragment to add key binding for this input, if
645 one exists.
646  
647 =cut
648  
649 0 ▶sub key_binding_javascript {
Subroutine key_binding_javascript: not called CC=5 SCAR=34.0
Pod documented
650 0 my $self = shift;
651 0 my $key = $self->key_binding;
652 0 ▶ my $label = defined $self->key_binding_label
Branch: 1 branch
BranchTrueFalseTotal
defined $self->key_binding_label ? : 0 0 0
653 ? $self->key_binding_label
654 : $self->label;
655 0 ▶ if ($key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key) 0 0 0
656 0 ▶ $self->log->warn("Keybinding '@{[uc $key]}' will NOT work because the"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->id and length $self->id 0 0 0
Condition0%0/3
defined $self->id and length $self->id
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->id and length $self->id
defined $self->id length $self->id
Truth table0%0/3
A and B
A= defined $self->idB= length $self->id
ABresult
0X0
100
111
657 ." element ('$label') does not have an ID.")
658 if not defined $self->id or not length $self->id;
659  
660 0 return "Jifty.KeyBindings.add("
661 . Jifty::JSON::encode_json( uc $key ).","
662 . "'click', "
663 . Jifty::JSON::encode_json( $self->id ).","
664 . Jifty::JSON::encode_json( $label )
665 . ");";
666 }
667 }
668  
669 =head2 render_key_binding
670  
671 Renders the javascript from L</key_binding_javscript> in a <script>
672 tag, if needed.
673  
674 =cut
675  
676 0 ▶sub render_key_binding {
Subroutine render_key_binding: not called CC=2 SCAR=17.9
Pod documented
677 0 my $self = shift;
678 0 ▶ return unless $self->key_binding;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->key_binding 0 0 0
679 0 Jifty->web->out(
680 '<script type="text/javascript">' .
681 $self->key_binding_javascript.
682 "</script>");
683 0 return '';
684 }
685  
686  
687 =head2 handler_allowed HANDLER_NAME
688  
689 Returns 1 if the handler (e.g. onclick) is allowed. Undef otherwise.
690  
691 The set defined here represents the typical handlers that are
692 permitted. Derived classes should override if they stray from the norm.
693  
694 By default we allow:
695  
696 onchange onclick ondblclick onmousedown onmouseup onmouseover onmousemove
697 onmouseout onfocus onblur onkeypress onkeydown onkeyup
698  
699 =cut
700  
701 0 ▶sub handler_allowed {
Subroutine handler_allowed: not called CC=1 SCAR=6.9
Pod documented
702 0 my $self = shift;
703 0 my ($handler) = @_;
704  
705 return {onchange => 1,
706 onclick => 1,
707 ondblclick => 1,
708 onmousedown => 1,
709 onmouseup => 1,
710 onmouseover => 1,
711 onmousemove => 1,
712 onmouseout => 1,
713 onfocus => 1,
714 onblur => 1,
715 onkeypress => 1,
716 onkeydown => 1,
717 onkeyup => 1
718 0 }->{$handler};
719  
720 }
721
722  
723  
724 113 1;
↑ Summary Jifty/Action/Record/Update.pm »