Jifty::Web::Form::Link - Creates a state-preserving HTML link


Jifty documentation Contained in the Jifty distribution.

Index


Code Index:

NAME

Top

Jifty::Web::Form::Link - Creates a state-preserving HTML link

DESCRIPTION

Top

Describes an HTML link that may be AJAX-enabled. Most of the computation of this comes from Jifty::Web::Form::Clickable, which generates Jifty::Web::Form::Links.

accessors

Link adds url and escape_label to the list of possible accessors and mutators, in addition to those offered by accessors in Jifty::Web::Form::Element.

new PARAMHASH

Creates a new Jifty::Web::Form::Link object. Possible arguments to the PARAMHASH are:

url (optional)

The URL of the link; defaults to the current URL.

tooltip

Additional information about the link.

target

Target of the link. Mostly useful when specified as "_blank" to open a new window or as the name of a already existing window.

escape_label

HTML escape the label and tooltip? Defaults to true

anything from Jifty::Web::Form::Element

Any parameter which new in Jifty::Web::Form::Element can take.

url [URL]

Gets or sets the URL that the link links to.

as_string

Returns the string of the link, including any necessary javascript.

render

Render the string of the link, including any necessary javascript.


Jifty documentation Contained in the Jifty distribution.
use warnings;
use strict;

package Jifty::Web::Form::Link;

use base 'Jifty::Web::Form::Element';

# Since we don't inherit from Form::Field, we don't otherwise stringify.
# We need the anonymous sub because otherwise the method of the base class is
# always called, instead of the appropriate overridden method in a possible
# child class.
use overload '""' => sub { shift->render }, bool => sub { 1 };

sub accessors { shift->SUPER::accessors(), qw(url escape_label tooltip target rel); }
__PACKAGE__->mk_accessors(qw(url escape_label tooltip target rel));

sub new {
    my $class = shift;
    my $args = ref($_[0]) ? $_[0] : {@_};
    my $self  = $class->SUPER::new(
      { url          => Jifty->web->request->top_request->path,
        label        => "Click me!",
        tooltip      => undef,
        escape_label => 1,
        class        => '',
        rel          => '',
        target       => '' }, $args );

    return $self;
}

sub as_string {
    my $self = shift;
    my $label = $self->label;
    my $web = Jifty->web;
    $label = $web->escape( $label )
        if ( $self->escape_label );

    my $tooltip = $self->tooltip;
    $tooltip = $web->escape( $tooltip )
        if ( defined $tooltip and $self->escape_label );

    my $output = '';

    $output .= (qq(<a));
    $output .= (qq( id="@{[$self->id]}"))         if $self->id;
    $output .= (qq( class="@{[$self->class]}"))   if $self->class;
    $output .= (qq( title="@{[$tooltip]}"))       if defined $tooltip;
    $output .= (qq( target="@{[$self->target]}")) if $self->target;
    $output .= (qq( accesskey="@{[$self->key_binding]}")) if $self->key_binding;
    $output .= (qq( rel="@{[$self->rel]}"))       if $self->rel;
    $output .= (qq( href="@{[$web->escape($self->url)]}"));
    $output .= ( $self->javascript() );
    $output .= (qq(>$label</a>));

    $output .= (
        '<script type="text/javascript">' .
        $self->key_binding_javascript.
        "</script>") if $self->key_binding;

    return $output;
}

sub render {
    my $self = shift;

    Jifty->web->out($self->as_string);
    return ('');
}

1;