Advertisement
cheako

Mojolicious/Plugin/TagHelpers/Shadow.pm

Apr 23rd, 2017
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.58 KB | None | 0 0
  1. cheako@debian:/usr/local/share/perl5/Mojolicious/Plugin/TagHelpers$ cat Shadow.pm
  2. package Mojolicious::Plugin::TagHelpers::Shadow;
  3. use Mojo::Base 'Mojolicious::Plugin';
  4.  
  5. require Mojolicious::Plugin::TagHelpers;
  6.  
  7. sub register {
  8.   my ($self, $app) = @_;
  9.  
  10.   # Text field variations
  11.   my @time = qw(date month time week);
  12.   for my $name (@time, qw(color email number range search tel text url)) {
  13.     $app->helper("${name}_field" => sub { _shadow(@_) . Mojolicious::Plugin::TagHelpers->_input(@_, type => $name) });
  14.   }
  15.   $app->helper(datetime_field => sub { _shadow(@_) . Mojolicious::Plugin::TagHelpers->_input(@_, type => 'datetime-local') });
  16.  
  17.   my @helpers = (
  18.     qw(select_field text_area)
  19.   );
  20.   $app->helper($_ => __PACKAGE__->can("_$_")) for @helpers;
  21.  
  22.   $app->helper(check_box => sub { _shadow(@_) . Mojolicious::Plugin::TagHelpers->_input(@_, type => 'checkbox') });
  23.   $app->helper(input_tag      => sub { _shadow(@_) . Mojolicious::Plugin::TagHelpers->_input(@_) });
  24.   $app->helper(radio_button   => sub { _shadow(@_) . Mojolicious::Plugin::TagHelpers->_input(@_, type => 'radio') });
  25.  
  26.   $app->helper('shadow_param' => sub {
  27.     my ($self, $name) = (shift, shift);
  28.     return $self->every_param("tag_helpers_shadow_$name")->[-1] unless @_;
  29.     $self->remove("tag_helpers_shadow_$name");
  30.     return $self->append("tag_helpers_shadow_$name" => ref $_[0] eq 'ARRAY' ? $_[0] : [@_]);
  31.   });
  32.  
  33.   $app->helper('shadow_diff' => sub {
  34.     my ($self, $name) = (shift, shift);
  35.     return $self->param($name) == $self->shadow_param($name);
  36.   });
  37.  
  38.   $app->helper('shadow_diff_multi' => sub {
  39.     my ($self, $name) = (shift, shift);
  40.     use Array::Utils qw(array_diff);
  41.     return !array_diff(@{$self->every_param($name)}, @{$self->every_param("tag_helpers_shadow_$name")});
  42.   });
  43.  
  44. }
  45.  
  46. sub _shadow {
  47.   my ($c, $name) = (shift, shift);
  48.   my %attrs = @_ % 2 ? (value => shift, @_) : @_;
  49.  
  50.   return Mojolicious::Plugin::TagHelpers->_hidden_field($c, "tag_helpers_shadow_$name" => $attrs{value} || 'taghelpers_shadow_undefined');
  51. }
  52.  
  53. sub _shadow_multi {
  54.   my ($c, $name, $options, %attrs) = (shift, shift, shift, @_);
  55.  
  56.   my $values = []; #TODO: Populate this from $options/%attrs.
  57.   return join('', map { Mojolicious::Plugin::TagHelpers->_hidden_field($c, "tag_helpers_shadow_$name" => $_) } @$values ? @$values : defined $values ? ('taghelpers_shadow_empty') : ('taghelpers_shadow_undefined') );
  58. }
  59.  
  60. sub _select_field {
  61.   _shadow_multi(@_) . Mojolicious::Plugin::TagHelpers->_select_field(@_)
  62. }
  63.  
  64. sub _text_area {
  65.   _shadow(@_) . Mojolicious::Plugin::TagHelpers->_text_area(@_)
  66. }
  67.  
  68. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement