[Jifty-commit] r5248 - in jifty/trunk: lib/Jifty/View/Declare lib/Jifty/Web/Form

Jifty commits jifty-commit at lists.jifty.org
Mon Mar 31 17:23:46 EDT 2008


Author: alexmv
Date: Mon Mar 31 17:23:45 2008
New Revision: 5248

Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/View/Declare/Helpers.pm
   jifty/trunk/lib/Jifty/Web/Form/Element.pm

Log:
 r28929 at kohr-ah:  chmrr | 2008-03-31 17:23:25 -0400
  * Allow onclick, etc, handlers on arbitrary elements


Modified: jifty/trunk/lib/Jifty/View/Declare/Helpers.pm
==============================================================================
--- jifty/trunk/lib/Jifty/View/Declare/Helpers.pm	(original)
+++ jifty/trunk/lib/Jifty/View/Declare/Helpers.pm	Mon Mar 31 17:23:45 2008
@@ -9,7 +9,7 @@
     qw(hyperlink tangent redirect new_action
     form_submit form_return form_next_page page content
     wrapper request get set render_param current_user
-    render_action render_region),
+    render_action render_region js_handlers),
     @Template::Declare::Tags::EXPORT,
     @Template::Declare::Tags::TagSubs,  # Backward compatibility only
     @Template::Declare::Tags::TAG_SUB_LIST,
@@ -367,7 +367,26 @@
     }
 }
 
+=head2 js_handlers
 
+Allows you to put javascript handlers, a la
+L<Jifty::Web::Form::Element>, onto arbitrary HTML elements:
+
+  div {
+      js_handlers {
+          onclick => { path => "/some/region/path" }
+      }
+  }
+
+=cut
+
+sub js_handlers(&;@) {
+    my $code = shift;
+    my $element = Jifty::Web::Form::Element->new({$code->()});
+    my %js = $element->javascript_attrs;
+    Template::Declare::Tags::append_attr($_ => $js{$_}) for keys %js;
+    return @_;
+}
 
 
 1;

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	Mon Mar 31 17:23:45 2008
@@ -357,13 +357,28 @@
 
 =head2 javascript
 
-Returns the javascript necessary to make the events happen.
+Returns the javascript necessary to make the events happen, as a
+string of HTML attributes.
 
 =cut
+
 sub javascript {
     my $self = shift;
+    my %response = $self->javascript_attrs;
+    return join "", map {qq| $_="| . Jifty->web->escape($response{$_}).qq|"|} sort keys %response;
+}
+
+=head2 javascript_attrs
+
+Returns the javascript necessary to make the events happen, as a
+hash of attribute-name and value.
+
+=cut
+
+sub javascript_attrs {
+    my $self = shift;
 
-    my $response = "";
+    my %response;
 
   HANDLER:
     for my $trigger ( $self->handlers ) {
@@ -471,7 +486,7 @@
         my $string = join ";", (grep {not ref $_} (ref $value eq "ARRAY" ? @{$value} : ($value)));
         if ( @fragments or ( !$actions || %$actions ) ) {
 
-            my $update = Jifty->web->escape(
+            my $update =
                 "Jifty.update( "
                     . Jifty::JSON::objToJson(
                     {   actions      => $actions,
@@ -480,8 +495,7 @@
                         continuation => $self->continuation
                     },
                     { singlequote => 1 }
-                    ) . ", this );"
-            );
+                    ) . ", this );";
             $string
                 .= 'if(event.ctrlKey||event.metaKey||event.altKey||event.shiftKey) return true; '
                 if ( $trigger eq 'onclick' );
@@ -490,14 +504,14 @@
                 : "$update; return true;";
         }
         if ($confirm) {
-            $string = Jifty->web->escape("if(!confirm(" . Jifty::JSON::objToJson($confirm, {singlequote => 1}) . ")) { Event.stop(event); return false }") . $string;
+            $string = "if(!confirm(" . Jifty::JSON::objToJson($confirm, {singlequote => 1}) . ")) { Event.stop(event); return false }" . $string;
         }
         if ($beforeclick) {
-           $string = Jifty->web->escape($beforeclick) . $string;
+           $string = $beforeclick . $string;
         }
-        $response .= qq| $trigger="$string"|;
+        $response{$trigger} = $string;
     }
-    return $response;
+    return %response;
 }
 
 =head2 javascript_preempt


More information about the Jifty-commit mailing list