[Jifty-commit] r4206 - in jifty/trunk: . lib/Jifty/Plugin

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Wed Oct 3 23:59:11 EDT 2007


Author: sterling
Date: Wed Oct  3 23:59:11 2007
New Revision: 4206

Added:
   jifty/trunk/lib/Jifty/Plugin/AutoReference/
   jifty/trunk/lib/Jifty/Plugin/AutoReference.pm
   jifty/trunk/lib/Jifty/Plugin/AutoReference/Widget.pm
Modified:
   jifty/trunk/   (props changed)

Log:
 r12837 at riddle:  andrew | 2007-10-03 22:57:39 -0500
 Added a new plugin that provides an autocomplete widget specially for record references. Helpful in cases when a select is just be too big to contemplate.


Added: jifty/trunk/lib/Jifty/Plugin/AutoReference.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/lib/Jifty/Plugin/AutoReference.pm	Wed Oct  3 23:59:11 2007
@@ -0,0 +1,132 @@
+use strict;
+use warnings;
+
+package Jifty::Plugin::AutoReference;
+use base qw/ Jifty::Plugin /;
+
+use Jifty::Plugin::AutoReference::Widget;
+
+=head1 NAME
+
+Jifty::Plugin::AutoReference - a plugin to provide a special reference completer
+
+=head1 SYNOPSIS
+
+Add this to your F<config.yml>:
+
+  Plugins:
+   - AutoReference: {}
+
+and then this to your models:
+
+  use MyApp::Record schema {
+      column employer =>
+          references MyApp::Model::Company,
+          label is 'Employer',
+          is AutoReference,
+          ;
+  };
+
+=head1 DESCRIPTION
+
+Provides a special autocompletion widget for reference columns. See L<Jifty::Plugin::AutoReference::Widget>.
+
+=cut
+
+sub init {
+    Jifty->web->add_javascript(qw/ autoreference.js /);
+}
+
+sub static_root {
+    return Jifty::Util->app_root.'/share/plugins/Jifty/Plugin/AutoReference/web/static';
+}
+
+sub _auto_reference_autocompleter {
+    my ($column, $from) = @_;
+
+    my $reference = $column->refers_to;
+    my $field     = $column->by || 'id';
+
+    my $brief     = $reference->can('_brief_description') ? 
+                        $reference->_brief_description : 'name';
+
+    return sub {
+        my $self  = shift;
+        my $value = shift;
+
+        my $collection = Jifty::Collection->new(
+            record_class => $reference,
+            current_user => $self->current_user,
+        );
+
+        $collection->unlimit;
+        $collection->rows_per_page(20);
+
+        if (length $value) {
+            $collection->limit(
+                column   => $brief,
+                value    => $value,
+                operator => 'MATCHES',
+                entry_aggregator => 'AND',
+            );
+        }
+
+        $collection->limit(
+            column   => $brief,
+            value    => 'NULL',
+            operator => 'IS NOT',
+            entry_aggregator => 'AND',
+        );
+
+        $collection->limit(
+            column   => $brief,
+            value    => '',
+            operator => '!=',
+            entry_aggregator => 'AND',
+        );
+
+        $collection->columns('id', $brief);
+        $collection->order_by(column => $brief);
+
+        Jifty->log->info($collection->build_select_query);
+
+        my @choices;
+        while (my $record = $collection->next) {
+            push @choices, { 
+                label => $record->brief_description.' [id:'.$record->id.']', 
+                value => $record->id,
+            };
+        }
+
+        return @choices;
+    };
+}
+
+sub _auto_reference {
+    my ($column, $from) = @_;
+
+    my $name = $column->name;
+
+    no strict 'refs';
+    *{$from.'::autocomplete_'.$name} 
+        = _auto_reference_autocompleter($column, $from);
+}
+
+use Jifty::DBI::Schema;
+Jifty::DBI::Schema->register_types(
+    AutoReference =>
+        sub { _init_handler is \&_auto_reference, render_as 'Jifty::Plugin::AutoReference::Widget' } );
+
+=head1 AUTHORS
+
+Andrew Sterling Hanenkamp C<< <hanenkamp at cpan.org> >>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2007 Boomer Consulting, Inc. All Rights Reserved.
+
+This program is free software and may be modified and distributed under the same terms as Perl itself.
+
+=cut
+
+1;

Added: jifty/trunk/lib/Jifty/Plugin/AutoReference/Widget.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/lib/Jifty/Plugin/AutoReference/Widget.pm	Wed Oct  3 23:59:11 2007
@@ -0,0 +1,76 @@
+use strict;
+use warnings;
+
+package Jifty::Plugin::AutoReference::Widget;
+use base qw/ Jifty::Web::Form::Field /;
+
+=head1 NAME
+
+Jifty::Plugin::AutoReference::Widget - an autocomplete widget for references
+
+=head1 SYNOPSIS
+
+  use MyApp::Record schema {
+      column employer =>
+          references MyApp::Model::Company,
+          label is 'Employer',
+          is AutoReference,
+          ;
+  };
+
+=head1 DESCRIPTION
+
+Provides a special autocomplete widget that can be useful when there are too many items for a Select box to be practical.
+
+B<WARNING:> As of this writing, it should be noted that this widget does not degrade gracefully. If you need a widget that operates properly even when JavaScript is unavailable, this widget won't do that job at this time.
+
+=cut
+
+sub render {
+    my $self = shift;
+
+    $self->autocompleter(1);
+    return $self->SUPER::render(@_);
+}
+
+sub render_widget {
+    my $self = shift;
+
+    # Render the shown autocomplete field first
+    my $input_name = $self->input_name;
+    $self->input_name($input_name.'-display');
+    my $element_id = $self->element_id;
+    $self->_element_id($element_id.'-display');
+    my $class = $self->class;
+    $self->class(join ' ', ($class||''), 'text');
+    $self->SUPER::render_widget(@_);
+    $self->input_name($input_name);
+    $self->_element_id($element_id);
+    $self->class($class);
+
+    # Render the hidden value field second
+    $self->type('hidden');
+    $self->SUPER::render_widget(@_);
+    $self->type('text');
+
+    return '';
+}
+
+sub autocomplete_javascript {
+    my $self = shift;
+    return qq{new Jifty.Plugin.AutoReference('@{[$self->element_id]}-display','@{[$self->element_id]}','@{[$self->element_id]}-autocomplete')};
+}
+
+=head1 AUTHORS
+
+Andrew Sterling Hanenkamp C<< <hanenkamp at cpan.org> >>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2007 Boomer Consulting, Inc. All Rights Reserved.
+
+This program is free software and may be modified and distributed under the same terms as Perl itself.
+
+=cut
+
+1;


More information about the Jifty-commit mailing list