| 1 |
/** |
| 2 |
* Woo Product Countdown widget behavior. |
| 3 |
* |
| 4 |
* Updates countdown timers on the frontend. |
| 5 |
*/ |
| 6 |
(function ($) { |
| 7 |
"use strict"; |
| 8 |
|
| 9 |
const INIT_FLAG = "kaWooCountdownInit"; |
| 10 |
|
| 11 |
const initCountdown = (el) => { |
| 12 |
if (!el || !el.dataset) return; |
| 13 |
if (el.dataset[INIT_FLAG] === "1") return; |
| 14 |
el.dataset[INIT_FLAG] = "1"; |
| 15 |
|
| 16 |
const endTs = parseInt(el.dataset.end || '0', 10); |
| 17 |
if (!endTs) return; |
| 18 |
const showSeconds = el.dataset.seconds === 'yes'; |
| 19 |
const expireMode = el.dataset.expire || 'hide'; |
| 20 |
const expireText = el.dataset.expireText || ''; |
| 21 |
const format = el.classList.contains('ka-woo-countdown--inline') ? 'inline' : 'blocks'; |
| 22 |
|
| 23 |
const numbers = { |
| 24 |
days: el.querySelector('[data-unit="days"]'), |
| 25 |
hours: el.querySelector('[data-unit="hours"]'), |
| 26 |
minutes: el.querySelector('[data-unit="minutes"]'), |
| 27 |
seconds: el.querySelector('[data-unit="seconds"]'), |
| 28 |
}; |
| 29 |
|
| 30 |
const tick = () => { |
| 31 |
const now = Math.floor(Date.now() / 1000); |
| 32 |
let diff = endTs - now; |
| 33 |
if (diff <= 0) { |
| 34 |
if (expireMode === 'text' && expireText) { |
| 35 |
const msg = document.createElement("div"); |
| 36 |
msg.className = "ka-woo-countdown__expired-text"; |
| 37 |
msg.textContent = expireText; |
| 38 |
el.innerHTML = ""; |
| 39 |
el.appendChild(msg); |
| 40 |
return; |
| 41 |
} |
| 42 |
if (expireMode === 'zero') { |
| 43 |
if (numbers.days) numbers.days.textContent = '0'; |
| 44 |
if (numbers.hours) numbers.hours.textContent = '00'; |
| 45 |
if (numbers.minutes) numbers.minutes.textContent = '00'; |
| 46 |
if (showSeconds && numbers.seconds) numbers.seconds.textContent = '00'; |
| 47 |
return; |
| 48 |
} |
| 49 |
el.remove(); |
| 50 |
return; |
| 51 |
} |
| 52 |
const days = Math.floor(diff / 86400); |
| 53 |
diff -= days * 86400; |
| 54 |
const hours = Math.floor(diff / 3600); |
| 55 |
diff -= hours * 3600; |
| 56 |
const minutes = Math.floor(diff / 60); |
| 57 |
diff -= minutes * 60; |
| 58 |
const seconds = diff; |
| 59 |
|
| 60 |
if (numbers.days) numbers.days.textContent = days; |
| 61 |
const hoursText = hours.toString().padStart(2, '0'); |
| 62 |
const minutesText = minutes.toString().padStart(2, '0'); |
| 63 |
const secondsText = seconds.toString().padStart(2, '0'); |
| 64 |
|
| 65 |
if (numbers.hours) numbers.hours.textContent = hoursText; |
| 66 |
if (numbers.minutes) numbers.minutes.textContent = minutesText; |
| 67 |
if (showSeconds && numbers.seconds) { |
| 68 |
numbers.seconds.textContent = secondsText; |
| 69 |
} else if (!showSeconds && numbers.seconds && 'inline' === format) { |
| 70 |
numbers.seconds.parentElement?.remove(); |
| 71 |
} |
| 72 |
}; |
| 73 |
|
| 74 |
tick(); |
| 75 |
setInterval(tick, 1000); |
| 76 |
}; |
| 77 |
|
| 78 |
const initAll = (root) => { |
| 79 |
const ctx = root && root.querySelectorAll ? root : document; |
| 80 |
ctx.querySelectorAll(".ka-woo-countdown").forEach(initCountdown); |
| 81 |
}; |
| 82 |
|
| 83 |
document.addEventListener("DOMContentLoaded", () => initAll(document)); |
| 84 |
|
| 85 |
$(window).on("elementor/frontend/init", function () { |
| 86 |
elementorFrontend.hooks.addAction( |
| 87 |
"frontend/element_ready/woo_product_countdown.default", |
| 88 |
function ($scope) { |
| 89 |
initAll($scope && $scope[0] ? $scope[0] : document); |
| 90 |
} |
| 91 |
); |
| 92 |
}); |
| 93 |
})(jQuery); |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|