| 1 |
'use strict'; |
| 2 |
(function ($) { |
| 3 |
const get_cookie = (cname) => { |
| 4 |
let name = cname + '='; |
| 5 |
let decodedCookie = decodeURIComponent(document.cookie); |
| 6 |
let ca = decodedCookie.split(';'); |
| 7 |
for (let i = 0; i < ca.length; i++) { |
| 8 |
let c = ca[i]; |
| 9 |
while (c.charAt(0) == ' ') { |
| 10 |
c = c.substring(1); |
| 11 |
} |
| 12 |
if (c.indexOf(name) == 0) { |
| 13 |
return c.substring(name.length, c.length); |
| 14 |
} |
| 15 |
} |
| 16 |
return ''; |
| 17 |
}; |
| 18 |
const currencyID = get_cookie('yay_currency_widget'); |
| 19 |
let applyCurrency = null; |
| 20 |
if (window.yayCurrency.converted_currency) { |
| 21 |
window.yayCurrency.converted_currency.forEach((currency) => { |
| 22 |
if (currency.ID == currencyID) { |
| 23 |
applyCurrency = currency; |
| 24 |
} |
| 25 |
}); |
| 26 |
if (window.wc_price_calculator_params) { |
| 27 |
let rate_after_fee = parseFloat(applyCurrency.rate); |
| 28 |
if ('percentage' === applyCurrency.fee.type) { |
| 29 |
rate_after_fee = |
| 30 |
parseFloat(applyCurrency.rate) + |
| 31 |
parseFloat(applyCurrency.rate) * |
| 32 |
(parseFloat(applyCurrency.fee.value) / 100); |
| 33 |
} else { |
| 34 |
rate_after_fee = |
| 35 |
parseFloat(applyCurrency.rate) + parseFloat(applyCurrency.fee.value); |
| 36 |
} |
| 37 |
window.wc_price_calculator_params.woocommerce_currency_pos = |
| 38 |
applyCurrency.currencyPosition; |
| 39 |
window.wc_price_calculator_params.woocommerce_price_decimal_sep = |
| 40 |
applyCurrency.decimalSeparator; |
| 41 |
window.wc_price_calculator_params.woocommerce_price_num_decimals = |
| 42 |
applyCurrency.numberDecimal; |
| 43 |
window.wc_price_calculator_params.woocommerce_price_thousand_sep = |
| 44 |
applyCurrency.thousandSeparator; |
| 45 |
|
| 46 |
window.wc_price_calculator_params.pricing_rules && |
| 47 |
window.wc_price_calculator_params.pricing_rules.forEach((rule) => { |
| 48 |
rule.price = (parseFloat(rule.price) * rate_after_fee).toString(); |
| 49 |
rule.regular_price = ( |
| 50 |
parseFloat(rule.regular_price) * rate_after_fee |
| 51 |
).toString(); |
| 52 |
rule.sale_price = ( |
| 53 |
parseFloat(rule.sale_price) * rate_after_fee |
| 54 |
).toString(); |
| 55 |
}); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
const yay_currency = () => { |
| 60 |
if (window.history.replaceState) { |
| 61 |
window.history.replaceState(null, null, window.location.href); |
| 62 |
} |
| 63 |
}; |
| 64 |
|
| 65 |
jQuery(document).ready(function ($) { |
| 66 |
yay_currency($); |
| 67 |
const { yayCurrency } = window; |
| 68 |
|
| 69 |
$(document.body).on('update_checkout', function (e) { |
| 70 |
e.stopImmediatePropagation(); |
| 71 |
}); |
| 72 |
|
| 73 |
$(document.body).trigger('wc_fragment_refresh'); |
| 74 |
|
| 75 |
if ( |
| 76 |
typeof wc_cart_fragments_params === 'undefined' || |
| 77 |
wc_cart_fragments_params === null |
| 78 |
) { |
| 79 |
} else { |
| 80 |
sessionStorage.removeItem(wc_cart_fragments_params.fragment_name); |
| 81 |
} |
| 82 |
|
| 83 |
const switcherUpwards = function () { |
| 84 |
const allSwitcher = $('.yay-currency-single-page-switcher'); |
| 85 |
|
| 86 |
allSwitcher.each(function () { |
| 87 |
const SWITCHER_LIST_HEIGT = 250; |
| 88 |
|
| 89 |
const offsetTop = |
| 90 |
$(this).offset().top + $(this).height() - $(window).scrollTop(); |
| 91 |
const offsetBottom = |
| 92 |
$(window).height() - |
| 93 |
$(this).height() - |
| 94 |
$(this).offset().top + |
| 95 |
$(window).scrollTop(); |
| 96 |
|
| 97 |
if ( |
| 98 |
offsetBottom < SWITCHER_LIST_HEIGT && |
| 99 |
offsetTop > SWITCHER_LIST_HEIGT |
| 100 |
) { |
| 101 |
$(this).find('.yay-currency-custom-options').addClass('upwards'); |
| 102 |
$(this).find('.yay-currency-custom-arrow').addClass('upwards'); |
| 103 |
$(this) |
| 104 |
.find('.yay-currency-custom-select__trigger') |
| 105 |
.addClass('upwards'); |
| 106 |
} else { |
| 107 |
$(this).find('.yay-currency-custom-options').removeClass('upwards'); |
| 108 |
$(this).find('.yay-currency-custom-arrow').removeClass('upwards'); |
| 109 |
$(this) |
| 110 |
.find('.yay-currency-custom-select__trigger') |
| 111 |
.removeClass('upwards'); |
| 112 |
} |
| 113 |
}); |
| 114 |
}; |
| 115 |
$(window).on('load resize scroll', switcherUpwards); |
| 116 |
|
| 117 |
$(document).on('click', '.yay-currency-custom-select-wrapper', function () { |
| 118 |
$('.yay-currency-custom-select', this).toggleClass('open'); |
| 119 |
$('#slide-out-widget-area') |
| 120 |
.find('.yay-currency-custom-options') |
| 121 |
.toggleClass('overflow-fix'); |
| 122 |
$('[id^=footer]').toggleClass('z-index-fix'); |
| 123 |
$('.yay-currency-custom-select', this) |
| 124 |
.parents('.handheld-navigation') |
| 125 |
.toggleClass('overflow-fix'); |
| 126 |
}); |
| 127 |
|
| 128 |
$(document).on('click', '.yay-currency-custom-option-row', function () { |
| 129 |
const currencyID = $(this).data('value'); |
| 130 |
const countryCode = $(this) |
| 131 |
.children('.yay-currency-flag') |
| 132 |
.data('country_code'); |
| 133 |
$('.yay-currency-switcher').val(currencyID).change(); |
| 134 |
|
| 135 |
if (!$(this).hasClass('selected')) { |
| 136 |
const clickedSwitcher = $(this).closest('.yay-currency-custom-select'); |
| 137 |
|
| 138 |
$(this) |
| 139 |
.parent() |
| 140 |
.find('.yay-currency-custom-option-row.selected') |
| 141 |
.removeClass('selected'); |
| 142 |
|
| 143 |
$(this).addClass('selected'); |
| 144 |
|
| 145 |
clickedSwitcher.find('.yay-currency-flag.selected').css({ |
| 146 |
background: `url(${yayCurrency.yayCurrencyPluginURL}assets/dist/flags/${countryCode}.svg)`, |
| 147 |
}); |
| 148 |
|
| 149 |
clickedSwitcher |
| 150 |
.find( |
| 151 |
'.yay-currency-custom-select__trigger .yay-currency-selected-option' |
| 152 |
) |
| 153 |
.text($(this).text()); |
| 154 |
|
| 155 |
clickedSwitcher.find('.yay-currency-custom-loader').addClass('active'); |
| 156 |
|
| 157 |
clickedSwitcher.find('.yay-currency-custom-arrow').hide(); |
| 158 |
} |
| 159 |
}); |
| 160 |
|
| 161 |
window.addEventListener('click', function (e) { |
| 162 |
const selects = document.querySelectorAll('.yay-currency-custom-select'); |
| 163 |
selects.forEach((select) => { |
| 164 |
if (!select.contains(e.target)) { |
| 165 |
select.classList.remove('open'); |
| 166 |
} |
| 167 |
}); |
| 168 |
}); |
| 169 |
}); |
| 170 |
})(jQuery); |
| 171 |
|