source: trunk/www.guidonia.net/wp/wp-includes/js/wp-ajax-response.dev.js@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 3.0 KB
Line 
1var wpAjax = jQuery.extend( {
2 unserialize: function( s ) {
3 var r = {}, q, pp, i, p;
4 if ( !s ) { return r; }
5 q = s.split('?'); if ( q[1] ) { s = q[1]; }
6 pp = s.split('&');
7 for ( i in pp ) {
8 if ( jQuery.isFunction(pp.hasOwnProperty) && !pp.hasOwnProperty(i) ) { continue; }
9 p = pp[i].split('=');
10 r[p[0]] = p[1];
11 }
12 return r;
13 },
14 parseAjaxResponse: function( x, r, e ) { // 1 = good, 0 = strange (bad data?), -1 = you lack permission
15 var parsed = {}, re = jQuery('#' + r).html(''), err = '';
16
17 if ( x && typeof x == 'object' && x.getElementsByTagName('wp_ajax') ) {
18 parsed.responses = [];
19 parsed.errors = false;
20 jQuery('response', x).each( function() {
21 var th = jQuery(this), child = jQuery(this.firstChild), response;
22 response = { action: th.attr('action'), what: child.get(0).nodeName, id: child.attr('id'), oldId: child.attr('old_id'), position: child.attr('position') };
23 response.data = jQuery( 'response_data', child ).text();
24 response.supplemental = {};
25 if ( !jQuery( 'supplemental', child ).children().each( function() {
26 response.supplemental[this.nodeName] = jQuery(this).text();
27 } ).size() ) { response.supplemental = false }
28 response.errors = [];
29 if ( !jQuery('wp_error', child).each( function() {
30 var code = jQuery(this).attr('code'), anError, errorData, formField;
31 anError = { code: code, message: this.firstChild.nodeValue, data: false };
32 errorData = jQuery('wp_error_data[code="' + code + '"]', x);
33 if ( errorData ) { anError.data = errorData.get(); }
34 formField = jQuery( 'form-field', errorData ).text();
35 if ( formField ) { code = formField; }
36 if ( e ) { wpAjax.invalidateForm( jQuery('#' + e + ' :input[name="' + code + '"]' ).parents('.form-field:first') ); }
37 err += '<p>' + anError.message + '</p>';
38 response.errors.push( anError );
39 parsed.errors = true;
40 } ).size() ) { response.errors = false; }
41 parsed.responses.push( response );
42 } );
43 if ( err.length ) { re.html( '<div class="error">' + err + '</div>' ); }
44 return parsed;
45 }
46 if ( isNaN(x) ) { return !re.html('<div class="error"><p>' + x + '</p></div>'); }
47 x = parseInt(x,10);
48 if ( -1 == x ) { return !re.html('<div class="error"><p>' + wpAjax.noPerm + '</p></div>'); }
49 else if ( 0 === x ) { return !re.html('<div class="error"><p>' + wpAjax.broken + '</p></div>'); }
50 return true;
51 },
52 invalidateForm: function ( selector ) {
53 return jQuery( selector ).addClass( 'form-invalid' ).change( function() { jQuery(this).removeClass( 'form-invalid' ); } );
54 },
55 validateForm: function( selector ) {
56 selector = jQuery( selector );
57 return !wpAjax.invalidateForm( selector.find('.form-required').filter( function() { return jQuery('input:visible', this).val() == ''; } ) ).size();
58 }
59}, wpAjax || { noPerm: 'You do not have permission to do that.', broken: 'An unidentified error has occurred.' } );
60
61// Basic form validation
62jQuery(document).ready( function($){
63 $('form.validate').submit( function() { return wpAjax.validateForm( $(this) ); } );
64});
Note: See TracBrowser for help on using the repository browser.