- Core\ClientIp: trusted-header resolution for rate limiting behind Cloudflare/proxies - Settings: show/hide Pay Later and Debit/Credit Card (Pay Later off by default) - add_time rule: optional max_timer_seconds (uncapped by default), editable in rule form Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
126 lines
5.4 KiB
JavaScript
126 lines
5.4 KiB
JavaScript
/* StreamGoals rules editor: show the right fields per type and preview what a donation would do. */
|
|
( function () {
|
|
'use strict';
|
|
var dataEl = document.getElementById( 'sg-rules-data' );
|
|
var form = document.querySelector( '.sg-rule-form' );
|
|
var cfg = { currency: 'USD', exp: 2, rules: [] };
|
|
try { if ( dataEl ) { cfg = JSON.parse( dataEl.textContent ); } } catch ( e ) { /* keep defaults */ }
|
|
|
|
/* Parse "1,000.50" into minor units using string maths only (no float rounding). Returns null if invalid. */
|
|
function toMinor( raw ) {
|
|
var s = String( raw == null ? '' : raw ).replace( /[,\s$€£¥]/g, '' );
|
|
var m = /^(\d+)(?:\.(\d+))?$/.exec( s );
|
|
if ( ! m ) { return null; }
|
|
var frac = m[ 2 ] || '';
|
|
if ( frac.length > cfg.exp ) { return null; }
|
|
while ( frac.length < cfg.exp ) { frac += '0'; }
|
|
var n = parseInt( m[ 1 ] + frac, 10 );
|
|
return isFinite( n ) ? n : null;
|
|
}
|
|
|
|
function money( minor ) {
|
|
var s = String( minor );
|
|
if ( cfg.exp === 0 ) { return cfg.currency + ' ' + s; }
|
|
while ( s.length < 3 ) { s = '0' + s; }
|
|
return cfg.currency + ' ' + s.slice( 0, -2 ) + '.' + s.slice( -2 );
|
|
}
|
|
|
|
function duration( secs ) {
|
|
var parts = [];
|
|
[ [ 'day', 86400 ], [ 'hour', 3600 ], [ 'minute', 60 ], [ 'second', 1 ] ].forEach( function ( u ) {
|
|
var n = Math.floor( secs / u[ 1 ] );
|
|
secs -= n * u[ 1 ];
|
|
if ( n > 0 ) { parts.push( n + ' ' + u[ 0 ] + ( n === 1 ? '' : 's' ) ); }
|
|
} );
|
|
return parts.length ? parts.join( ' ' ) : '0 seconds';
|
|
}
|
|
|
|
/* Same maths as the PHP Rules classes (integer division, caps). */
|
|
function effect( rule, amount ) {
|
|
var c = rule.config || {};
|
|
if ( rule.type === 'add_time' ) {
|
|
if ( ! c.unit_minor ) { return null; }
|
|
var d = Math.floor( amount * c.seconds_per_unit / c.unit_minor );
|
|
if ( c.cap_seconds_per_donation != null ) { d = Math.min( d, c.cap_seconds_per_donation ); }
|
|
var note = c.max_timer_seconds != null ? ' (the timer never goes above ' + duration( c.max_timer_seconds ) + ')' : '';
|
|
return ( d > 0 ? 'Timer: +' + duration( d ) : 'Timer: no time added' ) + note;
|
|
}
|
|
if ( rule.type === 'counter' ) {
|
|
if ( ! c.per_minor ) { return null; }
|
|
return ( c.label || 'Counter' ) + ': +' + ( Math.floor( amount / c.per_minor ) * c.step );
|
|
}
|
|
if ( rule.type === 'bonus' ) {
|
|
return 'Bonus "' + c.event + '" ' + ( amount >= c.min_minor ? 'triggers' : 'does not trigger' );
|
|
}
|
|
return null;
|
|
}
|
|
|
|
var amountEl = document.getElementById( 'sg-preview-amount' );
|
|
var out = document.getElementById( 'sg-preview-out' );
|
|
function renderPreview() {
|
|
if ( ! amountEl || ! out ) { return; }
|
|
var amt = toMinor( amountEl.value );
|
|
out.textContent = '';
|
|
var lines = [];
|
|
if ( amt === null ) {
|
|
lines.push( 'Enter an amount like 25.00' );
|
|
} else {
|
|
lines.push( 'A ' + money( amt ) + ' donation:' );
|
|
cfg.rules.forEach( function ( r ) { var l = effect( r, amt ); if ( l ) { lines.push( l ); } } );
|
|
if ( ! cfg.rules.length ) { lines.push( 'No rules are on, so it only fills the goal bar.' ); }
|
|
}
|
|
lines.forEach( function ( l ) { var li = document.createElement( 'li' ); li.textContent = l; out.appendChild( li ); } );
|
|
}
|
|
if ( amountEl ) { amountEl.addEventListener( 'input', renderPreview ); renderPreview(); }
|
|
|
|
/* Type switch + preview of the rule being edited. */
|
|
if ( ! form ) { return; }
|
|
|
|
var typeSel = document.getElementById( 'sg-rule-type' );
|
|
var typeHidden = form.querySelector( 'input[name="type"]' );
|
|
var formPrev = document.getElementById( 'sg-form-preview' );
|
|
|
|
function currentType() { return typeSel ? typeSel.value : ( typeHidden ? typeHidden.value : 'add_time' ); }
|
|
function val( group, f ) {
|
|
var el = group.querySelector( '[data-f="' + f + '"]' );
|
|
if ( ! el ) { return ''; }
|
|
return el.type === 'checkbox' ? el.checked : el.value;
|
|
}
|
|
function buildRule( type ) {
|
|
var g = form.querySelector( '.sg-rule-group[data-type="' + type + '"]' );
|
|
if ( ! g ) { return null; }
|
|
var unit = toMinor( val( g, 'unit_amount' ) );
|
|
if ( type === 'add_time' ) {
|
|
var mult = { seconds: 1, minutes: 60, hours: 3600 }[ val( g, 'time_unit' ) ] || 60;
|
|
var n = parseInt( val( g, 'time_amount' ), 10 );
|
|
if ( ! unit || ! n ) { return null; }
|
|
var cap = parseInt( val( g, 'cap_minutes' ), 10 );
|
|
var clock = parseInt( val( g, 'max_timer_minutes' ), 10 );
|
|
return { type: type, config: { seconds_per_unit: n * mult, unit_minor: unit, cap_seconds_per_donation: cap > 0 ? cap * 60 : null, max_timer_seconds: clock > 0 ? clock * 60 : null } };
|
|
}
|
|
if ( type === 'counter' ) {
|
|
var step = parseInt( val( g, 'step' ), 10 );
|
|
if ( ! unit || ! step ) { return null; }
|
|
return { type: type, config: { step: step, per_minor: unit, label: val( g, 'label' ) } };
|
|
}
|
|
var min = toMinor( val( g, 'min_amount' ) );
|
|
if ( ! min ) { return null; }
|
|
return { type: type, config: { min_minor: min, event: val( g, 'event' ) || 'big_donation' } };
|
|
}
|
|
function update() {
|
|
var type = currentType();
|
|
Array.prototype.forEach.call( form.querySelectorAll( '.sg-rule-group' ), function ( g ) {
|
|
g.hidden = g.getAttribute( 'data-type' ) !== type;
|
|
} );
|
|
if ( ! formPrev ) { return; }
|
|
var amt = amountEl ? toMinor( amountEl.value ) : null;
|
|
var rule = buildRule( type );
|
|
var line = ( rule && amt !== null ) ? effect( rule, amt ) : null;
|
|
formPrev.textContent = line ? 'With this rule, a ' + money( amt ) + ' donation gives: ' + line : '';
|
|
}
|
|
form.addEventListener( 'input', update );
|
|
form.addEventListener( 'change', update );
|
|
if ( amountEl ) { amountEl.addEventListener( 'input', update ); }
|
|
update();
|
|
}() );
|