| 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 |
var DV = { |
| 26 |
totalFat: 78, saturatedFat: 20, cholesterol: 300, |
| 27 |
sodium: 2300, totalCarbs: 275, dietaryFiber: 28, |
| 28 |
addedSugars: 50, protein: 50, |
| 29 |
vitaminD: 20, calcium: 1300, iron: 18, potassium: 4700 |
| 30 |
}; |
| 31 |
|
| 32 |
function dvPct(val, key) { |
| 33 |
if (!DV[key] || !val) return null; |
| 34 |
return Math.round((val / DV[key]) * 100); |
| 35 |
} |
| 36 |
|
| 37 |
function esc(s) { |
| 38 |
return String(s) |
| 39 |
.replace(/&/g, '&') |
| 40 |
.replace(/</g, '<') |
| 41 |
.replace(/>/g, '>') |
| 42 |
.replace(/"/g, '"'); |
| 43 |
} |
| 44 |
|
| 45 |
function row(label, val, unit, key, bold, indent) { |
| 46 |
var dv = key ? dvPct(val, key) : null; |
| 47 |
var indentClass = indent === 2 ? ' bkbg-nl-indent2' : indent ? ' bkbg-nl-indent' : ''; |
| 48 |
return '<div class="bkbg-nl-row' + (bold ? ' bkbg-nl-bold' : '') + indentClass + '">' + |
| 49 |
'<span class="bkbg-nl-nutrient">' + |
| 50 |
'<' + (bold ? 'strong' : 'span') + '>' + esc(label) + '</' + (bold ? 'strong' : 'span') + '> ' + |
| 51 |
esc(val) + esc(unit) + |
| 52 |
'</span>' + |
| 53 |
(dv !== null ? '<span class="bkbg-nl-dv"><strong>' + dv + '%</strong></span>' : '') + |
| 54 |
'</div>'; |
| 55 |
} |
| 56 |
|
| 57 |
function buildApp(app) { |
| 58 |
var opts; |
| 59 |
try { opts = JSON.parse(app.getAttribute('data-opts') || '{}'); } catch (e) { return; } |
| 60 |
var o = opts; |
| 61 |
|
| 62 |
var containerStyle = [ |
| 63 |
'padding-top:' + (o.paddingTop || 40) + 'px', |
| 64 |
'padding-bottom:' + (o.paddingBottom || 40) + 'px', |
| 65 |
o.containerBg ? 'background:' + o.containerBg : '', |
| 66 |
'display:flex', |
| 67 |
'justify-content:center' |
| 68 |
].filter(Boolean).join(';'); |
| 69 |
|
| 70 |
var labelStyle = [ |
| 71 |
'font-family:Arial,Helvetica,sans-serif', |
| 72 |
'font-size:' + (o.fontSize || 14) + 'px', |
| 73 |
'background:' + (o.bgColor || '#fff'), |
| 74 |
'color:' + (o.accentColor || '#000'), |
| 75 |
'border:' + (o.borderWidth || 3) + 'px solid ' + (o.borderColor || '#000'), |
| 76 |
'border-radius:' + (o.borderRadius || 0) + 'px', |
| 77 |
'width:' + (o.labelWidth || 340) + 'px', |
| 78 |
'max-width:100%', |
| 79 |
'padding:8px 8px 4px', |
| 80 |
'box-sizing:border-box' |
| 81 |
].join(';'); |
| 82 |
|
| 83 |
var fs = o.fontSize || 14; |
| 84 |
var bc = o.borderColor || '#000'; |
| 85 |
|
| 86 |
var html = '<div style="' + containerStyle + '"><div class="bkbg-nl-label" style="' + labelStyle + '">'; |
| 87 |
|
| 88 |
/* header */ |
| 89 |
html += '<div class="bkbg-nl-header">'; |
| 90 |
html += '<div class="bkbg-nl-title">Nutrition Facts</div>'; |
| 91 |
if (o.foodName) html += '<div style="font-size:' + (fs * 0.9) + 'px;margin-top:2px;">' + esc(o.foodName) + '</div>'; |
| 92 |
html += '<div style="border-top:4px solid ' + bc + ';border-bottom:4px solid ' + bc + ';padding:2px 0;margin:4px 0;font-size:' + (fs * 0.85) + 'px">'; |
| 93 |
html += '<div>' + esc(o.servingsPerContainer || '2') + ' servings per container</div>'; |
| 94 |
html += '<div style="display:flex;justify-content:space-between;font-weight:700"><span>Serving size</span><span>' + esc(o.servingSize || '1 cup') + '</span></div>'; |
| 95 |
html += '</div></div>'; |
| 96 |
|
| 97 |
/* calories */ |
| 98 |
html += '<div style="border-bottom:8px solid ' + bc + ';padding-bottom:4px">'; |
| 99 |
html += '<div style="font-size:' + (fs * 0.75) + 'px;font-weight:700">Amount per serving</div>'; |
| 100 |
html += '<div style="display:flex;justify-content:space-between;align-items:flex-end">'; |
| 101 |
html += '<div style="font-size:' + (fs * 1.1) + 'px;font-weight:700">Calories</div>'; |
| 102 |
html += '<div style="font-size:' + (fs * 2.8) + 'px;font-weight:900;line-height:1;color:' + (o.calorieColor || '#000') + '">' + esc(o.calories) + '</div>'; |
| 103 |
html += '</div></div>'; |
| 104 |
|
| 105 |
/* %DV header */ |
| 106 |
html += '<div style="text-align:right;font-size:' + (fs * 0.75) + 'px;border-bottom:1px solid ' + bc + ';padding-bottom:2px"><strong>% Daily Value*</strong></div>'; |
| 107 |
|
| 108 |
/* fats */ |
| 109 |
html += row('Total Fat', o.totalFat, 'g', 'totalFat', true, false); |
| 110 |
html += row('Saturated Fat', o.saturatedFat, 'g', 'saturatedFat', false, true); |
| 111 |
html += '<div class="bkbg-nl-row bkbg-nl-indent"><span class="bkbg-nl-nutrient"><em>Trans</em> Fat ' + esc(o.transFat) + 'g</span></div>'; |
| 112 |
if (o.showPolyMono) { |
| 113 |
html += row('Polyunsaturated Fat', o.polyFat, 'g', null, false, true); |
| 114 |
html += row('Monounsaturated Fat', o.monoFat, 'g', null, false, true); |
| 115 |
} |
| 116 |
|
| 117 |
/* cholesterol / sodium */ |
| 118 |
html += row('Cholesterol', o.cholesterol, 'mg', 'cholesterol', true, false); |
| 119 |
html += row('Sodium', o.sodium, 'mg', 'sodium', true, false); |
| 120 |
|
| 121 |
/* carbs */ |
| 122 |
html += row('Total Carbohydrate', o.totalCarbs, 'g', 'totalCarbs', true, false); |
| 123 |
html += row('Dietary Fiber', o.dietaryFiber, 'g', 'dietaryFiber', false, true); |
| 124 |
html += '<div class="bkbg-nl-row bkbg-nl-indent"><span class="bkbg-nl-nutrient">Total Sugars ' + esc(o.totalSugars) + 'g</span></div>'; |
| 125 |
if (o.showAddedSugars !== false) { |
| 126 |
var addedDv = dvPct(o.addedSugars, 'addedSugars'); |
| 127 |
html += '<div class="bkbg-nl-row bkbg-nl-indent2"><span class="bkbg-nl-nutrient">Includes ' + esc(o.addedSugars) + 'g Added Sugars</span>'; |
| 128 |
if (addedDv !== null) html += '<span class="bkbg-nl-dv" style="color:' + (o.dvColor || '#000') + '"><strong>' + addedDv + '%</strong></span>'; |
| 129 |
html += '</div>'; |
| 130 |
} |
| 131 |
|
| 132 |
/* protein */ |
| 133 |
html += '<div class="bkbg-nl-row" style="border-top:4px solid ' + bc + '"><span class="bkbg-nl-nutrient"><strong>Protein </strong>' + esc(o.protein) + 'g</span></div>'; |
| 134 |
|
| 135 |
/* vitamins */ |
| 136 |
if (o.showVitamins !== false) { |
| 137 |
html += '<div style="border-top:8px solid ' + bc + '"></div>'; |
| 138 |
html += '<div style="display:grid;grid-template-columns:1fr 1fr;gap:2px 8px;padding:4px 0;font-size:' + (fs * 0.85) + 'px">'; |
| 139 |
html += '<span>Vitamin D ' + esc(o.vitaminD) + 'mcg</span><span>' + (dvPct(o.vitaminD, 'vitaminD') || 0) + '%</span>'; |
| 140 |
html += '<span>Calcium ' + esc(o.calcium) + 'mg</span><span>' + (dvPct(o.calcium, 'calcium') || 0) + '%</span>'; |
| 141 |
html += '<span>Iron ' + esc(o.iron) + 'mg</span><span>' + (dvPct(o.iron, 'iron') || 0) + '%</span>'; |
| 142 |
html += '<span>Potassium ' + esc(o.potassium) + 'mg</span><span>' + (dvPct(o.potassium, 'potassium') || 0) + '%</span>'; |
| 143 |
html += '</div>'; |
| 144 |
} |
| 145 |
|
| 146 |
/* footnote */ |
| 147 |
if (o.showFootnote !== false) { |
| 148 |
html += '<div class="bkbg-nl-footnote" style="border-top:4px solid ' + bc + ';font-size:' + (fs * 0.72) + 'px;padding-top:4px">'; |
| 149 |
html += '* The % Daily Value (DV) tells you how much a nutrient in a serving of food contributes to a daily diet. ' + esc(o.dvRef || '2000') + ' calories a day is used for general nutrition advice.'; |
| 150 |
html += '</div>'; |
| 151 |
} |
| 152 |
|
| 153 |
html += '</div></div>'; |
| 154 |
|
| 155 |
app.innerHTML = html; |
| 156 |
|
| 157 |
/* Typography CSS vars — set on outer container so they cascade */ |
| 158 |
var wrap = app.querySelector('.bkbg-nl-label') || app; |
| 159 |
typoCssVarsForEl(wrap, o.titleTypo, '--bkbg-nutl-tt-'); |
| 160 |
} |
| 161 |
|
| 162 |
document.querySelectorAll('.bkbg-nl-app').forEach(buildApp); |
| 163 |
})(); |
| 164 |
|