--- trunk/lib/Frey/Action.pm 2008/11/19 00:39:23 430 +++ trunk/lib/Frey/Action.pm 2009/01/06 23:42:59 950 @@ -12,6 +12,11 @@ Invoke any L object creating html for with various default parameters if not supplied at invocation. +You can force rendering of fields if you define C sub with +desired rendering as in: + + sub render_pipe { 'radio' } + =cut has 'class' => ( @@ -26,6 +31,14 @@ default => sub { {} }, ); +has 'input_step_size' => ( + documentation => 'Resize input fields by this step', + is => 'rw', + isa => 'Int', +# required => 1, + default => 20, +); + =head2 required my @required_attributes = $self->required; @@ -36,20 +49,16 @@ sub required { my ( $self ) = @_; $self->load_class( $self->class ); + my @required = - grep { defined $_ } - map { - eval { - $_->can('name') && !defined( $self->params->{ $_->name } ) - }; - } grep { - my $required = eval { - my $attr = $self->class->meta->get_attribute($_); - $attr->is_required; - }; - warn "# attribute $_ error: $@" if $@; - $required; + defined $_ && $_->can('name') && + ! defined( $self->params->{ $_->name } ) && + ! $_->is_lazy + } + map { + my $attr = $self->class->meta->get_attribute($_); + blessed $attr && $attr->is_required && $attr; } $self->class->meta->get_attribute_list; warn "## required = ",dump( map { $_->name } @required ), " for ", $self->class if @required && $self->debug; @@ -102,63 +111,199 @@ my $default = clone $self->params; # XXX we really don't want to modify params! - my $config_params = {}; - $config_params = $self->config($class); - warn "# $class config = ",dump( $config_params ) if $self->debug; + my $params_config = {}; + $params_config = $self->config($class); + warn "# $class config = ",dump( $params_config ) if $self->debug; my $form; + my $form_id = $class; + $form_id =~ s{\W+}{_}g; + + sub select_values { + my ( $name, $attr_type, $values ) = @_; + + $attr_type ||= '?' and warn "$name doesn't have attr_type"; + + my $max_value_len = 0; + my @values; + my $html = ''; + + foreach ( @$values ) { + my $v = ref($_) eq 'HASH' ? $_->{$name} : $_; + warn "## value '$v'"; + push @values, $v; + $max_value_len = length($v) if length($v) > $max_value_len; + } + + warn "# max_value_len: $max_value_len"; + my $render = eval $class . '->render_' . $name; + warn "## render $@"; + + if ( $#values > 3 && $render !~ m{radio} ) { + my $options = join("\n", + map { + qq||; + } @values + ); + # onchange="alert(this.options[this.selectedIndex].value);" + $html = qq| + + | if $options; + } else { + my $delimiter = $max_value_len > $self->input_step_size ? qq|
| : ''; + my $radio = +# $delimiter . + join("\n", + map { strip(qq| + + + $_ + + $delimiter + |) } @values + ); + if ( $radio ) { + + my $size = int( $max_value_len / $self->input_step_size ) + 1; + $size = 5 if $size > 5; + $size *= $self->input_step_size; + $radio .= qq| + + + + + |; + } + $html = qq|
$radio
|; + } + + return +# qq|| . + $html + } + + + foreach my $checkbox ( split(/\s+/, $default->{'frey-checkboxes'} ) ) { + next if defined $default->{ $checkbox }; + + $default->{ $checkbox } = 0; + $self->params->{ $checkbox } = 0; + warn "# checkbox $checkbox not ticked"; + } - foreach my $name ( grep { ! $class->meta->get_attribute($_)->is_lazy } $self->attributes ) { + my @checkboxes; + + my $label_width = 1; # minimum + + foreach my $name ( + grep { + die "$_ doesn't have meta" unless $class->can('meta'); + ! $class->meta->get_attribute($_)->is_lazy + && ! defined $default->{$_} + && ! m{^_} # skip _private + } $self->attributes + ) { my $attr_type = ''; my $type = $name =~ m/^pass/ ? 'password' : 'text'; my $label = $name; - my $value = ''; my $label_title = ''; my $value_html = ''; - if ( ref($config_params) eq 'HASH' ) { - $value = $config_params->{$name}; - } elsif ( ref($config_params) eq 'ARRAY' ) { - $value_html = qq||; - $default->{$name} = $config_params->[0]->{$name}; - } elsif ( my $attr = $class->meta->get_attribute( $name ) ) { - if ( $attr->has_type_constraint ) { - $attr_type = $attr->type_constraint->name; - if ( $attr->type_constraint->can('values') ) { - $value_html = qq||; - } elsif ( $attr_type !~ m{^(Str|Int)$} ) { - $value_html = qq||; - } - } - $value = $attr->default( $name ) if ! $value && $attr->has_default; - $label_title = qq| title="| . $attr->documentation . qq|"| if $attr->has_documentation; - } else { - warn "wired attribute $name"; + + my $attr = $class->meta->get_attribute( $name ); + $attr_type = $attr->type_constraint->name if $attr->has_type_constraint; + + my $value = + defined $default->{$name} ? $default->{$name} : + $attr->has_default ? $attr->default( $name ) : + ''; + + if ( ref($params_config) eq 'HASH' && defined $params_config->{$name} ) { + $value = $params_config->{$name}; + } elsif ( ref($params_config) eq 'ARRAY' ) { + $value_html = select_values( $name, $attr_type, $params_config ); + $default->{$name} = $params_config->[0]->{$name}; + } elsif ( $attr->has_type_constraint && $attr->type_constraint->can('values') ) { + $value_html = select_values( $name, $attr_type, $attr->type_constraint->values ); + } elsif ( $attr_type =~ m{^Bool} ) { + my $suffix = ''; + $suffix = ' checked' if $value; + $value_html = qq||; + push @checkboxes, $name; + } elsif ( ! defined $value ) { + $value_html = qq|undef|; # FIXME if $self->debug + } elsif ( $attr_type !~ m{^(Str|Int)$} || $value =~ $Frey::Web::re_html || $name =~ m{text} ) { + $value_html = qq||; } - $value_html = qq|| unless $value_html; + $label_title = qq| title="| . $attr->documentation . qq|"| if $attr->has_documentation; $default->{$name} = $value unless defined $default->{$name}; + my $size = ( int( length($value) / $self->input_step_size ) + 1 ) * $self->input_step_size; + $value_html = qq|| unless $value_html; + # warn "# required $name ", $class->meta->get_attribute( $name )->dump( 2 ); - $form .= qq|| . $value_html; + $form .= qq|$value_html
|; + my $ll = length($label); + $label_width = $ll if $ll > $label_width; } - my $html = qq|

$class params

$form
|; - push @{ $self->status }, { 'Params' => - { - 'Config' => $config_params, - 'Default' => $default + $form .= qq|| if @checkboxes; + + $self->add_js('static/Frey/Action.js'); + + $self->add_css(qq| + label,input { + display: block; + float: left; + margin-bottom: 10px; + } + + input:focus { + border-color: #cc0; + } + + label { + text-align: right; + width: ${label_width}ex; + padding-right: 20px; + } + + br { + clear: left; + } + |); + + my $html; + + # http://www.quirksmode.org/oddsandends/forms.html +# $form =~ s{<([^>]+)(name=")([^"]+)(")([^>]*)>}{<$1$2$3$4 id="$3" $5}gs; + + $html = qq| +

$class params

+
+ $form + +
+ | if $form; + + $self->add_status({ + $self->class => { + params => $self->params, + params_config => $params_config, + default => $default, }, - }; + }); return ($html,$default) if wantarray; return $html; } +=head1 SEE ALSO + +L for info on CSS2 forms + +=cut + 1;