| 1 |
/* jshint asi: true */ |
| 2 |
jQuery(document).ready( function($) { |
| 3 |
|
| 4 |
//CHECK FOR ANY 'ACTIVE' OPTINS ( BARS AND SLIDE INS ) THAT SHOULD ALREADY BE SHOWING |
| 5 |
function check_for_active_popups() { |
| 6 |
var activeOptins = JSON.parse( get_cookie( 'fca_eoi_active_optins' ) ) |
| 7 |
if ( activeOptins.length > 0 ) { |
| 8 |
for ( var id in activeOptins ) { |
| 9 |
show_banner( activeOptins[id], true ) |
| 10 |
} |
| 11 |
} |
| 12 |
} |
| 13 |
|
| 14 |
if ( typeof(fcaEoiTargetingData) !== 'undefined' ) { |
| 15 |
for ( var id in fcaEoiTargetingData.popups ) { |
| 16 |
|
| 17 |
//TEST COOKIES (SUCCESS, FREQUENCY, PAGECOUNT |
| 18 |
var successOK = test_success( id, fcaEoiTargetingData.popups[id] ) |
| 19 |
var frequencyOK = test_frequency( id, fcaEoiTargetingData.popups[id].show_every ) |
| 20 |
var pageviewsOK = test_pageviews( fcaEoiTargetingData.popups[id] ) |
| 21 |
|
| 22 |
if ( frequencyOK && pageviewsOK && successOK ) { |
| 23 |
var waitTime = parse_time( fcaEoiTargetingData.popups[id] ) |
| 24 |
var scrollPercent = parse_scroll( fcaEoiTargetingData.popups[id] ) |
| 25 |
var exitIntervention = parse_exit_intervention( fcaEoiTargetingData.popups[id] ) |
| 26 |
|
| 27 |
popup_handler( id, waitTime, scrollPercent, exitIntervention ) |
| 28 |
|
| 29 |
} |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
if ( typeof(fcaEoiBannerTargetingData) !== 'undefined' ) { |
| 34 |
for ( var id in fcaEoiBannerTargetingData.banners ) { |
| 35 |
|
| 36 |
//TEST COOKIES (SUCCESS, FREQUENCY, PAGECOUNT |
| 37 |
var successOK = test_success( id, fcaEoiBannerTargetingData.banners[id] ) |
| 38 |
var frequencyOK = test_frequency( id, fcaEoiBannerTargetingData.banners[id].show_every ) |
| 39 |
var pageviewsOK = test_pageviews( fcaEoiBannerTargetingData.banners[id] ) |
| 40 |
|
| 41 |
if ( frequencyOK && pageviewsOK && successOK ) { |
| 42 |
var waitTime = parse_time( fcaEoiBannerTargetingData.banners[id] ) |
| 43 |
var scrollPercent = parse_scroll( fcaEoiBannerTargetingData.banners[id] ) |
| 44 |
var exitIntervention = parse_exit_intervention( fcaEoiBannerTargetingData.banners[id] ) |
| 45 |
|
| 46 |
banner_handler( id, waitTime, scrollPercent, exitIntervention ) |
| 47 |
|
| 48 |
} else { |
| 49 |
$('#fca_eoi_form_' + id)[0].remove() |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
function banner_handler( id, waitTime, scrollPercent, exitIntervention ) { |
| 55 |
window.setTimeout( function(){ |
| 56 |
$( window ).scroll( function() { |
| 57 |
if ( scrollPercent <= scrolled_percent() ) { |
| 58 |
if ( exitIntervention ) { |
| 59 |
$(document).on('mouseleave', function(e) { |
| 60 |
if( e.clientY < 0 ) { |
| 61 |
show_banner( id, false ) |
| 62 |
} |
| 63 |
}) |
| 64 |
} else { |
| 65 |
show_banner( id, false ) |
| 66 |
} |
| 67 |
} |
| 68 |
}).scroll() |
| 69 |
}, waitTime * 1000 ) |
| 70 |
} |
| 71 |
|
| 72 |
function popup_handler( id, waitTime, scrollPercent, exitIntervention ) { |
| 73 |
window.setTimeout( function(){ |
| 74 |
$( window ).scroll( function() { |
| 75 |
if ( scrollPercent <= scrolled_percent() ) { |
| 76 |
if ( exitIntervention ) { |
| 77 |
$(document).on('mouseleave', function(e) { |
| 78 |
if( e.clientY < 0 ) { |
| 79 |
show_lightbox( id ) |
| 80 |
} |
| 81 |
}) |
| 82 |
} else { |
| 83 |
show_lightbox( id ) |
| 84 |
} |
| 85 |
} |
| 86 |
}).scroll() |
| 87 |
}, waitTime * 1000 ) |
| 88 |
} |
| 89 |
|
| 90 |
function test_success( id, popup ) { |
| 91 |
if ( popup.hasOwnProperty( 'success_duration' ) ) { |
| 92 |
if ( get_cookie( 'fca_eoi_success_' + id ) ) { |
| 93 |
return false |
| 94 |
} |
| 95 |
} |
| 96 |
return true |
| 97 |
} |
| 98 |
|
| 99 |
function test_pageviews( popup ) { |
| 100 |
var condition = popup.conditions.filter( function( value ){ |
| 101 |
return value.parameter === 'pageviews' |
| 102 |
}) |
| 103 |
|
| 104 |
if ( condition.length === 0 ){ |
| 105 |
return true |
| 106 |
} else { |
| 107 |
var sessionViews = parseInt( get_cookie( 'fca_eoi_pagecount' ) ) |
| 108 |
|
| 109 |
return sessionViews >= condition[0].value |
| 110 |
} |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
function test_frequency( id, frequency ) { |
| 115 |
|
| 116 |
var lastShown = get_cookie( 'fca_eoi_frequency_' + id ) |
| 117 |
|
| 118 |
if ( !lastShown ) { |
| 119 |
return true |
| 120 |
} |
| 121 |
|
| 122 |
var difference = Math.floor(Date.now() / 1000) - lastShown |
| 123 |
|
| 124 |
switch( frequency ) { |
| 125 |
case 'always': |
| 126 |
return true |
| 127 |
|
| 128 |
case 'session': |
| 129 |
return false |
| 130 |
|
| 131 |
case 'day': |
| 132 |
return difference >= 86400 |
| 133 |
|
| 134 |
case 'once': |
| 135 |
return false |
| 136 |
|
| 137 |
case 'month': |
| 138 |
return difference >= 2592000 |
| 139 |
|
| 140 |
default: |
| 141 |
return true |
| 142 |
} |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
var fca_eoi_popups_shown = [] |
| 147 |
function show_lightbox( id ) { |
| 148 |
if ( fca_eoi_popups_shown.indexOf( id ) === -1 && id > 0 ) { |
| 149 |
fca_eoi_popups_shown.push( id ) |
| 150 |
add_impression( id, fcaEoiTargetingData ) |
| 151 |
var $lightbox = $( '#fca_eoi_lightbox_' + id ) |
| 152 |
$.featherlight( $lightbox, { variant: 'fca_eoi_featherlight', closeOnClick: false, afterOpen: function(){ |
| 153 |
var $instance = this.$instance |
| 154 |
|
| 155 |
//SET TABINDEX TO 0 |
| 156 |
$('.fca_eoi_form_input_element').attr('tabindex', 0) |
| 157 |
$('.fca_eoi_form_button_element').attr('tabindex', 0) |
| 158 |
$('.fca_eoi_gdpr_consent').attr('tabindex', 0) |
| 159 |
|
| 160 |
//BACKWARDS COMPATIBILITY CODE - REMOVE OLD HACK CLOSE BUTTON |
| 161 |
$instance.find('.fca_eoi_layout_popup_close').hide() |
| 162 |
|
| 163 |
var cookieDuration = fcaEoiTargetingData.popups[id].show_every === 'session' ? 0 : 365 |
| 164 |
set_cookie( 'fca_eoi_frequency_' + id, Math.floor(Date.now() / 1000), cookieDuration ) |
| 165 |
if ( $lightbox.find('.fca_eoi_layout_popup').hasClass('animated') ) { |
| 166 |
setTimeout( function(){ |
| 167 |
$instance.find('span.featherlight-close-icon.featherlight-close').show() |
| 168 |
$instance.find('.fca_eoi_form_input_element:visible').first().focus() |
| 169 |
}, 1000) |
| 170 |
} else { |
| 171 |
$instance.find('span.featherlight-close-icon.featherlight-close').show() |
| 172 |
$instance.find('.fca_eoi_form_input_element:visible').first().focus() |
| 173 |
} |
| 174 |
}}) |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
var fca_eoi_banners_shown = [] |
| 179 |
function show_banner( id, disableAnim ) { |
| 180 |
if ( fca_eoi_banners_shown.indexOf( id ) === -1 && typeof fcaEoiBannerTargetingData !== 'undefined' ) { |
| 181 |
|
| 182 |
if ( typeof fcaEoiBannerTargetingData.banners[id] !== 'undefined' ) { |
| 183 |
fca_eoi_banners_shown.push( id ) |
| 184 |
add_impression( id, fcaEoiBannerTargetingData ) |
| 185 |
if ( disableAnim ) { |
| 186 |
$( '#fca_eoi_banner_' + id ).find('form').removeClass('animated') |
| 187 |
} |
| 188 |
$( '#fca_eoi_banner_' + id ).show().load( maybe_push_page( id ) ) |
| 189 |
|
| 190 |
$( '#fca_eoi_banner_' + id ).find('.fca_eoi_form_input_element:visible').first().focus() |
| 191 |
var cookieDuration = fcaEoiBannerTargetingData.banners[id].show_every === 'session' ? 0 : 365 |
| 192 |
set_active_optins ( id ) |
| 193 |
set_cookie( 'fca_eoi_frequency_' + id, Math.floor(Date.now() / 1000), cookieDuration ) |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
function maybe_push_page ( id ) { |
| 199 |
var pushPage = $( '#fca_eoi_banner_' + id ).find('form').data('fca_eoi_push_page') |
| 200 |
|
| 201 |
if ( pushPage === 'down' ) { |
| 202 |
$('body').css('margin-top', $( '#fca_eoi_banner_' + id ).find('form').outerHeight() ) |
| 203 |
} |
| 204 |
|
| 205 |
if ( pushPage === 'up' ) { |
| 206 |
$('body').css('margin-bottom', $( '#fca_eoi_banner_' + id ).find('form').outerHeight() ) |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
//MAKE THE CLOSE BUTTONS FOR BANNERS & SCROLLBOXES REMOVE THE COOKIE FOR IT |
| 211 |
$('.fca_eoi_banner_close_btn, .fca_eoi_scrollbox_close_btn').click(function(){ |
| 212 |
|
| 213 |
var thisFormId = $(this).siblings( '#fca_eoi_form_id' ).val() |
| 214 |
var activeOptins = JSON.parse( get_cookie( 'fca_eoi_active_optins' ) ) |
| 215 |
|
| 216 |
if ( !Array.isArray( activeOptins ) ) { |
| 217 |
activeOptins = [] |
| 218 |
} |
| 219 |
var index = activeOptins.indexOf( thisFormId ) |
| 220 |
|
| 221 |
if ( index !== -1 ) { |
| 222 |
activeOptins.splice( index, 1 ) |
| 223 |
} |
| 224 |
set_cookie ( 'fca_eoi_active_optins', JSON.stringify ( activeOptins ) ) |
| 225 |
|
| 226 |
}) |
| 227 |
|
| 228 |
function set_active_optins( id ) { |
| 229 |
var activeOptins = JSON.parse( get_cookie( 'fca_eoi_active_optins' ) ) |
| 230 |
|
| 231 |
if ( !Array.isArray( activeOptins ) ) { |
| 232 |
activeOptins = [] |
| 233 |
} |
| 234 |
|
| 235 |
var index = activeOptins.indexOf( id ) |
| 236 |
|
| 237 |
if ( index === -1 ) { |
| 238 |
activeOptins.push( id ) |
| 239 |
set_cookie ( 'fca_eoi_active_optins', JSON.stringify ( activeOptins ) ) |
| 240 |
} |
| 241 |
|
| 242 |
} |
| 243 |
|
| 244 |
function add_impression( id, adminData ) { |
| 245 |
$.ajax({ |
| 246 |
url: adminData.ajax_url, |
| 247 |
type: 'POST', |
| 248 |
data: { |
| 249 |
nonce: adminData.nonce, |
| 250 |
form_id: id, |
| 251 |
action: 'fca_eoi_activity' |
| 252 |
} |
| 253 |
}) |
| 254 |
} |
| 255 |
|
| 256 |
function scrolled_percent() { |
| 257 |
var top = $( window ).scrollTop() |
| 258 |
var height = $( document ).height() - $( window ).height() |
| 259 |
if ( height == 0 ) { |
| 260 |
return 100 |
| 261 |
} |
| 262 |
return 100 * ( top / height ) |
| 263 |
} |
| 264 |
|
| 265 |
function parse_exit_intervention( popup ) { |
| 266 |
return popup.conditions.filter( function( value ){ |
| 267 |
return value.parameter === 'exit_intervention' |
| 268 |
}).length === 1 |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
function parse_scroll( popup ) { |
| 273 |
var condition = popup.conditions.filter( function( value ){ |
| 274 |
return value.parameter === 'scrolled_percent' |
| 275 |
}) |
| 276 |
|
| 277 |
if ( condition.length === 0 ){ |
| 278 |
return 0 |
| 279 |
} else { |
| 280 |
return condition[0].value |
| 281 |
} |
| 282 |
|
| 283 |
} |
| 284 |
|
| 285 |
function parse_time( popup ) { |
| 286 |
|
| 287 |
var condition = popup.conditions.filter( function( value ){ |
| 288 |
return value.parameter === 'time_on_page' |
| 289 |
}) |
| 290 |
|
| 291 |
if ( condition.length === 0 ){ |
| 292 |
return 0 |
| 293 |
} else { |
| 294 |
return condition[0].value |
| 295 |
} |
| 296 |
|
| 297 |
} |
| 298 |
|
| 299 |
function set_cookie( name, value, exdays ) { |
| 300 |
if ( exdays === 0 ) { |
| 301 |
document.cookie = name + "=" + value + "" + ";" + "path=/;" |
| 302 |
} else { |
| 303 |
var d = new Date() |
| 304 |
d.setTime( d.getTime() + ( exdays*24*60*60*1000 ) ) |
| 305 |
document.cookie = name + "=" + value + "" + ";" + "path=/;" + "expires=" + d.toUTCString() |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
function get_cookie( name ) { |
| 310 |
var value = "; " + document.cookie |
| 311 |
var parts = value.split( "; " + name + "=" ) |
| 312 |
|
| 313 |
if ( parts.length === 2 ) { |
| 314 |
return parts.pop().split(";").shift() |
| 315 |
} else { |
| 316 |
return false |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
check_for_active_popups() |
| 321 |
|
| 322 |
}) |