HTML-Prototype
view release on metacpan or search on metacpan
lib/HTML/Prototype.pm view on Meta::CPAN
$options->{html_options} ||= {};
$options->{html_options}->{onclick} =
_remote_function($options) . '; return false';
$options->{html_options}->{type} = 'button';
$options->{html_options}->{name} = $name;
$options->{html_options}->{value} = $value;
return $self->tag( 'input', $options->{html_options} );
}
=item $prototype->tag( $name, \%options, $starttag );
Returns a opening tag.
=cut
sub tag {
my ( $self, $name, $options, $starttag ) = @_;
return HTML::Prototype::Helper::Tag->_tag( $name, $options, $starttag );
}
=item $prototype->update_element_function( $element_id, \%options, \&code )
Returns a Javascript function (or expression) that'll update a DOM element
according to the options passed.
C<content>: The content to use for updating.
Can be left out if using block, see example.
C<action>: Valid options are C<update> (assumed by default), :empty, :remove
C<position>: If the :action is :update, you can optionally specify one
of the following positions: :before, :top, :bottom, :after.
Example:
$prototype->javascript_tag( $prototype->update_element_function(
'products', { position => 'bottom', content => '<p>New product!</p>'
) );
This method can also be used in combination with remote method call
where the result is evaluated afterwards to cause multiple updates
on a page.
Example:
# View
$prototype->form_remote_tag( {
url => { "http://foo.bar/buy" },
complete => $prototype->evaluate_remote_response
} );
# Returning view
$prototype->update_element_function( 'cart', {
action => 'update',
position => 'bottom',
content => "<p>New Product: $product_name</p>"
} );
$prototype->update_element_function( 'status',
{ binding => "You've bought a new product!" } );
=cut
sub update_element_function {
my ( $self, $element_id, $options, $code ) = @_;
$options ||= {};
my $content = $options->{content} || '';
$content = &$code if $code;
my $action = $options->{action} || $options->{update};
my $javascript_function = '';
if ( $action eq 'update' ) {
if ( my $position = $options->{position} ) {
$position = ucfirst $position;
$javascript_function =
"new Insertion.$position( '$element_id', '$content' )";
}
else {
$javascript_function = "\$('$element_id').innerHTML = '$content'";
}
}
elsif ( $action eq 'empty' ) {
$javascript_function = "\$('#$element_id').innerHTML = ''";
}
elsif ( $action eq 'remove' ) {
$javascript_function = "Element.remove('$element_id')";
}
else {
die "Invalid action, choose one of :update, :remove, :empty";
}
$javascript_function .= "\n";
return $options->{binding}
? ( $javascript_function . $options->{binding} )
: $javascript_function;
}
=item $prototype->visual_effect( $name, $element_id, \%js_options )
Returns a JavaScript snippet to be used on the Ajax callbacks for starting
visual effects.
$prototype->link_to_remote( 'Reload', {
update => 'posts',
url => 'http://foo.bar/baz',
complete => $prototype->visual_effect( 'highlight', 'posts', {
duration => '0.5'
} )
} );
=cut
sub visual_effect {
my ( $self, $name, $element_id, $js_options ) = @_;
$js_options ||= {};
$name = ucfirst $name;
my $options = _options_for_javascript($js_options);
return "new Effect.$name( '$element_id', $options )";
}
sub _build_callbacks {
my $options = shift;
my %callbacks;
for my $callback (@$callbacks) {
if ( my $code = $options->{$callback} ) {
my $name = 'on' . ucfirst $callback;
( run in 2.208 seconds using v1.01-cache-2.11-cpan-13bb782fe5a )