wp/wp-includes/js/wp-ajax-response.js
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    15 			p = pp[i].split('=');
    15 			p = pp[i].split('=');
    16 			r[p[0]] = p[1];
    16 			r[p[0]] = p[1];
    17 		}
    17 		}
    18 		return r;
    18 		return r;
    19 	},
    19 	},
    20 	parseAjaxResponse: function( x, r, e ) { // 1 = good, 0 = strange (bad data?), -1 = you lack permission
    20 	parseAjaxResponse: function( x, r, e ) { // 1 = good, 0 = strange (bad data?), -1 = you lack permission.
    21 		var parsed = {}, re = jQuery('#' + r).empty(), err = '';
    21 		var parsed = {}, re = jQuery('#' + r).empty(), err = '';
    22 
    22 
    23 		if ( x && typeof x == 'object' && x.getElementsByTagName('wp_ajax') ) {
    23 		if ( x && typeof x === 'object' && x.getElementsByTagName('wp_ajax') ) {
    24 			parsed.responses = [];
    24 			parsed.responses = [];
    25 			parsed.errors = false;
    25 			parsed.errors = false;
    26 			jQuery('response', x).each( function() {
    26 			jQuery('response', x).each( function() {
    27 				var th = jQuery(this), child = jQuery(this.firstChild), response;
    27 				var th = jQuery(this), child = jQuery(this.firstChild), response;
    28 				response = { action: th.attr('action'), what: child.get(0).nodeName, id: child.attr('id'), oldId: child.attr('old_id'), position: child.attr('position') };
    28 				response = { action: th.attr('action'), what: child.get(0).nodeName, id: child.attr('id'), oldId: child.attr('old_id'), position: child.attr('position') };
    49 			if ( err.length ) { re.html( '<div class="error">' + err + '</div>' ); }
    49 			if ( err.length ) { re.html( '<div class="error">' + err + '</div>' ); }
    50 			return parsed;
    50 			return parsed;
    51 		}
    51 		}
    52 		if ( isNaN(x) ) { return !re.html('<div class="error"><p>' + x + '</p></div>'); }
    52 		if ( isNaN(x) ) { return !re.html('<div class="error"><p>' + x + '</p></div>'); }
    53 		x = parseInt(x,10);
    53 		x = parseInt(x,10);
    54 		if ( -1 == x ) { return !re.html('<div class="error"><p>' + wpAjax.noPerm + '</p></div>'); }
    54 		if ( -1 === x ) { return !re.html('<div class="error"><p>' + wpAjax.noPerm + '</p></div>'); }
    55 		else if ( 0 === x ) { return !re.html('<div class="error"><p>' + wpAjax.broken  + '</p></div>'); }
    55 		else if ( 0 === x ) { return !re.html('<div class="error"><p>' + wpAjax.broken  + '</p></div>'); }
    56 		return true;
    56 		return true;
    57 	},
    57 	},
    58 	invalidateForm: function ( selector ) {
    58 	invalidateForm: function ( selector ) {
    59 		return jQuery( selector ).addClass( 'form-invalid' ).find('input').one( 'change wp-check-valid-field', function() { jQuery(this).closest('.form-invalid').removeClass( 'form-invalid' ); } );
    59 		return jQuery( selector ).addClass( 'form-invalid' ).find('input').one( 'change wp-check-valid-field', function() { jQuery(this).closest('.form-invalid').removeClass( 'form-invalid' ); } );
    62 		selector = jQuery( selector );
    62 		selector = jQuery( selector );
    63 		return !wpAjax.invalidateForm( selector.find('.form-required').filter( function() { return jQuery('input:visible', this).val() === ''; } ) ).length;
    63 		return !wpAjax.invalidateForm( selector.find('.form-required').filter( function() { return jQuery('input:visible', this).val() === ''; } ) ).length;
    64 	}
    64 	}
    65 }, wpAjax || { noPerm: 'Sorry, you are not allowed to do that.', broken: 'Something went wrong.' } );
    65 }, wpAjax || { noPerm: 'Sorry, you are not allowed to do that.', broken: 'Something went wrong.' } );
    66 
    66 
    67 // Basic form validation
    67 // Basic form validation.
    68 jQuery(document).ready( function($){
    68 jQuery(document).ready( function($){
    69 	$('form.validate').submit( function() { return wpAjax.validateForm( $(this) ); } );
    69 	$('form.validate').submit( function() { return wpAjax.validateForm( $(this) ); } );
    70 });
    70 });