package HTML::Prototype;
use strict;
use base qw/Class::Accessor::Fast/;
use vars qw/$VERSION $prototype $controls $dragdrop $effects/;
$VERSION = '1.35';
use HTML::Element;
use HTML::Prototype::Js;
use HTML::Prototype::Controls;
use HTML::Prototype::DragDrop;
use HTML::Prototype::Effects;
$prototype = do { package HTML::Prototype::Js; local $/; };
$controls = do { package HTML::Prototype::Controls; local $/; };
$dragdrop = do { package HTML::Prototype::DragDrop; local $/; };
$effects = do { package HTML::Prototype::Effects; local $/; };
my $callbacks = [qw/uninitialized loading loaded interactive complete/];
my $ajax_options = [qw/url asynchronous method insertion form with/];
=head1 NAME
HTML::Prototype - Generate HTML and Javascript for the Prototype library
=head1 SYNOPSIS
use HTML::Prototype;
my $prototype = HTML::Prototype->new;
print $prototype->auto_complete_field(...);
print $prototype->auto_complete_result(...);
print $prototype->auto_complete_stylesheet(...);
print $prototype->content_tag(...);
print $prototype->define_javascript_functions;
print $prototype->draggable_element(...);
print $prototype->drop_receiving_element(...);
print $prototype->evaluate_remote_response(...);
print $prototype->form_remote_tag(...);
print $prototype->javascript_tag(...);
print $prototype->link_to_function(...);
print $prototype->link_to_remote(...);
print $prototype->observe_field(...);
print $prototype->observe_form(...);
print $prototype->periodically_call_remote(...);
print $prototype->sortable_element(...);
print $prototype->submit_to_remote(...);
print $prototype->tag(...);
print $ptototype->update_element_function(...);
print $prototype->visual_effect(...);
=head1 DESCRIPTION
The module contains some code generators for Prototype, the famous JavaScript
OO library and the script.aculous extensions.
The Prototype library (http://prototype.conio.net/) is designed to make
AJAX easy. Catalyst::Plugin::Prototype makes it easy to connect to the
Prototype library.
This is mostly a port of the Ruby on Rails helper tags for JavaScript
for use in L.
=head2 METHODS
=over 4
=item $prototype->auto_complete_field( $field_id, \%options )
Adds Ajax autocomplete functionality to the text input field with the
DOM ID specified by C.
This function expects that the called action returns a HTML list,
or nothing if no entries should be displayed for autocompletion.
Required options are:
C: Specifies the URL to be used in the AJAX call.
Addtional options are:
C: Specifies the DOM ID of the element whose innerHTML should
be updated with the autocomplete entries returned by the Ajax request.
Defaults to field_id + '_auto_complete'.
C: A Javascript expression specifying the parameters for the
XMLHttpRequest.
This defaults to 'value', which in the evaluated context refers to the
new field value.
C: Specifies the DOM ID of an elment which will be displayed
while autocomplete is running.
=cut
sub auto_complete_field {
my ( $self, $id, $options ) = @_;
$options ||= {};
my $update = $options->{update} || "$id" . '_auto_complete';
my $function =
"new Ajax.Autocompleter( '$id', '$update', '" . $options->{url} . "'";
my $js_options = {};
$js_options->{callback} =
( 'function ( element, value ) { return ' . $options->{with} . ' }' )
if $options->{with};
$js_options->{indicator} = ( "'" . $options->{indicator} . "'" )
if $options->{indicator};
$function .= ',' . _options_for_javascript($js_options) . ')';
$self->javascript_tag($function);
}
=item $prototype->auto_complete_result(\@items)
Returns a list, to communcate with the Autocompleter.
Here's an example for L:
sub autocomplete : Global {
my ( $self, $c ) = @_;
my @items = qw/foo bar baz/;
$c->res->body( $c->prototype->auto_complete_result(\@items) );
}
=cut
sub auto_complete_result {
my ( $self, $items ) = @_;
my @elements;
for my $item (@$items) {
push @elements, HTML::Element->new('li')->push_content($item);
}
return HTML::Element->new('ul')->push_content(@elements)->as_HTML;
}
=item $prototype->auto_complete_stylesheet
Returns the auto_complete stylesheet.
=cut
sub auto_complete_stylesheet {
my $self = shift;
return $self->content_tag( 'style', <<"");
div.auto_complete {
width: 350px;
background: #fff;
}
div.auto_complete ul {
border:1px solid #888;
margin:0;
padding:0;
width:100%;
list-style-type:none;
}
div.auto_complete ul li {
margin:0;
padding:3px;
}
div.auto_complete ul li.selected {
background-color: #ffb;
}
div.auto_complete ul strong.highlight {
color: #800;
margin:0;
padding:0;
}
}
=item $prototype->content_tag( $name, $content, \%html_options )
Returns a block with opening tag, content, and ending tag. Useful for
autogenerating tags like B<Catalyst
Homepage>. The first parameter is the tag name, i.e. B<'a'> or
B<'img'>.
=cut
sub content_tag {
my ( $self, $name, $content, $html_options ) = @_;
$html_options ||= {};
my $tag = HTML::Element->new( $name, %$html_options );
$tag->push_content($content);
return $tag->as_HTML;
}
=item $prototype->define_javascript_functions
Returns the library of JavaScript functions and objects, in a script block.
Notes for L users:
You can use C