| 1 |
/* global jQuery */ |
| 2 |
|
| 3 |
( function ( $ ) { |
| 4 |
'use strict'; |
| 5 |
|
| 6 |
const script = { |
| 7 |
/** |
| 8 |
* Cache. |
| 9 |
* |
| 10 |
* @since 1.0.0 |
| 11 |
* |
| 12 |
* @return {void} |
| 13 |
*/ |
| 14 |
cache() { |
| 15 |
this.vars = {}; |
| 16 |
this.els = {}; |
| 17 |
this.vars.selector = 'woo-additional-terms'; |
| 18 |
this.vars.embed = `${ this.vars.selector }__link[data-action="embed"]`; |
| 19 |
this.vars.content = `${ script.vars.selector }__content`; |
| 20 |
this.vars.openClassName = `${ this.vars.selector }__link--open`; |
| 21 |
this.vars.closeClassName = `${ this.vars.selector }__link--closed`; |
| 22 |
this.vars.wrapper = `.woocommerce-terms-and-conditions-wrapper.${ this.vars.selector }`; |
| 23 |
}, |
| 24 |
|
| 25 |
/** |
| 26 |
* Initialize. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
* |
| 30 |
* @return {void} |
| 31 |
*/ |
| 32 |
init() { |
| 33 |
this.cache(); |
| 34 |
this.bindEvents(); |
| 35 |
}, |
| 36 |
|
| 37 |
/** |
| 38 |
* Bind events. |
| 39 |
* |
| 40 |
* @since 1.0.0 |
| 41 |
* |
| 42 |
* @return {void} |
| 43 |
*/ |
| 44 |
bindEvents() { |
| 45 |
$( document.body ).on( 'click', `a.${ this.vars.embed }`, this.handleEmbedToggle ); |
| 46 |
}, |
| 47 |
|
| 48 |
/** |
| 49 |
* Handle embed toggle. |
| 50 |
* |
| 51 |
* @since 1.0.0 |
| 52 |
* |
| 53 |
* @param {Event} event Event. |
| 54 |
* |
| 55 |
* @return {boolean} False. |
| 56 |
*/ |
| 57 |
handleEmbedToggle( event ) { |
| 58 |
// Prevent default. |
| 59 |
event.preventDefault(); |
| 60 |
|
| 61 |
const $this = $( this ); |
| 62 |
const $wrapper = $this.closest( script.vars.wrapper ); |
| 63 |
|
| 64 |
// Exit early if the wrapper doesn't exist. |
| 65 |
if ( ! $wrapper.length ) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
const $content = $wrapper.find( `> .${ script.vars.content }` ); |
| 70 |
|
| 71 |
$content.slideToggle( () => { |
| 72 |
const isVisible = $content.is( ':visible' ); |
| 73 |
|
| 74 |
$this.toggleClass( script.vars.openClassName, isVisible ); |
| 75 |
$this.toggleClass( script.vars.closeClassName, ! isVisible ); |
| 76 |
} ); |
| 77 |
|
| 78 |
return false; |
| 79 |
}, |
| 80 |
}; |
| 81 |
|
| 82 |
script.init(); |
| 83 |
} )( jQuery ); |
| 84 |
|