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.001'; # TRIAL |
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
1838178
|
use Moose; |
|
2
|
|
|
|
|
291278
|
|
|
2
|
|
|
|
|
13
|
|
6
|
|
|
|
|
|
|
extends 'HTML::FormHandler::Field::NoValue'; |
7
|
2
|
|
|
2
|
|
9997
|
use namespace::autoclean; |
|
2
|
|
|
|
|
5946
|
|
|
2
|
|
|
|
|
12
|
|
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
967
|
use JavaScript::Minifier::XS qw(); |
|
2
|
|
|
|
|
1039
|
|
|
2
|
|
|
|
|
554
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
has 'js_code' => ( |
12
|
|
|
|
|
|
|
is => 'rw', |
13
|
|
|
|
|
|
|
isa => 'Str', |
14
|
|
|
|
|
|
|
builder => 'build_js_code', |
15
|
|
|
|
|
|
|
lazy => 1 |
16
|
|
|
|
|
|
|
); |
17
|
0
|
|
|
0
|
0
|
0
|
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
|
3
|
|
|
3
|
0
|
5
|
my $self = shift; |
35
|
|
|
|
|
|
|
|
36
|
3
|
|
|
|
|
143
|
my $set_js_code = $self->set_js_code; |
37
|
3
|
|
66
|
|
|
16
|
$set_js_code ||= "js_code_" . HTML::FormHandler::Field::convert_full_name( $self->full_name ); |
38
|
2
|
|
|
2
|
|
3
|
return sub { my $self = shift; $self->wrap_js_code( $self->form->$set_js_code($self) ); } |
|
2
|
|
|
|
|
50
|
|
39
|
3
|
100
|
66
|
|
|
270
|
if ( $self->form && $self->form->can($set_js_code) ); |
40
|
|
|
|
|
|
|
return sub { |
41
|
3
|
|
|
3
|
|
4
|
my $self = shift; |
42
|
3
|
|
|
|
|
122
|
return $self->wrap_js_code( $self->js_code ); |
43
|
1
|
|
|
|
|
143
|
}; |
44
|
|
|
|
|
|
|
} ## end sub build_render_method |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub _result_from_object { |
47
|
0
|
|
|
0
|
|
0
|
my ( $self, $result, $value ) = @_; |
48
|
0
|
|
|
|
|
0
|
$self->_set_result($result); |
49
|
0
|
|
|
|
|
0
|
$self->value($value); |
50
|
0
|
|
|
|
|
0
|
$result->_set_field_def($self); |
51
|
0
|
|
|
|
|
0
|
return $result; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub wrap_js_code { |
55
|
5
|
|
|
5
|
0
|
20
|
my $self = shift; |
56
|
5
|
|
|
|
|
4
|
my $javascript = shift; |
57
|
|
|
|
|
|
|
|
58
|
5
|
|
|
|
|
6
|
my $output = qq{\n<script type="text/javascript">}; |
59
|
5
|
100
|
|
|
|
175
|
$output .= $self->do_minify ? JavaScript::Minifier::XS::minify($javascript) : "\n".$javascript; |
60
|
5
|
|
|
|
|
10
|
$output .= qq{\n</script>}; |
61
|
|
|
|
|
|
|
|
62
|
5
|
|
|
|
|
35
|
return $output; |
63
|
|
|
|
|
|
|
} ## end sub wrap_js_code |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
68
|
2
|
|
|
2
|
|
10
|
use namespace::autoclean; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
12
|
|
69
|
|
|
|
|
|
|
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.001 |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 SYNOPSIS |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
This class can be used for fields that need to supply JavaScript code |
88
|
|
|
|
|
|
|
for use by scripts in the form. It will render the value returned by a |
89
|
|
|
|
|
|
|
form's C<js_code_[field_name]> 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( $self->something ) { |
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 rendering 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
|
|
|
|
|
|
|
=head1 FIELD OPTIONS |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
We support the following additional field options, over what is inherited from |
137
|
|
|
|
|
|
|
L<HTML::FormHandler::Field> |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head2 do_minify |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Boolean to indicate whether code should be minified using L<JavaScript::Minifier::XS> |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head1 AUTHOR |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Charlie Garrison <garrison@zeta.org.au> |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
This software is copyright (c) 2014 by Charlie Garrison. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
152
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=cut |