1 |
package Frey::Action; |
2 |
use Moose; |
3 |
extends 'Frey::PPI'; |
4 |
with 'Frey::Web'; |
5 |
with 'Frey::Config'; |
6 |
|
7 |
use Clone qw/clone/; |
8 |
use Data::Dump qw/dump/; |
9 |
|
10 |
=head1 DESCRIPTION |
11 |
|
12 |
Invoke any L<Frey> object creating html for with various default parameters |
13 |
if not supplied at invocation. |
14 |
|
15 |
=cut |
16 |
|
17 |
has 'class' => ( |
18 |
is => 'rw', |
19 |
isa => 'Str', |
20 |
required => 1, |
21 |
); |
22 |
|
23 |
has 'params' => ( |
24 |
is => 'rw', |
25 |
isa => 'HashRef', |
26 |
default => sub { {} }, |
27 |
); |
28 |
|
29 |
has 'input_resize_step' => ( |
30 |
documentation => 'Resize input fields by this step', |
31 |
is => 'rw', |
32 |
isa => 'Int', |
33 |
# required => 1, |
34 |
default => 20, |
35 |
); |
36 |
|
37 |
=head2 required |
38 |
|
39 |
my @required_attributes = $self->required; |
40 |
my $required_attributes = $self->required; |
41 |
|
42 |
=cut |
43 |
|
44 |
sub required { |
45 |
my ( $self ) = @_; |
46 |
$self->load_class( $self->class ); |
47 |
|
48 |
my @required = |
49 |
grep { |
50 |
defined $_ && $_->can('name') && |
51 |
! defined( $self->params->{ $_->name } ) && |
52 |
! $_->is_lazy |
53 |
} |
54 |
map { |
55 |
my $attr = $self->class->meta->get_attribute($_); |
56 |
blessed $attr && $attr->is_required && $attr; |
57 |
} $self->class->meta->get_attribute_list; |
58 |
|
59 |
warn "## required = ",dump( map { $_->name } @required ), " for ", $self->class if @required && $self->debug; |
60 |
return @required if wantarray; |
61 |
return \@required; |
62 |
} |
63 |
|
64 |
=head2 attributes |
65 |
|
66 |
Generated from attributes specified in code (extracted using L<Frey::PPI>) |
67 |
and required atributes |
68 |
|
69 |
my @class_attributes = $self->attributes; |
70 |
my @class_attributes = $self->attributes; |
71 |
|
72 |
=cut |
73 |
|
74 |
sub attributes { |
75 |
my ( $self ) = @_; |
76 |
my $a; |
77 |
my @attrs = @{ $self->attribute_order }; |
78 |
@attrs = map { $a->{$_}++; $_ } @attrs; |
79 |
push @attrs, $_ foreach grep { ! $a->{$_} } map { $_->name } @{ $self->required }; |
80 |
warn "# attributes = ",dump( @attrs ) if $self->debug; |
81 |
return @attrs if wantarray; |
82 |
return \@attrs; |
83 |
} |
84 |
|
85 |
=head2 params_form |
86 |
|
87 |
my $html = $self->params_form; |
88 |
my ($html,$default_params) = $self->params_form; |
89 |
|
90 |
=cut |
91 |
|
92 |
sub params_form { |
93 |
my ( $self ) = @_; |
94 |
my @required = $self->required; |
95 |
if ( ! @required ) { |
96 |
warn "all params available ", dump( $self->params ), " not creating form" if $self->debug; |
97 |
return (undef,$self->params) if wantarray; |
98 |
return; |
99 |
} else { |
100 |
warn $self->class, " required params ", map { $_->dump(2) } @required if $self->debug; |
101 |
} |
102 |
|
103 |
my $class = $self->class; |
104 |
|
105 |
$self->load_class( $class ); |
106 |
|
107 |
my $default = clone $self->params; # XXX we really don't want to modify params! |
108 |
|
109 |
my $params_config = {}; |
110 |
$params_config = $self->config($class); |
111 |
warn "# $class config = ",dump( $params_config ) if $self->debug; |
112 |
|
113 |
my $form; |
114 |
|
115 |
sub select_values { |
116 |
my ( $name, $attr_type, $values ) = @_; |
117 |
|
118 |
if ( $#$values > 3 ) { |
119 |
my $options = join("\n", |
120 |
map { |
121 |
my $v = ref($_) eq 'HASH' ? $_->{$name} : $_; |
122 |
qq|<option value="$v">$v</option>| if $v; |
123 |
} @$values |
124 |
); |
125 |
qq|<select title="$attr_type" name="$name">$options</select>| if $options; |
126 |
} else { |
127 |
my $radio = join("\n", |
128 |
map { |
129 |
my $v = ref($_) eq 'HASH' ? $_->{$name} : $_; |
130 |
qq|<input type="radio" name="$name" value="$v" title="$attr_type"><span style="float: left; padding-right: 2em;">$v</span>|; |
131 |
} @$values |
132 |
); |
133 |
qq|<div>$radio</div>|; |
134 |
} |
135 |
} |
136 |
|
137 |
foreach my $checkbox ( split(/\s+/, $default->{'frey-checkboxes'} ) ) { |
138 |
next if defined $default->{ $checkbox }; |
139 |
|
140 |
$default->{ $checkbox } = 0; |
141 |
$self->params->{ $checkbox } = 0; |
142 |
warn "# checkbox $checkbox not ticked"; |
143 |
} |
144 |
|
145 |
my @checkboxes; |
146 |
|
147 |
my $label_width = 1; # minimum |
148 |
|
149 |
foreach my $name ( |
150 |
grep { |
151 |
! $class->meta->get_attribute($_)->is_lazy |
152 |
&& ! defined $default->{$_} |
153 |
&& ! m{^_} # skip _private |
154 |
} $self->attributes |
155 |
) { |
156 |
my $attr_type = ''; |
157 |
my $type = $name =~ m/^pass/ ? 'password' : 'text'; |
158 |
my $label = $name; |
159 |
my $label_title = ''; |
160 |
my $value_html = ''; |
161 |
|
162 |
my $attr = $class->meta->get_attribute( $name ); |
163 |
$attr_type = $attr->type_constraint->name if $attr->has_type_constraint; |
164 |
|
165 |
my $value = |
166 |
defined $default->{$name} ? $default->{$name} : |
167 |
$attr->has_default ? $attr->default( $name ) : |
168 |
''; |
169 |
|
170 |
if ( ref($params_config) eq 'HASH' ) { |
171 |
$value = $params_config->{$name}; |
172 |
} elsif ( ref($params_config) eq 'ARRAY' ) { |
173 |
$value_html = select_values( $name, $attr_type, $params_config ); |
174 |
$default->{$name} = $params_config->[0]->{$name}; |
175 |
} elsif ( $attr->has_type_constraint && $attr->type_constraint->can('values') ) { |
176 |
$value_html = select_values( $name, $attr_type, $attr->type_constraint->values ); |
177 |
} elsif ( $attr_type =~ m{^Bool} ) { |
178 |
my $suffix = ''; |
179 |
$suffix = ' checked' if $value; |
180 |
$value_html = qq|<input type="checkbox" name="$name" title="$attr_type" value="$value"$suffix>|; |
181 |
push @checkboxes, $name; |
182 |
} elsif ( ! defined $value ) { |
183 |
$value_html = qq|<tt id="$name">undef</tt><!-- $name = undef -->|; # FIXME if $self->debug |
184 |
} elsif ( $attr_type !~ m{^(Str|Int)$} || $value =~ $Frey::Web::re_html ) { |
185 |
$value_html = qq|<textarea name="$name" title="$attr_type">$value</textarea>|; |
186 |
} |
187 |
|
188 |
$label_title = qq| title="| . $attr->documentation . qq|"| if $attr->has_documentation; |
189 |
|
190 |
$default->{$name} = $value unless defined $default->{$name}; |
191 |
|
192 |
my $size = ( int( length($value) / $self->input_resize_step ) + 1 ) * $self->input_resize_step; |
193 |
$value_html = qq|<input type="$type" name="$name" title="$attr_type" value="$value" size="$size">| unless $value_html; |
194 |
|
195 |
# warn "# required $name ", $class->meta->get_attribute( $name )->dump( 2 ); |
196 |
$form .= qq|<label for="$name"$label_title>$label</label>$value_html<br>|; |
197 |
my $ll = length($label); |
198 |
$label_width = $ll if $ll > $label_width; |
199 |
} |
200 |
$form .= qq|<input type="hidden" name="frey-checkboxes" value="| . join(' ', @checkboxes) . qq|">| if @checkboxes; |
201 |
|
202 |
$self->add_css(qq| |
203 |
label,input { |
204 |
display: block; |
205 |
float: left; |
206 |
margin-bottom: 10px; |
207 |
} |
208 |
|
209 |
label { |
210 |
text-align: right; |
211 |
width: ${label_width}ex; |
212 |
padding-right: 20px; |
213 |
} |
214 |
|
215 |
br { |
216 |
clear: left; |
217 |
} |
218 |
|); |
219 |
|
220 |
my $html; |
221 |
|
222 |
# http://www.quirksmode.org/oddsandends/forms.html |
223 |
# $form =~ s{<([^>]+)(name=")([^"]+)(")([^>]*)>}{<$1$2$3$4 id="$3" $5}gs; |
224 |
|
225 |
$html = qq| |
226 |
<h1>$class params</h1> |
227 |
<form method="post"> |
228 |
$form |
229 |
<input type="submit" value="Run $class"> |
230 |
</form> |
231 |
| if $form; |
232 |
|
233 |
$self->add_status({ |
234 |
$self->class => { |
235 |
params => $self->params, |
236 |
params_config => $params_config, |
237 |
default => $default, |
238 |
}, |
239 |
}); |
240 |
|
241 |
return ($html,$default) if wantarray; |
242 |
return $html; |
243 |
} |
244 |
|
245 |
=head1 SEE ALSO |
246 |
|
247 |
L<http://www.quirksmode.org/css/forms.html> for info on CSS2 forms |
248 |
|
249 |
=cut |
250 |
|
251 |
1; |