HTML-Prototype

 view release on metacpan or  search on metacpan

lib/HTML/Prototype.pm  view on Meta::CPAN

    $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 <ul> list,
or nothing if no entries should be displayed for autocompletion.

Required options are:

C<url>: Specifies the URL to be used in the AJAX call.


Addtional options are:

C<update>: 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<with>: 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<indicator>: Specifies the DOM ID of an elment which will be displayed
Here's an example using L<Catalyst::View::Mason> with an indicator against the auto_complete_result example below on the server side.  Notice the 'style="display:none"' in the indicator <span>.

	<% $c->prototype->define_javascript_functions %>

	<form action="/bar" method="post" id="baz">
	<fieldset>
        	<legend>Type search terms</legend>
        	<label for="acomp"><span class="field">Search:</span></label>
        	<input type="text" name="acomp" id="acomp"/>
		<span style="display:none" id="acomp_stat">Searching...</span><br />
	</fieldset>
	</form>

        <span id="acomp_auto_complete"></span><br/>

	<% $c->prototype->auto_complete_field( 'acomp', { url => '/autocomplete', indicator => 'acomp_stat' } ) %>

while autocomplete is running.

C<tokens>: A  string or an array of strings containing separator tokens for
tokenized incremental autocompletion. Example: C<<tokens => ','>> would
allow multiple autocompletion entries, separated by commas.

C<min_chars>: The minimum number of characters that should be in the input
field before an Ajax call is made to the server.

C<on_hide>: 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<on_show>: Like on_hide, only now the expression is called then the div
is shown.

C<select>: Pick the class of the element from which the value for
insertion should be extracted. If this is not specified,
the entire element is used


=cut

sub auto_complete_field {
    my ( $self, $id, $options ) = @_;

    my %to_options = (
        'on_show'   => 'onShow',
        'on_hide'   => 'onHide',
        'min_chars' => 'minChars',
        'indicator' => \'indicator',
        'select'    => \'select',
    );
    $options ||= {};
    my $update = ( $options->{update} || "$id" ) . '_auto_complete';
    my $function =
      "new Ajax.Autocompleter( '$id', '$update', '"
      . ( $options->{url} || '' ) . "'";

    my $js_options = _options_to_js_options( \%to_options, $options );
    $js_options->{tokens} =
      _array_or_string_for_javascript( $options->{tokens} )
      if $options->{tokens};
    $js_options->{callback} =
      ( 'function ( element, value ) { return ' . $options->{with} . ' }' )
      if $options->{with};

    $function .= ', ' . _options_for_javascript($js_options)
      if keys %{$js_options};



( run in 1.105 second using v1.01-cache-2.11-cpan-39bf76dae61 )