[Bps-public-commit] r17147 - in Prophet/branches/actions: lib/Prophet lib/Prophet/Server lib/Prophet/Server/ViewHelpers t/WebToy/lib/App/WebToy/Model

jesse at bestpractical.com jesse at bestpractical.com
Mon Dec 8 22:12:02 EST 2008


Author: jesse
Date: Mon Dec  8 22:12:01 2008
New Revision: 17147

Modified:
   Prophet/branches/actions/lib/Prophet/Server.pm
   Prophet/branches/actions/lib/Prophet/Server/Controller.pm
   Prophet/branches/actions/lib/Prophet/Server/Dispatcher.pm
   Prophet/branches/actions/lib/Prophet/Server/View.pm
   Prophet/branches/actions/lib/Prophet/Server/ViewHelpers/Function.pm
   Prophet/branches/actions/lib/Prophet/Server/ViewHelpers/Widget.pm
   Prophet/branches/actions/lib/Prophet/Util.pm
   Prophet/branches/actions/t/WebToy/lib/App/WebToy/Model/WikiPage.pm
   Prophet/branches/actions/t/WebToy/lib/App/WebToy/Server/Dispatcher.pm
   Prophet/branches/actions/t/WebToy/lib/App/WebToy/Server/View.pm

Log:
a bit more hacking on autocompleters

Modified: Prophet/branches/actions/lib/Prophet/Server.pm
==============================================================================
--- Prophet/branches/actions/lib/Prophet/Server.pm	(original)
+++ Prophet/branches/actions/lib/Prophet/Server.pm	Mon Dec  8 22:12:01 2008
@@ -7,8 +7,24 @@
 use Prophet::Server::Dispatcher;
 use Prophet::Server::Controller;
 use Params::Validate qw/:all/;
+use File::ShareDir qw//;
+use File::Spec ();
+use Cwd ();
 use JSON;
 
+
+my $STATIC_ROOT = Cwd::fast_abs_path(
+    File::Spec->catdir(
+        Prophet::Util->updir($INC{'Prophet.pm'}),"..","share","web","static"
+    )
+);
+
+if (!-d $STATIC_ROOT) {
+ warn "not $STATIC_ROOT";
+    warn Cwd::cwd();
+    $STATIC_ROOT= File::Spec->catfile( File::ShareDir::dist_dir('Prophet'),'web/static');
+    }
+
 has app_handle => (
     isa     => 'Prophet::App',
     is      => 'rw',
@@ -66,7 +82,7 @@
  
      my $d =$dispatcher_class->new( server => $self );
 
-
+    warn "Handling ".$cgi->path_info;
     $d->run( $cgi->request_method .  $cgi->path_info, $d )
         || $self->_send_404;
 
@@ -161,7 +177,7 @@
             encode_as => 'json',
             content   => $self->handle->list_types
         );
-    }
+}
 
 
 sub serve_replica {
@@ -182,6 +198,7 @@
     my $p    = shift;
     if ( Template::Declare->has_template($p) ) {
         Prophet::Server::View->app_handle( $self->app_handle );
+        Prophet::Server::View->cgi( $self->cgi );
         my $content = Template::Declare->show($p, at _);
         return $self->send_content( content_type => 'text/html', content      => $content,);
     }
@@ -207,6 +224,28 @@
     return $record;
 }
 
