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