Няма описание

wp-ajax-response.js 3.5KB

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