wp/wp-includes/js/wp-ajax-response.js
author ymh <ymh.work@gmail.com>
Mon, 14 Oct 2019 17:39:30 +0200
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
permissions -rw-r--r--
resynchronize code repo with production
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
var wpAjax = jQuery.extend( {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
	unserialize: function( s ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
		var r = {}, q, pp, i, p;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
		if ( !s ) { return r; }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
		q = s.split('?'); if ( q[1] ) { s = q[1]; }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
		pp = s.split('&');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
		for ( i in pp ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
			if ( jQuery.isFunction(pp.hasOwnProperty) && !pp.hasOwnProperty(i) ) { continue; }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
			p = pp[i].split('=');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
			r[p[0]] = p[1];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
		return r;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
	parseAjaxResponse: function( x, r, e ) { // 1 = good, 0 = strange (bad data?), -1 = you lack permission
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    15
		var parsed = {}, re = jQuery('#' + r).empty(), err = '';
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
		if ( x && typeof x == 'object' && x.getElementsByTagName('wp_ajax') ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
			parsed.responses = [];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
			parsed.errors = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
			jQuery('response', x).each( function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
				var th = jQuery(this), child = jQuery(this.firstChild), response;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
				response = { action: th.attr('action'), what: child.get(0).nodeName, id: child.attr('id'), oldId: child.attr('old_id'), position: child.attr('position') };
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
				response.data = jQuery( 'response_data', child ).text();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
				response.supplemental = {};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
				if ( !jQuery( 'supplemental', child ).children().each( function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
					response.supplemental[this.nodeName] = jQuery(this).text();
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    27
				} ).length ) { response.supplemental = false; }
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
				response.errors = [];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
				if ( !jQuery('wp_error', child).each( function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
					var code = jQuery(this).attr('code'), anError, errorData, formField;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
					anError = { code: code, message: this.firstChild.nodeValue, data: false };
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
					errorData = jQuery('wp_error_data[code="' + code + '"]', x);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
					if ( errorData ) { anError.data = errorData.get(); }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
					formField = jQuery( 'form-field', errorData ).text();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
					if ( formField ) { code = formField; }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
					if ( e ) { wpAjax.invalidateForm( jQuery('#' + e + ' :input[name="' + code + '"]' ).parents('.form-field:first') ); }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
					err += '<p>' + anError.message + '</p>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
					response.errors.push( anError );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
					parsed.errors = true;
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    40
				} ).length ) { response.errors = false; }
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
				parsed.responses.push( response );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
			} );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
			if ( err.length ) { re.html( '<div class="error">' + err + '</div>' ); }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
			return parsed;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
		if ( isNaN(x) ) { return !re.html('<div class="error"><p>' + x + '</p></div>'); }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
		x = parseInt(x,10);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
		if ( -1 == x ) { return !re.html('<div class="error"><p>' + wpAjax.noPerm + '</p></div>'); }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
		else if ( 0 === x ) { return !re.html('<div class="error"><p>' + wpAjax.broken  + '</p></div>'); }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
	invalidateForm: function ( selector ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    53
		return jQuery( selector ).addClass( 'form-invalid' ).find('input').one( 'change wp-check-valid-field', function() { jQuery(this).closest('.form-invalid').removeClass( 'form-invalid' ); } );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
	validateForm: function( selector ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
		selector = jQuery( selector );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    57
		return !wpAjax.invalidateForm( selector.find('.form-required').filter( function() { return jQuery('input:visible', this).val() === ''; } ) ).length;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
	}
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    59
}, wpAjax || { noPerm: 'Sorry, you are not allowed to do that.', broken: 'Something went wrong.' } );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
// Basic form validation
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
jQuery(document).ready( function($){
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
	$('form.validate').submit( function() { return wpAjax.validateForm( $(this) ); } );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
});