wp-ajax-response.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 ( jQuery.isFunction(pp.hasOwnProperty) && !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 = '';
  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. response.supplemental[this.nodeName] = jQuery(this).text();
  30. } ).length ) { response.supplemental = false; }
  31. response.errors = [];
  32. if ( !jQuery('wp_error', child).each( function() {
  33. var code = jQuery(this).attr('code'), anError, errorData, formField;
  34. anError = { code: code, message: this.firstChild.nodeValue, data: false };
  35. errorData = jQuery('wp_error_data[code="' + code + '"]', x);
  36. if ( errorData ) { anError.data = errorData.get(); }
  37. formField = jQuery( 'form-field', errorData ).text();
  38. if ( formField ) { code = formField; }
  39. if ( e ) { wpAjax.invalidateForm( jQuery('#' + e + ' :input[name="' + code + '"]' ).parents('.form-field:first') ); }
  40. err += '<p>' + anError.message + '</p>';
  41. response.errors.push( anError );
  42. parsed.errors = true;
  43. } ).length ) { response.errors = false; }
  44. parsed.responses.push( response );
  45. } );
  46. if ( err.length ) { re.html( '<div class="error">' + err + '</div>' ); }
  47. return parsed;
  48. }
  49. if ( isNaN(x) ) { return !re.html('<div class="error"><p>' + x + '</p></div>'); }
  50. x = parseInt(x,10);
  51. if ( -1 == x ) { return !re.html('<div class="error"><p>' + wpAjax.noPerm + '</p></div>'); }
  52. else if ( 0 === x ) { return !re.html('<div class="error"><p>' + wpAjax.broken + '</p></div>'); }
  53. return true;
  54. },
  55. invalidateForm: function ( selector ) {
  56. return jQuery( selector ).addClass( 'form-invalid' ).find('input').one( 'change wp-check-valid-field', function() { jQuery(this).closest('.form-invalid').removeClass( 'form-invalid' ); } );
  57. },
  58. validateForm: function( selector ) {
  59. selector = jQuery( selector );
  60. return !wpAjax.invalidateForm( selector.find('.form-required').filter( function() { return jQuery('input:visible', this).val() === ''; } ) ).length;
  61. }
  62. }, wpAjax || { noPerm: 'Sorry, you are not allowed to do that.', broken: 'Something went wrong.' } );
  63. // Basic form validation
  64. jQuery(document).ready( function($){
  65. $('form.validate').submit( function() { return wpAjax.validateForm( $(this) ); } );
  66. });