Jifty/Web/Form/Field.pm

filter: Statementstmt 11.2% 27 / 241 Branchbran 0.0% 0 / 90 Conditioncond 0.0% 0 / 41 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 18.6% 8 / 43 Podpod 100% 33 / 33 totaltotal 14.4% 68 / 471 CC 64 SCAR 33.7
CC
64
Cov
7%
new
132.0
render_widget
72.0
available_values
42.0
CRAP 29.0
« Jifty/Script/Server.pm ↑ Summary Jifty/View/Mason/Handler.pm »
1 =begin properties
2  
3 constructor
4 canonicalizer
5 available_values
6 ajax_validates
7 autocompleter
8  
9 default_value
10 valid_values
11 validator
12 render_as
13 label
14 hints
15 placeholder
16 display_length
17 max_length
18 mandatory
19  
20 =end properties
21  
22 =cut
23  
24 113 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 113 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26
27 package Jifty::Web::Form::Field;
28  
29 =head1 NAME
30  
31 Jifty::Web::Form::Field - Web input of some sort
32  
33 =head1 DESCRIPTION
34  
35 Describes a form input in a L<Jifty::Action>.
36 C<Jifty::Web::Form::Field>s know what action they are on, and acquire
37 properties from the L<Jifty::Action> which they are part of. Each key
38 in the L<Jifty::Action/arguments> hash becomes a
39 C<Jifty::Web::Form::Field> whose L</name> is that key.
40  
41 C<Jifty::Web::Form::Field>s stringify using the L</render> method, to
42 aid in placing them in L<HTML::Mason> components.
43  
44 =cut
45  
46 113 ▶use base 'Jifty::Web::Form::Element';
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 113 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 113 ▶use Scalar::Defer qw/force/;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 113 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 # We need the anonymous sub because otherwise the method of the base class is
53 # always called, instead of the appropriate overridden method in a child class.
54 0 ▶use overload '""' => sub { shift->render }, bool => sub { 1 };
Subroutine BEGIN: called CC=1 SCAR=0.6
Subroutine __ANON__: not called CC=1 SCAR=0.6
Subroutine __ANON__: not called CC=1 SCAR=0.6
55  
56 =head2 new
57  
58 Creates a new L<Jifty::Web::Form::Field> (possibly magically blessing into a subclass).
59 Should only be called from C<< $action->arguments >>.
60  
61 =cut
62  
63 0 ▶sub new {
Subroutine new: not called CC=11 SCAR=48.8
Pod documented
64 0 my $class = shift;
65 0 my $self = $class->SUPER::new(
66 { type => 'text',
67 class => '',
68 input_name => '',
69 default_value => '',
70 sticky_value => '',
71 render_mode => 'update' });
72 0 ▶ my $args = ref($_[0]) ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
73  
74 0 ▶ $self->rebless( $args->{render_as} || $args->{type} || 'text' );
Condition0%0/5
$args->{'render_as'} || $args->{'type'} || 'text'
l!l
00
$args->{"render_as"} or $args->{"type"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'render_as'} || $args->{'type'} || 'text'
$args->{"render_as"} $args->{"type"}
Truth table0%0/3
A or B
A= $args->{"render_as"}B= $args->{"type"}
ABresult
1X1
011
000
75  
76 0 for my $field ( $self->accessors() ) {
77 0 ▶ $self->$field( $args->{$field} ) if exists $args->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{$field} 0 0 0
78 }
79  
80 # If they key and/or value imply that this argument is going to be
81 # a mapped argument, then do the mapping and mark the field as hidden.
82 # but ignore that if the field is a container in the model
83 0 my ($key, $value) = Jifty::Request::Mapper->query_parameters($self->input_name, $self->current_value);
84 0 ▶ if ($key ne $self->input_name && !$self->action->arguments->{$self->name}{container}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key ne $self->input_name and not $self->action->arguments->{$self->name}{"container"}) 0 0 0
Condition0%0/3
$key ne $self->input_name and not $self->action->arguments->{$self->name}{"container"}
!ll&&!rl&&r
000
MC/DC0%0/2
$key ne $self->input_name and not $self->action->arguments->{$self->name}{"container"}
$key ne $self->input_name not $self->action->arguments->{$self->name}{"container"}
Truth table0%0/3
A and not(B)
A= $key ne $self->input_nameB= not $self->action->arguments->{$self->name}{"container"}
ABresult
0X0
100
111
85 0 $self->rebless('Hidden');
86 0 $self->input_name($key);
87 0 $self->default_value($value);
88 0 $self->sticky_value(undef);
89 }
90  
91 # now that the form field has been instantiated, register the action with the form.
92 0 ▶ if ($self->action and Jifty->web->form->is_open and not (Jifty->web->form->printed_actions->{$self->action->moniker})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action and "Jifty"->web->form->is_open and not "Jifty"->web->form->printed_actions->{$self->action->moniker}) 0 0 0
Condition0%0/6
$self->action and "Jifty"->web->form->is_open and not "Jifty"->web->form->printed_actions->{$self->action->moniker}
!ll&&!rl&&r
000
$self->action and "Jifty"->web->form->is_open
!ll&&!rl&&r
000
MC/DC0%0/3
$self->action and "Jifty"->web->form->is_open and not "Jifty"->web->form->printed_actions->{$self->action->moniker}
$self->action "Jifty"->web->form->is_open not "Jifty"->web->form->printed_actions->{$self->action->moniker}
Truth table0%0/4
A and B and not(C)
A= $self->actionB= "Jifty"->web->form->is_openC= not "Jifty"->web->form->printed_actions->{$self->action->moniker}
ABCresult
0XX0
10X0
1100
1111
93 0 Jifty->web->form->register_action( $self->action);
94 0 Jifty->web->form->print_action_registration($self->action->moniker);
95 }
96 0 return $self;
97 }
98  
99 =head2 $self->rebless($widget)
100  
101 Turn the current blessed class into the given widget class.
102  
103 =cut
104  
105 0 ▶sub rebless {
Subroutine rebless: not called CC=3 SCAR=24.8
Pod documented
106 0 my ($self, $widget) = @_;
107 0 ▶ my $widget_class = $widget =~ m/::/ ? $widget : "Jifty::Web::Form::Field::".ucfirst($widget);
Branch: 1 branch
BranchTrueFalseTotal
$widget =~ /::/ ? : 0 0 0
108  
109 0 ▶ $self->log->error("Invalid widget class $widget_class")
Branch: 1 branch
BranchTrueFalseTotal
unless "Jifty::Util"->require($widget_class) 0 0 0
110 unless Jifty::Util->require($widget_class);
111  
112 0 bless $self, $widget_class;
113 }
114  
115 =head2 accessors
116  
117 Lists the accessors that are able to be called from within a call to
118 C<new>. Subclasses should extend this list.
119  
120 =cut
121  
122 113 my @new_fields = qw(
123 name type sticky sticky_value default_value action
124 mandatory ajax_validates ajax_canonicalizes autocompleter preamble hints
125 placeholder focus render_mode display_length max_length _element_id
126 disable_autocomplete multiple attributes
127 );
128  
129 113 my @semiexposed_fields = qw(
130 label input_name available_values
131 );
132  
133 113 ▶sub accessors {
Subroutine accessors: called CC=1 SCAR=0.0
Pod documented
134 113 shift->SUPER::accessors(), @new_fields, @semiexposed_fields;
135 }
136  
137 113 __PACKAGE__->mk_accessors(@new_fields, map { "_$_" } @semiexposed_fields);
138  
139 =head2 name [VALUE]
140  
141 Gets or sets the name of the field. This is separate from the name of
142 the label (see L</label>) and the form input name (see
143 L</input_name>), though both default to this name. This name should
144 match to a key in the L<Jifty::Action/arguments> hash. If this
145 C<Jifty::Web::Form::Field> was created via L<Jifty::Action/form_field>,
146 this is automatically set for you.
147  
148 =head2 class [VALUE]
149  
150 Gets or sets the CSS display class applied to the label and widget.
151  
152 =head2 type [VALUE]
153  
154 Gets or sets the type of the HTML <input> field -- that is, 'text' or
155 'password'. Defaults to 'text'.
156  
157 =head2 key_binding VALUE
158  
159 Sets this form field's "submit" key binding to VALUE.
160  
161 =head2 key_binding_label VALUE
162  
163 Sets this form field's key binding label to VALUE. If none is specified
164 the normal label is used.
165  
166 =head2 default_value [VALUE]
167  
168 Gets or sets the default value for the form.
169  
170 =head2 sticky_value [VALUE]
171  
172 Gets or sets the value for the form field that was submitted in the last action.
173  
174 =head2 mandatory [VALUE]
175  
176 A boolean indicating that the argument B<must> be present when the
177 user submits the form.
178  
179 =head2 focus [VALUE]
180  
181 If true, put focus on this form field when the page loads.
182  
183 =head2 ajax_validates [VALUE]
184  
185 A boolean value indicating if user input into an HTML form field for
186 this argument should be L<validated|Jifty::Manual::Glossary/validate>
187 via L<AJAX|Jifty::Manual::Glossary/AJAX> as the user fills out the
188 form, instead of waiting until submit. Arguments will B<always> be
189 validated before the action is run, whether or not they also
190 C<ajax_validate>.
191  
192 =head2 ajax_canonicalizes [VALUE]
193  
194 A boolean value indicating if user input into an HTML form field for
195 this argument should be L<canonicalized|Jifty::Manual::Glossary/canonicalize>
196 via L<AJAX|Jifty::Manual::Glassary/AJAX> as the user fills out the
197 form, instead of waiting until submit. Arguments will B<always> be
198 canonicalized before the action is run, whether or not they also
199 C<ajax_canonicalize>
200  
201 =head2 disable_autocomplete [VALUE]
202  
203 Gets or sets whether to disable _browser_ autocomplete for this field.
204  
205 =head2 preamble [VALUE]
206  
207 Gets or sets the preamble located in front of the field.
208  
209 =head2 multiple [VALUE]
210  
211 A boolean indicating that the field is multiple.
212 aka. has multiple attribute, which is useful for select field.
213  
214 =head2 id
215  
216 For the purposes of L<Jifty::Web::Form::Element>, the unique id of
217 this field is its input name.
218  
219 =head2 attributes
220  
221 If the field object is generated by L<Jifty::Action::Record>, this
222 holds the additional attributes declared to the corresponding column
223 of the model.
224  
225 =cut
226  
227 0 ▶sub id {
Subroutine id: not called CC=1 SCAR=6.9
Pod documented
228 0 my $self = shift;
229 0 return $self->input_name;
230 }
231  
232 =head2 input_name [VALUE]
233  
234 Gets or sets the form field input name, as it is rendered in the HTML.
235 If we've been explicitly named, return that, otherwise return a name
236 based on the moniker of the action and the name of the form.
237  
238 =cut
239  
240 0 ▶sub input_name {
Subroutine input_name: not called CC=3 SCAR=24.8
Pod documented
241 0 my $self = shift;
242  
243 # If we've been explicitly handed a name, we should run with it.
244 # Otherwise, we should ask our action, how to turn our "name"
245 # into a form input name.
246  
247 0 my $ret = $self->_input_name(@_);
248 0 ▶ return $ret if $ret;
Branch: 1 branch
BranchTrueFalseTotal
if $ret 0 0 0
249  
250 0 my $action = $self->action;
251 0 ▶ return $action ? $self->action->form_field_name( $self->name )
Branch: 1 branch
BranchTrueFalseTotal
$action ? : 0 0 0
252 : '';
253 }
254  
255  
256 =head2 fallback_name
257  
258 Return the form field's fallback name. This should be used to create a
259 hidden input with a value of 0 to accompany checkboxes or to let
260 comboboxes fall back to the text input if, and only if no value is
261 selected from the SELECT. (We use this order, so that we can stick the
262 label and not the value in the INPUT field. To make that work, we also need
263 to clear the SELECT after the user types in the INPUT.
264  
265 =cut
266  
267 0 ▶sub fallback_name {
Subroutine fallback_name: not called CC=2 SCAR=17.9
Pod documented
268 0 my $self = shift;
269  
270 0 ▶ if ($self->action) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action) { } 0 0 0
271 0 return $self->action->fallback_form_field_name( $self->name );
272 }
273 else {
274 # XXX TODO, we should have a more graceful way to compose these in the absence of an action
275 0 my $name = $self->input_name;
276 0 $name =~ s/^J:A:F/J:A:F:F/;
277 0 return($name)
278 }
279 }
280  
281  
282 =head2 label [VALUE]
283  
284 Gets or sets the label on the field. This defaults to the name of the
285 object.
286  
287 =cut
288  
289 0 ▶sub label {
Subroutine label: not called CC=2 SCAR=17.9
Pod documented
290 0 my $self = shift;
291 0 my $val = $self->_label(@_);
292 0 ▶ defined $val ? $val : $self->name;
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 0 0 0
293  
294 }
295  
296 =head2 hints [VALUE]
297  
298 Hints for the user to explain this field
299  
300 =cut
301  
302 0 ▶sub hints {
Subroutine hints: not called CC=3 SCAR=24.8
Pod documented
303 0 my $self = shift;
304 0 ▶ return $self->_hints_accessor unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
305  
306 0 my $hint = shift;
307 # people sometimes say hints are "foo" rather than hints is "foo"
308 0 ▶ if (ref $hint eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hint eq "ARRAY") 0 0 0
309 0 $hint = shift @$hint;
310 }
311 0 return $self->_hints_accessor($hint);
312 }
313  
314  
315 =head2 element_id
316  
317 Returns a unique C<id> attribute for this field based on the field name. This is
318 consistent for the life of the L<Jifty::Web::Form::Field> object but isn't predictable;
319  
320 =cut
321  
322  
323 0 ▶sub element_id {
Subroutine element_id: not called CC=2 SCAR=17.9
Pod documented
324 0 my $self = shift;
325 0 ▶ return $self->_element_id || $self->_element_id( $self->input_name ."-".Jifty->web->serial);
Condition0%0/3
$self->_element_id || $self->_element_id($self->input_name . '-' . 'Jifty'->web->serial)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_element_id || $self->_element_id($self->input_name . '-' . 'Jifty'->web->serial)
$self->_element_id $self->_element_id($self->input_name . '-' . 'Jifty'->web->serial)
Truth table0%0/3
A || B
A= $self->_element_idB= $self->_element_id($self->input_name . '-' . 'Jifty'->web->serial)
ABresult
1X1
011
000
326 }
327  
328 =head2 action [VALUE]
329  
330 Gets or sets the L<Jifty::Action> object that this
331 C<Jifty::Web::Form::Field> is associated with. This is called
332 automatically if this C<Jifty::Action> was created via
333 L<Jifty::Web::Form::Field/form_field>.
334  
335 =cut
336  
337 0 ▶sub action {
Subroutine action: not called CC=2 SCAR=17.9
Pod documented
338 0 my $self = shift;
339  
340 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
341 0 $self->{action} = shift;
342  
343 # weaken our circular reference
344 0 weaken $self->{action};
345 }
346  
347 0 return $self->{action};
348  
349 }
350  
351 =head2 current_value
352  
353 Gets the current value we should be using for this form field.
354  
355 If the argument is marked as "sticky" (default) and there is a value for this
356 field from a previous action submit AND that action did not have a "success"
357 response, returns that submit field's value. Otherwise, returns the action's argument's
358 default_value for this field.
359  
360 =cut
361  
362 0 ▶sub current_value {
Subroutine current_value: not called CC=3 SCAR=24.8
Pod documented
363 0 my $self = shift;
364  
365 0 ▶ if ($self->sticky_value and $self->sticky) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sticky_value and $self->sticky) { } 0 0 0
Condition0%0/3
$self->sticky_value and $self->sticky
!ll&&!rl&&r
000
MC/DC0%0/2
$self->sticky_value and $self->sticky
$self->sticky_value $self->sticky
Truth table0%0/3
A and B
A= $self->sticky_valueB= $self->sticky
ABresult
0X0
100
111
366 0 return $self->sticky_value;
367 } else {
368 # the force is here because very often this will be a Scalar::Defer object that we REALLY
369 # want to be able to check definedness on.
370 # Because of a core limitation in perl, Scalar::Defer can't return undef for an object.
371 0 return force $self->default_value;
372 }
373 }
374  
375 =head2 render
376  
377 Outputs this form element in a span with class C<form_field>. This
378 outputs the label, the widget itself, any hints, any errors, and any
379 warnings using L</render_label>, L</render_widget>, L</render_hints>,
380 L</render_errors>, and L</render_warnings>, respectively. Returns an
381 empty string.
382  
383 This is also what C<Jifty::Web::Form::Field>s do when stringified.
384  
385 =cut
386  
387 0 ▶sub render {
Subroutine render: not called CC=3 SCAR=24.8
Pod documented
388 0 my $self = shift;
389 0 $self->render_wrapper_start();
390 0 $self->render_preamble();
391  
392  
393 0 $self->render_label();
394 0 ▶ if ($self->render_mode eq 'update') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->render_mode eq 'update') { } 0 0 0
elsif ($self->render_mode eq 'read') { } 0 0 0
395 0 $self->render_widget();
396 0 $self->render_inline_javascript();
397 0 $self->render_hints();
398 0 $self->render_errors();
399 0 $self->render_warnings();
400 0 $self->render_canonicalization_notes();
401 } elsif ($self->render_mode eq 'read'){
402 0 $self->render_value();
403 0 $self->render_preload_javascript();
404 }
405 0 $self->render_wrapper_end();
406 0 return ('');
407 }
408  
409 =head2 render_inline_javascript
410  
411 Render a <script> tag (if necessary) containing any inline javascript that
412 should follow this form field. This is used to add an autocompleter,
413 placeholder, keybinding, or preloading to form fields where needed.
414  
415 =cut
416  
417 0 ▶sub render_inline_javascript {
Subroutine render_inline_javascript: not called CC=2 SCAR=17.9
Pod documented
418 0 my $self = shift;
419  
420 0 my $javascript;
421  
422 $javascript = join(
423 "\n",
424 0 grep {$_} (
425 $self->autocomplete_javascript(),
426 $self->placeholder_javascript(),
427 $self->key_binding_javascript(),
428 $self->preload_javascript(),
429 )
430 );
431
432 0 ▶ if($javascript =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($javascript =~ /\S/) 0 0 0
433 0 Jifty->web->out(qq{<script type="text/javascript">$javascript</script>
434 });
435 }
436 }
437  
438 =head2 render_preload_javascript
439  
440 Render a <script> tag (if necessary) containing any inline preload javascript
441 that should follow this form field.
442  
443 =cut
444  
445 0 ▶sub render_preload_javascript {
Subroutine render_preload_javascript: not called CC=2 SCAR=17.9
Pod documented
446 0 my $self = shift;
447  
448 0 my $javascript = $self->preload_javascript;
449  
450 0 ▶ if($javascript =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($javascript =~ /\S/) 0 0 0
451 0 Jifty->web->out(qq{<script type="text/javascript">$javascript</script>
452 });
453 }
454 }
455  
456 =head2 classes
457  
458 Renders a default CSS class for each part of our widget.
459  
460 =cut
461  
462  
463 0 ▶sub classes {
Subroutine classes: not called CC=3 SCAR=24.8
Pod documented
464 0 my $self = shift;
465 0 my $name = $self->name;
466 0 ▶ return join(' ', ($self->class||()), ($name ? "argument-".$name : ()));
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 0 0 0
Condition0%0/3
$self->class || ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->class || ()
$self->class ()
Truth table0%0/3
A || B
A= $self->classB= ()
ABresult
1X1
011
000
467 }
468  
469  
470 =head2 render_wrapper_start
471  
472 Output the start of div that wraps the form field
473  
474 =cut
475  
476 0 ▶sub render_wrapper_start {
Subroutine render_wrapper_start: not called CC=3 SCAR=24.8
Pod documented
477 0 my $self = shift;
478 0 my @classes = qw(form_field);
479 0 ▶ if ($self->mandatory) { push @classes, 'mandatory' }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mandatory) 0 0 0
480 0 ▶ if ($self->name) { push @classes, 'argument-'.$self->name }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->name) 0 0 0
481 0 Jifty->web->out('<div class="'.join(' ', @classes).'">' ."\n");
482 }
483  
484 =head2 render_wrapper_end
485  
486 Output the div that wraps the form field
487  
488 =cut
489  
490 0 ▶sub render_wrapper_end {
Subroutine render_wrapper_end: not called CC=1 SCAR=6.9
Pod documented
491 0 my $self = shift;
492 0 Jifty->web->out("</div>"."\n");
493 }
494  
495 =head2 render_preamble
496  
497 Outputs the preamble of this form field, using a <span> HTML element
498 with CSS class C<preamble> and whatever L</class> specifies. Returns an
499 empty string.
500  
501 Use this for sticking instructions right in front of a widget
502  
503 =cut
504  
505  
506 0 ▶sub render_preamble {
Subroutine render_preamble: not called CC=2 SCAR=17.9
Pod documented
507 0 my $self = shift;
508 0 Jifty->web->out(
509 0 ▶qq!<span class="preamble @{[$self->classes]}">@{[_($self->preamble) || '' ]}</span>\n!
Condition0%0/2
_($self->preamble) || ''
l!l
00
MC/DC0%0/1
_($self->preamble) || ''
_($self->preamble)
Truth table0%0/2
A
A= _($self->preamble)
Aresult
11
00
510 );
511  
512 0 return '';
513 }
514  
515 =head2 render_label
516  
517 Outputs the label of this form field, using a <label> HTML element
518 with the CSS class C<label> and whatever L</class> specifies. Returns
519 an empty string.
520  
521 =cut
522  
523 0 ▶sub render_label {
Subroutine render_label: not called CC=4 SCAR=30.0
Pod documented
524 0 my $self = shift;
525 0 ▶ return '' unless defined $self->label and length $self->label;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->label and CORE::length $self->label 0 0 0
Condition0%0/3
defined $self->label and CORE::length $self->label
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->label and CORE::length $self->label
defined $self->label CORE::length $self->label
Truth table0%0/3
A and B
A= defined $self->labelB= CORE::length $self->label
ABresult
0X0
100
111
526 0 ▶ if ( $self->render_mode eq 'update' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->render_mode eq 'update') { } 0 0 0
527 0 Jifty->web->out(
528 0 qq!<label class="label @{[$self->classes]}" for="@{[$self->element_id ]}">@{[_($self->label) ]}</label>\n!
529 );
530 } else {
531 0 Jifty->web->out(
532 0 qq!<span class="label @{[$self->classes]}">@{[_($self->label) ]}</span>\n!
533 );
534 }
535  
536 0 return '';
537 }
538  
539  
540 =head2 render_widget
541  
542 Outputs the actual entry widget for this form element. This defaults
543 to an <input> element, though subclasses commonly override this.
544 Returns an empty string.
545  
546 =cut
547  
548 0 ▶sub render_widget {
Subroutine render_widget: not called CC=8 SCAR=42.8
Pod documented
549 0 my $self = shift;
550 0 my $field = qq! <input !;
551 0 $field .= qq! type="@{[ $self->type ]}"!;
552 0 ▶ $field .= qq! name="@{[ $self->input_name ]}"! if ($self->input_name);
Branch: 1 branch
BranchTrueFalseTotal
if $self->input_name 0 0 0
553 0 ▶ $field .= qq! title="@{[ $self->title ]}"! if ($self->title);
Branch: 1 branch
BranchTrueFalseTotal
if $self->title 0 0 0
554 0 $field .= qq! id="@{[ $self->element_id ]}"!;
555 0 ▶ $field .= qq! value="@{[$self->canonicalize_value(Jifty->web->escape($self->current_value))]}"! if defined $self->current_value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->current_value 0 0 0
556 0 $field .= $self->_widget_class;
557  
558 0 ▶ if ($self->display_length) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->display_length) { } 0 0 0
elsif ($self->max_length) { } 0 0 0
559 0 $field .= qq! size="@{[ $self->display_length() ]}"!;
560 }
561 elsif ($self->max_length) {
562 0 $field .= qq! size="@{[ $self->max_length() ]}"!;
563 }
564  
565 0 ▶ $field .= qq! maxlength="@{[ $self->max_length() ]}"! if ($self->max_length());
Branch: 1 branch
BranchTrueFalseTotal
if $self->max_length 0 0 0
566 0 ▶ $field .= qq! autocomplete="off"! if defined $self->disable_autocomplete;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->disable_autocomplete 0 0 0
567 0 $field .= " " .$self->other_widget_properties;
568 0 $field .= $self->javascript;
569 0 $field .= qq! />\n!;
570 0 Jifty->web->out($field);
571 0 return '';
572 }
573  
574  
575 =head2 canonicalize_value
576  
577 Called when a value is about to be displayed. Can be overridden to, for example,
578 display only the C<YYYY-MM-DD> portion of a L<DateTime>.
579  
580 =cut
581  
582 0 ▶sub canonicalize_value {
Subroutine canonicalize_value: not called CC=1 SCAR=6.9
Pod documented
583 0 my $self = shift;
584 0 return $_[0];
585 }
586  
587 =head2 other_widget_properties
588  
589 If your widget subclass has other properties it wants to insert into the html
590 of the main widget and you haven't subclassed render_widget then you can just
591 subclass this.
592  
593 If you have subclassed render_widget then just stick them in your local sub
594 render_widget.
595  
596 We use this for marking password fields as not-autocomplete so the browser does
597 not try to use its form autocompletion on them.
598  
599  
600 =cut
601  
602 0 ▶sub other_widget_properties {''}
Subroutine other_widget_properties: not called CC=1 SCAR=6.9
Pod documented
603  
604 =head2 _widget_class
605  
606 Returns the "class=" line for our widget. Optionally takes extra classes to append to our list.
607  
608 =cut
609  
610 0 ▶sub _widget_class {
Subroutine _widget_class: not called CC=5 SCAR=34.0
611 0 my $self = shift;
612 0 ▶ my @classes = ( 'widget',
Branch: 4 branches
BranchTrueFalseTotal
$self->ajax_validates ? : 0 0 0
$self->ajax_canonicalizes ? : 0 0 0
$self->autocompleter ? : 0 0 0
$self->focus ? : 0 0 0
613 $self->classes,
614 ( $self->ajax_validates ? ' ajaxvalidation' : () ),
615 ( $self->ajax_canonicalizes ? ' ajaxcanonicalization' : () ),
616 ( $self->autocompleter ? ' ajaxautocompletes' : () ),
617 ( $self->focus ? ' focus' : ()),
618 @_ );
619  
620 0 return qq! class="!. join(' ',@classes). qq!"!
621  
622 }
623  
624 =head2 render_value
625  
626 Renders a "view" version of the widget for field. Usually, this is just plain text.
627  
628 =cut
629  
630  
631 0 ▶sub render_value {
Subroutine render_value: not called CC=2 SCAR=17.9
Pod documented
632 0 my $self = shift;
633 0 my $field = '<span';
634 0 $field .= qq! class="@{[ $self->classes ]} value"> !;
635 # XXX: force stringify the value because maketext is buggy with overloaded objects.
636 0 ▶ $field .= $self->canonicalize_value(Jifty->web->escape("@{[$self->current_value]}")) if defined $self->current_value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->current_value 0 0 0
637 0 $field .= qq!</span>\n!;
638 0 Jifty->web->out($field);
639 0 return '';
640 }
641  
642  
643  
644 =head2 render_autocomplete
645  
646 Renders the div tag and javascript necessary to do autocompletion for
647 this form field. Deprecated internally in favor of L</autocomplete_javascript>,
648 but kept for backwards compatibility since there exists external code that uses
649 it.
650  
651 =cut
652  
653 0 ▶sub render_autocomplete {
Subroutine render_autocomplete: not called CC=2 SCAR=17.9
Pod documented
654 0 my $self = shift;
655 0 ▶ return unless $self->autocompleter;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->autocompleter 0 0 0
656 0 Jifty->web->out(qq!<script type="text/javascript">@{[$self->autocomplete_javascript]}</script>!);
657 0 return '';
658 }
659  
660 =head2 autocomplete_javascript
661  
662 Returns renders the tiny snippet of javascript to make an autocomplete
663 call, if necessary.
664  
665 =cut
666  
667 0 ▶sub autocomplete_javascript {
Subroutine autocomplete_javascript: not called CC=2 SCAR=17.9
Pod documented
668 0 my $self = shift;
669 0 ▶ return unless($self->autocompleter);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->autocompleter 0 0 0
670 0 my $element_id = $self->element_id;
671 0 return "jQuery(document).ready(function(){Jifty.addAutocompleter('$element_id');});"
672 }
673  
674 =head2 placeholder_javascript
675  
676 Returns the javascript necessary to insert a placeholder into this
677 form field (greyed-out text that is written in using javascript, and
678 vanishes when the user focuses the field).
679  
680 =cut
681  
682 0 ▶sub placeholder_javascript {
Subroutine placeholder_javascript: not called CC=2 SCAR=17.9
Pod documented
683 0 my $self = shift;
684 0 ▶ return unless $self->placeholder;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->placeholder 0 0 0
685 0 my $placeholder = $self->placeholder;
686 0 $placeholder =~ s{(['\\])}{\\$1}g;
687 0 $placeholder =~ s{\n}{\\n}g;
688 0 $placeholder =~ s{\r}{\\r}g;
689 0 return qq!new Jifty.Placeholder('@{[$self->element_id]}', '$placeholder');!;
690 }
691  
692 =head2 preload_javascript
693  
694 Returns the javascript necessary to load regions that have been marked for
695 preloading, as plain javascript. The L</javascript> method will look for
696 regions marked with preloading and swap them in, instead of loading them
697 directly.
698  
699 =cut
700  
701 0 ▶sub preload_javascript {
Subroutine preload_javascript: not called CC=3 SCAR=24.8
Pod documented
702 0 my $self = shift;
703  
704 0 my $structure = $self->_javascript_attrs_structure;
705 0 my @javascript;
706 0 for my $trigger (keys %$structure) {
707 0 my $trigger_structure = $structure->{$trigger};
708 0 ▶ next unless $trigger_structure->{preload_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $trigger_structure->{"preload_key"} 0 0 0
709  
710 0 my @preloaded;
711  
712 my $preload_json = Jifty::JSON::encode_json(
713 {
714 fragments => $trigger_structure->{fragments},
715 preload_key => $trigger_structure->{preload_key},
716 }
717 0 );
718  
719 0 push @javascript, "Jifty.preload($preload_json, this);";
720 }
721  
722 0 return join "\n", @javascript;
723 }
724  
725 =head2 render_hints
726  
727 Renders any hints for using this input. Defaults to nothing, though
728 subclasses commonly override this. Returns an empty string.
729  
730 =cut
731  
732 0 ▶sub render_hints {
Subroutine render_hints: not called CC=2 SCAR=17.9
Pod documented
733 0 my $self = shift;
734 0 Jifty->web->out(
735 0 ▶qq!<span class="hints @{[$self->classes]}">@{[_($self->hints) || '']}</span>\n!
Condition0%0/2
_($self->hints) || ''
l!l
00
MC/DC0%0/1
_($self->hints) || ''
_($self->hints)
Truth table0%0/2
A
A= _($self->hints)
Aresult
11
00
736 );
737  
738 0 return '';
739  
740 }
741  
742  
743 =head2 render_errors
744  
745 Outputs a <div> with any errors for this action, even if there are
746 none -- AJAX could fill it in.
747  
748 =cut
749  
750 0 ▶sub render_errors {
Subroutine render_errors: not called CC=3 SCAR=24.8
Pod documented
751 0 my $self = shift;
752  
753 0 ▶ return unless $self->action;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->action 0 0 0
754  
755 0 Jifty->web->out(
756 0 ▶qq!<span class="error @{[$self->classes]}" id="@{[$self->action->error_div_id($self->name)]}">@{[ $self->action->result->field_error( $self->name ) || '']}</span>\n!
Condition0%0/2
$self->action->result->field_error($self->name) || ''
l!l
00
MC/DC0%0/1
$self->action->result->field_error($self->name) || ''
$self->action->result->field_error($self->name)
Truth table0%0/2
A
A= $self->action->result->field_error($self->name)
Aresult
11
00
757 );
758 0 return '';
759 }
760  
761 =head2 render_warnings
762  
763 Outputs a <div> with any warnings for this action, even if there are
764 none -- AJAX could fill it in.
765  
766 =cut
767  
768 0 ▶sub render_warnings {
Subroutine render_warnings: not called CC=3 SCAR=24.8
Pod documented
769 0 my $self = shift;
770  
771 0 ▶ return unless $self->action;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->action 0 0 0
772  
773 0 Jifty->web->out(
774 0 ▶qq!<span class="warning @{[$self->classes]}" id="@{[$self->action->warning_div_id($self->name)]}">@{[ $self->action->result->field_warning( $self->name ) || '']}</span>\n!
Condition0%0/2
$self->action->result->field_warning($self->name) || ''
l!l
00
MC/DC0%0/1
$self->action->result->field_warning($self->name) || ''
$self->action->result->field_warning($self->name)
Truth table0%0/2
A
A= $self->action->result->field_warning($self->name)
Aresult
11
00
775 );
776 0 return '';
777 }
778  
779 =head2 render_canonicalization_notes
780  
781 Outputs a <div> with any canonicalization notes for this action, even if there are
782 none -- AJAX could fill it in.
783  
784 =cut
785  
786 0 ▶sub render_canonicalization_notes {
Subroutine render_canonicalization_notes: not called CC=3 SCAR=24.8
Pod documented
787 0 my $self = shift;
788  
789 0 ▶ return unless $self->action;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->action 0 0 0
790  
791 0 Jifty->web->out(
792 0 ▶qq!<span class="canonicalization_note @{[$self->classes]}" id="@{[$self->action->canonicalization_note_div_id($self->name)]}">@{[$self->action->result->field_canonicalization_note( $self->name ) || '']}</span>\n!
Condition0%0/2
$self->action->result->field_canonicalization_note($self->name) || ''
l!l
00
MC/DC0%0/1
$self->action->result->field_canonicalization_note($self->name) || ''
$self->action->result->field_canonicalization_note($self->name)
Truth table0%0/2
A
A= $self->action->result->field_canonicalization_note($self->name)
Aresult
11
00
793 );
794 0 return '';
795 }
796  
797 =head2 available_values
798  
799 Returns the available values for this field.
800  
801 =cut
802  
803 0 ▶sub available_values {
Subroutine available_values: not called CC=6 SCAR=37.4
Pod documented
804 0 my $self = shift;
805  
806 # Setter
807 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
808 0 return $self->_available_values(@_);
809 }
810  
811 # If the available_values are available in the field..
812 0 ▶ if (my $available = $self->_available_values) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $available = $self->_available_values) 0 0 0
813 0 return @$available;
814 }
815  
816 # Otherwise consult the action
817  
818 0 my $values = $self->action->available_values($self->name);
819 0 ▶ if (!ref($values) || ref($values) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $values or ref $values ne "ARRAY") 0 0 0
Condition0%0/3
not ref $values or ref $values ne "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/2
not ref $values or ref $values ne "ARRAY"
not ref $values ref $values ne "ARRAY"
Truth table0%0/3
not(A) or B
A= not ref $valuesB= ref $values ne "ARRAY"
ABresult
1X1
011
000
820 0 ▶ die "available_values of parameter '" . $self->name . "' returned a " . (ref($values) || 'nonreference') . ", expected array reference";
Condition0%0/2
ref $values || 'nonreference'
l!l
00
MC/DC0%0/1
ref $values || 'nonreference'
ref $values
Truth table0%0/2
A
A= ref $values
Aresult
11
00
821 }
822  
823 0 return @$values;
824 }
825  
826 =for private
827  
828 =head2 length
829  
830 # Deprecated API
831  
832  
833 =cut
834  
835 0 ▶sub length {
Subroutine length: not called CC=1 SCAR=6.9
Pod documented
836 0 my $self = shift;
837 0 Carp::carp("->length is deprecated; use ->max_length instead");
838 0 $self->max_length(@_);
839 }
840  
841  
842 113 1;
« Jifty/Script/Server.pm ↑ Summary Jifty/View/Mason/Handler.pm »