Guest User

Untitled

a guest
May 26th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. /**
  2. * when cf7 ajax-loader
  3. * @param {[type]} form [description]
  4. * @return {[type]} [description]
  5. */
  6. wpcf7.submit = function( form ) {
  7. if ( typeof window.FormData !== 'function' ) {
  8. return;
  9. }
  10.  
  11. var $form = $( form );
  12.  
  13. $( '.button.wpcf7-submit', $form ).addClass( 'is-loading' );
  14.  
  15. $( '[placeholder].placeheld', $form ).each( function( i, n ) {
  16. $( n ).val( '' );
  17. } );
  18.  
  19. wpcf7.clearResponse( $form );
  20.  
  21. var formData = new FormData( $form.get( 0 ) );
  22.  
  23. var detail = {
  24. id: $form.closest( 'div.wpcf7' ).attr( 'id' ),
  25. status: 'init',
  26. inputs: [],
  27. formData: formData
  28. };
  29.  
  30. $.each( $form.serializeArray(), function( i, field ) {
  31. if ( '_wpcf7' == field.name ) {
  32. detail.contactFormId = field.value;
  33. } else if ( '_wpcf7_version' == field.name ) {
  34. detail.pluginVersion = field.value;
  35. } else if ( '_wpcf7_locale' == field.name ) {
  36. detail.contactFormLocale = field.value;
  37. } else if ( '_wpcf7_unit_tag' == field.name ) {
  38. detail.unitTag = field.value;
  39. } else if ( '_wpcf7_container_post' == field.name ) {
  40. detail.containerPostId = field.value;
  41. } else if ( field.name.match( /^_wpcf7_\w+_free_text_/ ) ) {
  42. var owner = field.name.replace( /^_wpcf7_\w+_free_text_/, '' );
  43. detail.inputs.push( {
  44. name: owner + '-free-text',
  45. value: field.value
  46. } );
  47. } else if ( field.name.match( /^_/ ) ) {
  48. // do nothing
  49. } else {
  50. detail.inputs.push( field );
  51. }
  52. } );
  53.  
  54. wpcf7.triggerEvent( $form.closest( 'div.wpcf7' ), 'beforesubmit', detail );
  55.  
  56. var ajaxSuccess = function( data, status, xhr, $form ) {
  57. detail.id = $( data.into ).attr( 'id' );
  58. detail.status = data.status;
  59. detail.apiResponse = data;
  60.  
  61. var $message = $( '.wpcf7-response-output', $form );
  62.  
  63. switch ( data.status ) {
  64. case 'validation_failed':
  65. $.each( data.invalidFields, function( i, n ) {
  66. $( n.into, $form ).each( function() {
  67. wpcf7.notValidTip( this, n.message );
  68. $( '.wpcf7-form-control', this ).addClass( 'wpcf7-not-valid' );
  69. $( '[aria-invalid]', this ).attr( 'aria-invalid', 'true' );
  70. } );
  71. } );
  72.  
  73. $message.addClass( 'wpcf7-validation-errors' );
  74. $form.addClass( 'invalid' );
  75.  
  76. wpcf7.triggerEvent( data.into, 'invalid', detail );
  77. break;
  78. case 'acceptance_missing':
  79. $message.addClass( 'wpcf7-acceptance-missing' );
  80. $form.addClass( 'unaccepted' );
  81.  
  82. wpcf7.triggerEvent( data.into, 'unaccepted', detail );
  83. break;
  84. case 'spam':
  85. $message.addClass( 'wpcf7-spam-blocked' );
  86. $form.addClass( 'spam' );
  87.  
  88. $( '[name="g-recaptcha-response"]', $form ).each( function() {
  89. if ( '' === $( this ).val() ) {
  90. var $recaptcha = $( this ).closest( '.wpcf7-form-control-wrap' );
  91. wpcf7.notValidTip( $recaptcha, wpcf7.recaptcha.messages.empty );
  92. }
  93. } );
  94.  
  95. wpcf7.triggerEvent( data.into, 'spam', detail );
  96. break;
  97. case 'aborted':
  98. $message.addClass( 'wpcf7-aborted' );
  99. $form.addClass( 'aborted' );
  100.  
  101. wpcf7.triggerEvent( data.into, 'aborted', detail );
  102. break;
  103. case 'mail_sent':
  104. $message.addClass( 'wpcf7-mail-sent-ok' );
  105. $form.addClass( 'sent' );
  106.  
  107. wpcf7.triggerEvent( data.into, 'mailsent', detail );
  108. break;
  109. case 'mail_failed':
  110. $message.addClass( 'wpcf7-mail-sent-ng' );
  111. $form.addClass( 'failed' );
  112.  
  113. wpcf7.triggerEvent( data.into, 'mailfailed', detail );
  114. break;
  115. default:
  116. var customStatusClass = 'custom-'
  117. + data.status.replace( /[^0-9a-z]+/i, '-' );
  118. $message.addClass( 'wpcf7-' + customStatusClass );
  119. $form.addClass( customStatusClass );
  120. }
  121.  
  122. wpcf7.refill( $form, data );
  123.  
  124. wpcf7.triggerEvent( data.into, 'submit', detail );
  125.  
  126. if ( 'mail_sent' == data.status ) {
  127. $form.each( function() {
  128. this.reset();
  129. } );
  130. }
  131.  
  132. $form.find( '[placeholder].placeheld' ).each( function( i, n ) {
  133. $( n ).val( $( n ).attr( 'placeholder' ) );
  134. } );
  135.  
  136. $message.html( '' ).append( data.message ).slideDown( 'fast' );
  137. $message.attr( 'role', 'alert' );
  138.  
  139. $( '.screen-reader-response', $form.closest( '.wpcf7' ) ).each( function() {
  140. var $response = $( this );
  141. $response.html( '' ).attr( 'role', '' ).append( data.message );
  142.  
  143. if ( data.invalidFields ) {
  144. var $invalids = $( '<ul></ul>' );
  145.  
  146. $.each( data.invalidFields, function( i, n ) {
  147. if ( n.idref ) {
  148. var $li = $( '<li></li>' ).append( $( '<a></a>' ).attr( 'href', '#' + n.idref ).append( n.message ) );
  149. } else {
  150. var $li = $( '<li></li>' ).append( n.message );
  151. }
  152.  
  153. $invalids.append( $li );
  154. } );
  155.  
  156. $response.append( $invalids );
  157. }
  158.  
  159. $response.attr( 'role', 'alert' ).focus();
  160. } );
  161. };
  162.  
  163. $.ajax( {
  164. type: 'POST',
  165. url: wpcf7.apiSettings.getRoute(
  166. '/contact-forms/' + wpcf7.getId( $form ) + '/feedback' ),
  167. data: formData,
  168. dataType: 'json',
  169. processData: false,
  170. contentType: false
  171. } ).done( function( data, status, xhr ) {
  172. ajaxSuccess( data, status, xhr, $form );
  173. $( '.button.wpcf7-submit', $form ).removeClass( 'is-loading' );
  174. } ).fail( function( xhr, status, error ) {
  175. var $e = $( '<div class="ajax-error"></div>' ).text( error.message );
  176. $form.after( $e );
  177. } );
  178. };
  179.  
  180. });
Add Comment
Please, Sign In to add comment