PageSourceSearch

https://smenlightenmentmedia.com/wp-content/plugins/opt-in-hound/a…/js/script-front-opt-in-hound.js?ver=1.4.3

js smenlightenmentmedia.com collected 2026-10-04 18:39:06 UTC 15,107 bytes, 726 lines download raw bytes

1jQuery( function($) {
2
3	var $opt_ins = $('.opt-in-hound-opt-in-wrapper');
4
5	/**
6	 * Handle opt-in dimensions on ready and window resize
7	 *
8	 */
9	$opt_ins.each( function() {
10		resize_opt_in( $(this) );
11	});
12
13	$(window).resize( function() {
14
15		$opt_ins.each( function() {
16
17			if( $(this).data('type') == 'pop_up' )
18				position_opt_in( $(this) );
19
20			resize_opt_in( $(this) );
21
22		});
23
24	});
25
26
27	/**
28	 * Add a trigger for when the opt-ins are being displayed
29	 *
30	 */
31	$opt_ins.each( function() {
32
33		if( $(this).css('visibility') != 'hidden' )
34			on_show_opt_in_trigger( $(this) );
35
36	});
37
38
39	/**
40	 * Add a data-display false to opt-ins if they are not in the active
41	 * date range
42	 *
43	 */
44	$opt_ins.each( function() {
45
46		// Get current date
47		var current_date = new Date();
48
49		if( typeof $(this).data('schedules') == 'undefined' )
50			return false;
51
52		// Set schedules
53		var schedules = $(this).data('schedules');
54
55		// Set display to true at the beginning
56		var display = true;
57		
58
59		/**
60		 * Handle date range schedules
61		 *
62		 */
63		for( var index in schedules ) {
64
65			if( schedules[index].type != 'date_range' )
66				continue;
67
68			// Make display false if we find our first date range
69			display = false;
70
71			schedule = schedules[index];
72
73			if( schedule.start_date ) {
74
75				var start_date = new Date( schedule.start_date );
76
77				display = ( current_date < start_date ? false : true );
78
79			} else
80				display = true;
81
82			if( display && schedule.end_date ) {
83
84				var end_date = new Date( schedule.end_date );
85
86				display = ( current_date > end_date ? false : true );
87
88			}
89
90			if( display )
91				break;
92
93		}
94
95		if( true == display ) {
96
97			/**
98			 * Handle recurring weekly
99			 *
100			 */
101			for( var index in schedules ) {
102
103				if( schedules[index].type != 'recurring_weekly' )
104					continue;
105
106				schedule = schedules[index];
107
108				if( ! schedule.weekdays )
109					continue;
110
111				var current_day = current_date.getDay();
112
113				// Sunday is saved as 7 in the db
114				if( current_day == 0 )
115					current_day = 7;
116
117				display = ( schedule.weekdays.indexOf( current_day ) == -1 ? false : true );
118
119				if( display )
120					break;
121
122			}
123
124			/**
125			 * Handle recurring monthly
126			 *
127			 */
128			if( false == display ) {
129
130				for( var index in schedules ) {
131
132					if( schedules[index].type != 'recurring_monthly' )
133						continue;
134
135					schedule = schedules[index];
136
137					if( ! schedule.monthdays )
138						continue;
139
140					var current_day = current_date.getDate();
141
142					display = ( schedule.monthdays.indexOf( current_day ) == -1 ? false : true );
143
144					if( display )
145						break;
146
147				}
148
149			}
150
151		}
152
153		if( false == display )
154			$(this).data( 'display', false );
155
156		else
157			$(this).css( 'display', 'block' );
158
159	});
160
161
162	/**
163	 * Calculate scroll percentage
164	 *
165	 */
166	var has_scrolled = false;
167	var scroll_top 	 = 0;
168
169	$(window).scroll( function() {
170
171		// User has scrolled
172		has_scrolled = true;
173			
174		// Calculate scrollTop in percentages on user scroll
175		scroll_top = parseInt( $(window).scrollTop() / ($(document).innerHeight() - $(window).height()) * 100 );
176
177	});
178
179	$(window).load( function() {
180
181		has_scrolled = false;
182
183		// Calculate scrollTop in percentages on page load
184		scroll_top = parseInt( $(window).scrollTop() / ($(document).innerHeight() - $(window).height()) * 100 );
185
186	});
187
188
189	/**
190	 * On window load show opt-ins that should be displayed on page load
191	 *
192	 */
193	$(window).load( function() {
194
195		$opt_ins.each( function() {
196
197			$this = $(this);
198
199			if( $this.data('type') == 'pop_up' || $this.data('type') == 'fly_in' || $this.data('type') == 'floating_bar' ) {
200
201				$wrapper = $this.parent();
202
203				if( typeof $wrapper.data('page-load') != 'undefined' ) {
204					show_opt_in( $this );			
205				}
206			}
207			
208		});
209
210	});
211
212
213	/**
214	 * On document ready handle opt-ins
215	 *
216	 */
217	$opt_ins.each( function() {
218
219		$this = $(this);
220		
221		// Handle pop-ups
222		if( $this.data('type') == 'pop_up' || $this.data('type') == 'fly_in' || $this.data('type') == 'floating_bar' ) {
223
224			$wrapper = $this.parent();
225
226			// Remove if the device doesn't match
227			if( typeof $wrapper.data('show-on-device') != 'undefined' ) {
228
229				if( is_mobile() && $wrapper.data('show-on-device') == 'desktop' ) {
230					$wrapper.parents('.opt-in-hound-opt-in-pop-up-overlay').remove();
231					$wrapper.remove();
232				}
233
234				if( ! is_mobile() && $wrapper.data('show-on-device') == 'mobile' ) {
235					$wrapper.parents('.opt-in-hound-opt-in-pop-up-overlay').remove();
236					$wrapper.remove();
237				}
238
239			}
240
241
242			// Remove if the cookie is set
243			var session_length = get_opt_in_session_length( $this );
244
245			if( session_length != 0 ) {
246
247				if( get_cookie( $wrapper.attr('id') ) != '' ) {
248
249					$wrapper.parents('.opt-in-hound-opt-in-pop-up-overlay').remove();
250					$wrapper.remove();
251
252				}
253
254			} else
255				set_cookie( $wrapper.attr('id'), '', -1 );
256
257			// Set time on page trigger
258			if( typeof $wrapper.data('time-on-page') != 'undefined' ) {
259
260				setTimeout( function() {
261					show_opt_in( $this );
262				}, parseInt( $wrapper.data('time-on-page') ) * 1000 );
263
264			}
265
266			// Listen for on click events
267			if( typeof $wrapper.data('element-on-click') != 'undefined' ) {
268
269				$(document).on( 'click', $wrapper.data('element-on-click'), function(e) {
270					e.preventDefault();
271					show_opt_in( $this );
272					$this.removeData('dom-remove');
273				});
274
275			}
276
277			// Show opt-in on user exit
278			if( typeof $wrapper.data( 'user-exit' ) != 'undefined' ) {
279
280				document.documentElement.addEventListener( 'mouseleave', documentMouseLeave );
281
282				function documentMouseLeave(e) {
283					if( e.clientY < 1 )
284						show_opt_in( $this );
285				}
286
287			}
288
289		}
290
291	});
292
293
294	/**
295	 * Set user scroll distance trigger
296	 *
297	 */
298	$(window).scroll( function() {
299			
300		if( has_scrolled == true ) {
301
302			$opt_ins.each( function() {
303
304				$this   = $(this);
305				$pop_up = $this.parent();
306
307				// Trigger for scroll position
308				if( ( typeof $pop_up.data('user-scrolls') != 'undefined' ) && scroll_top >= parseInt( $pop_up.data('user-scrolls') ) )
309					show_opt_in( $this );
310
311			});
312
313		}
314		
315	});
316
317
318	/**
319	 * Close opt-in pop-up on overlay click
320	 *
321	 */
322	$(document).on( 'click', '.opt-in-hound-opt-in-pop-up-overlay.opened', function(e) {
323
324		// Take into account only overlay clicks, not children
325		if( e.target != this )
326			return;
327		
328		e.preventDefault();
329
330		$('.opt-in-hound-opt-in-pop-up.opt-in-hound-close-overlay-click[data-id=' + $(this).data('id') + ']').find('.opt-in-hound-opt-in-pop-up-close').click();
331
332	});
333
334
335	/**
336	 * Close opt-in pop-up on ESC key press
337	 *
338	 */
339	$(document).on( 'keyup', function(e) {
340
341		if( e.keyCode == 27 ) {
342
343			$('.opt-in-hound-opt-in-pop-up.opt-in-hound-close-esc-key.opened').find('.opt-in-hound-opt-in-pop-up-close').click();
344
345		}
346
347	});
348
349
350	/**
351	 * Close opt-in
352	 *
353	 */
354	$(document).on( 'click', '.opt-in-hound-opt-in-close', function(e) {
355
356		e.preventDefault();
357
358		hide_opt_in( $(this).siblings('.opt-in-hound-opt-in-wrapper') );
359
360	});
361
362
363	/**
364	 * Detects if the current device is a mobile device
365	 *
366	 */
367	function is_mobile() {
368
369		if( /Android|webOS|iPhone|iPad|BlackBerry|Windows Phone|Opera Mini|IEMobile|Mobile/i.test( navigator.userAgent ) )
370			return true;
371		else
372			return false;
373
374	}
375
376
377	/**
378	 * Checks the width of the opt-in and makes it narrow if it
379	 * gets under a certain width
380	 *
381	 */
382	function resize_opt_in( $opt_in ) {
383
384		if( $opt_in.outerWidth() < 900 ) {
385			$opt_in.addClass('opt-in-hound-max-width-900');
386		} else {
387			$opt_in.removeClass('opt-in-hound-max-width-900');
388		}
389
390		if( $opt_in.outerWidth() < 700 ) {
391			$opt_in.addClass('opt-in-hound-max-width-700');
392		} else {
393			$opt_in.removeClass('opt-in-hound-max-width-700');
394		}
395
396		if( $opt_in.outerWidth() < 500 ) {
397			$opt_in.addClass('opt-in-hound-narrow');
398		} else {
399			$opt_in.removeClass('opt-in-hound-narrow');
400		}
401
402	}
403
404
405	/*
406	 * Shows the pop-up
407	 *
408	 */
409	function show_opt_in( $opt_in ) {
410
411		if( $opt_in.data('display') == false )
412			return false;
413
414		if( $('.opt-in-hound-opt-in-wrapper').parent().hasClass('opened') )
415			return false;
416
417		if( $opt_in.data('visibility') == 'visible' )
418			return false;
419
420		// Set cookies for pop-up and fly-in
421		if( $opt_in.data('type') == 'pop_up' || $opt_in.data('type') == 'fly_in' || $opt_in.data('type') == 'floating_bar' ) {
422
423			$wrapper = $this.parent();
424
425			var session_length = get_opt_in_session_length( $this );
426
427			if( session_length != 0 )
428				set_cookie( $wrapper.attr('id'), '1', session_length, '/' );
429
430		}
431
432		var data_id = $opt_in.data('id');
433
434		$opt_in.data('visibility', 'visible');
435		$opt_in.data('dom-remove', 1);
436
437		$opt_in.parent().addClass('opened');
438		$opt_in.parent().parent().addClass('opened');
439
440		if( $opt_in.data('type') == 'pop_up' )
441			position_opt_in( $opt_in );
442
443		// Add on show trigger
444		on_show_opt_in_trigger( $opt_in );
445
446	}
447
448
449	/*
450	 * Hides the pop-up and maybe removes it from the DOM
451	 *
452	 */
453	function hide_opt_in( $opt_in ) {
454
455		var data_id = $opt_in.data('id');
456
457		$opt_in_parent 	 	  = $opt_in.parent();
458		$opt_in_parent_parent = $opt_in_parent.parents('[data-id="' + data_id + '"]');
459
460		$opt_in_parent.removeClass('opened');
461		$opt_in_parent_parent.removeClass('opened');
462
463		$opt_in.data('visibility', 'hidden');
464
465		// Remove overflow
466		remove_opt_in_overflow( $opt_in );
467
468		if( typeof $opt_in.data('dom-remove') != 'undefined' ) {
469
470			setTimeout( function() {
471				$opt_in_parent.remove();
472				$opt_in_parent_parent.remove();
473
474				// Re-cache the remaining opt-ins
475				$opt_ins = $('.opt-in-hound-opt-in-wrapper');
476			}, 350 );
477
478		}
479
480	}
481
482
483	/**
484	 * Positions the opt-in in the center of the screen
485	 *
486	 */
487	function position_opt_in( $opt_in ) {
488
489		$pop_up = $opt_in.parent();
490
491		var windowHeight = window.innerHeight;
492		var windowWidth  = window.innerWidth;
493
494		var popUpHeight  = $pop_up.outerHeight();
495		var popUpWidth   = $pop_up.outerWidth();
496
497		$pop_up.css({
498			top  : ( windowHeight - popUpHeight ) / 2,
499			left : ( windowWidth - popUpWidth ) / 2
500		});
501
502		add_opt_in_overflow( $opt_in );
503
504	}
505
506
507	/**
508	 * Checks to see if the opt-in's height is greater than the window
509	 * and adds an overflow-y property so that the opt-in looks awesome
510	 *
511	 */
512	function add_opt_in_overflow( $opt_in ) {
513
514		var data_id = $opt_in.data('id');
515
516		if( $opt_in.data('type') != 'pop_up' )
517			return false;
518
519		$opt_in_parent 	 	  = $opt_in.parent();
520		$opt_in_parent_parent = $opt_in_parent.parents('[data-id="' + data_id + '"]');
521
522		if( $opt_in_parent.outerHeight() > $(window).innerHeight() ) {
523
524			$('html').addClass('opt-in-hound-overflow');
525			$('body').addClass('opt-in-hound-overflow');
526			$opt_in_parent.addClass('opt-in-hound-overflowing');
527			$opt_in_parent_parent.addClass('opt-in-hound-overlay-overflowing');
528
529		} else {
530			remove_opt_in_overflow( $opt_in );
531		}
532
533	}
534
535	/**
536	 * Removes the opt-in overflow
537	 *
538	 */
539	function remove_opt_in_overflow( $opt_in ) {
540
541		var data_id = $opt_in.data('id');
542
543		if( $opt_in.data('type') != 'pop_up' )
544			return false;
545
546		$opt_in_parent 	 	  = $opt_in.parent();
547		$opt_in_parent_parent = $opt_in_parent.parents('[data-id="' + data_id + '"]');
548
549		$('html').removeClass('opt-in-hound-overflow');
550		$opt_in_parent.removeClass('opt-in-hound-overflowing');
551		$opt_in_parent_parent.removeClass('opt-in-hound-overlay-overflowing');
552
553	}
554
555
556	/**
557	 * If the opt-in has an on_show_trigger than do an ajax call for
558	 * the trigger
559	 *
560	 */
561	function on_show_opt_in_trigger( $opt_in ) {
562
563		if( $opt_in.data('on_show_trigger') != 1 )
564			return false;
565
566		var data = {
567			'action'	  : 'oih_opt_in_show_trigger',
568			'opt_in_id'   : parseInt( $opt_in.data('id') ),
569			'opt_in_type' : $opt_in.data('type'),
570			'oih_token'	  : $opt_in.find('[data-name=oih_token]').val(),
571			'page_data'	  : JSON.stringify( oih_current_page_data )
572		}
573
574		$.post( oih_ajax_url, data, function( response ) {});
575
576	}
577
578
579	/**
580	 * Returns the session lengths ( in days ) set by the admin
581	 * for a given opt-in
582	 *
583	 */
584	function get_opt_in_session_length( $opt_in ) {
585
586		$wrapper = $opt_in.parent();
587
588		var session_length = 0;
589
590		if( typeof $wrapper.data( 'session-length' ) != 'undefined' )
591			session_length = parseInt( $wrapper.data( 'session-length' ) );
592
593		return session_length;
594
595	}
596
597
598	/**
599	 * Set a cookie
600	 *
601	 */
602	function set_cookie( cname, cvalue, exdays, path ) {
603
604	    var d = new Date();
605	    d.setTime(d.getTime() + (exdays*24*60*60*1000));
606	    var expires = "expires="+d.toUTCString();
607
608	    if( path )
609	    	path = "path=" + path;
610
611	    document.cookie = cname + "=" + cvalue + "; " + expires + "; " + path;
612
613	}
614
615
616	/**
617	 * Get a cookie
618	 *
619	 */
620	function get_cookie( cname ) {
621
622	    var name = cname + "=";
623	    var ca = document.cookie.split(';');
624
625	    for(var i=0; i<ca.length; i++) {
626	        var c = ca[i];
627	        while (c.charAt(0)==' ') c = c.substring(1);
628	        if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
629	    }
630
631	    return "";
632
633	}
634
635
636	/**
637	 * Handle form submission
638	 *
639	 */
640	$(document).on( 'click', '.opt-in-hound-opt-in-form button', function(e) {
641
642		e.preventDefault();
643
644		var $form 		 = $(this).closest('form');
645		var $form_fields = $form.find( '.opt-in-hound-opt-in-form-input input' );
646		var $opt_in 	 = $form.closest('.opt-in-hound-opt-in-wrapper');
647
648		var data = {};
649
650		// Add the action we want to make
651		data['action'] 	  = 'oih_opt_in_form_submission';
652		data['opt_in_id'] = parseInt( $opt_in.data('id') );
653
654		// Add data for each input
655		$form_fields.each( function() {
656			data[ $(this).data('name').replace( 'oih_', '' ) ] = $(this).val();
657		});
658
659		// Add token data
660		data['oih_token'] = $form.find('[data-name=oih_token]').val();
661
662		// Add current page data
663		data['page_data'] = JSON.stringify( oih_current_page_data );
664
665		// Show loading spinner instead of the button label
666		$form.find('button span').fadeOut( 200, function() {
667			$form.find('.oih-loading-spinner').fadeIn( 300 );
668		});
669
670		// Disable the fields
671		$form_fields.attr( 'disabled', true );
672
673		$.post( oih_ajax_url, data, function( response ) {
674
675			var response 	= JSON.parse( response );
676			var enable_form = true;
677
678			// Show the thank you message if everythin is okay
679			if( typeof response.success != 'undefined' ) {
680
681				// Display success message
682				if( response.success_type == 'message' ) {
683
684					$opt_in.find('.opt-in-hound-opt-in-content-wrapper').fadeOut( 400 );
685					$opt_in.find('.opt-in-hound-opt-in-form-wrapper').fadeOut( 400, function() {
686						$opt_in.find('.opt-in-hound-opt-in-success-message-wrapper').fadeIn( 400 );
687					});
688
689				}
690
691				// Redirect to a page
692				if( response.success_type == 'redirect_to_page' ) {
693
694					if( typeof response.success_redirect_page != 'undefined' )
695						window.location = response.success_redirect_page;
696
697					enable_form = false;
698
699				}
700
701			}
702
703			// Add the errors message if any
704			if( typeof response.error != 'undefined' ) {
705
706				$form.siblings('.opt-in-hound-opt-in-form-errors').addClass('opt-in-hound-active').html( response.error );
707
708			}
709
710			if( enable_form ) {
711
712				// Show button label instead of the loading spinner
713				$form.find('.oih-loading-spinner').fadeOut( 300, function() {
714					$form.find('button span').fadeIn( 200 );
715				});
716
717				// Enable the fields
718				$form_fields.attr( 'disabled', false );
719
720			}
721
722		});
723
724	});
725
726});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.