wp/wp-includes/js/wp-ajax-response.js
author ymh <ymh.work@gmail.com>
Tue, 27 Sep 2022 16:37:53 +0200
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
permissions -rw-r--r--
upgrade wordpress to 6.0.2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
     1
/**
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
     2
 * @output wp-includes/js/wp-ajax-response.js
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
     3
 */
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
     4
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
     5
 /* global wpAjax */
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
     6
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
     7
window.wpAjax = jQuery.extend( {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
	unserialize: function( s ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
		var r = {}, q, pp, i, p;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
		if ( !s ) { return r; }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
		q = s.split('?'); if ( q[1] ) { s = q[1]; }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
		pp = s.split('&');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
		for ( i in pp ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    14
			if ( typeof pp.hasOwnProperty === 'function' && !pp.hasOwnProperty(i) ) { continue; }
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
			p = pp[i].split('=');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
			r[p[0]] = p[1];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
		return r;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
	},
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    20
	parseAjaxResponse: function( x, r, e ) { // 1 = good, 0 = strange (bad data?), -1 = you lack permission.
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    21
		var parsed = {}, re = jQuery('#' + r).empty(), err = '', noticeMessage = '';
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    23
		if ( x && typeof x === 'object' && x.getElementsByTagName('wp_ajax') ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
			parsed.responses = [];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
			parsed.errors = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
			jQuery('response', x).each( function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
				var th = jQuery(this), child = jQuery(this.firstChild), response;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
				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
    29
				response.data = jQuery( 'response_data', child ).text();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
				response.supplemental = {};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
				if ( !jQuery( 'supplemental', child ).children().each( function() {
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    32
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    33
					if ( this.nodeName === 'notice' ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    34
						noticeMessage += jQuery(this).text();
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    35
						return;
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    36
					}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    37
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
					response.supplemental[this.nodeName] = jQuery(this).text();
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    39
				} ).length ) { response.supplemental = false; }
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
				response.errors = [];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
				if ( !jQuery('wp_error', child).each( function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
					var code = jQuery(this).attr('code'), anError, errorData, formField;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
					anError = { code: code, message: this.firstChild.nodeValue, data: false };
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
					errorData = jQuery('wp_error_data[code="' + code + '"]', x);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
					if ( errorData ) { anError.data = errorData.get(); }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
					formField = jQuery( 'form-field', errorData ).text();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
					if ( formField ) { code = formField; }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
					if ( e ) { wpAjax.invalidateForm( jQuery('#' + e + ' :input[name="' + code + '"]' ).parents('.form-field:first') ); }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
					err += '<p>' + anError.message + '</p>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
					response.errors.push( anError );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
					parsed.errors = true;
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    52
				} ).length ) { response.errors = false; }
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
				parsed.responses.push( response );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
			} );
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    55
			if ( err.length ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    56
				re.html( '<div class="error">' + err + '</div>' );
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    57
				wp.a11y.speak( err );
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    58
			} else if ( noticeMessage.length ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    59
				re.html( '<div class="updated notice is-dismissible"><p>' + noticeMessage + '</p></div>');
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    60
				jQuery(document).trigger( 'wp-updates-notice-added' );
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    61
				wp.a11y.speak( noticeMessage );
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    62
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
			return parsed;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
		if ( isNaN(x) ) { return !re.html('<div class="error"><p>' + x + '</p></div>'); }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
		x = parseInt(x,10);
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    67
		if ( -1 === x ) { return !re.html('<div class="error"><p>' + wpAjax.noPerm + '</p></div>'); }
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
		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
    69
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
	invalidateForm: function ( selector ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    72
		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
    73
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
	validateForm: function( selector ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
		selector = jQuery( selector );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    76
		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
    77
	}
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    78
}, 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
    79
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    80
// Basic form validation.
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    81
jQuery( function($){
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    82
	$('form.validate').on( 'submit', function() { return wpAjax.validateForm( $(this) ); } );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
});