wp/wp-includes/js/wp-ajax-response.js
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
    22 				response = { action: th.attr('action'), what: child.get(0).nodeName, id: child.attr('id'), oldId: child.attr('old_id'), position: child.attr('position') };
    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();
    23 				response.data = jQuery( 'response_data', child ).text();
    24 				response.supplemental = {};
    24 				response.supplemental = {};
    25 				if ( !jQuery( 'supplemental', child ).children().each( function() {
    25 				if ( !jQuery( 'supplemental', child ).children().each( function() {
    26 					response.supplemental[this.nodeName] = jQuery(this).text();
    26 					response.supplemental[this.nodeName] = jQuery(this).text();
    27 				} ).size() ) { response.supplemental = false; }
    27 				} ).length ) { response.supplemental = false; }
    28 				response.errors = [];
    28 				response.errors = [];
    29 				if ( !jQuery('wp_error', child).each( function() {
    29 				if ( !jQuery('wp_error', child).each( function() {
    30 					var code = jQuery(this).attr('code'), anError, errorData, formField;
    30 					var code = jQuery(this).attr('code'), anError, errorData, formField;
    31 					anError = { code: code, message: this.firstChild.nodeValue, data: false };
    31 					anError = { code: code, message: this.firstChild.nodeValue, data: false };
    32 					errorData = jQuery('wp_error_data[code="' + code + '"]', x);
    32 					errorData = jQuery('wp_error_data[code="' + code + '"]', x);
    35 					if ( formField ) { code = formField; }
    35 					if ( formField ) { code = formField; }
    36 					if ( e ) { wpAjax.invalidateForm( jQuery('#' + e + ' :input[name="' + code + '"]' ).parents('.form-field:first') ); }
    36 					if ( e ) { wpAjax.invalidateForm( jQuery('#' + e + ' :input[name="' + code + '"]' ).parents('.form-field:first') ); }
    37 					err += '<p>' + anError.message + '</p>';
    37 					err += '<p>' + anError.message + '</p>';
    38 					response.errors.push( anError );
    38 					response.errors.push( anError );
    39 					parsed.errors = true;
    39 					parsed.errors = true;
    40 				} ).size() ) { response.errors = false; }
    40 				} ).length ) { response.errors = false; }
    41 				parsed.responses.push( response );
    41 				parsed.responses.push( response );
    42 			} );
    42 			} );
    43 			if ( err.length ) { re.html( '<div class="error">' + err + '</div>' ); }
    43 			if ( err.length ) { re.html( '<div class="error">' + err + '</div>' ); }
    44 			return parsed;
    44 			return parsed;
    45 		}
    45 		}
    48 		if ( -1 == x ) { return !re.html('<div class="error"><p>' + wpAjax.noPerm + '</p></div>'); }
    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>'); }
    49 		else if ( 0 === x ) { return !re.html('<div class="error"><p>' + wpAjax.broken  + '</p></div>'); }
    50 		return true;
    50 		return true;
    51 	},
    51 	},
    52 	invalidateForm: function ( selector ) {
    52 	invalidateForm: function ( selector ) {
    53 		return jQuery( selector ).addClass( 'form-invalid' ).find('input:visible').change( function() { jQuery(this).closest('.form-invalid').removeClass( 'form-invalid' ); } );
    53 		return jQuery( selector ).addClass( 'form-invalid' ).find('input').one( 'change wp-check-valid-field', function() { jQuery(this).closest('.form-invalid').removeClass( 'form-invalid' ); } );
    54 	},
    54 	},
    55 	validateForm: function( selector ) {
    55 	validateForm: function( selector ) {
    56 		selector = jQuery( selector );
    56 		selector = jQuery( selector );
    57 		return !wpAjax.invalidateForm( selector.find('.form-required').filter( function() { return jQuery('input:visible', this).val() === ''; } ) ).size();
    57 		return !wpAjax.invalidateForm( selector.find('.form-required').filter( function() { return jQuery('input:visible', this).val() === ''; } ) ).length;
    58 	}
    58 	}
    59 }, wpAjax || { noPerm: 'You do not have permission to do that.', broken: 'An unidentified error has occurred.' } );
    59 }, wpAjax || { noPerm: 'Sorry, you are not allowed to do that.', broken: 'Something went wrong.' } );
    60 
    60 
    61 // Basic form validation
    61 // Basic form validation
    62 jQuery(document).ready( function($){
    62 jQuery(document).ready( function($){
    63 	$('form.validate').submit( function() { return wpAjax.validateForm( $(this) ); } );
    63 	$('form.validate').submit( function() { return wpAjax.validateForm( $(this) ); } );
    64 });
    64 });