[jifty-devel] AJAX canonicalizations

Jesse Vincent jesse at bestpractical.com
Tue May 9 22:50:05 EDT 2006




On Tue, May 09, 2006 at 10:15:27PM -0400, Sean E. Millichamp wrote:
> I decided that AJAX-based display of the result of canonicalizations
> would be nice to have too.  So here they are.
> 
> It borrows the AJAX validation infrastructure.  I looked at doing it
> separately but it seemed to make more sense to me to do them both in one
> call.
> 
> It requires that the action have 'ajax_canonicalizations => 1' in the
> return of the actions arguments.  I wasn't really sure where it should
> be documented though.  Jifty::Action's pod says to look at
> Jifty::Web::Form::Field, but I didn't add any logic or handling to
> Jifty::Web::Form::Field to do this so that didn't seem right.

Hm. I suspect we need a section in Jifty::Action. That's probably the
_right_ place. 

In general: Woot.

In specific: once we've got tests and docs for it, I'll be thrilled.


> Sean
> 
> -- 
>  Sean E. Millichamp, Enertron LLC, 586-757-1200 x102
> 

> Index: share/web/static/js/jifty.js
> ===================================================================
> --- share/web/static/js/jifty.js	(revision 1029)
> +++ share/web/static/js/jifty.js	(working copy)
> @@ -105,22 +105,34 @@
>                      function (request) {
>                          var response  = request.responseXML.documentElement;
>                          for (var action = response.firstChild; action != null; action = action.nextSibling) {
> -                            if ((action.nodeName != 'action') || (action.getAttribute("id") != id))
> -                                continue;
> -                            for (var field = action.firstChild; field != null; field = field.nextSibling) {
> -                                // Possibilities for field.nodeName: it could be #text (whitespace),
> -                                // or 'blank' (the field was blank, don't mess with the error div), or 'ok'
> -                                // (clear the error and warning div!) or 'error' (fill in the error div, clear 
> -				// the warning div!) or 'warning' (fill in the warning div and clear the error div!)
> -                                if (field.nodeName == 'error' || field.nodeName == 'warning') {
> -                                    var err_div = document.getElementById(field.getAttribute("id"));
> -                                    if (err_div != null) {
> -                                        err_div.innerHTML = field.firstChild.data;
> +                            if ((action.nodeName == 'validationaction') && (action.getAttribute("id") == id)) {
> +                                for (var field = action.firstChild; field != null; field = field.nextSibling) {
> +                                    // Possibilities for field.nodeName: it could be #text (whitespace),
> +                                    // or 'blank' (the field was blank, don't mess with the error div), or 'ok'
> +                                    // (clear the error and warning div!) or 'error' (fill in the error div, clear 
> +                                    // the warning div!) or 'warning' (fill in the warning div and clear the error div!)
> +                                    if (field.nodeName == 'error' || field.nodeName == 'warning') {
> +                                        var err_div = document.getElementById(field.getAttribute("id"));
> +                                        if (err_div != null) {
> +                                            err_div.innerHTML = field.firstChild.data;
> +                                        }
> +                                    } else if (field.nodeName == 'ok') {
> +                                        var err_div = document.getElementById(field.getAttribute("id"));
> +                                        if (err_div != null) {
> +                                            err_div.innerHTML = '';
> +                                        }
>                                      }
> -                                } else if (field.nodeName == 'ok') {
> -                                    var err_div = document.getElementById(field.getAttribute("id"));
> -                                    if (err_div != null) {
> -                                        err_div.innerHTML = '';
> +                                }
> +                            } else if ((action.nodeName == 'canonicalizeaction') && (action.getAttribute("id") == id)) {
> +                                for (var field = action.firstChild; field != null; field = field.nextSibling) {
> +                                    // Possibilities for field.nodeName: it could be 'ignored', 'blank' or 'update'
> +                                    if (field.nodeName == 'update') {
> +                                        var field_name = field.getAttribute("name");
> +                                        for (var form_number in document.forms) {
> +                                            if (document.forms[form_number].elements[field_name] == null)
> +                                                continue;
> +                                            document.forms[form_number].elements[field_name].value = field.firstChild.data;
> +                                        }
>                                      }
>                                  }
>                              }
> Index: share/web/templates/__jifty/validator.xml
> ===================================================================
> --- share/web/templates/__jifty/validator.xml	(revision 1029)
> +++ share/web/templates/__jifty/validator.xml	(working copy)
> @@ -8,7 +8,7 @@
>  $writer->startTag("validation");
>  for my $ra ( Jifty->web->request->actions ) {
>      my $action = Jifty->web->new_action_from_request($ra);
> -    $writer->startTag( "action", id => $action->register_name );
> +    $writer->startTag( "validationaction", id => $action->register_name );
>      for my $arg ( $action->argument_names ) {
>          if ( not $action->arguments->{$arg}->{ajax_validates} ) {
>              $writer->emptyTag( "ignored", id => $action->error_div_id($arg) );
> @@ -37,6 +37,22 @@
>          }
>      }
>      $writer->endTag();
> +    $writer->startTag( "canonicalizeaction", id => $action->register_name );
> +    for my $arg ( $action->argument_names ) {
> +        if ( not $action->arguments->{$arg}->{ajax_canonicalizes} ) {
> +            $writer->emptyTag( "ignored", name => $action->form_field_name($arg) );
> +        } elsif ( not defined $action->argument_value($arg)
> +            or length $action->argument_value($arg) == 0 ) {
> +            $writer->emptyTag( "blank", name => $action->form_field_name($arg) );
> +        } else {
> +            $writer->dataElement(
> +                "update",
> +                $action->argument_value($arg),
> +                name => $action->form_field_name($arg)
> +            );
> +        }
> +    }
> +    $writer->endTag();
>  }
>  $writer->endTag();
>  $m->out($output);




> _______________________________________________
> jifty-devel mailing list
> jifty-devel at lists.jifty.org
> http://lists.jifty.org/cgi-bin/mailman/listinfo/jifty-devel


-- 


More information about the jifty-devel mailing list