| 1 |
/** |
| 2 |
* Pricing Table Builder - Frontend JavaScript |
| 3 |
* Handles billing toggle and animations |
| 4 |
*/ |
| 5 |
|
| 6 |
(function() { |
| 7 |
'use strict'; |
| 8 |
|
| 9 |
const KNG_PT = { |
| 10 |
/** |
| 11 |
* Initialize all pricing tables |
| 12 |
*/ |
| 13 |
init: function() { |
| 14 |
document.querySelectorAll('.kng-pt-wrapper').forEach(wrapper => { |
| 15 |
this.initToggle(wrapper); |
| 16 |
}); |
| 17 |
}, |
| 18 |
|
| 19 |
/** |
| 20 |
* Initialize billing toggle for a wrapper |
| 21 |
*/ |
| 22 |
initToggle: function(wrapper) { |
| 23 |
const toggle = wrapper.querySelector('.kng-pt-toggle'); |
| 24 |
if (!toggle) return; |
| 25 |
|
| 26 |
const buttons = toggle.querySelectorAll('.kng-pt-toggle-btn'); |
| 27 |
const cards = wrapper.querySelectorAll('.kng-pt-card'); |
| 28 |
|
| 29 |
buttons.forEach(btn => { |
| 30 |
btn.addEventListener('click', (e) => { |
| 31 |
e.preventDefault(); |
| 32 |
const period = btn.dataset.period; |
| 33 |
|
| 34 |
// Update button states |
| 35 |
buttons.forEach(b => { |
| 36 |
b.classList.remove('is-active'); |
| 37 |
b.setAttribute('aria-checked', 'false'); |
| 38 |
}); |
| 39 |
btn.classList.add('is-active'); |
| 40 |
btn.setAttribute('aria-checked', 'true'); |
| 41 |
|
| 42 |
// Update wrapper data attribute |
| 43 |
wrapper.dataset.period = period; |
| 44 |
|
| 45 |
// Update price displays in each card |
| 46 |
cards.forEach(card => { |
| 47 |
this.updateCardPricing(card, period); |
| 48 |
}); |
| 49 |
}); |
| 50 |
}); |
| 51 |
}, |
| 52 |
|
| 53 |
/** |
| 54 |
* Update pricing display in a card |
| 55 |
*/ |
| 56 |
updateCardPricing: function(card, period) { |
| 57 |
const priceGroups = card.querySelectorAll('.kng-pt-price-group'); |
| 58 |
|
| 59 |
priceGroups.forEach(group => { |
| 60 |
if (group.dataset.period === period) { |
| 61 |
group.style.display = ''; |
| 62 |
// Trigger animation |
| 63 |
group.style.animation = 'none'; |
| 64 |
group.offsetHeight; // Force reflow |
| 65 |
group.style.animation = ''; |
| 66 |
} else { |
| 67 |
group.style.display = 'none'; |
| 68 |
} |
| 69 |
}); |
| 70 |
}, |
| 71 |
|
| 72 |
/** |
| 73 |
* Get current period for a wrapper |
| 74 |
*/ |
| 75 |
getCurrentPeriod: function(wrapper) { |
| 76 |
return wrapper.dataset.period || 'monthly'; |
| 77 |
} |
| 78 |
}; |
| 79 |
|
| 80 |
// Expose for admin preview |
| 81 |
window.kngPTFrontend = KNG_PT; |
| 82 |
|
| 83 |
// Initialize on DOM ready |
| 84 |
if (document.readyState === 'loading') { |
| 85 |
document.addEventListener('DOMContentLoaded', () => KNG_PT.init()); |
| 86 |
} else { |
| 87 |
KNG_PT.init(); |
| 88 |
} |
| 89 |
|
| 90 |
})(); |
| 91 |
|