package HTML::Prototype; use strict; use base 'Class::Accessor::Fast'; our $VERSION = '1.23'; use HTML::Prototype::Js; our $prototype = do { package HTML::Prototype::Js; local $/; }; my $callbacks = [qw/uninitialized loading loaded interactive complete/]; =head1 NAME HTML::Prototype - Generate HTML and Javascript for the Prototype library =head1 SYNOPSIS use HTML::Prototype; my $prototype = HTML::Prototype->new; print $prototype->define_javascript_functions; print $prototype->form_remote_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->submit_to_remote(...); =head1 DESCRIPTION Some code generators for Prototype, the famous JavaScript OO library. This library allows you to do Ajax without writing lots of javascript code. This is mostly a port of the Ruby on Rails helper tags for JavaScript for use in L. =head2 METHODS =head3 $prototype->define_javascript_functions Returns the library of JavaScript functions and objects, in a script block. Notes for L users: You can use C } =head3 $prototype->form_remote_tag(\%options) Returns a form tag that will submit in the background using XMLHttpRequest, instead of the regular reloading POST arrangement. Even though it's using JavaScript to serialize the form elements, the form submission will work just like a regular submission as viewed by the receiving side. The options for specifying the target with C and defining callbacks is the same as C. =cut sub form_remote_tag { my ( $self, $options ) = @_; $options->{form} = 1; my $code = _remote_function($options); return qq/
/; } =head3 $prototype->link_to_function( $name, $function ) Returns a link that will trigger a JavaScript function using the onClick handler and return false after the fact. Examples: $prototype->link_to_function( "Greeting", "alert('Hello world!') ) $prototype->link_to_function( '', 'do_delete()' ) =cut sub link_to_function { my ( $self, $name, $function ) = @_; return qq|$name|; } =head3 $prototype->link_to_remote( $content, \%options ) Returns a link to a remote action defined by options C that's called in the background using XMLHttpRequest. The result of that request can then be inserted into a DOM object whose id can be specified with options->{update}. Examples: $prototype->link_to_remote( 'Delete', { update => 'posts', url => 'http://localhost/posts/' } ) $prototype->link_to_remote( '', { update => 'emails', url => 'http://localhost/refresh/' } ) By default, these remote requests are processed asynchronously, during which various callbacks can be triggered (e.g. for progress indicators and the like). Example: $prototype->link_to_remote( 'count', { url => 'http://localhost/count/', complete => 'doStuff(request)' } ) The callbacks that may be specified are: C: Called when the remote document is being loaded with data by the browser. C: Called when the browser has finished loading the remote document. C: Called when the user can interact with the remote document, even though it has not finished loading. C: Called when the XMLHttpRequest is complete. If you do need synchronous processing (this will block the browser while the request is happening), you can specify $options->{type} = 'synchronous'. =cut sub link_to_remote { my ( $self, $id, $options ) = @_; $self->link_to_function( $id, _remote_function($options) ); } =head3 $prototype->observe_field( $id, \%options) Observes the field with the DOM ID specified by $id and makes an Ajax when its contents have changed. Required options are: C: The frequency (in seconds) at which changes to this field will be detected. C: url to be called when field content has changed. Additional options are: C: Specifies the DOM ID of the element whose innerHTML should be updated with the XMLHttpRequest response text. 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. Additionally, you may specify any of the options documented in C. Example TT2 template in L: [% c.prototype.define_javascript_functions %]

[% page.title %]

[% url = base _ 'edit/' _ page.title %] [% c.prototype.observe_field( 'editor', { url => url, with => "'body='+value", update => 'view' } ) %] =cut sub observe_field { my ( $self, $id, $options ) = @_; _build_observer( 'Form.Element.Observer', $id, $options ); } =head3 $prototype->observe_form( $id, \%options ) Like C, but operates on an entire form identified by the DOM ID $id. Options are the same as C, except the default value of the C option evaluates to the serialized (request string) value of the form. =cut sub observe_form { my ( $self, $id, $options ) = @_; _build_observer( 'Form.Observer', $id, $options ); } =head3 $prototype->periodically_call_remote( \%options ) Periodically calls the specified url $options->{url} every $options->{frequency} seconds (default is 10). Usually used to update a specified div $options->{update} with the results of the remote call. The options for specifying the target with C and defining callbacks is the same as C. =cut sub periodically_call_remote { my ( $self, $options ) = @_; my $frequency = $options->{frequency} || 10; my $code = _remote_function($options); return <<""; } =head3 $prototype->submit_to_remote( $name, $value, \%options ) Returns a button input tag that will submit a form using XMLHttpRequest in the background instead of a typical reloading via POST. C argument is the same as in C =cut sub submit_to_remote { my ( $self, $name, $value, $options ) = @_; my $code = _remote_function($options); $code = "$code; return false;"; return qq||; } sub _build_callbacks { my $options = shift; my %callbacks; for my $callback (@$callbacks) { if ( my $code = $options->{$callback} ) { my $name = 'on' . ucfirst $callback; $callbacks{$name} = "function(request){$code}"; } } return \%callbacks; } sub _build_observer { my ( $class, $name, $options ) = @_; $options->{with} ||= 'value' if $options->{update}; my $freq = $options->{frequency} || 2; my $callback = _remote_function($options); return <<""; } sub _options_for_ajax { my $options = shift; my $js_options = _build_callbacks($options); $options->{type} ||= ''; $js_options->{asynchronous} = $options->{type} eq 'synchronous' ? 0 : 1; $js_options->{method} = $options->{method} if $options->{method}; my $position = $options->{position}; $js_options->{insertion} = "Insertion.$position" if $position; if ( $options->{form} ) { $js_options->{parameters} = 'Form.serialize(this)'; } elsif ( $options->{with} ) { $js_options->{parameters} = $options->{with}; } return '{ ' . join( ',', map { "$_: " . $js_options->{$_} } keys %$js_options ) . ' }'; } sub _remote_function { my $options = shift; my $js_options = _options_for_ajax($options); my $update = $options->{update}; my $function = $update ? " new Ajax.Updater( '$update', " : ' new Ajax.Request( '; my $url = $options->{url} || ''; $function .= " '$url', $js_options ) "; my $before = $options->{before}; $function = "$before; $function " if $before; my $after = $options->{after}; $function = "$function; $after;" if $after; my $condition = $options->{condition}; $function = "if ($condition) { $function; }" if $condition; return $function; } =head1 SEE ALSO L, L. L =head1 AUTHOR Sebastian Riedel, C Marcus Ramberg, C Built around Prototype by Sam Stephenson. Much code is ported from Ruby on Rails javascript helpers. =head1 THANK YOU Drew Taylor, Leon Brocard =head1 LICENSE This library is free software. You can redistribute it and/or modify it under the same terms as perl itself. =cut 1;