| 1 |
(function () { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
var _typoKeys = { |
| 5 |
family:'font-family', weight:'font-weight', style:'font-style', |
| 6 |
decoration:'text-decoration', transform:'text-transform', |
| 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 |
}; |
| 12 |
function typoCssVarsForEl(el, obj, prefix) { |
| 13 |
if (!obj || typeof obj !== 'object') return; |
| 14 |
Object.keys(_typoKeys).forEach(function (k) { |
| 15 |
var v = obj[k]; |
| 16 |
if (v === undefined || v === '' || v === null) return; |
| 17 |
if (k === 'sizeDesktop' || k === 'sizeTablet' || k === 'sizeMobile') v = v + (obj.sizeUnit || 'px'); |
| 18 |
else if (k === 'lineHeightDesktop' || k === 'lineHeightTablet' || k === 'lineHeightMobile') v = v + (obj.lineHeightUnit || ''); |
| 19 |
else if (k === 'letterSpacingDesktop' || k === 'letterSpacingTablet' || k === 'letterSpacingMobile') v = v + (obj.letterSpacingUnit || 'px'); |
| 20 |
else if (k === 'wordSpacingDesktop' || k === 'wordSpacingTablet' || k === 'wordSpacingMobile') v = v + (obj.wordSpacingUnit || 'px'); |
| 21 |
el.style.setProperty(prefix + _typoKeys[k], String(v)); |
| 22 |
}); |
| 23 |
} |
| 24 |
|
| 25 |
function fmtMoney(n, currency) { |
| 26 |
var abs = Math.abs(n); |
| 27 |
var str; |
| 28 |
if (abs >= 1e9) str = (abs / 1e9).toFixed(2) + 'B'; |
| 29 |
else if (abs >= 1e6) str = (abs / 1e6).toFixed(2) + 'M'; |
| 30 |
else if (abs >= 1e3) str = abs.toLocaleString(undefined, { minimumFractionDigits: 0, maximumFractionDigits: 0 }); |
| 31 |
else str = abs.toFixed(2); |
| 32 |
return (n < 0 ? '-' : '') + currency + str; |
| 33 |
} |
| 34 |
|
| 35 |
var ASSET_CATS = [ |
| 36 |
{ key: 'cash', label: 'Cash & Bank', icon: '🏦', colorKey: 'cashColor' }, |
| 37 |
{ key: 'invest', label: 'Investments', icon: '📈', colorKey: 'investColor' }, |
| 38 |
{ key: 'realestate', label: 'Real Estate', icon: '🏠', colorKey: 'realEstateColor'}, |
| 39 |
{ key: 'vehicles', label: 'Vehicles', icon: '🚗', colorKey: 'vehicleColor' }, |
| 40 |
{ key: 'otherasset', label: 'Other Assets', icon: '💎', colorKey: 'otherAssetColor'} |
| 41 |
]; |
| 42 |
var LIAB_CATS = [ |
| 43 |
{ key: 'mortgage', label: 'Mortgage', icon: '🏡', color: '#ef4444' }, |
| 44 |
{ key: 'carloan', label: 'Car Loans', icon: '🚙', color: '#f97316' }, |
| 45 |
{ key: 'creditcard', label: 'Credit Cards', icon: '💳', color: '#ec4899' }, |
| 46 |
{ key: 'studentloan', label: 'Student Loans', icon: '🎓', color: '#8b5cf6' }, |
| 47 |
{ key: 'otherdebt', label: 'Other Debts', icon: '📋', color: '#9ca3af' } |
| 48 |
]; |
| 49 |
|
| 50 |
function initApp(app) { |
| 51 |
var opts; |
| 52 |
try { opts = JSON.parse(app.getAttribute('data-opts') || '{}'); } catch (e) { return; } |
| 53 |
|
| 54 |
var currency = opts.currency || '$'; |
| 55 |
var assetC = opts.assetColor || '#10b981'; |
| 56 |
var liabC = opts.liabilityColor || '#ef4444'; |
| 57 |
var netPosC = opts.netPositiveColor || '#10b981'; |
| 58 |
var netNegC = opts.netNegativeColor || '#ef4444'; |
| 59 |
var cardR = (opts.cardRadius || 16) + 'px'; |
| 60 |
var panelR = (opts.panelRadius || 12) + 'px'; |
| 61 |
var inpR = (opts.inputRadius || 8) + 'px'; |
| 62 |
var maxW = (opts.maxWidth || 720) + 'px'; |
| 63 |
var lblClr = opts.labelColor || '#374151'; |
| 64 |
|
| 65 |
app.style.paddingTop = (opts.paddingTop || 60) + 'px'; |
| 66 |
app.style.paddingBottom = (opts.paddingBottom || 60) + 'px'; |
| 67 |
if (opts.sectionBg) app.style.background = opts.sectionBg; |
| 68 |
|
| 69 |
var card = document.createElement('div'); |
| 70 |
card.className = 'bkbg-nwc-card'; |
| 71 |
Object.assign(card.style, { background: opts.cardBg || '#fff', borderRadius: cardR, maxWidth: maxW }); |
| 72 |
app.appendChild(card); |
| 73 |
|
| 74 |
if (opts.showTitle && opts.title) { |
| 75 |
var ttl = document.createElement('div'); ttl.className = 'bkbg-nwc-title'; |
| 76 |
ttl.textContent = opts.title; |
| 77 |
if (opts.titleColor) ttl.style.color = opts.titleColor; |
| 78 |
card.appendChild(ttl); |
| 79 |
} |
| 80 |
if (opts.showSubtitle && opts.subtitle) { |
| 81 |
var sub = document.createElement('div'); sub.className = 'bkbg-nwc-subtitle'; |
| 82 |
sub.textContent = opts.subtitle; |
| 83 |
if (opts.subtitleColor) sub.style.color = opts.subtitleColor; |
| 84 |
card.appendChild(sub); |
| 85 |
} |
| 86 |
|
| 87 |
typoCssVarsForEl(app.parentNode, opts.titleTypo, '--bkbg-nwc-tt-'); |
| 88 |
typoCssVarsForEl(app.parentNode, opts.subtitleTypo, '--bkbg-nwc-st-'); |
| 89 |
|
| 90 |
/* State */ |
| 91 |
var vals = {}; |
| 92 |
ASSET_CATS.forEach(function (c) { vals[c.key] = 0; }); |
| 93 |
LIAB_CATS.forEach(function (c) { vals[c.key] = 0; }); |
| 94 |
|
| 95 |
/* Two-column layout */ |
| 96 |
var cols = document.createElement('div'); cols.className = 'bkbg-nwc-columns'; card.appendChild(cols); |
| 97 |
|
| 98 |
function mkPanel(title, icon, borderColor, categories) { |
| 99 |
var panel = document.createElement('div'); panel.className = 'bkbg-nwc-panel'; |
| 100 |
panel.style.background = opts.panelBg || '#f9fafb'; |
| 101 |
panel.style.borderRadius = panelR; |
| 102 |
panel.style.borderTopColor = borderColor; |
| 103 |
var pt = document.createElement('div'); pt.className = 'bkbg-nwc-panel-title'; pt.style.color = borderColor; |
| 104 |
pt.textContent = icon + ' ' + title; panel.appendChild(pt); |
| 105 |
|
| 106 |
categories.forEach(function (cat) { |
| 107 |
var catColor = opts[cat.colorKey] || cat.color || borderColor; |
| 108 |
var row = document.createElement('div'); row.className = 'bkbg-nwc-row'; |
| 109 |
var lbl = document.createElement('label'); lbl.className = 'bkbg-nwc-lbl'; lbl.style.color = lblClr; |
| 110 |
var dot = document.createElement('span'); dot.className = 'bkbg-nwc-cat-dot'; dot.style.background = catColor; |
| 111 |
lbl.appendChild(dot); lbl.appendChild(document.createTextNode(cat.icon + ' ' + cat.label)); |
| 112 |
var wrap = document.createElement('div'); wrap.className = 'bkbg-nwc-input-wrap'; |
| 113 |
var pfx = document.createElement('span'); pfx.className = 'bkbg-nwc-prefix'; pfx.textContent = currency; |
| 114 |
var inp = document.createElement('input'); inp.type = 'number'; inp.className = 'bkbg-nwc-input'; |
| 115 |
inp.min = '0'; inp.step = '100'; inp.value = '0'; inp.placeholder = '0'; |
| 116 |
inp.style.borderRadius = inpR; inp.style.background = opts.inputBg || '#fff'; inp.style.borderColor = opts.inputBorder || '#d1d5db'; |
| 117 |
inp.addEventListener('input', function () { |
| 118 |
vals[cat.key] = parseFloat(inp.value) || 0; |
| 119 |
recalc(); |
| 120 |
}); |
| 121 |
wrap.appendChild(pfx); wrap.appendChild(inp); |
| 122 |
row.appendChild(lbl); row.appendChild(wrap); panel.appendChild(row); |
| 123 |
}); |
| 124 |
return panel; |
| 125 |
} |
| 126 |
|
| 127 |
cols.appendChild(mkPanel('Assets', '💰', assetC, ASSET_CATS)); |
| 128 |
cols.appendChild(mkPanel('Liabilities', '💳', liabC, LIAB_CATS)); |
| 129 |
|
| 130 |
/* Result */ |
| 131 |
var resultEl = document.createElement('div'); resultEl.className = 'bkbg-nwc-result'; card.appendChild(resultEl); |
| 132 |
|
| 133 |
/* Chart */ |
| 134 |
var chartEl = document.createElement('div'); chartEl.className = 'bkbg-nwc-chart'; |
| 135 |
if (opts.showChart !== false) card.appendChild(chartEl); |
| 136 |
|
| 137 |
/* Breakdown */ |
| 138 |
var sep = document.createElement('div'); sep.className = 'bkbg-nwc-sep'; card.appendChild(sep); |
| 139 |
var breakdownEl = document.createElement('div'); breakdownEl.className = 'bkbg-nwc-breakdown'; |
| 140 |
if (opts.showBreakdown !== false) card.appendChild(breakdownEl); |
| 141 |
|
| 142 |
function recalc() { |
| 143 |
var totalAssets = 0; ASSET_CATS.forEach(function (c) { totalAssets += vals[c.key]; }); |
| 144 |
var totalLiabs = 0; LIAB_CATS.forEach(function (c) { totalLiabs += vals[c.key]; }); |
| 145 |
var net = totalAssets - totalLiabs; |
| 146 |
var isPos = net >= 0; |
| 147 |
var netC = isPos ? netPosC : netNegC; |
| 148 |
|
| 149 |
/* Result box */ |
| 150 |
resultEl.innerHTML = ''; |
| 151 |
resultEl.style.background = netC + '14'; |
| 152 |
resultEl.style.borderColor = netC + '44'; |
| 153 |
var rl = document.createElement('div'); rl.className = 'bkbg-nwc-result-label'; rl.style.color = netC; rl.textContent = 'Net Worth'; |
| 154 |
var rv = document.createElement('div'); rv.className = 'bkbg-nwc-result-val'; rv.style.color = netC; rv.textContent = fmtMoney(net, currency); |
| 155 |
var rs = document.createElement('div'); rs.className = 'bkbg-nwc-result-sub'; |
| 156 |
rs.textContent = 'Assets: ' + fmtMoney(totalAssets, currency) + ' − Liabilities: ' + fmtMoney(totalLiabs, currency); |
| 157 |
resultEl.appendChild(rl); resultEl.appendChild(rv); resultEl.appendChild(rs); |
| 158 |
|
| 159 |
/* Chart */ |
| 160 |
if (opts.showChart !== false) { |
| 161 |
chartEl.innerHTML = ''; |
| 162 |
var ct = document.createElement('div'); ct.className = 'bkbg-nwc-chart-title'; ct.textContent = 'Assets vs Liabilities'; chartEl.appendChild(ct); |
| 163 |
var total = totalAssets + totalLiabs || 1; |
| 164 |
var bars = document.createElement('div'); bars.className = 'bkbg-nwc-bars'; |
| 165 |
var ba = document.createElement('div'); ba.className = 'bkbg-nwc-bar-asset'; |
| 166 |
ba.style.width = ((totalAssets / total) * 100) + '%'; ba.style.background = assetC; |
| 167 |
ba.title = 'Assets: ' + fmtMoney(totalAssets, currency); |
| 168 |
var bl = document.createElement('div'); bl.className = 'bkbg-nwc-bar-liability'; |
| 169 |
bl.style.width = ((totalLiabs / total) * 100) + '%'; bl.style.background = liabC; |
| 170 |
bl.title = 'Liabilities: ' + fmtMoney(totalLiabs, currency); |
| 171 |
bars.appendChild(ba); bars.appendChild(bl); chartEl.appendChild(bars); |
| 172 |
var bls = document.createElement('div'); bls.className = 'bkbg-nwc-bar-labels'; |
| 173 |
var la = document.createElement('span'); la.style.color = assetC; la.textContent = '● Assets ' + fmtMoney(totalAssets, currency); |
| 174 |
var ll = document.createElement('span'); ll.style.color = liabC; ll.textContent = '● Liabilities ' + fmtMoney(totalLiabs, currency); |
| 175 |
bls.appendChild(la); bls.appendChild(ll); chartEl.appendChild(bls); |
| 176 |
} |
| 177 |
|
| 178 |
/* Breakdown */ |
| 179 |
if (opts.showBreakdown !== false) { |
| 180 |
breakdownEl.innerHTML = ''; |
| 181 |
function mkBreakdownSection(title, cats, colorFn) { |
| 182 |
var sec = document.createElement('div'); sec.className = 'bkbg-nwc-breakdown-section'; |
| 183 |
var st = document.createElement('div'); st.className = 'bkbg-nwc-breakdown-title'; st.textContent = title; sec.appendChild(st); |
| 184 |
cats.forEach(function (cat) { |
| 185 |
var v = vals[cat.key]; if (v === 0) return; |
| 186 |
var r = document.createElement('div'); r.className = 'bkbg-nwc-breakdown-row'; |
| 187 |
var k = document.createElement('div'); k.className = 'bkbg-nwc-breakdown-key'; |
| 188 |
var dot = document.createElement('span'); dot.className = 'bkbg-nwc-cat-dot'; |
| 189 |
dot.style.background = colorFn(cat); |
| 190 |
k.appendChild(dot); k.appendChild(document.createTextNode(cat.icon + ' ' + cat.label)); |
| 191 |
var vEl = document.createElement('div'); vEl.className = 'bkbg-nwc-breakdown-val'; |
| 192 |
vEl.style.color = colorFn(cat); vEl.textContent = fmtMoney(v, currency); |
| 193 |
r.appendChild(k); r.appendChild(vEl); sec.appendChild(r); |
| 194 |
}); |
| 195 |
return sec; |
| 196 |
} |
| 197 |
breakdownEl.appendChild(mkBreakdownSection( |
| 198 |
'💰 Asset Breakdown', ASSET_CATS, |
| 199 |
function (c) { return opts[c.colorKey] || '#10b981'; } |
| 200 |
)); |
| 201 |
breakdownEl.appendChild(mkBreakdownSection( |
| 202 |
'💳 Liability Breakdown', LIAB_CATS, |
| 203 |
function (c) { return c.color || liabC; } |
| 204 |
)); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
recalc(); |
| 209 |
} |
| 210 |
|
| 211 |
document.querySelectorAll('.bkbg-nwc-app').forEach(initApp); |
| 212 |
})(); |
| 213 |
|