Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- cheako@debian:/usr/local/share/perl5/Mojolicious/Plugin/TagHelpers$ cat Shadow.pm
- package Mojolicious::Plugin::TagHelpers::Shadow;
- use Mojo::Base 'Mojolicious::Plugin';
- require Mojolicious::Plugin::TagHelpers;
- sub register {
- my ($self, $app) = @_;
- # Text field variations
- my @time = qw(date month time week);
- for my $name (@time, qw(color email number range search tel text url)) {
- $app->helper("${name}_field" => sub { _shadow(@_) . Mojolicious::Plugin::TagHelpers->_input(@_, type => $name) });
- }
- $app->helper(datetime_field => sub { _shadow(@_) . Mojolicious::Plugin::TagHelpers->_input(@_, type => 'datetime-local') });
- my @helpers = (
- qw(select_field text_area)
- );
- $app->helper($_ => __PACKAGE__->can("_$_")) for @helpers;
- $app->helper(check_box => sub { _shadow(@_) . Mojolicious::Plugin::TagHelpers->_input(@_, type => 'checkbox') });
- $app->helper(input_tag => sub { _shadow(@_) . Mojolicious::Plugin::TagHelpers->_input(@_) });
- $app->helper(radio_button => sub { _shadow(@_) . Mojolicious::Plugin::TagHelpers->_input(@_, type => 'radio') });
- $app->helper('shadow_param' => sub {
- my ($self, $name) = (shift, shift);
- return $self->every_param("tag_helpers_shadow_$name")->[-1] unless @_;
- $self->remove("tag_helpers_shadow_$name");
- return $self->append("tag_helpers_shadow_$name" => ref $_[0] eq 'ARRAY' ? $_[0] : [@_]);
- });
- $app->helper('shadow_diff' => sub {
- my ($self, $name) = (shift, shift);
- return $self->param($name) == $self->shadow_param($name);
- });
- $app->helper('shadow_diff_multi' => sub {
- my ($self, $name) = (shift, shift);
- use Array::Utils qw(array_diff);
- return !array_diff(@{$self->every_param($name)}, @{$self->every_param("tag_helpers_shadow_$name")});
- });
- }
- sub _shadow {
- my ($c, $name) = (shift, shift);
- my %attrs = @_ % 2 ? (value => shift, @_) : @_;
- return Mojolicious::Plugin::TagHelpers->_hidden_field($c, "tag_helpers_shadow_$name" => $attrs{value} || 'taghelpers_shadow_undefined');
- }
- sub _shadow_multi {
- my ($c, $name, $options, %attrs) = (shift, shift, shift, @_);
- my $values = []; #TODO: Populate this from $options/%attrs.
- return join('', map { Mojolicious::Plugin::TagHelpers->_hidden_field($c, "tag_helpers_shadow_$name" => $_) } @$values ? @$values : defined $values ? ('taghelpers_shadow_empty') : ('taghelpers_shadow_undefined') );
- }
- sub _select_field {
- _shadow_multi(@_) . Mojolicious::Plugin::TagHelpers->_select_field(@_)
- }
- sub _text_area {
- _shadow(@_) . Mojolicious::Plugin::TagHelpers->_text_area(@_)
- }
- 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement