Passed
Push — master ( 886f4f...0bd761 )
by Fernando
02:46
created

lsx.wc_footer_bar_toggle_handler   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
/**
2
 * LSX Scripts
3
 *
4
 * @package    lsx
5
 * @subpackage scripts
6
 */
7
8
var lsx = Object.create( null );
9
10
;( function( $, window, document, undefined ) {
0 ignored issues
show
Unused Code introduced by
The parameter undefined is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
11
12
	'use strict';
13
14
	var $document    = $( document ),
15
		$window      = $( window ),
16
		windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight,
0 ignored issues
show
Unused Code introduced by
The variable windowHeight seems to be never used. Consider removing it.
Loading history...
17
		windowWidth  = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
18
19
	/**
20
	 * Adds browser class to html tag.
21
	 *
22
	 * @package    lsx
23
	 * @subpackage scripts
24
	 */
25
	lsx.add_class_browser_to_html = function() {
26
		if ( 'undefined' !== typeof platform ) {
0 ignored issues
show
Bug introduced by
The variable platform seems to be never declared. If this is a global, consider adding a /** global: platform */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
27
			var platform_name = platform.name.toLowerCase(),
28
				platform_version = platform.version.toLowerCase().replace(/\..*$/g, '');
29
30
			$( 'html' ).addClass( platform_name ).addClass( platform_version );
31
		}
32
	};
33
34
	/**
35
	 * Test if the sidebar exists (if exists, add a class to the body).
36
	 *
37
	 * @package    lsx
38
	 * @subpackage scripts
39
	 */
40
	lsx.add_class_sidebar_to_body = function() {
41
		if ( $( '#secondary' ).length > 0 ) {
42
			$( 'body' ).addClass( 'has-sidebar' );
43
		}
44
	};
45
46
	/**
47
	 * Add Bootstrap class to WordPress tables.
48
	 *
49
	 * @package    lsx
50
	 * @subpackage scripts
51
	 */
52
	lsx.add_class_bootstrap_to_table = function() {
53
		var tables = $( 'table#wp-calendar' );
54
55
		if ( tables.length > 0 ) {
56
			tables.addClass( 'table' );
57
		}
58
	};
59
60
	/**
61
	 * Add a class to identify when the mobile nav is open
62
	 *
63
	 * @package    lsx
64
	 * @subpackage scripts
65
	 */
66
67
	lsx.navbar_toggle_handler = function () {
68
		$( '.navbar-toggle' ).parent().on( 'click', function () {
69
			var $parent = $( this );
70
			$parent.toggleClass( 'open' );
71
		});
72
	};
73
74
	/**
75
	 * Fix Bootstrap menus (touchstart).
76
	 *
77
	 * @package    lsx
78
	 * @subpackage scripts
79
	 */
80
	// lsx.fix_bootstrap_menus_touchstart = function() {
81
	// 	$( '.dropdown-menu' ).on( 'touchstart.dropdown.data-api', function( e ) {
82
	// 		e.stopPropagation();
83
	// 	} );
84
	// };
85
86
	/**
87
	 * Fix Bootstrap menus (dropdown).
88
	 *
89
	 * @package    lsx
90
	 * @subpackage scripts
91
	 */
92
	lsx.fix_bootstrap_menus_dropdown = function() {
93
		$( '.navbar-nav .dropdown, #top-menu .dropdown' ).on( 'show.bs.dropdown', function() {
94
			if ( windowWidth < 1200 ) {
95
				$( this ).siblings( '.open' ).removeClass( 'open' ).find( 'a.dropdown-toggle' ).attr( 'data-toggle', 'dropdown' );
96
				$( this ).find( 'a.dropdown-toggle' ).removeAttr( 'data-toggle' );
97
			}
98
		} );
99
100
		if ( windowWidth > 1199 ) {
101
			$( '.navbar-nav li.dropdown a, #top-menu li.dropdown a' ).each( function() {
102
				$( this ).removeClass( 'dropdown-toggle' );
103
				$( this ).removeAttr( 'data-toggle' );
104
			} );
105
		}
106
107
		$window.resize( function() {
108
			if ( windowWidth > 1199 ) {
109
				$( '.navbar-nav li.dropdown a, #top-menu li.dropdown a' ).each( function() {
110
					$( this ).removeClass( 'dropdown-toggle' );
111
					$( this ).removeAttr( 'data-toggle' );
112
				} );
113
			} else {
114
				$( '.navbar-nav li.dropdown a, #top-menu li.dropdown a' ).each( function() {
115
					$( this ).addClass( 'dropdown-toggle' );
116
					$( this ).attr( 'data-toggle', 'dropdown' );
117
				} );
118
			}
119
		} );
120
	};
121
122
	/**
123
	 * Fix Bootstrap menus (dropdown inside dropdown - click).
124
	 *
125
	 * @package    lsx
126
	 * @subpackage scripts
127
	 */
128
	lsx.fix_bootstrap_menus_dropdown_click = function() {
129
		if ( windowWidth < 1200 ) {
130
			$( '.navbar-nav .dropdown .dropdown > a, #top-menu .dropdown .dropdown > a' ).on( 'click', function( e ) {
131
				if ( ! $( this ).parent().hasClass( 'open' ) ) {
132
					$( this ).parent().addClass( 'open' );
133
					$( this ).next( '.dropdown-menu' ).dropdown( 'toggle' );
134
					e.stopPropagation();
135
					e.preventDefault();
136
				}
137
			} );
138
139
			$( '.navbar-nav .dropdown .dropdown .dropdown-menu a, #top-menu .dropdown .dropdown > a' ).on( 'click', function( e ) {
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
140
				document.location.href = this.href;
141
			} );
142
		}
143
	};
144
145
	/**
146
	 * Fix LazyLoad on Envira
147
	 *
148
	 * @package    lsx
149
	 * @subpackage scripts
150
	 */
151
	lsx.fix_lazyload_envira_gallery = function() {
152
		if ( $( '.lazyload, .lazyloaded' ).length > 0 ) {
153
			if ( typeof envira_isotopes == 'object' ) {
0 ignored issues
show
Bug introduced by
The variable envira_isotopes seems to be never declared. If this is a global, consider adding a /** global: envira_isotopes */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
154
				$window.scroll( function() {
155
					$( '.envira-gallery-wrap' ).each( function() {
156
						var id = $( this ).attr( 'id' );
157
						id = id.replace( 'envira-gallery-wrap-', '' );
158
159
						if ( typeof envira_isotopes[ id ] == 'object' ) {
0 ignored issues
show
Bug introduced by
The variable envira_isotopes seems to be never declared. If this is a global, consider adding a /** global: envira_isotopes */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
160
							envira_isotopes[ id ].enviratope( 'layout' );
161
						}
162
					} );
163
				} );
164
			}
165
		}
166
	};
167
168
	/**
169
	 * Fixed main menu.
170
	 *
171
	 * @package    lsx
172
	 * @subpackage scripts
173
	 */
174
	lsx.set_main_menu_as_fixed = function() {
175
		if ( windowWidth > 1199 ) {
176
			if ( $( 'body' ).hasClass( 'top-menu-fixed' ) ) {
177
				$( 'header.navbar' ).scrollToFixed( {
178
					marginTop: function() {
179
						var wpadminbar = $( '#wpadminbar' );
180
181
						if ( wpadminbar.length > 0 ) {
182
							return wpadminbar.outerHeight();
183
						}
184
185
						return 0;
186
					},
187
188
					minWidth: 768,
189
190
					preFixed: function() {
191
						$( this ).addClass( 'scrolled' );
192
					},
193
194
					preUnfixed: function() {
195
						$( this ).removeClass( 'scrolled' );
196
					}
197
				} );
198
			}
199
		}
200
	};
201
202
	/**
203
	 * Banner effect (parallax).
204
	 *
205
	 * @package    lsx
206
	 * @subpackage scripts
207
	 */
208
	lsx.set_banner_effect_parallax = function() {
209
		var $banner,
210
			$bannerImage,
211
			bannerEndPosition,
212
			base = -40,
213
214
			bannerParallax = function() {
215
				if ( $window.scrollTop() <= bannerEndPosition ) {
216
					var scrolled  = $window.scrollTop() / bannerEndPosition * 100,
217
						top       = base + scrolled,
218
						bottom    = base - scrolled;
219
220
					$bannerImage.css({
221
						'top': top + 'px',
222
						'bottom': bottom + 'px'
223
					});
224
				}
225
			};
226
227
		if ( windowWidth > 1199 ) {
228
			$banner = $( '.page-banner:not(.gmap-banner)' );
229
230
			if ( $banner.length > 0 ) {
231
				bannerEndPosition = $banner.height() + $banner.offset().top;
232
				$bannerImage = $banner.children( '.page-banner-image' );
233
234
				if ( $bannerImage.length > 0 ) {
235
					bannerParallax();
236
237
					$window.scroll( function() {
238
						bannerParallax();
239
					} );
240
				}
241
			}
242
		}
243
	};
244
245
	/**
246
	 * Search form effect (on mobile).
247
	 *
248
	 * @package    lsx
249
	 * @subpackage scripts
250
	 */
251
	lsx.set_search_form_effect_mobile = function() {
252
		$document.on( 'click', 'header.navbar #searchform button.search-submit', function( e ) {
253
			if ( windowWidth < 1200 ) {
254
				e.preventDefault();
255
				var form = $( this ).closest( 'form' );
256
257
				if ( form.hasClass( 'hover' ) ) {
258
					form.submit();
259
				} else {
260
					form.addClass( 'hover' );
261
					form.find( '.search-field' ).focus();
262
				}
263
			}
264
		} );
265
266
		$document.on( 'blur', 'header.navbar #searchform .search-field', function( e ) {
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
267
			if ( windowWidth < 1200 ) {
268
				var form = $( this ).closest( 'form' );
269
				form.removeClass( 'hover' );
270
			}
271
		} );
272
	};
273
274
	/**
275
	 * Search form effect (on mobile).
276
	 *
277
	 * @package    lsx
278
	 * @subpackage scripts
279
	 */
280
	lsx.search_form_prevent_empty_submissions = function() {
281
		$document.on( 'submit', '#searchform', function( e ) {
282
			if ( '' === $( this ).find('input[name="s"]').val() ) {
283
				e.preventDefault();
284
			}
285
		} );
286
287
		$document.on( 'blur', 'header.navbar #searchform .search-field', function( e ) {
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
288
			if ( windowWidth < 1200 ) {
289
				var form = $( this ).closest( 'form' );
290
				form.removeClass( 'hover' );
291
			}
292
		} );
293
	};
294
295
	/**
296
	 * Slider Lightbox.
297
	 *
298
	 * @package    lsx
299
	 * @subpackage scripts
300
	 */
301
	lsx.build_slider_lightbox = function() {
302
		$( 'body:not(.single-tour-operator) .gallery' ).slickLightbox( {
303
			caption: function( element, info ) {
0 ignored issues
show
Unused Code introduced by
The parameter info is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
304
				return $( element ).find( 'img' ).attr( 'alt' );
305
			}
306
		} );
307
	};
308
309
	/**
310
	 * Init WooCommerce slider.
311
	 *
312
	 * @package	lsx
313
	 * @subpackage scripts
314
	 */
315
	lsx.init_wc_slider = function () {
316
		var $wcSlider = $( '.lsx-woocommerce-slider' );
317
318
		$wcSlider.each( function( index, el ) {
0 ignored issues
show
Unused Code introduced by
The parameter el is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter index is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
319
			var $self = $( this ),
320
				_slidesToShow = 4,
321
				_slidesToScroll = 4,
322
				_slidesToShow_992 = 3,
323
				_slidesToScroll_992 = 3,
324
				_slidesToShow_768 = 1,
325
				_slidesToScroll_768 = 1;
326
327
			if ( $self.find( '.lsx-woocommerce-review-slot' ).length > 0 ) {
328
				_slidesToShow = 2;
329
				_slidesToScroll = 2;
330
				_slidesToShow_992 = 2;
331
				_slidesToScroll_992 = 2;
332
			}
333
334
			$self.on( 'init', function( event, slick ) {
335
				if ( slick.options.arrows && slick.slideCount > slick.options.slidesToShow ) {
336
					$self.addClass( 'slick-has-arrows' );
337
				}
338
			});
339
340
			$self.on( 'setPosition', function( event, slick ) {
341
				if ( ! slick.options.arrows ) {
342
					$self.removeClass('slick-has-arrows');
343
				} else if ( slick.slideCount > slick.options.slidesToShow ) {
344
					$self.addClass('slick-has-arrows');
345
				}
346
			});
347
348
			$self.slick( {
349
				draggable: false,
350
				infinite: true,
351
				swipe: false,
352
				cssEase: 'ease-out',
353
				dots: true,
354
				slidesToShow: _slidesToShow,
355
				slidesToScroll: _slidesToScroll,
356
				responsive: [{
357
					breakpoint: 992,
358
					settings: {
359
						slidesToShow: _slidesToShow_992,
360
						slidesToScroll: _slidesToScroll_992,
361
						draggable: true,
362
						arrows: false,
363
						swipe: true
364
					}
365
				}, {
366
					breakpoint: 768,
367
					settings: {
368
						slidesToShow: _slidesToShow_768,
369
						slidesToScroll: _slidesToScroll_768,
370
						draggable: true,
371
						arrows: false,
372
						swipe: true
373
					}
374
				}]
375
			} );
376
		} );
377
	}
378
379
	/**
380
	 * Remove gallery IMG width and height.
381
	 *
382
	 * @package	lsx
383
	 * @subpackage scripts
384
	 */
385
	lsx.remove_gallery_img_width_height = function () {
386
		$( '.gallery-size-full img' ).each( function() {
387
			var $self = $( this );
388
389
			$self.removeAttr( 'height' );
390
			$self.removeAttr( 'width' );
391
		} );
392
	};
393
394
	/**
395
	 * Helper function to scroll to an element.
396
	 *
397
	 * @package	lsx
398
	 * @subpackage scripts
399
	 */
400
	lsx.do_scroll = function( _$el ) {
401
		var _href = ( _$el.href ).replace( /^[^#]*(#.+$)/gi, '$1' ),
402
			_$to = $( _href ),
403
			_top = parseInt( _$to.offset().top ),
404
			_extra = -100;
405
406
		$( 'html, body' ).animate( {
407
			scrollTop: ( _top+_extra )
408
		}, 800);
409
	};
410
411
	/**
412
	 * Fix WooCommerce API orders style/HTML.
413
	 * Fix WooCommerce checkboxes style/HTML.
414
	 *
415
	 * @package	lsx
416
	 * @subpackage scripts
417
	 */
418
	lsx.fix_wc_elements = function( _$el ) {
0 ignored issues
show
Unused Code introduced by
The parameter _$el is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
419
		$( '.woocommerce-MyAccount-content .api-manager-changelog, .woocommerce-MyAccount-content .api-manager-download' ).each( function() {
420
			var $this = $( this );
421
422
			$this.children( 'br:first-child' ).remove();
423
			$this.children( 'hr:first-child' ).remove();
424
425
			$this.children( 'hr:last-child' ).remove();
426
			$this.children( 'br:last-child' ).remove();
427
		} );
428
429
		$( '.woocommerce-form__label-for-checkbox.checkbox' ).removeClass( 'checkbox' );
430
431
		$( document.body ).on( 'updated_checkout', function() {
432
			$( '.woocommerce-form__label-for-checkbox.checkbox' ).removeClass( 'checkbox' );
433
		} );
434
	};
435
436
	/**
437
	 * Fix Caldera Forms modal title.
438
	 *
439
	 * @package	lsx
440
	 * @subpackage scripts
441
	 */
442
	lsx.fix_caldera_form_modal_title = function() {
443
		$( '[data-remodal-id]' ).each( function() {
444
			var $form = $( this ),
445
				$button = $( '[data-remodal-target="' + $form.attr( 'id' ) + '"]' ),
446
				title = $button.text();
447
448
			$form.find( '[role="field"]' ).first().before( '<div><h4>' + title + '</h4></div>' );
449
		} );
450
	};
451
452
	/**
453
	 * Open/close WC footer bar search.
454
	 *
455
	 * @package    lsx
456
	 * @subpackage scripts
457
	 */
458
459
	lsx.wc_footer_bar_toggle_handler = function () {
460
		$( '.lsx-wc-footer-bar-link-toogle' ).on( 'click', function ( event ) {
461
			event.preventDefault();
462
			$( '.lsx-wc-footer-bar-form' ).slideToggle();
463
			$( '.lsx-wc-footer-bar' ).toggleClass( 'lsx-wc-footer-bar-search-on' );
464
		});
465
	};
466
467
	/**
468
	 * On window resize.
469
	 *
470
	 * @package    lsx
471
	 * @subpackage scripts
472
	 */
473
	$window.resize( function() {
474
475
		windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
0 ignored issues
show
Unused Code introduced by
The variable windowHeight seems to be never used. Consider removing it.
Loading history...
476
		windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
477
478
	} );
479
480
	/**
481
	 * On document ready.
482
	 *
483
	 * @package    lsx
484
	 * @subpackage scripts
485
	 */
486
	$document.ready( function() {
487
488
		lsx.navbar_toggle_handler();
489
490
		// lsx.fix_bootstrap_menus_touchstart();
491
492
		lsx.add_class_browser_to_html();
493
		lsx.add_class_sidebar_to_body();
494
		lsx.add_class_bootstrap_to_table();
495
496
		lsx.set_main_menu_as_fixed();
497
498
		lsx.search_form_prevent_empty_submissions();
499
		lsx.remove_gallery_img_width_height();
500
501
		lsx.init_wc_slider();
502
		lsx.fix_wc_elements();
503
		lsx.fix_caldera_form_modal_title();
504
		lsx.wc_footer_bar_toggle_handler();
505
506
	} );
507
508
	/**
509
	 * On window load.
510
	 *
511
	 * @package    lsx
512
	 * @subpackage scripts
513
	 */
514
	$window.load( function() {
515
516
		lsx.fix_bootstrap_menus_dropdown();
517
		lsx.fix_bootstrap_menus_dropdown_click();
518
		lsx.fix_lazyload_envira_gallery();
519
520
		lsx.set_search_form_effect_mobile();
521
522
		lsx.build_slider_lightbox();
523
524
		lsx.set_banner_effect_parallax();
525
526
		/* LAST CODE TO EXECUTE */
527
		$( 'body.preloader-content-enable' ).addClass( 'html-loaded' );
528
529
	} );
530
531
} )( jQuery, window, document );
532