| 1 |
|
| 2 |
jQuery( document ).ready(function(){ |
| 3 |
|
| 4 |
|
| 5 |
jQuery( document ).on( 'submit', '.fca_eoi_form', function (e) { |
| 6 |
|
| 7 |
e.preventDefault(); |
| 8 |
|
| 9 |
var $this = jQuery( this ); |
| 10 |
var $email_field = jQuery( '[name=email]', $this ); |
| 11 |
var $name_field = jQuery( '[name=name]:visible', $this ); |
| 12 |
var name = $name_field.val(); |
| 13 |
var email = $email_field.val(); |
| 14 |
var list_id = $this.data( 'fca_eoi_list_id' ); |
| 15 |
var $button = jQuery( '[type=submit]', $this ); |
| 16 |
var $input_div = jQuery( '.fca_eoi_layout_submit_button_wrapper', $this ); |
| 17 |
var $spinner = jQuery( '.fca_eoi_loading_spinner', $this ); |
| 18 |
var $div = jQuery( '.fca_eoi_spiner_div', $this ); |
| 19 |
var button_initial_val; |
| 20 |
var highlight_interval = false; |
| 21 |
var thank_you_mode = $this.data( 'fca_eoi_thank_you_mode' ); |
| 22 |
var thank_you_page = $this.data( 'fca_eoi_thank_you_page' ); |
| 23 |
var has_error = false; |
| 24 |
var form_id = jQuery( '[name=fca_eoi_form_id]', $this ).val(); |
| 25 |
|
| 26 |
|
| 27 |
// Attach tooltips |
| 28 |
jQuery( '[name=email], [name=name]', $this ).each( function() { |
| 29 |
|
| 30 |
var $this = jQuery( this ); |
| 31 |
var tooltipWidth = $this.width() * .8; |
| 32 |
|
| 33 |
$this.tooltipster( { |
| 34 |
contentAsHTML: true, |
| 35 |
fixedWidth: tooltipWidth, |
| 36 |
minWidth: tooltipWidth, |
| 37 |
maxWidth: tooltipWidth, |
| 38 |
trigger: 'none', |
| 39 |
theme: ['tooltipster-borderless', 'tooltipster-optin-cat'] |
| 40 |
} ) |
| 41 |
} ) |
| 42 |
|
| 43 |
// Remove tooltip and tick on focus |
| 44 |
jQuery( '[name=email], [name=name]', jQuery( this ) ).focus( function() { |
| 45 |
var $this = jQuery( this ); |
| 46 |
var $button = jQuery( '[type=submit]', $this.closest( '.fca_eoi_form' ) ); |
| 47 |
var button_initial_val = $button.data( 'fca_eoi_initial_val' ); |
| 48 |
|
| 49 |
// Remove any previous icon |
| 50 |
if( 'undefined' !== typeof( button_initial_val ) ) { |
| 51 |
$button.val( button_initial_val ); |
| 52 |
} |
| 53 |
|
| 54 |
// Hide tooltip |
| 55 |
jQuery( this ).tooltipster( 'hide' ); |
| 56 |
} ); |
| 57 |
|
| 58 |
// Save or save and get initial button value |
| 59 |
if( $button.data( 'fca_eoi_initial_val' ) ) { |
| 60 |
button_initial_val = $button.data( 'fca_eoi_initial_val' ); |
| 61 |
} else { |
| 62 |
button_initial_val = $button.val(); |
| 63 |
$button.data( 'fca_eoi_initial_val', button_initial_val ); |
| 64 |
} |
| 65 |
|
| 66 |
// Remove any previous icon |
| 67 |
$button.val( button_initial_val ); |
| 68 |
|
| 69 |
// Get Error Messages from hidden fields |
| 70 |
fca_eoi.invalid_email = $this.find('.fca_eoi_error_texts_email').val(); |
| 71 |
fca_eoi.field_required = $this.find('.fca_eoi_error_texts_required').val(); |
| 72 |
|
| 73 |
|
| 74 |
// Check email address |
| 75 |
if( ! email || ! is_email( email ) ) { |
| 76 |
|
| 77 |
$email_field.tooltipster( 'content', email ? fca_eoi.invalid_email : fca_eoi.field_required ); |
| 78 |
$email_field.tooltipster( 'show' ); |
| 79 |
$button.val( '✗ ' + button_initial_val ); |
| 80 |
has_error = true; |
| 81 |
} |
| 82 |
|
| 83 |
// Check name |
| 84 |
if( $name_field.length && ! name ) { |
| 85 |
|
| 86 |
$name_field.tooltipster( 'content', fca_eoi.field_required ); |
| 87 |
$name_field.tooltipster( 'show' ); |
| 88 |
$button.val( '✗ ' + button_initial_val ); |
| 89 |
has_error = true; |
| 90 |
} |
| 91 |
|
| 92 |
// Exit if there is any error |
| 93 |
if( has_error ) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
//DISABLE BUTTON CLICK EVENT |
| 98 |
$button.click(function(event) { |
| 99 |
event.preventDefault(); |
| 100 |
}); |
| 101 |
|
| 102 |
if ( $button.closest('.fca_eoi_form').hasClass('fca_eoi_layout_1') ) { |
| 103 |
//without extra border |
| 104 |
scaleSpinnerDiv($button, $div); |
| 105 |
|
| 106 |
} else { |
| 107 |
//width extra border |
| 108 |
scaleSpinnerDiv($input_div, $div); |
| 109 |
} |
| 110 |
scaleSpinnerSize($button, $spinner); |
| 111 |
|
| 112 |
//Match div border radius with button's |
| 113 |
var radius = $button.css("borderRadius"); |
| 114 |
$div.css('borderRadius', radius); |
| 115 |
|
| 116 |
//animate out the button and in the spinner div |
| 117 |
$div.animate({ width: '100%'}); |
| 118 |
|
| 119 |
$spinner.css("display", "block"); |
| 120 |
$button.attr('style', 'cursor: default' ); |
| 121 |
|
| 122 |
jQuery.ajax({ |
| 123 |
url: fca_eoi.ajax_url, |
| 124 |
data: { 'email': email, 'name': name, 'action': 'fca_eoi_subscribe', 'list_id': list_id ,'form_id': form_id, 'nonce': fca_eoi.nonce }, |
| 125 |
type: 'POST', |
| 126 |
datatype: 'text', |
| 127 |
timeout: 15000 |
| 128 |
}).done( function( data ) { |
| 129 |
|
| 130 |
if ( thank_you_mode == 'ajax' ) { |
| 131 |
|
| 132 |
// Stop highlighting and add an icon for success/failure |
| 133 |
if ( data === '✓' ) { |
| 134 |
$button.val( data + ' ' + button_initial_val ); |
| 135 |
} else { |
| 136 |
$button.val( data ); |
| 137 |
} |
| 138 |
|
| 139 |
$spinner.css("display", "none"); |
| 140 |
$div.animate({ width: '0', padding: '0', left: '100%' }); |
| 141 |
|
| 142 |
clearInterval( highlight_interval ); |
| 143 |
if ( thank_you_page && '✓' === data ) { |
| 144 |
tooltipWidth = $button.width(); |
| 145 |
$button.tooltipster( { |
| 146 |
contentAsHTML: true, |
| 147 |
trigger: 'none', |
| 148 |
fixedWidth: tooltipWidth, |
| 149 |
minWidth: tooltipWidth, |
| 150 |
maxWidth: tooltipWidth, |
| 151 |
arrow: false, |
| 152 |
theme: ['tooltipster-borderless', 'tooltipster-optin-cat'] |
| 153 |
} ); |
| 154 |
|
| 155 |
$button.tooltipster( 'content', thank_you_page ); |
| 156 |
$button.tooltipster( 'show' ); |
| 157 |
$email_field.prop('disabled', true); |
| 158 |
$name_field.prop('disabled', true); |
| 159 |
|
| 160 |
jQuery('.tooltipster-content').css("background-color", "#148544"); |
| 161 |
setTimeout(function() { |
| 162 |
$button.tooltipster( 'hide' ); |
| 163 |
}, 5000); |
| 164 |
} |
| 165 |
} else { |
| 166 |
window.location.href = thank_you_page; |
| 167 |
} |
| 168 |
}); |
| 169 |
}); |
| 170 |
}); |
| 171 |
|
| 172 |
//set height, top and (-)margin top to same size as the fca_eoi_layout_submit_button_wrapper height |
| 173 |
|
| 174 |
function scaleSpinnerDiv ($button, $div){ |
| 175 |
|
| 176 |
var h = $button.outerHeight(); |
| 177 |
|
| 178 |
$div.css('height', h + 'px'); |
| 179 |
$div.css('top', h + 'px'); |
| 180 |
$div.css('margin-top', '-' + h + 'px'); |
| 181 |
$div.css('z-index', '1'); |
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
function scaleSpinnerSize( $button, $spinner ) { |
| 186 |
var h = $button.outerHeight(); |
| 187 |
var w = $button.outerWidth(); |
| 188 |
h = h - 8; //account for borders |
| 189 |
|
| 190 |
var newWidth = ( w / 2 ) - ( h / 2); |
| 191 |
|
| 192 |
$spinner.css('margin-left', newWidth + 'px' ); |
| 193 |
$spinner.css('height', h + 'px'); |
| 194 |
$spinner.css('width', h + 'px'); |
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
function is_email( email ) { |
| 199 |
var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/; |
| 200 |
return regex.test(email); |
| 201 |
} |