HTML-TagHelper

 view release on metacpan or  search on metacpan

lib/HTML/TagHelper.pm  view on Meta::CPAN

    my $day_id     = $html_options{id} . "_day";
    delete $html_options{name};
    delete $html_options{id};
    delete $html_options{year_start};
    delete $html_options{year_end};
    my $year_options = "";
    my $tmp_option;

    foreach my $year ( $year_start .. $year_end ) {
        $tmp_option = HTML::Element->new('option');
        $tmp_option->attr( 'value', $year );
        $tmp_option->attr( 'selected', 'true' ) if ( $year == $sel_year );
        $tmp_option->push_content($year);
        $year_options .= $tmp_option->as_HTML("");
    }

    my $month_options = "";
    foreach my $month ( 1 .. 12 ) {
        $tmp_option = HTML::Element->new('option');
        $tmp_option->attr( 'value', $month );
        $tmp_option->attr( 'selected', 'true' ) if ( $month == $sel_month );
        $tmp_option->push_content($month);
        $month_options .= $tmp_option->as_HTML("");
    }

    my $day_options = "";
    foreach my $day ( 1 .. 31 ) {
        $tmp_option = HTML::Element->new('option');
        $tmp_option->attr( 'value', $day );
        $tmp_option->attr( 'selected', 'true' ) if ( $day == $sel_day );
        $tmp_option->push_content($day);
        $day_options .= $tmp_option->as_HTML("");
    }

    my $date_select = "";

    my $day_tag = HTML::Element->new( 'select', %html_options );
    $day_tag->attr( 'id',   $day_id );
    $day_tag->attr( 'name', $day_name );
    $day_tag->push_content($day_options);
    $date_select .= $day_tag->as_HTML("");

    my $month_tag = HTML::Element->new( 'select', %html_options );
    $month_tag->attr( 'id',   $month_id );
    $month_tag->attr( 'name', $month_name );
    $month_tag->push_content($month_options);
    $date_select .= $month_tag->as_HTML("");

    my $year_tag = HTML::Element->new( 'select', %html_options );
    $year_tag->attr( 'id',   $year_id );
    $year_tag->attr( 'name', $year_name );
    $year_tag->push_content($year_options);
    $date_select .= $year_tag->as_HTML("");

    return $date_select;
}

sub _convert_options_to_javascript {
    my ( $self, $html_options, $url ) = @_;
    my $confirm = delete $html_options->{confirm};
    my $popup   = delete $html_options->{popup};
    my $method  = delete $html_options->{method};
    my $href    = delete $html_options->{href};

    $html_options->{onclick} =
      ( $popup && $method )
      ? return("You can't use :popup and :method in the same link\n")
      : ( $confirm && $popup ) ? "if ("
      . $self->_confirm_javascript_function($confirm) . ") { "
      . $self->_popup_javascript_function($popup)
      . " };return false;"
      : ( $confirm && $method ) ? "if ("
      . $self->_confirm_javascript_function($confirm) . ") { "
      . $self->_method_javascript_function($method)
      . " };return false;"
      : ($confirm)
      ? "return " . $self->_confirm_javascript_function($confirm) . ";"
      : ($method) ? $self->_method_javascript_function( $method, $url, $href )
      . "return false;"
      : ($popup) ? $self->_popup_javascript_function($popup) . ' return false;'
      :            $html_options->{onclick};
    return $html_options;
}

sub _confirm_javascript_function {
    my ( $self, $confirm ) = @_;
    return "confirm('" . $self->_escape_javascript($confirm) . "')";
}

sub _popup_javascript_function {
    my ( $self, $popup ) = @_;
    return ( ref $popup eq 'ARRAY' )
      ? "window.open(this.href, '"
      . shift(@$popup) . "', '"
      . pop(@$popup) . "');"
      : "window.open(this.href);";
}

sub _method_javascript_function {
    my ( $self, $method, $url, $href ) = @_;
    $url  = ""    unless defined $url;
    $href = undef unless defined $href;
    my $action = ( $href && length($url) > 0 ) ? "'" . $url . "'" : "this.href";
    my $submit_function =
        "var f = document.createElement('form'); f.style.display = 'none'; "
      . "this.parentNode.appendChild(f); f.method = 'POST'; f.action = "
      . $action . ";";
    unless ( $method eq 'post' ) {
        $submit_function .=
"var m = document.createElement('input'); m.setAttribute('type', 'hidden'); ";
        $submit_function .=
            "m.setAttribute('name', '_method'); m.setAttribute('value', '"
          . $method
          . "'); f.appendChild(m);";
    }

    $submit_function .= "f.submit();";
    return $submit_function;
}

sub _tag_options {
    my ( $self, $options, $escape ) = @_;
    $escape = 1 unless defined $escape;

    my @boolean_attributes = qw/disabled readonly multiple/;

    if ($options) {
        if ($escape) {
            while ( my ( $key, $value ) = each %$options ) {
                next unless ($value);
                $value =
                  ( grep { $_ eq $key } @boolean_attributes ) ? $key : $value;
                $options->{$key} = $value;
            }
        }
    }
    return $options;
}

sub _escape_javascript {
    my ( $self, $javascript ) = @_;

    $javascript ||= '';
    $javascript =~ s|\\|\0\0|g;
    $javascript =~ s|</|<\/|g;
    $javascript =~ s|\r\n|\\n|g;
    $javascript =~ s|["']||g;
    return $javascript;
}

=head1 AUTHOR

Gitte Wange Olrik, C<< <gitte at olrik.dk> >>

Chenryn, C<< <chenlin.rao at gmail.com> >>



( run in 1.197 second using v1.01-cache-2.11-cpan-364913b4093 )