Apache2-Autocomplete

 view release on metacpan or  search on metacpan

lib/Apache2/Autocomplete.pm  view on Meta::CPAN

    unless (ref($header_extra) eq 'HASH') {
      $r->log_error("Extra headers must be a hash ref");
      return;
    }
  }
  $self->header($header_extra);
  unless ($self->param('js')) {
    $r->print($self->no_js);
    return;
  }
  my ($query, $names, $values, $prefix) = $self->expand($self->query);
  $r->print($self->output($query, $names, $values, $prefix));
}

sub header { shift->cgi->header( @_ ) }

sub no_js {
  my $no_js = <<HTML;
<html>
<head>
<script>

lib/Apache2/Autocomplete.pm  view on Meta::CPAN

  </Location>
  ######################################################
  
  ###################################################### 
  # module file Apache2/MyAutoComplete.pm
  package Apache2::MyAutoComplete;
  use base qw(Apache2::Autocomplete);
  # use whatever else
  
  my @NAMES = qw(bob carol ted alice);
  sub expand {
    my ($self, $query) = @_;
    my $re = qr/^\Q$query\E/i;
    my @names = grep /$re/, @NAMES;
    my @values = map {"some description"} @names;
    (lc $query, \@names, \@values, [""]);
  }
  
  sub handler {
    my $r = shift;
    my $ac = __PACKAGE__->new($r);

lib/Apache2/Autocomplete.pm  view on Meta::CPAN

Here, I<search> is the value specified by the fourth argument
to I<InstallAC>, as discussed above - the I<complete> part
of the location is hard-coded in I<ac.js>. The Perl module
which handles requests for this location has the form

  # module file Apache2/MyAutoComplete.pm
  package Apache2::MyAutoComplete;
  use base qw(Apache2::Autocomplete);
  # use whatever else
  
  sub expand {
    my ($self, $query) = @_;
    # decide what completions to return, based on the $query
    (lc $query, $names, $values, [""]);
  }
  
  sub handler {
    my $r = shift;
    my $ac = __PACKAGE__->new($r);
    $ac->run();
    return Apache2::Const::OK;
  }

This must inherit from I<Apache2::Autocomplete>. Within this
handler must be an I<expand> method:

  sub expand {
    my ($self, $query) = @_;
    # decide what completions to return, based on the $query
    (lc $query, $names, $values, [""]);
  }

which is to return a list of 4 elements
to be used for autocompletion of the I$query> argument
passed in from the form. This list has the
following elements:

lib/Apache2/Autocomplete.pm  view on Meta::CPAN


=over

=item * my $ac = __PACKAGE__-E<gt>new($r);

This creates the object, and takes a mandatory
argument of the L<Apache2::RequestRec> object I<$r>.

=item * $ac-E<gt>run();

This is the main method which calls the I<expand> method
to find and format the results to be returned for the
autocompletion.

By default, the only header Apache::Autocomplete sets is
the I<Content-Type>, for which I<text/html> is used.
If additional headers are required, they may
be passed as an optional argument into I<run()>in the
form of a hash reference, as in

  my $header = {'Content-Type' => 'text/html; charset=utf-8',

t/response/TestAutocomplete/basic.pm  view on Meta::CPAN

use Apache2::Const -compile => qw(OK SERVER_ERROR);
use Apache2::RequestRec ();
use Apache2::RequestIO ();
use Apache2::RequestUtil ();

use base qw(Apache2::Autocomplete);
my $i = 0;
my @NAMES = qw(alice bob charlie tom dick jane janice allen diane);
my %NAMES = map {$_ => $i++} @NAMES;

sub expand {
  my ($self, $query) = @_;
  my $re = qr/^\Q$query\E/i;
  my @names = grep /$re/, @NAMES;
  my @desc = map {$NAMES{$_}} @names;
  (lc $query, \@names, \@desc, [""]);
}


sub handler {
  my ($r) = @_;
  plan $r, tests => 12;
  my $ac = __PACKAGE__->new($r);
  isa_ok($ac, __PACKAGE__);
  for my $method(qw(expand run header no_js param query)) {
    can_ok($ac, $method);
  }
  my $cgi = $ac->cgi;
  like(ref($cgi), qr{^CGI});
  my $self_r = $ac->r;
  isa_ok($self_r, 'Apache2::RequestRec');

 SKIP: {
    eval {require CGI::Apache2::Wrapper;};
    skip "CGI::Apache2::Wrapper not installed", 3 if $@;

t/response/TestAutocomplete/names.pm  view on Meta::CPAN

use strict;
use warnings;
use Apache2::Const -compile => qw(OK SERVER_ERROR);
use Apache2::RequestRec ();
use Apache2::RequestIO ();
use Apache2::RequestUtil ();

use base qw(Apache2::Autocomplete);
my @NAMES = qw(alice bob charlie tom dick jane janice allen diane);

sub expand {
  my ($self, $query) = @_;
  my $re = qr/^\Q$query\E/i;
  my @names = grep /$re/, @NAMES;
  my @desc = map {"42 is the answer"} @names;
  (lc $query, \@names, \@desc, [""]);
}

sub handler {
  my $r = shift;
  my $ac = __PACKAGE__->new($r);

t/response/TestAutocomplete/names_header.pm  view on Meta::CPAN

use strict;
use warnings;
use Apache2::Const -compile => qw(OK SERVER_ERROR);
use Apache2::RequestRec ();
use Apache2::RequestIO ();
use Apache2::RequestUtil ();

use base qw(Apache2::Autocomplete);
my @NAMES = qw(alice bob charlie tom dick jane janice allen diane);

sub expand {
  my ($self, $query) = @_;
  my $re = qr/^\Q$query\E/i;
  my @names = grep /$re/, @NAMES;
  my @desc = map {"42 is the answer"} @names;
  (lc $query, \@names, \@desc, [""]);
}

sub handler {
  my $r = shift;
  my $header = {'Content-Type' => 'text/html; charset=utf-8',



( run in 0.690 second using v1.01-cache-2.11-cpan-5b529ec07f3 )