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