Files
blog/themes/Ying/assets/js/modules/reward.js
T
2026-02-02 21:36:53 +08:00

65 lines
1.9 KiB
JavaScript

/**
* Reward System Module
*/
(function() {
function initReward() {
const btn = document.getElementById('reward-btn');
const modal = document.getElementById('reward-modal');
const close = document.getElementById('reward-close');
if (!btn || !modal) return;
// Helper to show modal
const showModal = (e) => {
if(e) {
e.preventDefault();
e.stopPropagation();
}
modal.classList.add('show');
document.body.style.overflow = 'hidden'; // Prevent scrolling
};
// Helper to hide modal
const hideModal = () => {
modal.classList.remove('show');
document.body.style.overflow = '';
};
// Button click
btn.addEventListener('click', showModal);
// Close button click
if (close) {
close.addEventListener('click', hideModal);
}
// Click outside modal content
modal.addEventListener('click', (e) => {
if (e.target === modal) {
hideModal();
}
});
// Escape key to close
if (!window.rewardKeyHandlerAttached) {
document.addEventListener('keydown', function(e) {
const activeModal = document.getElementById('reward-modal');
if (e.key === 'Escape' && activeModal && activeModal.classList.contains('show')) {
activeModal.classList.remove('show');
document.body.style.overflow = '';
}
});
window.rewardKeyHandlerAttached = true;
}
}
// Initialize on load
document.addEventListener('DOMContentLoaded', initReward);
// Initialize on Pjax complete
document.addEventListener('pjax:complete', initReward);
// Export
window.initReward = initReward;
})();