angelovcom.net

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

wp-ajax-response.js (3198B)


      1 /**
      2  * @output wp-includes/js/wp-ajax-response.js
      3  */
      4 
      5  /* global wpAjax */
      6 
      7 window.wpAjax = jQuery.extend( {
      8 	unserialize: function( s ) {
      9 		var r = {}, q, pp, i, p;
     10 		if ( !s ) { return r; }
     11 		q = s.split('?'); if ( q[1] ) { s = q[1]; }
     12 		pp = s.split('&');
     13 		for ( i in pp ) {
     14 			if ( typeof pp.hasOwnProperty === 'function' && !pp.hasOwnProperty(i) ) { continue; }
     15 			p = pp[i].split('=');
     16 			r[p[0]] = p[1];
     17 		}
     18 		return r;
     19 	},
     20 	parseAjaxResponse: function( x, r, e ) { // 1 = good, 0 = strange (bad data?), -1 = you lack permission.
     21 		var parsed = {}, re = jQuery('#' + r).empty(), err = '';
     22 
     23 		if ( x && typeof x === 'object' && x.getElementsByTagName('wp_ajax') ) {
     24 			parsed.responses = [];
     25 			parsed.errors = false;
     26 			jQuery('response', x).each( function() {
     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') };
     29 				response.data = jQuery( 'response_data', child ).text();
     30 				response.supplemental = {};
     31 				if ( !jQuery( 'supplemental', child ).children().each( function() {
     32 					response.supplemental[this.nodeName] = jQuery(this).text();
     33 				} ).length ) { response.supplemental = false; }
     34 				response.errors = [];
     35 				if ( !jQuery('wp_error', child).each( function() {
     36 					var code = jQuery(this).attr('code'), anError, errorData, formField;
     37 					anError = { code: code, message: this.firstChild.nodeValue, data: false };
     38 					errorData = jQuery('wp_error_data[code="' + code + '"]', x);
     39 					if ( errorData ) { anError.data = errorData.get(); }
     40 					formField = jQuery( 'form-field', errorData ).text();
     41 					if ( formField ) { code = formField; }
     42 					if ( e ) { wpAjax.invalidateForm( jQuery('#' + e + ' :input[name="' + code + '"]' ).parents('.form-field:first') ); }
     43 					err += '<p>' + anError.message + '</p>';
     44 					response.errors.push( anError );
     45 					parsed.errors = true;
     46 				} ).length ) { response.errors = false; }
     47 				parsed.responses.push( response );
     48 			} );
     49 			if ( err.length ) { re.html( '<div class="error">' + err + '</div>' ); }
     50 			return parsed;
     51 		}
     52 		if ( isNaN(x) ) { return !re.html('<div class="error"><p>' + x + '</p></div>'); }
     53 		x = parseInt(x,10);
     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>'); }
     56 		return true;
     57 	},
     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' ); } );
     60 	},
     61 	validateForm: function( selector ) {
     62 		selector = jQuery( selector );
     63 		return !wpAjax.invalidateForm( selector.find('.form-required').filter( function() { return jQuery('input:visible', this).val() === ''; } ) ).length;
     64 	}
     65 }, wpAjax || { noPerm: 'Sorry, you are not allowed to do that.', broken: 'Something went wrong.' } );
     66 
     67 // Basic form validation.
     68 jQuery( function($){
     69 	$('form.validate').on( 'submit', function() { return wpAjax.validateForm( $(this) ); } );
     70 });