1use warnings; 2use strict; 3 4package Jifty::Web::Form::Link; 5 6=head1 NAME 7 8Jifty::Web::Form::Link - Creates a state-preserving HTML link 9 10=head1 DESCRIPTION 11 12Describes an HTML link that may be AJAX-enabled. Most of the 13computation of this comes from L<Jifty::Web::Form::Clickable>, which 14generates L<Jifty::Web::Form::Link>s. 15 16=cut 17 18use base 'Jifty::Web::Form::Element'; 19 20# Since we don't inherit from Form::Field, we don't otherwise stringify. 21# We need the anonymous sub because otherwise the method of the base class is 22# always called, instead of the appropriate overridden method in a possible 23# child class. 24use overload '""' => sub { shift->render }, bool => sub { 1 }; 25 26=head2 accessors 27 28Link adds C<url> and C<escape_label> to the list of possible accessors 29and mutators, in addition to those offered by 30L<Jifty::Web::Form::Element/accessors>. 31 32=cut 33 34sub accessors { shift->SUPER::accessors(), qw(url escape_label tooltip target rel); } 35__PACKAGE__->mk_accessors(qw(url escape_label tooltip target rel)); 36 37=head2 new PARAMHASH 38 39Creates a new L<Jifty::Web::Form::Link> object. Possible arguments to 40the C<PARAMHASH> are: 41 42=over 43 44=item url (optional) 45 46The URL of the link; defaults to the current URL. 47 48=item tooltip 49 50Additional information about the link. 51 52=item target 53 54Target of the link. Mostly useful when specified as "_blank" to open 55a new window or as the name of a already existing window. 56 57=item escape_label 58 59HTML escape the label and tooltip? Defaults to true 60 61=item anything from L<Jifty::Web::Form::Element> 62 63Any parameter which L<Jifty::Web::Form::Element/new> can take. 64 65=back 66 67=cut 68 69sub new { 70 my $class = shift; 71 my $args = ref($_[0]) ? $_[0] : {@_}; 72 my $self = $class->SUPER::new( 73 { url => Jifty->web->request->top_request->path, 74 label => "Click me!", 75 tooltip => undef, 76 escape_label => 1, 77 class => '', 78 rel => '', 79 target => '' }, $args ); 80 81 return $self; 82} 83 84=head2 url [URL] 85 86Gets or sets the URL that the link links to. 87 88=cut 89 90=head2 as_string 91 92Returns the string of the link, including any necessary javascript. 93 94=cut 95 96sub as_string { 97 my $self = shift; 98 my $label = $self->label; 99 my $web = Jifty->web; 100 $label = $web->escape( $label ) 101 if ( $self->escape_label ); 102 103 my $tooltip = $self->tooltip; 104 $tooltip = $web->escape( $tooltip ) 105 if ( defined $tooltip and $self->escape_label ); 106 107 my $output = ''; 108 109 $output .= (qq(<a)); 110 $output .= (qq( id="@{[$self->id]}")) if $self->id; 111 $output .= (qq( class="@{[$self->class]}")) if $self->class; 112 $output .= (qq( title="@{[$tooltip]}")) if defined $tooltip; 113 $output .= (qq( target="@{[$self->target]}")) if $self->target; 114 $output .= (qq( accesskey="@{[$self->key_binding]}")) if $self->key_binding; 115 $output .= (qq( rel="@{[$self->rel]}")) if $self->rel; 116 $output .= (qq( href="@{[$web->escape($self->url)]}")); 117 $output .= ( $self->javascript() ); 118 $output .= (qq(>$label</a>)); 119 120 $output .= ( 121 '<script type="text/javascript">' . 122 $self->key_binding_javascript. 123 "</script>") if $self->key_binding; 124 125 return $output; 126} 127 128=head2 render 129 130Render the string of the link, including any necessary javascript. 131 132=cut 133 134sub render { 135 my $self = shift; 136 137 Jifty->web->out($self->as_string); 138 return (''); 139} 140 1411; 142