JavaScript.pm

filter: Statementstmt 85.7% 36 / 42 Branchbran 100% 4 / 4 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 80.0% 8 / 10 Podpod 33.3% 1 / 3 totaltotal 82.0% 55 / 67 CC 5 SCAR 7.6
CC
5
Cov
86%
build_render_method
4.0
build_js_code
2.0
_result_from_object
2.0
CRAP 2.1
« JSON.pm ↑ Summary
1 package HTML::FormHandlerX::Field::JavaScript;
2 # ABSTRACT: a script tag with javascript code supplied via field for HTML::FormHandler.
3 2 $HTML::FormHandlerX::Field::JavaScript::VERSION = '0.003';
4  
5 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 extends 'HTML::FormHandler::Field::NoValue';
7 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use JavaScript::Minifier::XS qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 has 'js_code' => (
12 is => 'rw',
13 isa => 'Str',
14 builder => 'build_js_code',
15 lazy => 1
16 );
17 0 ▶sub build_js_code { '' }
Subroutine build_js_code: not called CC=1 SCAR=6.9
Pod undocumented
18 2 has 'set_js_code' => ( isa => 'Str', is => 'ro' );
19 2 has '+do_label' => ( default => 0 );
20 #has '+do_wrapper' => ( default => 0 );
21 2 has 'do_minify' => ( isa => 'Bool', is => 'rw', default => 0 );
22  
23 2 has 'render_method' => (
24 traits => ['Code'],
25 is => 'ro',
26 isa => 'CodeRef',
27 lazy => 1,
28 predicate => 'does_render_method',
29 handles => { 'render' => 'execute_method' },
30 builder => 'build_render_method',
31 );
32  
33 sub build_render_method {
34 3 ▶ my $self = shift;
Subroutine build_render_method: called CC=4 SCAR=13.9
Pod undocumented
35  
36 3 my $set_js_code = $self->set_js_code;
37 3 ▶ $set_js_code ||= "js_code_" . HTML::FormHandler::Field::convert_full_name( $self->full_name );
Condition100%2/2
$set_js_code ||= 'js_code_' . HTML::FormHandler::Field::convert_full_name($self->full_name)
l!l
12
MC/DC100%1/1
$set_js_code ||= 'js_code_' . HTML::FormHandler::Field::convert_full_name($self->full_name)
$set_js_code
Truth table100%2/2
A
A= $set_js_code
Aresult
11
00
38 2 ▶ return sub { my $self = shift; $self->wrap_js_code( $self->form->$set_js_code($self) ); }
Subroutine __ANON__: called CC=1 SCAR=0.0
39 3 ▶ if ( $self->form && $self->form->can($set_js_code) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->form and $self->form->can($set_js_code) 2 1 3
Condition66%2/3
$self->form and $self->form->can($set_js_code)
!ll&&!rl&&r
012
MC/DC50%1/2
$self->form and $self->form->can($set_js_code)
$self->form $self->form->can($set_js_code)
Truth table66%2/3
A and B
A= $self->formB= $self->form->can($set_js_code)
ABresult
0X0
100
111
40 return sub {
41 3 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
42 3 return $self->wrap_js_code( $self->js_code );
43 1 };
44 } ## end sub build_render_method
45  
46 sub _result_from_object {
47 0 ▶ my ( $self, $result, $value ) = @_;
Subroutine _result_from_object: not called CC=1 SCAR=6.9
48 0 $self->_set_result($result);
49 0 $self->value($value);
50 0 $result->_set_field_def($self);
51 0 return $result;
52 }
53  
54 sub wrap_js_code {
55 5 ▶ my $self = shift;
Subroutine wrap_js_code: called CC=2 SCAR=6.9
Pod documented
56 5 my $javascript = shift;
57  
58 5 my $output = qq{\n<script type="text/javascript">};
59 5 ▶ $output .= $self->do_minify ? JavaScript::Minifier::XS::minify($javascript) : "\n".$javascript;
Branch: 1 branch
BranchTrueFalseTotal
$self->do_minify ? : 2 3 5
60 5 $output .= qq{\n</script>};
61  
62 5 return $output;
63 } ## end sub wrap_js_code
64  
65  
66  
67 2 __PACKAGE__->meta->make_immutable;
68 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 2 1;
70  
71 __END__
72  
73 =pod
74  
75 =encoding UTF-8
76  
77 =head1 NAME
78  
79 HTML::FormHandlerX::Field::JavaScript - a script tag with javascript code supplied via field for HTML::FormHandler.
80  
81 =head1 VERSION
82  
83 version 0.003
84  
85 =head1 SYNOPSIS
86  
87 This class can be used for fields that need to supply JavaScript code
88 to control or modify the form. It will render the value returned by a
89 form's C<js_code_E<lt>field_nameE<gt>> method, or the field's C<js_code>
90 attribute.
91  
92 has_field 'user_update' => ( type => 'JavaScript',
93 js_code => q`$('#fldId').on('change', myFunction);`
94 );
95  
96 or using a method:
97  
98 has_field 'user_update' => ( type => 'JavaScript' );
99 sub js_code_user_update {
100 my ( $self, $field ) = @_;
101 if( $field->value == 'foo' ) {
102 return q`$('#fldId').on('change', myFunction);`;
103 }
104 else {
105 return q`$('#otherFldId').on('change', myOtherFunction);`;
106 }
107 }
108 #----
109 has_field 'usernames' => ( type => 'JavaScript' );
110 sub js_code_usernames {
111 my ( $self, $field ) = @_;
112 return q`$('#fldId').on('change', myFunction);`;
113 }
114  
115 or set the name of the code generation method:
116  
117 has_field 'user_update' => ( type => 'JavaScript', set_js_code => 'my_user_update' );
118 sub my_user_update {
119 ....
120 }
121  
122 or provide a 'render_method':
123  
124 has_field 'user_update' => ( type => 'JavaScript', render_method => \&render_user_update );
125 sub render_user_update {
126 my $self = shift;
127 ....
128 return q(
129 <script type="text/javascript">
130 // code here
131 </script>);
132 }
133  
134 The code generation methods should return a scalar string which will be
135 wrapped in script tags. If you supply your own 'render_method' then you
136 are responsible for calling C<$self-E<gt>wrap_data> yourself.
137  
138 =head1 FIELD OPTIONS
139  
140 We support the following additional field options, over what is inherited from
141 L<HTML::FormHandler::Field>
142  
143 =over
144  
145 =item js_code
146  
147 String containing the JavaScript code to be rendered inside script tags.
148  
149 =item set_js_code
150  
151 Name of method that gets called to generate the JavaScript code.
152  
153 =item do_minify
154  
155 Boolean to indicate whether code should be minified using L<JavaScript::Minifier::XS>
156  
157 =back
158  
159 =head1 FIELD METHODS
160  
161 The following methods can be called on the field.
162  
163 =over
164  
165 =item wrap_js_code
166  
167 The C<wrap_js_code> method minifies the JavaScript code and wraps it in script tags.
168  
169 =back
170  
171 =head1 AUTHOR
172  
173 Charlie Garrison <garrison@zeta.org.au>
174  
175 =head1 COPYRIGHT AND LICENSE
176  
177 This software is copyright (c) 2014 by Charlie Garrison.
178  
179 This is free software; you can redistribute it and/or modify it under
180 the same terms as the Perl 5 programming language system itself.
181  
182 =cut
« JSON.pm ↑ Summary