| 1 |
"use strict"; |
| 2 |
|
| 3 |
; |
| 4 |
(function ($, window, document, undefined) { |
| 5 |
'use strict'; |
| 6 |
|
| 7 |
var countdownTimer, countdownTimerDate, countdownMinExpiration, countdownMaxExpiration; |
| 8 |
var countdownTimerId = 'merchant-countdown-timer'; |
| 9 |
var countdownTimerCookie = 'merchant_countdown_timer_date'; |
| 10 |
var countdownTimerCoolOffCookie = 'merchant_countdown_timer_cool_off_date'; |
| 11 |
var setCookie = function setCookie(name, value, expirationDays) { |
| 12 |
var date = new Date(); |
| 13 |
date.setTime(date.getTime() + expirationDays * 24 * 60 * 60 * 1000); // Calculate expiration date |
| 14 |
|
| 15 |
var expires = "expires=" + date.toUTCString(); |
| 16 |
document.cookie = name + "=" + value + ";" + expires + ";path=/"; |
| 17 |
}; |
| 18 |
var getCookie = function getCookie(name) { |
| 19 |
var cookieArr = document.cookie.split(';'); |
| 20 |
for (var i = 0; i < cookieArr.length; i++) { |
| 21 |
var cookiePair = cookieArr[i].split('='); |
| 22 |
var cookieName = cookiePair[0].trim(); |
| 23 |
var cookieValue = cookiePair[1].trim(); |
| 24 |
if (cookieName === name) { |
| 25 |
return decodeURIComponent(cookieValue); |
| 26 |
} |
| 27 |
} |
| 28 |
return null; |
| 29 |
}; |
| 30 |
var deleteCookie = function deleteCookie(name) { |
| 31 |
document.cookie = name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;"; |
| 32 |
}; |
| 33 |
var updateCountdown = function updateCountdown() { |
| 34 |
var now = new Date().getTime(); |
| 35 |
var minExpirationTime = countdownMinExpiration.getTime(); |
| 36 |
var maxExpirationTime = countdownMaxExpiration.getTime(); |
| 37 |
if (now < minExpirationTime) { |
| 38 |
// The current time is before the minimum expiration deadline |
| 39 |
displayCountdown(maxExpirationTime - now); |
| 40 |
} else if (now > maxExpirationTime) { |
| 41 |
// The current time is after the maximum expiration deadline |
| 42 |
destroyCountDown(); |
| 43 |
} else { |
| 44 |
// The current time is between the minimum and maximum expiration deadlines |
| 45 |
destroyCountDown(); |
| 46 |
} |
| 47 |
}; |
| 48 |
var displayCountdown = function displayCountdown(timeLeft) { |
| 49 |
var days = Math.floor(timeLeft / (1000 * 60 * 60 * 24)); |
| 50 |
var hours = Math.floor(timeLeft % (1000 * 60 * 60 * 24) / (1000 * 60 * 60)).toString().padStart(2, '0'); |
| 51 |
var minutes = Math.floor(timeLeft % (1000 * 60 * 60) / (1000 * 60)).toString().padStart(2, '0'); |
| 52 |
var seconds = Math.floor(timeLeft % (1000 * 60) / 1000).toString().padStart(2, '0'); |
| 53 |
document.getElementById(countdownTimerId).innerHTML = "".concat(days, " days ").concat(hours, ":").concat(minutes, ":").concat(seconds); |
| 54 |
if (timeLeft > 0) { |
| 55 |
setTimeout(updateCountdown, 1000); |
| 56 |
} |
| 57 |
}; |
| 58 |
var destroyCountDown = function destroyCountDown() { |
| 59 |
displayCountdown(0); |
| 60 |
deleteCookie(countdownTimerCookie); |
| 61 |
setCoolOffDate(); |
| 62 |
countdownTimer.hide(); |
| 63 |
}; |
| 64 |
var setCoolOffDate = function setCoolOffDate() { |
| 65 |
if (!getCookie(countdownTimerCoolOffCookie)) { |
| 66 |
var coolOffDate = new Date(); |
| 67 |
coolOffDate.setMinutes(coolOffDate.getMinutes() + parseInt(countdownTimer.attr('data-cop'))); |
| 68 |
setCookie(countdownTimerCoolOffCookie, coolOffDate, 1); |
| 69 |
} |
| 70 |
}; |
| 71 |
$(document).ready(function () { |
| 72 |
countdownTimer = $('.merchant-countdown-timer'); |
| 73 |
if (countdownTimer.length) { |
| 74 |
var maxExpirationHours = parseInt(countdownTimer.attr('data-max')); |
| 75 |
var minExpirationHours = maxExpirationHours - parseInt(countdownTimer.attr('data-min')); |
| 76 |
var startCountdown = true; |
| 77 |
if (getCookie(countdownTimerCoolOffCookie)) { |
| 78 |
var coolOffPeriodDate = new Date(getCookie(countdownTimerCoolOffCookie)); |
| 79 |
var now = new Date().getTime(); |
| 80 |
if (now > coolOffPeriodDate) { |
| 81 |
deleteCookie(countdownTimerCoolOffCookie); |
| 82 |
} else { |
| 83 |
startCountdown = false; |
| 84 |
} |
| 85 |
} |
| 86 |
if (startCountdown) { |
| 87 |
countdownTimer.show(); |
| 88 |
if (!getCookie(countdownTimerCookie)) { |
| 89 |
countdownTimerDate = new Date(); |
| 90 |
setCookie(countdownTimerCookie, countdownTimerDate, 9); |
| 91 |
} else { |
| 92 |
countdownTimerDate = new Date(getCookie(countdownTimerCookie)); |
| 93 |
} |
| 94 |
countdownMaxExpiration = new Date(countdownTimerDate.getTime()); |
| 95 |
countdownMaxExpiration.setHours(countdownMaxExpiration.getHours() + maxExpirationHours); |
| 96 |
countdownMinExpiration = new Date(countdownTimerDate.getTime()); |
| 97 |
countdownMinExpiration.setHours(countdownMinExpiration.getHours() + minExpirationHours); |
| 98 |
updateCountdown(); |
| 99 |
} |
| 100 |
} |
| 101 |
}); |
| 102 |
})(jQuery, window, document); |