[Jifty-commit] r1861 - jifty/trunk/lib/Jifty/Web/Form

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Thu Aug 24 22:24:38 EDT 2006


Author: nelhage
Date: Thu Aug 24 22:24:38 2006
New Revision: 1861

Modified:
   jifty/trunk/lib/Jifty/Web/Form/Clickable.pm
   jifty/trunk/lib/Jifty/Web/Form/Element.pm

Log:
A little more Element/Clickable refactoring, and implementing a
C<disable> option to onclick handlers that toggles whether or not to
disable form fields for an action.

Modified: jifty/trunk/lib/Jifty/Web/Form/Clickable.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web/Form/Clickable.pm	(original)
+++ jifty/trunk/lib/Jifty/Web/Form/Clickable.pm	Thu Aug 24 22:24:38 2006
@@ -513,11 +513,11 @@
     for my $trigger ( $self->handlers ) {
         my $value = $self->$trigger;
         next unless $value;
-        my @hooks = ref $value eq "ARRAY" ? @{$value} : ($value);
+        my @hooks = @{$value};
         for my $hook (@hooks) {
             next unless ref $hook eq "HASH";
             $hook->{region} ||= $hook->{refresh} || Jifty->web->qualified_region;
-            $hook->{args}   ||= {};
+
             my $region = ref $hook->{region} ? $hook->{region} : Jifty->web->get_region( $hook->{region} );
 
             if ($hook->{replace_with}) {

Modified: jifty/trunk/lib/Jifty/Web/Form/Element.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web/Form/Element.pm	(original)
+++ jifty/trunk/lib/Jifty/Web/Form/Element.pm	Thu Aug 24 22:24:38 2006
@@ -86,6 +86,15 @@
 An action, moniker of an action, or array reference to such; these
 actions are submitted when the event is fired.
 
+=item disable => BOOLEAN
+
+If true, disable all form fields associated with the actions in
+C<submit> when this Element is clicked. This serves to give immediate
+visual indication that the request is being processed, as well as to
+prevent double-submits.
+
+Defaults to true.
+
 =item args => HASHREF
 
 Arguments to the region.  These will override the arguments to the
@@ -148,15 +157,35 @@
 
     my ($arg) = @_;
 
-    # Normalize actions to monikers to prevent circular references,
-    # since Jifty::Action caches instances of Jifty::Web::Form::Clickable.
-    for my $hook ( ref($arg) eq 'ARRAY' ? @$arg : $arg ) {
+    $arg = [$arg] unless ref $arg eq 'ARRAY';
+
+    #Normalize arguments as much as possible here, to simplify later
+    #processing of them here and in Clickable.
+    for my $hook ( @$arg ) {
         next unless ref $hook eq 'HASH';
-        next unless $hook->{submit};
-        $hook->{submit} = [ $hook->{submit} ] unless ref $hook->{submit} eq "ARRAY";
-        $hook->{submit} = [ map { ref $_ ? $_->moniker : $_ } @{ $hook->{submit} } ];
-    }
 
+        # Normalize actions to monikers to prevent circular references,
+        # since Jifty::Action caches instances of Jifty::Web::Form::Clickable.
+        if ( $hook->{submit} ) {
+            $hook->{submit} = [ $hook->{submit} ] unless ref $hook->{submit} eq "ARRAY";
+            $hook->{submit} = [ map { ref $_ ? $_->moniker : $_ } @{ $hook->{submit} } ];
+        }
+
+        if ( $hook->{args} ) {
+            # We're going to pass complex query mapping structures
+            # as-is to the server, but we need to make sure we're not
+            # trying to pass around Actions, merely their monikers.
+            for my $key ( keys %{ $hook->{args} } ) {
+                next unless ref $hook->{args}{$key} eq "HASH";
+                $hook->{args}{$key}{$_} = $hook->{args}{$key}{$_}->moniker
+                  for grep { ref $hook->{args}{$key}{$_} }
+                  keys %{ $hook->{args}{$key} };
+            }
+        } else {
+            $hook->{args} = {};
+        }
+
+    }
 
     $self->_onclick($arg);
 }
@@ -176,16 +205,19 @@
         next unless $value;
 
         my @fragments;
-        my @actions;
+        my %actions;    # Maps actions => disable?
         my $confirm;
 
-        for my $hook (grep {ref $_ eq "HASH"} (ref $value eq "ARRAY" ? @{$value} : ($value))) {
+        for my $hook (grep {ref $_ eq "HASH"} (@{$value})) {
 
             my %args;
 
             # Submit action
             if ( $hook->{submit} ) {
-                push @actions, @{ $hook->{submit} };
+                my $disable = exists $hook->{disable} ? $hook->{disable} : 1;
+                # Normalize to 1/0 to pass to JS
+                $disable = $disable ? 1 : 0;
+                $actions{$_} = $disable for (@{ $hook->{submit} }); 
             }
 
             $hook->{region} ||= Jifty->web->qualified_region;
@@ -235,20 +267,11 @@
                 $args{region}  = $args{element} =~ /^#region-(\S+)/ ? "$1-".Jifty->web->serial : Jifty->web->serial;
             }
 
-            # Toggle functionality
-            $args{toggle} = 1 if $hook->{toggle};
-
             # Arguments
-            $args{args} = $hook->{args} || {};
+            $args{args} = $hook->{args};
 
-            # We're going to pass complex query mapping structures
-            # as-is to the server, but we need to make sure we're not
-            # trying to pass around Actions, merely their monikers.
-            for my $key (keys %{$args{args}}) {
-                next unless ref $args{args}{$key} eq "HASH";
-                $args{args}{$key}{$_} = $args{args}{$key}{$_}->moniker
-                  for grep {ref $args{args}{$key}{$_}} keys %{$args{args}{$key}};
-            }
+            # Toggle functionality
+            $args{toggle} = 1 if $hook->{toggle};
 
             # Effects
             $args{$_} = $hook->{$_} for grep {exists $hook->{$_}} qw/effect effect_args/;
@@ -257,12 +280,9 @@
         }
 
         my $string = join ";", (grep {not ref $_} (ref $value eq "ARRAY" ? @{$value} : ($value)));
-        if (@fragments or @actions) {
-            # Uniqify action monikers
-            my %uniq;
-            @actions = grep {not $uniq{$_}++} @actions;
+        if (@fragments or %actions) {
 
-            my $update = "update( ". Jifty::JSON::objToJson( {actions => \@actions, fragments => \@fragments }, {singlequote => 1}) .", this );";
+            my $update = "update( ". Jifty::JSON::objToJson( {actions => \%actions, fragments => \@fragments }, {singlequote => 1}) .", this );";
             $string .= $self->javascript_preempt ? "return $update" : "$update; return true;";
         }
         if ($confirm) {


More information about the Jifty-commit mailing list