+
+sub send_static_file {
+    my $self     = shift;
+    my $filename = shift;
+    my $type = 'text/html';
+
+    if ( $filename =~ /.js$/ ) {
+        $type = 'text/javascript';
+    } elsif ( $filename =~ /.css$/ ) {
+        $type = 'text/css';
+
+    }
+
+    my $qualified_file = Cwd::fast_abs_path( File::Spec->catfile($STATIC_ROOT => $filename));
+    return $self->_send_404
+        if substr( $qualified_file, 0, length($STATIC_ROOT) ) ne $STATIC_ROOT;
+    my $content = Prophet::Util->slurp($qualified_file);
+
+    $self->send_content( content => $content, content_type => $type );
+
+}
+
 sub send_content {
     my $self = shift;
     my %args

Modified: Prophet/branches/actions/lib/Prophet/Server/Controller.pm
==============================================================================
--- Prophet/branches/actions/lib/Prophet/Server/Controller.pm	(original)
+++ Prophet/branches/actions/lib/Prophet/Server/Controller.pm	Mon Dec  8 22:12:01 2008
@@ -1,6 +1,6 @@
 package Prophet::Server::Controller;
 use Moose;
-
+use Prophet::Util;
 
 has cgi => (is => 'rw', isa => 'CGI');
 has failed => ( is => 'rw', isa => 'Bool');
@@ -26,7 +26,7 @@
 
     my $actions = {};
    foreach my $param ($self->cgi->all_parameters){
-        next unless $param =~ /^prophet-function\|(.*)$/;
+        next unless $param =~ /^prophet-function-(.*)$/;
         my $name = $1;
         warn "Duplicate action definition for @{[$name]}." if (exists $actions->{$name});
 
@@ -46,13 +46,12 @@
 
     my $values;
     for my $field ( $self->cgi->all_parameters ) {
-        next unless ( $field =~ /^prophet-field\|function=$action\|(.*)\|$/ );
-        my $metadata = $1;
-        my $meta     = $self->string_to_hash($metadata);
-        my $name     = $meta->{prop};
+        next unless ( $field =~ /^prophet-field-function-$action-prop-(.*)$/ );
+        my $name     = $1;
+        my $meta     = {};
+        $meta->{prop} = $name;
         $meta->{value} = $self->cgi->param($field);
         $meta->{original_value} = $self->cgi->param( "original-value-" . $field );
-
         $values->{$name} = $meta;
 
     }
@@ -61,9 +60,6 @@
 }
 
 
-sub _bundle_to_hash {
-    my $self = shift;
-}
 
 sub handle_actions {
     my $self = shift;
@@ -153,10 +149,9 @@
     my $self = shift;
     my $action = shift;
 
-    die $action->{class} ." is not a valid class " unless (UNIVERSAL::isa($action->{class}, 'Prophet::Record'));
-    my $object = $action->{class}->new( uuid => $action->{uuid}, app_handle => $self->app_handle);
-    die "Did not find the object " unless $object->uuid;
-    warn "YAY we got ".$object->uuid;
+    my $object = Prophet::Util->instantiate_record( uuid => $action->{uuid}, class=>$action->{class}, app_handle=> $self->app_handle);
+    warn "My reocrd is $object";
+    warn YAML::Dump($action); use YAML;
     my ( $val, $msg ) = $object->set_props(
         props => {
             map {
@@ -165,7 +160,7 @@
             }
 
     );
-    warn $val, $msg;
+    warn "Updated the record" . $val, $msg;
 
 }
 

Modified: Prophet/branches/actions/lib/Prophet/Server/Dispatcher.pm
==============================================================================
--- Prophet/branches/actions/lib/Prophet/Server/Dispatcher.pm	(original)
+++ Prophet/branches/actions/lib/Prophet/Server/Dispatcher.pm	Mon Dec  8 22:12:01 2008
@@ -22,6 +22,10 @@
 };
 
 under 'GET' => sub {
+    my $self = shift;
+    on qr'^=/prophet/autocomplete' => sub {
+        shift->server->show_template('/_prophet_autocompleter') };
+    on qr'^static/prophet/(.*)$' => sub { shift->server->send_static_file($1)};
     on qr'replica/+(.*)$' => sub { shift->server->serve_replica($1) };
     on 'records.json' => sub { shift->server->get_record_types };
     under 'records' => sub {

Modified: Prophet/branches/actions/lib/Prophet/Server/View.pm
==============================================================================
--- Prophet/branches/actions/lib/Prophet/Server/View.pm	(original)
+++ Prophet/branches/actions/lib/Prophet/Server/View.pm	Mon Dec  8 22:12:01 2008
@@ -12,6 +12,38 @@
     $APP_HANDLE = shift if (@_);
     return $APP_HANDLE;
 }
+
+our $CGI;
+sub cgi {
+    my $self = shift;
+    $CGI = shift if (@_);
+    return $CGI;
+}
+
+
+
+
+template '_prophet_autocompleter' => sub {
+        my $self = shift;
+        my %args;
+        for (qw(q function record type class prop)) {
+            $args{$_} = $self->cgi->param($_);
+        }
+        my $obj = Prophet::Util->instantiate_record(
+            class      => $self->cgi->param('class'),
+            uuid       => $self->cgi->param('uuid'),
+            app_handle => $self->app_handle
+        );
+
+        outs_raw(
+        $obj->prop($self->cgi->param('prop')). " | ".
+        $obj->prop($self->cgi->param('prop'))
+        );
+
+};
+
+
+
 sub default_page_title { 'Prophet' }
 
 template head => sub {
@@ -19,6 +51,10 @@
     my $args = shift;
     head {
         title { shift @$args };
+
+    script {{ src is '/static/prophet/js/jquery.js', type is "text/javascript"}};
+    script {{ src is '/static/prophet/js/jquery-autocomplete.js', type is "text/javascript"}};
+    link {{ rel is 'stylesheet', href is '/static/prophet/css/jquery.autocomplete.css', type is "text/css"}};
     }
 
 };
@@ -26,6 +62,7 @@
 template footer => sub {};
 
 
+
 template '/' => page {
             h1 { "This is a Prophet replica!" }
 };

Modified: Prophet/branches/actions/lib/Prophet/Server/ViewHelpers/Function.pm
==============================================================================
--- Prophet/branches/actions/lib/Prophet/Server/ViewHelpers/Function.pm	(original)
+++ Prophet/branches/actions/lib/Prophet/Server/ViewHelpers/Function.pm	Mon Dec  8 22:12:01 2008
@@ -54,7 +54,7 @@
 
 sub render {
     my $self = shift;
-    my %bits =( 
+    my %bits = ( 
         order  => $self->order,
         action => $self->action,
         type => $self->record->type,
@@ -70,7 +70,7 @@
     input {
         attr {
             type  => 'hidden',
-            name  => "prophet-function|" . $self->name,
+            name  => "prophet-function-" . $self->name,
             value => $string
         };
     };

Modified: Prophet/branches/actions/lib/Prophet/Server/ViewHelpers/Widget.pm
==============================================================================
--- Prophet/branches/actions/lib/Prophet/Server/ViewHelpers/Widget.pm	(original)
+++ Prophet/branches/actions/lib/Prophet/Server/ViewHelpers/Widget.pm	Mon Dec  8 22:12:01 2008
@@ -27,29 +27,65 @@
 has name => ( isa => 'Str', is => 'rw' );
 has prop => ( isa => 'Str', is => 'ro' );
 
-
+has field => ( isa => 'Prophet::Web::Field', is => 'rw');
 
 
 sub render {
     my $self = shift;
 
-    my $f = Prophet::Web::Field->new(
-        name   => $self->_generate_name(),
-        record => $self->function->record,
+    my $unique_name = $self->_generate_name();
+   
+    my $record = $self->function->record;
+
+    my $value;
+
+    if ( $self->function->action eq 'create' ) {
+        if ( my $method = $self->function->record->can( 'default_prop_' . $self->prop ) ) {
+            $value = $method->( $self->function->record );
+        } else {
+            $value = '';
+        }
+    } elsif ( $self->function->action eq 'update' && $self->function->record->loaded ) {
+        $value = $self->function->record->prop( $self->prop ) || '';
+    } else {
+        $value = '';
+    }
+
+    $self->field( Prophet::Web::Field->new(
+        name   => $unique_name,
+        id      => $unique_name,
+        record => $record,
         label  => $self->prop,
-        value  => ($self->function->record->loaded ? $self->function->record->prop( $self->prop ) : ''),
+        class  => 'prop-'.$self->prop.' function-'.$self->function->name,
+        value  => $value
         
-    );
+    ));
 
     my $orig = Prophet::Web::Field->new(
-        name  => "original-value-". $self->_generate_name(),
-        value =>
-        ($self->function->record->loaded ? $self->function->record->prop( $self->prop ) : ''),
+        name  => "original-value-". $unique_name,
+        value => $value,
         type  => 'hidden'
     );
-    outs_raw( $orig->render_input );
 
-    outs_raw( $f->render );
+    outs_raw( $orig->render_input );
+    outs_raw( $self->field->render );
+        outs_raw('<script>
+        $("#'.$self->field->id.'").autocomplete("/=/prophet/autocomplete",{ 
+       
+        selectFirst: false, 
+        autoFill: true,
+        minChars: 0,
+        delay: 0,
+        extraParams: {
+                    "function": "'.$self->field->name.'",
+                    "class": "'.ref($record).'",
+                    "uuid": "'.$record->uuid.'",
+                    "type": "'.$record->type.'",
+                    "prop": "'.$self->prop.'",
+                }
+                 }   
+                );
+        </script> ');
 }
 
 
@@ -58,10 +94,10 @@
 sub _generate_name {
     my $self = shift;
     return
-          "prophet-field|function="
+          "prophet-field-function-"
         . $self->function->name
-        . "|prop="
-        . $self->prop . "|";
+        . "-prop-"
+        . $self->prop;
 }
 
 =head1 METHODS

Modified: Prophet/branches/actions/lib/Prophet/Util.pm
==============================================================================
--- Prophet/branches/actions/lib/Prophet/Util.pm	(original)
+++ Prophet/branches/actions/lib/Prophet/Util.pm	Mon Dec  8 22:12:01 2008
@@ -1,6 +1,7 @@
 package Prophet::Util;
 use strict;
 use File::Basename;
+use Params::Validate;
 
 sub updir {
     my $self = shift;
@@ -19,4 +20,19 @@
     
     return wantarray ? @lines : join('', at lines);
 }
+
+sub instantiate_record {
+    my $self = shift;
+    my %args = validate(@_, { 
+        class => 1,
+        uuid => 1,
+        app_handle => 1
+
+        });
+    die $args{class} ." is not a valid class " unless (UNIVERSAL::isa($args{class}, 'Prophet::Record'));
+    my $object = $args{class}->new( uuid => $args{uuid}, app_handle => $args{app_handle});
+    die "Did not find the object " unless $object->uuid;
+    return $object;
+}
+
 1;

Modified: Prophet/branches/actions/t/WebToy/lib/App/WebToy/Model/WikiPage.pm
==============================================================================
--- Prophet/branches/actions/t/WebToy/lib/App/WebToy/Model/WikiPage.pm	(original)
+++ Prophet/branches/actions/t/WebToy/lib/App/WebToy/Model/WikiPage.pm	Mon Dec  8 22:12:01 2008
@@ -20,6 +20,10 @@
 =cut
 
 
+sub default_prop_content {
+    'This page has no content yet';
+}
+
 
 
 __PACKAGE__->meta->make_immutable;

Modified: Prophet/branches/actions/t/WebToy/lib/App/WebToy/Server/Dispatcher.pm
==============================================================================
--- Prophet/branches/actions/t/WebToy/lib/App/WebToy/Server/Dispatcher.pm	(original)
+++ Prophet/branches/actions/t/WebToy/lib/App/WebToy/Server/Dispatcher.pm	Mon Dec  8 22:12:01 2008
@@ -1,8 +1,6 @@
 package App::WebToy::Server::Dispatcher;
 use Prophet::Server::Dispatcher -base;
 
-on qr'^GET/(.*)$' => sub {show_template($1)->(@_)};
-
 redispatch_to 'Prophet::Server::Dispatcher';
 
 

Modified: Prophet/branches/actions/t/WebToy/lib/App/WebToy/Server/View.pm
==============================================================================
--- Prophet/branches/actions/t/WebToy/lib/App/WebToy/Server/View.pm	(original)
+++ Prophet/branches/actions/t/WebToy/lib/App/WebToy/Server/View.pm	Mon Dec  8 22:12:01 2008
@@ -16,26 +16,27 @@
 
 =cut
 
-
-template 'abc' => sub {
+template 'abc' => page {
     my $self = shift;
     my $c = App::WebToy::Collection::WikiPage->new(app_handle => $self->app_handle);
     $c->matching(sub { return 1});
     my $r = $c->items->[0];
     h1 { $r->prop('title')};
     
-    
     form {
         my $f = function( record => $r, action => 'update');
-        widget( function => $f, prop => 'title');
+        my $w = widget( function => $f, prop => 'title');
+        widget( function => $f, prop => 'content');
         input {attr { label => 'save', type => 'submit'}};
     };
 
 
+
     form {
         my $f = function( record => App::WebToy::Model::WikiPage->new(app_handle => $self->app_handle ), 
                           action => 'create');
         widget( function => $f, prop => 'title');
+        widget( function => $f, prop => 'content');
         input {attr { label => 'save', type => 'submit'}};
 
 }



More information about the Bps-public-commit mailing list