line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
15
|
|
|
15
|
|
312907
|
use 5.006; |
|
15
|
|
|
|
|
56
|
|
2
|
15
|
|
|
15
|
|
91
|
use strict; |
|
15
|
|
|
|
|
29
|
|
|
15
|
|
|
|
|
346
|
|
3
|
15
|
|
|
15
|
|
85
|
use warnings; |
|
15
|
|
|
|
|
28
|
|
|
15
|
|
|
|
|
929
|
|
4
|
|
|
|
|
|
|
package HTML::Widget::Factory; |
5
|
|
|
|
|
|
|
# ABSTRACT: churn out HTML widgets |
6
|
|
|
|
|
|
|
$HTML::Widget::Factory::VERSION = '0.204'; |
7
|
15
|
|
|
15
|
|
88
|
use Carp (); |
|
15
|
|
|
|
|
27
|
|
|
15
|
|
|
|
|
301
|
|
8
|
15
|
|
|
15
|
|
12112
|
use Module::Load (); |
|
15
|
|
|
|
|
16216
|
|
|
15
|
|
|
|
|
303
|
|
9
|
15
|
|
|
15
|
|
10892
|
use MRO::Compat; |
|
15
|
|
|
|
|
50889
|
|
|
15
|
|
|
|
|
12963
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
#pod =head1 SYNOPSIS |
12
|
|
|
|
|
|
|
#pod |
13
|
|
|
|
|
|
|
#pod my $factory = HTML::Widget::Factory->new(); |
14
|
|
|
|
|
|
|
#pod |
15
|
|
|
|
|
|
|
#pod my $html = $factory->select({ |
16
|
|
|
|
|
|
|
#pod name => 'flavor', |
17
|
|
|
|
|
|
|
#pod options => [ |
18
|
|
|
|
|
|
|
#pod [ minty => 'Peppermint', ], |
19
|
|
|
|
|
|
|
#pod [ perky => 'Fresh and Warm', ], |
20
|
|
|
|
|
|
|
#pod [ super => 'Red and Blue', ], |
21
|
|
|
|
|
|
|
#pod ], |
22
|
|
|
|
|
|
|
#pod value => 'minty', |
23
|
|
|
|
|
|
|
#pod }); |
24
|
|
|
|
|
|
|
#pod |
25
|
|
|
|
|
|
|
#pod =head1 DESCRIPTION |
26
|
|
|
|
|
|
|
#pod |
27
|
|
|
|
|
|
|
#pod HTML::Widget::Factory provides a simple, pluggable system for constructing HTML |
28
|
|
|
|
|
|
|
#pod form controls. |
29
|
|
|
|
|
|
|
#pod |
30
|
|
|
|
|
|
|
#pod =cut |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
#pod =head1 METHODS |
33
|
|
|
|
|
|
|
#pod |
34
|
|
|
|
|
|
|
#pod Most of the useful methods in an HTML::Widget::Factory object will be provided |
35
|
|
|
|
|
|
|
#pod by its plugins. Consult the documentation for the HTML::Widget::Plugin |
36
|
|
|
|
|
|
|
#pod modules. |
37
|
|
|
|
|
|
|
#pod |
38
|
|
|
|
|
|
|
#pod =head2 new |
39
|
|
|
|
|
|
|
#pod |
40
|
|
|
|
|
|
|
#pod my $factory = HTML::Widget::Factory->new(\%arg); |
41
|
|
|
|
|
|
|
#pod |
42
|
|
|
|
|
|
|
#pod This constructor returns a new widget factory. |
43
|
|
|
|
|
|
|
#pod |
44
|
|
|
|
|
|
|
#pod The only valid arguments are C and C, which provide |
45
|
|
|
|
|
|
|
#pod arrayrefs of plugins to be used. If C is not given, the default |
46
|
|
|
|
|
|
|
#pod plugin list is used, which is those plugins that ship with |
47
|
|
|
|
|
|
|
#pod HTML::Widget::Factory. The plugins in C are loaded in addition |
48
|
|
|
|
|
|
|
#pod to these. |
49
|
|
|
|
|
|
|
#pod |
50
|
|
|
|
|
|
|
#pod Plugins may be provided as class names or as objects. |
51
|
|
|
|
|
|
|
#pod |
52
|
|
|
|
|
|
|
#pod =cut |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
my %default_instance; |
55
|
|
|
|
|
|
|
sub _default_instance { |
56
|
0
|
|
0
|
0
|
|
0
|
$default_instance{ $_[0] } ||= $_[0]->new; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
my $LOADED_DEFAULTS; |
60
|
|
|
|
|
|
|
my @DEFAULT_PLUGINS = qw( |
61
|
|
|
|
|
|
|
HTML::Widget::Plugin::Attrs |
62
|
|
|
|
|
|
|
HTML::Widget::Plugin::Button |
63
|
|
|
|
|
|
|
HTML::Widget::Plugin::Checkbox |
64
|
|
|
|
|
|
|
HTML::Widget::Plugin::Image |
65
|
|
|
|
|
|
|
HTML::Widget::Plugin::Input |
66
|
|
|
|
|
|
|
HTML::Widget::Plugin::Link |
67
|
|
|
|
|
|
|
HTML::Widget::Plugin::Multiselect |
68
|
|
|
|
|
|
|
HTML::Widget::Plugin::Password |
69
|
|
|
|
|
|
|
HTML::Widget::Plugin::Radio |
70
|
|
|
|
|
|
|
HTML::Widget::Plugin::Select |
71
|
|
|
|
|
|
|
HTML::Widget::Plugin::Submit |
72
|
|
|
|
|
|
|
HTML::Widget::Plugin::Textarea |
73
|
|
|
|
|
|
|
); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub _default_plugins { |
76
|
16
|
|
66
|
16
|
|
93
|
$LOADED_DEFAULTS ||= do { |
77
|
15
|
|
|
|
|
124
|
Module::Load::load("$_") for @DEFAULT_PLUGINS; |
78
|
15
|
|
|
|
|
227
|
1; |
79
|
|
|
|
|
|
|
}; |
80
|
16
|
|
|
|
|
105
|
return @DEFAULT_PLUGINS; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub new { |
84
|
21
|
|
|
21
|
1
|
2914
|
my ($self, $arg) = @_; |
85
|
21
|
|
100
|
|
|
138
|
$arg ||= {}; |
86
|
|
|
|
|
|
|
|
87
|
21
|
|
66
|
|
|
111
|
my $class = ref $self || $self; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# XXX: I think we need to use default plugins when new is invoked on the |
90
|
|
|
|
|
|
|
# class, but get the parent object's plugins when it's called on an existing |
91
|
|
|
|
|
|
|
# factory. -- rjbs, 2014-02-21 |
92
|
|
|
|
|
|
|
my @plugins = $arg->{plugins} |
93
|
21
|
100
|
|
|
|
119
|
? @{ $arg->{plugins} } |
|
5
|
|
|
|
|
19
|
|
94
|
|
|
|
|
|
|
: $class->_default_plugins; |
95
|
|
|
|
|
|
|
|
96
|
21
|
100
|
|
|
|
108
|
unshift @plugins, @{ $self->{plugins} } if ref $self; |
|
1
|
|
|
|
|
2
|
|
97
|
|
|
|
|
|
|
|
98
|
21
|
100
|
66
|
|
|
137
|
if ($arg->{plugins} or $arg->{extra_plugins}) { |
99
|
5
|
50
|
|
|
|
21
|
push @plugins, @{ $arg->{extra_plugins} } if $arg->{extra_plugins}; |
|
0
|
|
|
|
|
0
|
|
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# make sure plugins given as classes become objects |
103
|
21
|
|
100
|
|
|
260
|
ref $_ or $_ = $_->new for @plugins; |
104
|
|
|
|
|
|
|
|
105
|
21
|
|
|
|
|
46
|
my %widget; |
106
|
21
|
|
|
|
|
59
|
for my $plugin (@plugins) { |
107
|
201
|
|
|
|
|
624
|
for my $widget ($plugin->provided_widgets) { |
108
|
220
|
100
|
|
|
|
627
|
my ($method, $name) = ref $widget ? @$widget : ($widget) x 2; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Carp::croak "$plugin tried to provide $name, already provided by $widget{$name}{plugin}" |
111
|
220
|
50
|
|
|
|
527
|
if $widget{$name}; |
112
|
|
|
|
|
|
|
|
113
|
220
|
50
|
|
|
|
740
|
Carp::croak |
114
|
|
|
|
|
|
|
"$plugin claims to provide widget via ->$method but has no such method" |
115
|
|
|
|
|
|
|
unless $plugin->can($method); |
116
|
|
|
|
|
|
|
|
117
|
220
|
|
|
|
|
885
|
$widget{$name} = { plugin => $plugin, method => $method }; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
# for some reason PPI/Perl::Critic think this is multiple statements: |
122
|
|
|
|
|
|
|
bless { ## no critic |
123
|
21
|
|
|
|
|
182
|
plugins => \@plugins, |
124
|
|
|
|
|
|
|
widgets => \%widget, |
125
|
|
|
|
|
|
|
} => $class; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
#pod =head2 provides_widget |
129
|
|
|
|
|
|
|
#pod |
130
|
|
|
|
|
|
|
#pod if ($factory->provides_widget($name)) { ... } |
131
|
|
|
|
|
|
|
#pod |
132
|
|
|
|
|
|
|
#pod This method returns true if the given name is a widget provided by the factory. |
133
|
|
|
|
|
|
|
#pod This, and not C should be used to determine whether a factory can provide |
134
|
|
|
|
|
|
|
#pod a given widget. |
135
|
|
|
|
|
|
|
#pod |
136
|
|
|
|
|
|
|
#pod =cut |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub provides_widget { |
139
|
10
|
|
|
10
|
1
|
2860
|
my ($self, $name) = @_; |
140
|
10
|
50
|
|
|
|
30
|
$self = $self->_default_instance unless ref $self; |
141
|
|
|
|
|
|
|
|
142
|
10
|
100
|
|
|
|
58
|
return 1 if $self->{widgets}{$name}; |
143
|
|
|
|
|
|
|
|
144
|
1
|
|
|
|
|
4
|
return; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
#pod =head2 provided_widgets |
148
|
|
|
|
|
|
|
#pod |
149
|
|
|
|
|
|
|
#pod for my $name ($fac->provided_widgets) { ... } |
150
|
|
|
|
|
|
|
#pod |
151
|
|
|
|
|
|
|
#pod This method returns an unordered list of the names of the widgets provided by |
152
|
|
|
|
|
|
|
#pod this factory. |
153
|
|
|
|
|
|
|
#pod |
154
|
|
|
|
|
|
|
#pod =cut |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub provided_widgets { |
157
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
158
|
0
|
0
|
|
|
|
0
|
$self = $self->_default_instance unless ref $self; |
159
|
|
|
|
|
|
|
|
160
|
0
|
|
|
|
|
0
|
return keys %{ $self->{widgets} }; |
|
0
|
|
|
|
|
0
|
|
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
my $ErrorMsg = qq{Can\'t locate object method "%s" via package "%s" }. |
164
|
|
|
|
|
|
|
qq{at %s line %d.\n}; |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
sub AUTOLOAD { |
167
|
58
|
|
|
58
|
|
123774
|
my $widget_name = our $AUTOLOAD; |
168
|
58
|
|
|
|
|
297
|
$widget_name =~ s/.*:://; |
169
|
|
|
|
|
|
|
|
170
|
58
|
50
|
33
|
|
|
393
|
return if $widget_name eq 'DESTROY' or $widget_name eq 'CLONE'; |
171
|
|
|
|
|
|
|
|
172
|
58
|
|
|
|
|
130
|
my ($self, $given_arg) = @_; |
173
|
58
|
|
33
|
|
|
160
|
my $class = ref $self || $self; |
174
|
58
|
|
|
|
|
168
|
my $howto = $self->{widgets}{$widget_name}; |
175
|
|
|
|
|
|
|
|
176
|
58
|
50
|
|
|
|
137
|
unless ($howto) { |
177
|
0
|
|
|
|
|
0
|
my ($callpack, $callfile, $callline) = caller; |
178
|
0
|
|
|
|
|
0
|
die sprintf $ErrorMsg, $widget_name, $class, $callfile, $callline; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
58
|
|
|
|
|
194
|
return $self->_build_widget(@$howto{qw(plugin method)}, $given_arg); |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
sub _build_widget { |
185
|
58
|
|
|
58
|
|
119
|
my ($self, $plugin, $method, $given_arg) = @_; |
186
|
|
|
|
|
|
|
|
187
|
58
|
|
|
|
|
273
|
my $arg = $plugin->rewrite_arg($given_arg, $method); |
188
|
|
|
|
|
|
|
|
189
|
58
|
|
|
|
|
221
|
return $plugin->$method($self, $arg); |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
sub can { |
193
|
22
|
|
|
22
|
0
|
643
|
my ($self, $method) = @_; |
194
|
|
|
|
|
|
|
|
195
|
0
|
|
|
0
|
|
0
|
return sub { $self->$method(@_) } |
196
|
22
|
50
|
33
|
|
|
122
|
if ref $self and $self->{widgets}{$method}; |
197
|
|
|
|
|
|
|
|
198
|
22
|
|
|
|
|
294
|
return $self->SUPER::can($method); |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
#pod =head2 plugins |
202
|
|
|
|
|
|
|
#pod |
203
|
|
|
|
|
|
|
#pod This returns a list of the plugins loaded by the factory. |
204
|
|
|
|
|
|
|
#pod |
205
|
|
|
|
|
|
|
#pod =cut |
206
|
|
|
|
|
|
|
|
207
|
1
|
|
|
1
|
1
|
494
|
sub plugins { @{ $_[0]->{plugins} } } |
|
1
|
|
|
|
|
9
|
|
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
#pod =head1 TODO |
210
|
|
|
|
|
|
|
#pod |
211
|
|
|
|
|
|
|
#pod =over |
212
|
|
|
|
|
|
|
#pod |
213
|
|
|
|
|
|
|
#pod =item * fixed_args for args that are fixed, like (type => 'checkbox') |
214
|
|
|
|
|
|
|
#pod |
215
|
|
|
|
|
|
|
#pod =item * a simple way to say "only include this output if you haven't before" |
216
|
|
|
|
|
|
|
#pod |
217
|
|
|
|
|
|
|
#pod This will make it easy to do JavaScript inclusions: if you've already made a |
218
|
|
|
|
|
|
|
#pod calendar (or whatever) widget, don't bother including this hunk of JS, for |
219
|
|
|
|
|
|
|
#pod example. |
220
|
|
|
|
|
|
|
#pod |
221
|
|
|
|
|
|
|
#pod =item * giving the constructor a data store |
222
|
|
|
|
|
|
|
#pod |
223
|
|
|
|
|
|
|
#pod Create a factory that has a CGI.pm object and let it default values to the |
224
|
|
|
|
|
|
|
#pod param that matches the passed name. |
225
|
|
|
|
|
|
|
#pod |
226
|
|
|
|
|
|
|
#pod =item * include id attribute where needed |
227
|
|
|
|
|
|
|
#pod |
228
|
|
|
|
|
|
|
#pod =item * optional labels (before or after control, or possibly return a list) |
229
|
|
|
|
|
|
|
#pod |
230
|
|
|
|
|
|
|
#pod =back |
231
|
|
|
|
|
|
|
#pod |
232
|
|
|
|
|
|
|
#pod =head1 SEE ALSO |
233
|
|
|
|
|
|
|
#pod |
234
|
|
|
|
|
|
|
#pod =over |
235
|
|
|
|
|
|
|
#pod |
236
|
|
|
|
|
|
|
#pod =item L |
237
|
|
|
|
|
|
|
#pod |
238
|
|
|
|
|
|
|
#pod =item L |
239
|
|
|
|
|
|
|
#pod |
240
|
|
|
|
|
|
|
#pod =item L |
241
|
|
|
|
|
|
|
#pod |
242
|
|
|
|
|
|
|
#pod =item L |
243
|
|
|
|
|
|
|
#pod |
244
|
|
|
|
|
|
|
#pod =item L |
245
|
|
|
|
|
|
|
#pod |
246
|
|
|
|
|
|
|
#pod =item L |
247
|
|
|
|
|
|
|
#pod |
248
|
|
|
|
|
|
|
#pod =item L |
249
|
|
|
|
|
|
|
#pod |
250
|
|
|
|
|
|
|
#pod =item L |
251
|
|
|
|
|
|
|
#pod |
252
|
|
|
|
|
|
|
#pod =item L |
253
|
|
|
|
|
|
|
#pod |
254
|
|
|
|
|
|
|
#pod =item L |
255
|
|
|
|
|
|
|
#pod |
256
|
|
|
|
|
|
|
#pod =item L |
257
|
|
|
|
|
|
|
#pod |
258
|
|
|
|
|
|
|
#pod =item L |
259
|
|
|
|
|
|
|
#pod |
260
|
|
|
|
|
|
|
#pod =item L |
261
|
|
|
|
|
|
|
#pod |
262
|
|
|
|
|
|
|
#pod =back |
263
|
|
|
|
|
|
|
#pod |
264
|
|
|
|
|
|
|
#pod =cut |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
1; |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
__END__ |