fl-subscribe-form.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. ( function( $ ) {
  2. FLBuilderSubscribeForm = function( settings )
  3. {
  4. this.settings = settings;
  5. this.nodeClass = '.fl-node-' + settings.id;
  6. this.form = $( this.nodeClass + ' .fl-subscribe-form' );
  7. this.button = this.form.find( 'a.vamtam-button' );
  8. this._init();
  9. };
  10. FLBuilderSubscribeForm.prototype = {
  11. settings : {},
  12. nodeClass : '',
  13. form : null,
  14. button : null,
  15. _init: function()
  16. {
  17. this.button.on( 'click', $.proxy( this._submitForm, this ) );
  18. },
  19. _submitForm: function( e )
  20. {
  21. var postId = this.form.closest( '.fl-builder-content' ).data( 'post-id' ),
  22. templateId = this.form.data( 'template-id' ),
  23. templateNodeId = this.form.data( 'template-node-id' ),
  24. nodeId = this.form.closest( '.fl-module' ).data( 'node' ),
  25. buttonText = this.button.find( '.vamtam-button-text' ).text(),
  26. waitText = this.button.closest( '.fl-form-button' ).data( 'wait-text' ),
  27. name = this.form.find( 'input[name=fl-subscribe-form-name]' ),
  28. email = this.form.find( 'input[name=fl-subscribe-form-email]' ),
  29. re = /\S+@\S+\.\S+/,
  30. valid = true;
  31. e.preventDefault();
  32. if ( this.button.hasClass( 'fl-form-button-disabled' ) ) {
  33. return; // Already submitting
  34. }
  35. if ( name.length > 0 && name.val() == '' ) {
  36. name.addClass( 'fl-form-error' );
  37. name.siblings( '.fl-form-error-message' ).show();
  38. valid = false;
  39. }
  40. if ( '' == email.val() || ! re.test( email.val() ) ) {
  41. email.addClass( 'fl-form-error' );
  42. email.siblings( '.fl-form-error-message' ).show();
  43. valid = false;
  44. }
  45. if ( valid ) {
  46. this.form.find( '> .fl-form-error-message' ).hide();
  47. this.button.find( '.vamtam-button-text' ).text( waitText );
  48. this.button.data( 'original-text', buttonText );
  49. this.button.addClass( 'fl-form-button-disabled' );
  50. $.post( FLBuilderLayoutConfig.paths.wpAjaxUrl, {
  51. action : 'fl_builder_subscribe_form_submit',
  52. name : name.val(),
  53. email : email.val(),
  54. post_id : postId,
  55. template_id : templateId,
  56. template_node_id : templateNodeId,
  57. node_id : nodeId
  58. }, $.proxy( this._submitFormComplete, this ) );
  59. }
  60. },
  61. _submitFormComplete: function( response )
  62. {
  63. var data = JSON.parse( response ),
  64. buttonText = this.button.data( 'original-text' );
  65. if ( data.error ) {
  66. if ( data.error ) {
  67. this.form.find( '> .fl-form-error-message' ).text( data.error );
  68. }
  69. this.form.find( '> .fl-form-error-message' ).show();
  70. this.button.removeClass( 'fl-form-button-disabled' );
  71. this.button.find( '.vamtam-button-text' ).text( buttonText );
  72. } else if ( 'message' == data.action ) {
  73. this.form.find( '> *' ).hide();
  74. this.form.append( '<div class="fl-form-success-message">' + data.message + '</div>' );
  75. } else if ( 'redirect' == data.action ) {
  76. window.location.href = data.url;
  77. }
  78. }
  79. }
  80. })( jQuery );