package HTML::Prototype; use strict; use base qw/Class::Accessor::Fast/; use vars qw/$VERSION $prototype $controls $dragdrop $effects/; $VERSION = '1.45'; use HTML::Element; use HTML::Prototype::Js; use HTML::Prototype::Controls; use HTML::Prototype::DragDrop; use HTML::Prototype::Effects; use HTML::Prototype::Helper; $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->in_place_editor(...); print $prototype->in_place_editor_field(...); print $prototype->in_place_editor_stylesheet(...); 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 $prototype->text_field_with_auto_complete(...); print $prototype->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->in_place_editor( $field_id, \%options ) Makes an HTML element specified by the DOM ID C<$field_id> become an in-place editor of a property. A form is automatically created and displayed when the user clicks the element, something like this:
cancel
The form is serialized and sent to the server using an Ajax call, the action on the server should process the value and return the updated value in the body of the reponse. The element will automatically be updated with the changed value (as returned from the server). Required options are: C: Specifies the url where the updated value should be sent after the user presses "ok". Addtional options are: C: Number of rows (more than 1 will use a TEXTAREA) C: The number of columns the text area should span (works for both single line or multi line). C: Synonym for ‘cols’ when using single-line (rows=1) input C: The text on the cancel link. (default: "cancel") C: CSS class used for the in place edit form. (default: "inplaceeditor-form") C: The text on the save link. (default: "ok") C: CSS class added to the element while displaying "Saving..." (removed when server responds). (default: "inplaceeditor-saving") C: Will cause the text to be loaded from the server (useful if your text is actually textile and formatted on the server) C: If the C option is specified then this text is displayed while the text is being loaded from the server. (default: "Loading...") C: The text on the click-to-edit link. (default: "click to edit") C: The id of an external control used to enter edit mode. C: Pass through options to the AJAX call (see prototype's Ajax.Updater) C: JavaScript snippet that should return what is to be sent in the Ajax call, C
and C are implicit parameters =cut sub in_place_editor { my ( $self, $id, $options ) = @_; my %to_options = ( 'cancel_text' => \'cancelText', 'save_text' => \'okText', 'rows' => 'rows', 'external_control' => \'externalControl', 'ajax_options' => 'ajaxOptions', 'saving_text' => \'savingText', 'saving_class_name' => \'savingClassName', 'form_id' => \'formId', 'cols' => 'cols', 'size' => 'size', 'load_text_url' => \'loadTextURL', 'loading_text' => \'loadingText', 'form_class_name' => \'formClassName', 'click_to_edit_text' => \'clickToEditText', ); my $function = "new Ajax.InPlaceEditor( '$id', '" . $options->{url} . "'"; my $js_options = _options_to_js_options( \%to_options, $options ); $js_options->{callback} = ( 'function ( form, value ) { return ' . $options->{with} . ' }' ) if $options->{with}; $function .= ',' . _options_for_javascript($js_options) if keys %{$js_options}; $function .= ')'; return $self->javascript_tag($function); } =item $prototype->in_place_editor_field( $object, $method, \%tag_options, \%in_place_editor_options ) Renders the value of the specified object and method with in-place editing capabilities. =cut sub in_place_editor_field { my ( $self, $object, $method, $tag_options, $in_place_editor_options ) = @_; $tag_options ||= {}; $in_place_editor_options ||= {}; my $tag = HTML::Prototype::Helper::Tag->new( $object, $method, $self ); $tag_options = { tag => 'span', id => "$object\_$method\_" . $tag->object->id . '_in_place_editor', class => 'in_place_editor_field', %{$tag_options}, }; return $tag->to_content_tag( delete $tag_options->{tag}, $tag_options ) . $self->in_place_editor( $tag_options->{id}, $in_place_editor_options ); } =item $prototype->in_place_editor_stylesheet Returns the in_place_editor stylesheet. =cut sub in_place_editor_stylesheet { my $self = shift; return $self->content_tag( 'style', <<""); .inplaceeditor-saving { background: url(wait.gif) bottom right no-repeat; } } =item $prototype->auto_complete_field( $field_id, \%options ) Adds Ajax autocomplete functionality to the text input field with the DOM ID specified by C<$field_id>. 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 Here's an example using L with an indicator against the auto_complete_result example below on the server side. Notice the 'style="display:none"' in the indicator . <% $c->prototype->define_javascript_functions %>
    Type search terms

    <% $c->prototype->auto_complete_field( 'acomp', { url => '/autocomplete', indicator => 'acomp_stat' } ) %> while autocomplete is running. C: A string or an array of strings containing separator tokens for tokenized incremental autocompletion. Example: C< ','>> would allow multiple autocompletion entries, separated by commas. C: The minimum number of characters that should be in the input field before an Ajax call is made to the server. C: A Javascript expression that is called when the autocompletion div is hidden. The expression should take two variables: element and update. Element is a DOM element for the field, update is a DOM element for the div from which the innerHTML is replaced. C: Like on_hide, only now the expression is called then the div is shown. C