Files
wordpress-theme-aksrt/assets/js/reward.js
T

98 lines
3.2 KiB
JavaScript

/**
* Reward (打赏) — dedicated module.
*
* Handles the reward modal: amount selection, custom amount input,
* QR code display, and open/close interaction.
*
* @package AKSRT
*/
;(function () {
'use strict';
function initReward() {
var btn = document.getElementById('action-reward');
if (!btn) return;
var modal = document.getElementById('reward-modal');
if (!modal) return;
var closeBtn = modal.querySelector('.reward-modal__close');
var overlay = modal.querySelector('.reward-modal__overlay');
var amountBtns = modal.querySelectorAll('.reward-amounts__btn');
var customWrap = modal.querySelector('.reward-custom');
var customInput = modal ? modal.querySelector('.reward-custom__input') : null;
var customConfirm = modal ? modal.querySelector('.reward-custom__confirm') : null;
var selectedAmount = 0;
/* ---- open / close ---- */
function openReward() {
modal.classList.add('is-open');
document.body.style.overflow = 'hidden';
}
function closeReward() {
modal.classList.remove('is-open');
document.body.style.overflow = '';
// reset selection
selectedAmount = 0;
amountBtns.forEach(function (b) { b.classList.remove('is-active'); });
if (customInput) customInput.value = '';
}
btn.addEventListener('click', openReward);
if (closeBtn) closeBtn.addEventListener('click', closeReward);
if (overlay) overlay.addEventListener('click', closeReward);
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape' && modal.classList.contains('is-open')) closeReward();
});
/* ---- amount selection ---- */
amountBtns.forEach(function (b) {
b.addEventListener('click', function () {
amountBtns.forEach(function (x) { x.classList.remove('is-active'); });
b.classList.add('is-active');
selectedAmount = parseInt(b.dataset.amount || '0', 10);
// clear custom input when preset is chosen
if (customInput) customInput.value = '';
});
});
/* ---- custom amount ---- */
if (customInput) {
customInput.addEventListener('focus', function () {
amountBtns.forEach(function (x) { x.classList.remove('is-active'); });
selectedAmount = 0;
});
customInput.addEventListener('input', function () {
// strip non-digits
customInput.value = customInput.value.replace(/[^\d]/g, '');
});
}
if (customConfirm) {
customConfirm.addEventListener('click', function () {
var val = customInput ? parseInt(customInput.value, 10) : 0;
if (val > 0) {
selectedAmount = val;
amountBtns.forEach(function (x) { x.classList.remove('is-active'); });
}
});
}
}
/* ---- register with AksrtFrontend lifecycle ---- */
if (window.AksrtFrontend && typeof window.AksrtFrontend.register === 'function') {
window.AksrtFrontend.register('aksrt-reward', function () {
initReward();
});
} else {
// fallback: run on DOMContentLoaded
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initReward);
} else {
initReward();
}
}
})();