Advertisement
Guest User

TagHelpersShadow.pm

a guest
Apr 23rd, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.77 KB | None | 0 0
  1. cheako@debian:/usr/local/share/perl5/Mojolicious/Plugin$ cat TagHelpersShadow.pm
  2. package Mojolicious::Plugin::TagHelpers;
  3.  
  4. sub _input {
  5.   my ($c, $name) = (shift, shift);
  6.   my %attrs = @_ % 2 ? (value => shift, @_) : @_;
  7.  
  8.   if (my @values = @{$c->every_param($name)}) {
  9.  
  10.     # Checkbox or radiobutton
  11.     my $type = $attrs{type} || '';
  12.     if ($type eq 'checkbox' || $type eq 'radio') {
  13.       delete $attrs{checked} if @values;
  14.       my $value = $attrs{value} // 'on';
  15.       $attrs{checked} = undef if grep { $_ eq $value } @values;
  16.     }
  17.  
  18.     # Others
  19.     else { $attrs{value} = $values[-1] }
  20.   }
  21.  
  22.   return _hidden_field($c, "tag_helpers_shadow_$name" => $attrs{value}) . _validation($c, $name, 'input', name => $name, %attrs);
  23. }
  24.  
  25. sub _select_field {
  26.   my ($c, $name, $options, %attrs) = (shift, shift, shift, @_);
  27.  
  28.   my %values = map { $_ => 1 } @{$c->every_param($name)};
  29.  
  30.   my $groups = '';
  31.   for my $group (@$options) {
  32.  
  33.     # "optgroup" tag
  34.     if (blessed $group && $group->isa('Mojo::Collection')) {
  35.       my ($label, $values, %attrs) = @$group;
  36.       my $content = join '', map { _option(\%values, $_) } @$values;
  37.       $groups .= _tag('optgroup', label => $label, %attrs, sub {$content});
  38.     }
  39.  
  40.     # "option" tag
  41.     else { $groups .= _option(\%values, $group) }
  42.   }
  43.  
  44.   return join('', (map { _hidden_field($c, "tag_helpers_shadow_$name" => $_) } @{$c->every_param($name)} ) ) . _validation($c, $name, 'select', name => $name, %attrs, sub {$groups});
  45. }
  46.  
  47. sub _text_area {
  48.   my ($c, $name) = (shift, shift);
  49.  
  50.   my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
  51.   my $content = @_ % 2 ? shift : undef;
  52.   $content = $c->param($name) // $content // $cb // '';
  53.  
  54.   return _hidden_field($c, "tag_helpers_shadow_$name" => $attrs{value}) . _validation($c, $name, 'textarea', name => $name, @_, $content);
  55. }
  56.  
  57. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement