Files
blog/themes/Ying/assets/js/modules/reward.js
T

120 lines
4.6 KiB
JavaScript
Raw Normal View History

2026-02-02 21:36:53 +08:00
/**
* 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;
2026-02-03 13:06:58 +08:00
// Global Click Handler
// We attach this to document only when modal is open to handle "click anywhere outside content"
// This is more robust than attaching to the modal container, which might have size/z-index issues.
const handleGlobalClick = (e) => {
const activeModal = document.getElementById('reward-modal');
if (!activeModal || !activeModal.classList.contains('show')) return;
// If click is inside the content, ignore
if (e.target.closest('.reward-content')) return;
// If click is on the open button, ignore (it has its own handler, or we might double-toggle)
if (e.target.closest('#reward-btn')) return;
// Otherwise, close it
hideModal();
};
2026-02-02 21:36:53 +08:00
// Helper to show modal
const showModal = (e) => {
if(e) {
e.preventDefault();
e.stopPropagation();
}
modal.classList.add('show');
document.body.style.overflow = 'hidden'; // Prevent scrolling
2026-02-03 13:06:58 +08:00
// Attach global click listener with a slight delay to avoid current click triggering it
setTimeout(() => {
document.addEventListener('click', handleGlobalClick);
}, 10);
2026-02-02 21:36:53 +08:00
};
// Helper to hide modal
const hideModal = () => {
modal.classList.remove('show');
document.body.style.overflow = '';
2026-02-03 13:06:58 +08:00
document.removeEventListener('click', handleGlobalClick);
2026-02-02 21:36:53 +08:00
};
// Button click
btn.addEventListener('click', showModal);
// Close button click
if (close) {
2026-02-03 13:06:58 +08:00
close.addEventListener('click', (e) => {
e.stopPropagation(); // Prevent bubbling to global handler (though redundant as handler checks)
2026-02-02 21:36:53 +08:00
hideModal();
2026-02-03 13:06:58 +08:00
});
}
// Remove old direct listeners if any (cleaner approach replaces them)
2026-02-02 21:36:53 +08:00
// Escape key to close
if (!window.rewardKeyHandlerAttached) {
2026-02-03 13:06:58 +08:00
window.rewardKeyHandler = function(e) {
2026-02-02 21:36:53 +08:00
const activeModal = document.getElementById('reward-modal');
if (e.key === 'Escape' && activeModal && activeModal.classList.contains('show')) {
2026-02-03 13:06:58 +08:00
hideModal(); // Use the same hide function to clean up listeners
2026-02-02 21:36:53 +08:00
}
2026-02-03 13:06:58 +08:00
};
document.addEventListener('keydown', window.rewardKeyHandler);
2026-02-02 21:36:53 +08:00
window.rewardKeyHandlerAttached = true;
}
2026-02-03 13:06:58 +08:00
// Expose hideModal for destroy function if needed?
// We can just rely on the fact that destroyReward queries DOM.
// But we need to make sure destroyReward removes the global listener too.
// Since handleGlobalClick is local scope, we need a way to reference it or just define it outside?
// Or better: make handleGlobalClick a named function available to destroyReward via a shared scope or window.
// Let's attach it to window for safety in this module pattern.
window.rewardGlobalClickHandler = handleGlobalClick;
}
function destroyReward() {
// Cleanup keydown listener
if (window.rewardKeyHandlerAttached && window.rewardKeyHandler) {
document.removeEventListener('keydown', window.rewardKeyHandler);
window.rewardKeyHandlerAttached = false;
window.rewardKeyHandler = null;
}
// Cleanup global click listener
if (window.rewardGlobalClickHandler) {
document.removeEventListener('click', window.rewardGlobalClickHandler);
window.rewardGlobalClickHandler = null;
}
// Hide modal if open (restore body scroll)
const modal = document.getElementById('reward-modal');
if (modal && modal.classList.contains('show')) {
modal.classList.remove('show');
document.body.style.overflow = '';
}
2026-02-02 21:36:53 +08:00
}
// Initialize on load
document.addEventListener('DOMContentLoaded', initReward);
// Initialize on Pjax complete
document.addEventListener('pjax:complete', initReward);
2026-02-03 13:06:58 +08:00
// Destroy on Pjax send
document.addEventListener('pjax:send', destroyReward);
2026-02-02 21:36:53 +08:00
// Export
window.initReward = initReward;
2026-02-03 13:06:58 +08:00
window.destroyReward = destroyReward;
2026-02-02 21:36:53 +08:00
})();