You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

65 lines
3.1 KiB

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