diff --git a/assets/donate/donate.css b/assets/donate/donate.css index 80680be..fb17372 100644 --- a/assets/donate/donate.css +++ b/assets/donate/donate.css @@ -35,3 +35,6 @@ .sg-donate label.sg-inline input[type=checkbox]{flex:none;width:22px;height:22px;margin:0} .sg-donate .sg-cover{gap:.25rem} .sg-donate .sg-act-title,.sg-donate .sg-act-desc,.sg-donate .sg-tip,.sg-donate .sg-msg,.sg-donate .sg-total{overflow-wrap:anywhere} + +.sg-donate .sg-next-unlock{margin:0 0 .9rem;padding:.5rem .75rem;border:1px solid rgba(128,128,128,.5);border-left-width:4px;border-radius:.4rem;font-weight:600;line-height:1.4;overflow-wrap:anywhere} +.sg-donate .sg-next-unlock[hidden]{display:none} diff --git a/assets/donate/donate.js b/assets/donate/donate.js index 95536e9..29405cb 100644 --- a/assets/donate/donate.js +++ b/assets/donate/donate.js @@ -82,6 +82,18 @@ var status = el('div', { 'class': 'sg-msg', role: 'status' }); var buttons = el('div', { 'class': 'sg-buttons' }); + // "Next unlock" line for tiered goals (server computes title + remaining minor units per goal). + var nextLine = el('p', { 'class': 'sg-next-unlock', 'aria-live': 'polite' }); + nextLine.hidden = true; + function showNext() { + var n = state.goal.nextUnlock; + if (!n || !n.title || !(n.remaining_minor > 0)) { nextLine.hidden = true; nextLine.textContent = ''; return; } + nextLine.textContent = String(T.nextUnlock || 'Next unlock: %1$s \u2014 %2$s to go') + .replace('%1$s', function () { return n.title; }) + .replace('%2$s', function () { return fmt(n.remaining_minor, state.goal.exp, state.goal.currency); }); + nextLine.hidden = false; + } + root.appendChild(nextLine); if (goals.length > 1) { root.appendChild(row([el('label', { 'for': uid + '-goal', text: T.goal }), goalSel])); } root.appendChild(row([el('label', { text: T.amount }), presetBox, el('label', { 'class': 'sg-sub', 'for': uid + '-amt', text: T.customAmount || 'Or enter your own amount' }), amount, hint])); var nameRow = row([el('label', { 'for': uid + '-name', text: T.name }), name]); @@ -223,9 +235,9 @@ goalSel.addEventListener('change', function () { state.goal = goals.filter(function (g) { return String(g.id) === goalSel.value; })[0] || goals[0]; - buildPresets(); refresh(); renderButtons(); + showNext(); buildPresets(); refresh(); renderButtons(); }); - buildPresets(); refresh(); renderButtons(); + showNext(); buildPresets(); refresh(); renderButtons(); } function boot() { Array.prototype.forEach.call(document.querySelectorAll('.sg-donate'), init); } diff --git a/assets/overlay/overlay.css b/assets/overlay/overlay.css index 1e88fb2..f923600 100644 --- a/assets/overlay/overlay.css +++ b/assets/overlay/overlay.css @@ -49,3 +49,13 @@ body.sg-offline #sg-root{opacity:.6} .sg-menu-track.sg-scrolling{animation:sg-scroll var(--sg-scroll-s,20s) linear infinite} @keyframes sg-scroll{from{transform:translateY(0)}to{transform:translateY(-50%)}} @media (prefers-reduced-motion:reduce){.sg-menu-track.sg-scrolling{animation:none}} + +/* Tiered goals */ +.sg-tier{font-size:.85em;font-weight:600;margin-bottom:.3em;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;color:var(--sg-accent2)} +.sg-tier[hidden]{display:none} +.sg-card.sg-tiers-done .sg-tier{font-weight:800} +.sg-card.sg-tier-up{animation:sg-tier-pop 1.1s cubic-bezier(.22,1,.36,1) 2,sg-tier-glow 1.3s ease-in-out 2} +.sg-bonus-line.sg-tier-line{background:linear-gradient(90deg,#f59e0b,#ef4444);color:#fff;border-radius:.3em;letter-spacing:.04em} +@keyframes sg-tier-pop{0%{transform:scale(1)}30%{transform:scale(1.06)}100%{transform:scale(1)}} +@keyframes sg-tier-glow{0%,100%{box-shadow:0 0 0 transparent}50%{box-shadow:0 0 1em #f59e0b,0 0 2em var(--sg-accent2)}} +@media (prefers-reduced-motion:reduce){.sg-card.sg-tier-up{animation:none;box-shadow:0 0 .8em #f59e0b}} diff --git a/assets/overlay/overlay.js b/assets/overlay/overlay.js index 6a3f9a9..8cf822c 100644 --- a/assets/overlay/overlay.js +++ b/assets/overlay/overlay.js @@ -16,26 +16,52 @@ } // ---- goal ---- - var goal = null, shownRaised = 0, raf = 0; + var goal = null, shownRaised = 0, raf = 0, lastTierKey = null, celebrateTimer = 0; function renderGoal(d) { - if (!d.active) { root.textContent = ''; goal = null; return; } + if (!d.active) { root.textContent = ''; goal = null; lastTierKey = null; return; } if (!goal) { root.textContent = ''; var card = el('div', 'sg-card'); - goal = { title: el('div', 'sg-title'), fill: el('div', 'sg-fill'), pct: el('div', 'sg-pct'), + goal = { card: card, tier: el('div', 'sg-tier'), title: el('div', 'sg-title'), fill: el('div', 'sg-fill'), pct: el('div', 'sg-pct'), raised: el('span'), target: el('span') }; var track = el('div', 'sg-track'); track.appendChild(goal.fill); track.appendChild(goal.pct); var nums = el('div', 'sg-nums'); nums.appendChild(goal.raised); nums.appendChild(goal.target); - card.appendChild(goal.title); card.appendChild(track); card.appendChild(nums); + goal.tier.hidden = true; + card.appendChild(goal.title); card.appendChild(goal.tier); card.appendChild(track); card.appendChild(nums); root.appendChild(card); } goal.title.textContent = d.title; goal.target.textContent = d.target_display; goal.fill.style.width = d.percent + '%'; goal.pct.textContent = d.percent + '%'; + renderTier(d.tiers); tweenRaised(d); } + // Tiered goals only (d.tiers present): "Tier 2 of 4 \u00b7 title" above the bar, or "All tiers unlocked!". + // Untiered goals keep the tier line hidden, so they look exactly as before. textContent only. + function renderTier(t) { + var valid = t && typeof t === 'object' && isFinite(Number(t.count)) && Number(t.count) > 0; + goal.tier.hidden = !valid; + goal.card.classList.toggle('sg-tiered', !!valid); + goal.card.classList.toggle('sg-tiers-done', !!(valid && t.all_reached)); + if (!valid) { goal.tier.textContent = ''; lastTierKey = null; return; } + var cur = t.current && typeof t.current === 'object' ? t.current : null; + goal.tier.textContent = t.all_reached || !cur + ? 'All tiers unlocked!' + : 'Tier ' + parseInt(t.index, 10) + ' of ' + parseInt(t.count, 10) + ' \u00b7 ' + String(cur.title || ''); + // Celebrate when the current tier changed between polls (never on the first poll / after a reload). + var key = (t.all_reached ? 'done' : String(t.index)) + '/' + t.count; + if (lastTierKey !== null && lastTierKey !== key) { + goal.card.classList.remove('sg-tier-up'); + void goal.card.offsetWidth; // restart the animation + goal.card.classList.add('sg-tier-up'); + clearTimeout(celebrateTimer); + var card = goal.card; + celebrateTimer = setTimeout(function () { card.classList.remove('sg-tier-up'); }, 2600); + } + lastTierKey = key; + } function tweenRaised(d) { cancelAnimationFrame(raf); var from = shownRaised, to = d.raised_minor, t0 = performance.now(), dur = 1200; @@ -203,11 +229,16 @@ } } var acts = Array.isArray(a.actions) ? a.actions : []; - if (acts.length) card.classList.add('sg-bonus'); + var tiersUnlocked = Array.isArray(a.tiers) ? a.tiers : []; + if (acts.length || tiersUnlocked.length) card.classList.add('sg-bonus'); for (var k = 0; k < acts.length; k++) { var times = parseInt(acts[k].times, 10) || 1; card.appendChild(el('div', 'sg-bonus-line', 'ACTION: ' + String(acts[k].title) + (times > 1 ? ' x' + times : ''))); } + for (var q = 0; q < tiersUnlocked.length; q++) { + var tt = tiersUnlocked[q] && tiersUnlocked[q].title; + if (tt) card.appendChild(el('div', 'sg-bonus-line sg-tier-line', 'TIER UNLOCKED: ' + String(tt))); + } if (a.message) card.appendChild(el('div', 'sg-alert-msg', a.message)); root.appendChild(card); diff --git a/includes/Admin/Admin.php b/includes/Admin/Admin.php index 2e61bc6..ac9513e 100644 --- a/includes/Admin/Admin.php +++ b/includes/Admin/Admin.php @@ -25,6 +25,8 @@ final class Admin { 'sg_rule_save' => array( GoalsPage::class, 'handleRuleSave' ), 'sg_rule_toggle' => array( GoalsPage::class, 'handleRuleToggle' ), 'sg_rule_delete' => array( GoalsPage::class, 'handleRuleDelete' ), + 'sg_tier_save' => array( GoalsPage::class, 'handleTierSave' ), + 'sg_tier_delete' => array( GoalsPage::class, 'handleTierDelete' ), 'sg_action_save' => array( ActionsPage::class, 'handleSave' ), 'sg_action_toggle' => array( ActionsPage::class, 'handleToggle' ), 'sg_action_delete' => array( ActionsPage::class, 'handleDelete' ), diff --git a/includes/Admin/GoalsPage.php b/includes/Admin/GoalsPage.php index f9ea020..29c1384 100644 --- a/includes/Admin/GoalsPage.php +++ b/includes/Admin/GoalsPage.php @@ -4,6 +4,9 @@ declare( strict_types=1 ); namespace StreamGoals\Admin; use StreamGoals\Core\Money; +use StreamGoals\Core\Settings; +use StreamGoals\Tiers\TierPolicy; +use StreamGoals\Tiers\TierRepository; /** Goals list/create/edit/status/delete, plus the per-goal rules editor. */ final class GoalsPage { @@ -17,6 +20,8 @@ final class GoalsPage { self::renderForm( 'edit' === $view ? $id : 0 ); } elseif ( 'rules' === $view && $id ) { self::renderRules( $id ); + } elseif ( 'tiers' === $view && $id ) { + self::renderTiers( $id ); } else { self::renderList(); } @@ -29,6 +34,7 @@ final class GoalsPage { echo '
'; echo 'Overlays and the donation form use your newest active goal(s). Pause a goal to stop accepting donations; mark it complete when you are done. A goal that has donations cannot be deleted (complete it instead), so your records stay intact.
'; $goals = Store::goals()->all(); + $tierCounts = $goals ? ( new TierRepository() )->countsByGoal() : array(); if ( ! $goals ) { echo 'No goals yet. Create your first goal or run the setup guide.
| Target (' . esc_html( $cur ) . ') | ' . esc_html( Format::money( (int) $g['target_minor'], $cur ) ) . ' Set automatically from your highest tier. To change it, edit the tiers. | |||
|---|---|---|---|---|
| Currency | ' . esc_html( $cur ) . ' One currency is used across the whole site. Change it in Settings (applies to new goals only). | |||
| Optional. Your site time zone. Empty = starts immediately. |
| # | Unlock | Target | Status | Actions |
|---|---|---|---|---|
| ' . (int) ( $i + 1 ) . ' | ' . esc_html( $t['title'] ) . '';
+ if ( '' !== $t['description'] ) {
+ echo ' ' . esc_html( $t['description'] ) . ''; + } + echo ' | ' . esc_html( Format::money( (int) $t['target_minor'], $cur ) ) . ' | '; + echo '' . esc_html( $label ) . ' | '; + echo 'Edit '; + Admin::form( 'sg_tier_delete', array( 'goal_id' => $goalId, 'tier_id' => $tid ), 'sg-inline sg-confirm' ); + echo ' |
| ';
+ echo ' ' . ( $locked ? 'This tier has been reached, so its target is locked. You can still change its name and description.' : 'When your total reaches this amount, this tier unlocks and the goal bar moves on to the next one. Must be above what has been raised so far (' . esc_html( Format::money( $raised, $cur ) ) . ') and at least ' . esc_html( Format::money( $min, $cur ) ) . '.' ) . ' | |
What viewers get when this tier is reached, for example "New microphone". Up to 120 characters. Shown on your overlay and the donation form. | |
Optional. A short note, up to 255 characters. |
'; + if ( $edit ) { + echo ' Cancel'; + } + echo '