| 1 |
(function () { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
function typoCssVarsForEl(typo, prefix, el) { |
| 5 |
if (!typo || typeof typo !== 'object') return; |
| 6 |
var m = { family:'font-family', weight:'font-weight', transform:'text-transform', style:'font-style', decoration:'text-decoration', |
| 7 |
sizeDesktop:'font-size-d', sizeTablet:'font-size-t', sizeMobile:'font-size-m', |
| 8 |
lineHeightDesktop:'line-height-d', lineHeightTablet:'line-height-t', lineHeightMobile:'line-height-m', |
| 9 |
letterSpacingDesktop:'letter-spacing-d', letterSpacingTablet:'letter-spacing-t', letterSpacingMobile:'letter-spacing-m', |
| 10 |
wordSpacingDesktop:'word-spacing-d', wordSpacingTablet:'word-spacing-t', wordSpacingMobile:'word-spacing-m' }; |
| 11 |
Object.keys(m).forEach(function (k) { |
| 12 |
if (typo[k] !== undefined && typo[k] !== '') { |
| 13 |
var v = typo[k], u = typo[k + 'Unit'] || ''; |
| 14 |
if (/Desktop|Tablet|Mobile/.test(k) && typeof v === 'number') v = v + (u || 'px'); |
| 15 |
el.style.setProperty(prefix + m[k], '' + v); |
| 16 |
} |
| 17 |
}); |
| 18 |
} |
| 19 |
|
| 20 |
function rollDie(sides) { |
| 21 |
return Math.floor(Math.random() * sides) + 1; |
| 22 |
} |
| 23 |
|
| 24 |
function parseSides(die) { |
| 25 |
// d100 = 1-100, d10 special case already covered |
| 26 |
return parseInt(die.replace('d', ''), 10) || 6; |
| 27 |
} |
| 28 |
|
| 29 |
function buildBlock(root) { |
| 30 |
var o; |
| 31 |
try { o = JSON.parse(root.getAttribute('data-opts')); } catch (e) { return; } |
| 32 |
|
| 33 |
// CSS vars |
| 34 |
root.style.setProperty('--dr-accent', o.accentColor || '#dc2626'); |
| 35 |
root.style.setProperty('--dr-die-bg', o.dieColor || '#1e1b4b'); |
| 36 |
root.style.setProperty('--dr-die-face', o.dieFace || '#ffffff'); |
| 37 |
root.style.setProperty('--dr-total-bg', o.totalBg || '#fef2f2'); |
| 38 |
root.style.setProperty('--dr-total-color', o.totalColor || '#dc2626'); |
| 39 |
root.style.setProperty('--dr-title-color', o.titleColor || '#111827'); |
| 40 |
root.style.setProperty('--dr-subtitle-color',o.subtitleColor || '#6b7280'); |
| 41 |
root.style.setProperty('--dr-label-color', o.labelColor || '#374151'); |
| 42 |
root.style.setProperty('--dr-max-width', (o.contentMaxWidth||580)+'px'); |
| 43 |
root.style.setProperty('--bkbg-dr-ttl-fs', (o.titleFontSize ||26)+'px'); |
| 44 |
root.style.setProperty('--bkbg-dr-sub-fs', (o.subtitleFontSize||14)+'px'); |
| 45 |
if (o.typoTitle) typoCssVarsForEl(o.typoTitle, '--bkbg-dr-ttl-', root); |
| 46 |
if (o.typoSubtitle) typoCssVarsForEl(o.typoSubtitle, '--bkbg-dr-sub-', root); |
| 47 |
|
| 48 |
var DICE_TYPES = ['d4','d6','d8','d10','d12','d20','d100']; |
| 49 |
|
| 50 |
// State |
| 51 |
var currentDie = o.defaultDie || 'd6'; |
| 52 |
var currentCount = o.defaultCount || 2; |
| 53 |
var modifier = 0; |
| 54 |
var history = []; |
| 55 |
var maxHistory = o.historySize || 8; |
| 56 |
|
| 57 |
// Build wrap |
| 58 |
var wrap = document.createElement('div'); |
| 59 |
wrap.className = 'bkbg-dr-wrap'; |
| 60 |
wrap.style.background = o.sectionBg || '#fff1f2'; |
| 61 |
root.appendChild(wrap); |
| 62 |
|
| 63 |
if (o.showTitle && o.title) { |
| 64 |
var h = document.createElement('h3'); |
| 65 |
h.className = 'bkbg-dr-title'; |
| 66 |
h.textContent = o.title; |
| 67 |
wrap.appendChild(h); |
| 68 |
} |
| 69 |
if (o.showSubtitle && o.subtitle) { |
| 70 |
var sub = document.createElement('p'); |
| 71 |
sub.className = 'bkbg-dr-subtitle'; |
| 72 |
sub.textContent = o.subtitle; |
| 73 |
wrap.appendChild(sub); |
| 74 |
} |
| 75 |
|
| 76 |
// Controls |
| 77 |
var controls = document.createElement('div'); |
| 78 |
controls.className = 'bkbg-dr-controls'; |
| 79 |
wrap.appendChild(controls); |
| 80 |
|
| 81 |
// Die selector |
| 82 |
var dieGroup = document.createElement('div'); |
| 83 |
dieGroup.className = 'bkbg-dr-ctrl-group'; |
| 84 |
var dieLbl = document.createElement('div'); |
| 85 |
dieLbl.className = 'bkbg-dr-ctrl-label'; |
| 86 |
dieLbl.textContent = 'Die Type'; |
| 87 |
var dieSelect = document.createElement('select'); |
| 88 |
dieSelect.className = 'bkbg-dr-select'; |
| 89 |
DICE_TYPES.forEach(function (d) { |
| 90 |
var opt = document.createElement('option'); |
| 91 |
opt.value = d; opt.textContent = d; |
| 92 |
if (d === currentDie) opt.selected = true; |
| 93 |
dieSelect.appendChild(opt); |
| 94 |
}); |
| 95 |
dieSelect.addEventListener('change', function () { currentDie = dieSelect.value; updateRollBtn(); }); |
| 96 |
dieGroup.appendChild(dieLbl); |
| 97 |
dieGroup.appendChild(dieSelect); |
| 98 |
controls.appendChild(dieGroup); |
| 99 |
|
| 100 |
// Count control |
| 101 |
var cntGroup = document.createElement('div'); |
| 102 |
cntGroup.className = 'bkbg-dr-ctrl-group'; |
| 103 |
var cntLbl = document.createElement('div'); |
| 104 |
cntLbl.className = 'bkbg-dr-ctrl-label'; |
| 105 |
cntLbl.textContent = 'Number'; |
| 106 |
var counter = document.createElement('div'); |
| 107 |
counter.className = 'bkbg-dr-counter'; |
| 108 |
var minusBtn = document.createElement('button'); |
| 109 |
minusBtn.className = 'bkbg-dr-counter-btn'; |
| 110 |
minusBtn.textContent = '−'; |
| 111 |
var countVal = document.createElement('span'); |
| 112 |
countVal.className = 'bkbg-dr-counter-val'; |
| 113 |
countVal.textContent = currentCount; |
| 114 |
var plusBtn = document.createElement('button'); |
| 115 |
plusBtn.className = 'bkbg-dr-counter-btn'; |
| 116 |
plusBtn.textContent = '+'; |
| 117 |
minusBtn.addEventListener('click', function () { |
| 118 |
if (currentCount > 1) { currentCount--; countVal.textContent = currentCount; updateRollBtn(); } |
| 119 |
}); |
| 120 |
plusBtn.addEventListener('click', function () { |
| 121 |
if (currentCount < 10) { currentCount++; countVal.textContent = currentCount; updateRollBtn(); } |
| 122 |
}); |
| 123 |
counter.appendChild(minusBtn); |
| 124 |
counter.appendChild(countVal); |
| 125 |
counter.appendChild(plusBtn); |
| 126 |
cntGroup.appendChild(cntLbl); |
| 127 |
cntGroup.appendChild(counter); |
| 128 |
controls.appendChild(cntGroup); |
| 129 |
|
| 130 |
// Modifier |
| 131 |
var modInput = null; |
| 132 |
if (o.showModifier) { |
| 133 |
var modGroup = document.createElement('div'); |
| 134 |
modGroup.className = 'bkbg-dr-ctrl-group'; |
| 135 |
var modLbl = document.createElement('div'); |
| 136 |
modLbl.className = 'bkbg-dr-ctrl-label'; |
| 137 |
modLbl.textContent = 'Modifier'; |
| 138 |
modInput = document.createElement('input'); |
| 139 |
modInput.type = 'number'; |
| 140 |
modInput.className = 'bkbg-dr-mod-input'; |
| 141 |
modInput.value = '0'; |
| 142 |
modInput.addEventListener('input', function () { |
| 143 |
modifier = parseInt(modInput.value, 10) || 0; |
| 144 |
}); |
| 145 |
modGroup.appendChild(modLbl); |
| 146 |
modGroup.appendChild(modInput); |
| 147 |
controls.appendChild(modGroup); |
| 148 |
} |
| 149 |
|
| 150 |
// Roll button |
| 151 |
var rollBtn = document.createElement('button'); |
| 152 |
rollBtn.className = 'bkbg-dr-roll-btn'; |
| 153 |
wrap.appendChild(rollBtn); |
| 154 |
|
| 155 |
function updateRollBtn() { |
| 156 |
rollBtn.textContent = '🎲 Roll ' + currentCount + currentDie; |
| 157 |
} |
| 158 |
updateRollBtn(); |
| 159 |
|
| 160 |
// Dice display |
| 161 |
var diceRow = document.createElement('div'); |
| 162 |
diceRow.className = 'bkbg-dr-dice-row'; |
| 163 |
wrap.appendChild(diceRow); |
| 164 |
|
| 165 |
// Total |
| 166 |
var totalWrap = null, totalVal = null; |
| 167 |
if (o.showTotal) { |
| 168 |
totalWrap = document.createElement('div'); |
| 169 |
totalWrap.className = 'bkbg-dr-total-wrap'; |
| 170 |
var totalLabel = document.createElement('span'); |
| 171 |
totalLabel.className = 'bkbg-dr-total-label'; |
| 172 |
totalLabel.textContent = 'Total'; |
| 173 |
totalVal = document.createElement('span'); |
| 174 |
totalVal.className = 'bkbg-dr-total-val'; |
| 175 |
totalVal.textContent = '—'; |
| 176 |
totalWrap.appendChild(totalLabel); |
| 177 |
totalWrap.appendChild(totalVal); |
| 178 |
wrap.appendChild(totalWrap); |
| 179 |
} |
| 180 |
|
| 181 |
// History |
| 182 |
var histEl = null, histBody = null, histCount = null; |
| 183 |
if (o.showHistory) { |
| 184 |
histEl = document.createElement('div'); |
| 185 |
histEl.className = 'bkbg-dr-history'; |
| 186 |
histEl.style.background = o.histBg || '#f9fafb'; |
| 187 |
var histHead = document.createElement('div'); |
| 188 |
histHead.className = 'bkbg-dr-hist-head'; |
| 189 |
var histTitle = document.createElement('span'); |
| 190 |
histTitle.textContent = 'Roll History'; |
| 191 |
histCount = document.createElement('span'); |
| 192 |
histCount.textContent = '0 rolls'; |
| 193 |
histHead.appendChild(histTitle); |
| 194 |
histHead.appendChild(histCount); |
| 195 |
histBody = document.createElement('div'); |
| 196 |
histEl.appendChild(histHead); |
| 197 |
histEl.appendChild(histBody); |
| 198 |
wrap.appendChild(histEl); |
| 199 |
} |
| 200 |
|
| 201 |
// ── Perform roll ── |
| 202 |
function doRoll(savedResults) { |
| 203 |
var sides = parseSides(currentDie); |
| 204 |
var results = savedResults || Array.from({ length: currentCount }, function () { return rollDie(sides); }); |
| 205 |
var rawSum = results.reduce(function (s, v) { return s + v; }, 0); |
| 206 |
var total = rawSum + modifier; |
| 207 |
|
| 208 |
// Animate button |
| 209 |
rollBtn.classList.add('rolling'); |
| 210 |
setTimeout(function () { rollBtn.classList.remove('rolling'); }, 400); |
| 211 |
|
| 212 |
// Render dice |
| 213 |
diceRow.innerHTML = ''; |
| 214 |
results.forEach(function (val) { |
| 215 |
var die = document.createElement('div'); |
| 216 |
die.className = 'bkbg-dr-die rolling'; |
| 217 |
die.textContent = val; |
| 218 |
diceRow.appendChild(die); |
| 219 |
setTimeout(function () { die.classList.remove('rolling'); }, 400); |
| 220 |
}); |
| 221 |
|
| 222 |
// Update total |
| 223 |
if (totalVal) { |
| 224 |
var label = modifier !== 0 ? total + (modifier > 0 ? ' (+'+modifier+')' : ' ('+modifier+')') : String(total); |
| 225 |
totalVal.textContent = label; |
| 226 |
} |
| 227 |
|
| 228 |
// Update history |
| 229 |
if (!savedResults) { |
| 230 |
var entry = { |
| 231 |
label: currentCount + currentDie, |
| 232 |
results: results, |
| 233 |
total: total, |
| 234 |
modifier: modifier |
| 235 |
}; |
| 236 |
history.unshift(entry); |
| 237 |
if (history.length > maxHistory) history.pop(); |
| 238 |
renderHistory(); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
function renderHistory() { |
| 243 |
if (!histBody) return; |
| 244 |
histBody.innerHTML = ''; |
| 245 |
if (histCount) histCount.textContent = history.length + ' roll' + (history.length !== 1 ? 's' : ''); |
| 246 |
if (history.length === 0) { |
| 247 |
var empty = document.createElement('div'); |
| 248 |
empty.style.cssText = 'padding:14px;text-align:center;font-size:13px;opacity:.5;color:var(--dr-label-color,#374151)'; |
| 249 |
empty.textContent = 'No rolls yet — press Roll!'; |
| 250 |
histBody.appendChild(empty); |
| 251 |
return; |
| 252 |
} |
| 253 |
history.forEach(function (entry, idx) { |
| 254 |
var row = document.createElement('div'); |
| 255 |
row.className = 'bkbg-dr-hist-row'; |
| 256 |
var info = document.createElement('span'); |
| 257 |
var modStr = entry.modifier ? (entry.modifier > 0 ? ' +'+entry.modifier : ' '+entry.modifier) : ''; |
| 258 |
info.textContent = '#' + (history.length - idx) + ' — ' + entry.label + modStr + ' — [' + entry.results.join(', ') + '] = ' + entry.total; |
| 259 |
var rerollBtn = document.createElement('button'); |
| 260 |
rerollBtn.className = 'bkbg-dr-hist-reroll'; |
| 261 |
rerollBtn.textContent = '↺'; |
| 262 |
rerollBtn.setAttribute('title', 'Reroll same dice'); |
| 263 |
rerollBtn.addEventListener('click', function () { |
| 264 |
currentDie = entry.label.replace(/^\d+/, '') || 'd6'; |
| 265 |
currentCount = parseInt(entry.label, 10) || currentCount; |
| 266 |
modifier = entry.modifier; |
| 267 |
dieSelect.value = currentDie; |
| 268 |
countVal.textContent = currentCount; |
| 269 |
if (modInput) modInput.value = modifier; |
| 270 |
updateRollBtn(); |
| 271 |
doRoll(); |
| 272 |
}); |
| 273 |
row.appendChild(info); |
| 274 |
row.appendChild(rerollBtn); |
| 275 |
histBody.appendChild(row); |
| 276 |
}); |
| 277 |
} |
| 278 |
|
| 279 |
rollBtn.addEventListener('click', function () { doRoll(); }); |
| 280 |
renderHistory(); |
| 281 |
} |
| 282 |
|
| 283 |
document.querySelectorAll('.bkbg-dr-app').forEach(buildBlock); |
| 284 |
})(); |
| 285 |
|