| 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 buildStars(rating, best, starSize, starColor, starEmpty) { |
| 26 |
var wrap = document.createElement('div'); |
| 27 |
wrap.className = 'bkbg-rs-stars'; |
| 28 |
for (var i = 1; i <= best; i++) { |
| 29 |
var s = document.createElement('span'); |
| 30 |
s.className = 'bkbg-rs-star'; |
| 31 |
s.style.fontSize = starSize + 'px'; |
| 32 |
s.style.color = i <= Math.round(rating) ? starColor : starEmpty; |
| 33 |
s.textContent = i <= Math.round(rating) ? '� |
| 34 |
' : '☆'; |
| 35 |
wrap.appendChild(s); |
| 36 |
} |
| 37 |
return wrap; |
| 38 |
} |
| 39 |
|
| 40 |
document.querySelectorAll('.bkbg-rs-app').forEach(function (app) { |
| 41 |
var o; |
| 42 |
try { o = JSON.parse(app.dataset.opts || '{}'); } catch (e) { return; } |
| 43 |
|
| 44 |
var itemName = o.itemName || 'Our Product'; |
| 45 |
var itemType = o.itemType || 'Product'; |
| 46 |
var description = o.description || ''; |
| 47 |
var imageUrl = o.imageUrl || ''; |
| 48 |
var ratingValue = o.ratingValue || 4.8; |
| 49 |
var ratingCount = o.ratingCount || 127; |
| 50 |
var bestRating = o.bestRating || 5; |
| 51 |
var worstRating = o.worstRating || 1; |
| 52 |
var reviews = Array.isArray(o.reviews) ? o.reviews : []; |
| 53 |
var showReviews = !!o.showReviews; |
| 54 |
var showSchema = !!o.showSchema; |
| 55 |
var starSize = o.starSize || 32; |
| 56 |
var reviewColumns = o.reviewColumns || 3; |
| 57 |
var cardRadius = o.cardRadius || 16; |
| 58 |
var starColor = o.starColor || '#f59e0b'; |
| 59 |
var starEmpty = o.starEmpty || '#e2e8f0'; |
| 60 |
var bgColor = o.bgColor || '#ffffff'; |
| 61 |
var cardBg = o.cardBg || '#f8fafc'; |
| 62 |
var cardBorder = o.cardBorder || '#e2e8f0'; |
| 63 |
var headingColor = o.headingColor || '#0f172a'; |
| 64 |
var bodyColor = o.bodyColor || '#475569'; |
| 65 |
var authorColor = o.authorColor || '#64748b'; |
| 66 |
var ratingSize = o.ratingSize || 56; |
| 67 |
var sectionBg = o.sectionBg || ''; |
| 68 |
|
| 69 |
if (sectionBg) app.style.background = sectionBg; |
| 70 |
|
| 71 |
typoCssVarsForEl(app, o.headingTypo, '--bkrs-ht-'); |
| 72 |
typoCssVarsForEl(app, o.bodyTypo, '--bkrs-bt-'); |
| 73 |
|
| 74 |
var inner = document.createElement('div'); |
| 75 |
if (sectionBg) { inner.style.padding = '40px'; } |
| 76 |
|
| 77 |
/* ── Aggregate summary ── */ |
| 78 |
var summary = document.createElement('div'); |
| 79 |
summary.className = 'bkbg-rs-summary'; |
| 80 |
summary.style.background = bgColor; |
| 81 |
summary.style.borderRadius = cardRadius + 'px'; |
| 82 |
summary.style.border = '1px solid ' + cardBorder; |
| 83 |
summary.style.padding = '32px'; |
| 84 |
summary.style.marginBottom = '32px'; |
| 85 |
|
| 86 |
var nameEl = document.createElement('h3'); |
| 87 |
nameEl.className = 'bkbg-rs-item-name'; |
| 88 |
nameEl.style.color = headingColor; |
| 89 |
nameEl.textContent = itemName; |
| 90 |
summary.appendChild(nameEl); |
| 91 |
|
| 92 |
summary.appendChild(buildStars(ratingValue, bestRating, starSize, starColor, starEmpty)); |
| 93 |
|
| 94 |
var rv = document.createElement('div'); |
| 95 |
rv.className = 'bkbg-rs-rating-value'; |
| 96 |
rv.style.fontSize = ratingSize + 'px'; |
| 97 |
rv.style.color = headingColor; |
| 98 |
rv.textContent = ratingValue.toFixed(1); |
| 99 |
summary.appendChild(rv); |
| 100 |
|
| 101 |
var countEl = document.createElement('p'); |
| 102 |
countEl.className = 'bkbg-rs-count'; |
| 103 |
countEl.style.color = authorColor; |
| 104 |
countEl.textContent = 'Based on ' + ratingCount + ' reviews'; |
| 105 |
summary.appendChild(countEl); |
| 106 |
|
| 107 |
inner.appendChild(summary); |
| 108 |
|
| 109 |
/* ── Review cards ── */ |
| 110 |
if (showReviews && reviews.length > 0) { |
| 111 |
var grid = document.createElement('div'); |
| 112 |
grid.className = 'bkbg-rs-grid'; |
| 113 |
grid.style.gridTemplateColumns = 'repeat(' + reviewColumns + ', 1fr)'; |
| 114 |
|
| 115 |
reviews.forEach(function (r) { |
| 116 |
var card = document.createElement('div'); |
| 117 |
card.className = 'bkbg-rs-card'; |
| 118 |
card.style.background = cardBg; |
| 119 |
card.style.border = '1px solid ' + cardBorder; |
| 120 |
card.style.borderRadius = cardRadius + 'px'; |
| 121 |
|
| 122 |
var cardStars = buildStars(r.rating || 5, bestRating, 18, starColor, starEmpty); |
| 123 |
cardStars.className = 'bkbg-rs-card-stars'; |
| 124 |
card.appendChild(cardStars); |
| 125 |
|
| 126 |
var body = document.createElement('p'); |
| 127 |
body.className = 'bkbg-rs-body'; |
| 128 |
body.style.color = bodyColor; |
| 129 |
body.textContent = r.body || ''; |
| 130 |
card.appendChild(body); |
| 131 |
|
| 132 |
var meta = document.createElement('div'); |
| 133 |
meta.className = 'bkbg-rs-meta'; |
| 134 |
|
| 135 |
var author = document.createElement('strong'); |
| 136 |
author.className = 'bkbg-rs-author'; |
| 137 |
author.style.color = headingColor; |
| 138 |
author.textContent = r.author || ''; |
| 139 |
|
| 140 |
var date = document.createElement('span'); |
| 141 |
date.className = 'bkbg-rs-date'; |
| 142 |
date.style.color = authorColor; |
| 143 |
date.textContent = r.date || ''; |
| 144 |
|
| 145 |
meta.appendChild(author); |
| 146 |
meta.appendChild(date); |
| 147 |
card.appendChild(meta); |
| 148 |
|
| 149 |
grid.appendChild(card); |
| 150 |
}); |
| 151 |
|
| 152 |
inner.appendChild(grid); |
| 153 |
} |
| 154 |
|
| 155 |
app.appendChild(inner); |
| 156 |
|
| 157 |
/* ── JSON-LD Schema ── */ |
| 158 |
if (showSchema) { |
| 159 |
var schema = { |
| 160 |
'@context': 'https://schema.org', |
| 161 |
'@type': itemType, |
| 162 |
'name': itemName |
| 163 |
}; |
| 164 |
|
| 165 |
if (description) schema['description'] = description; |
| 166 |
if (imageUrl) schema['image'] = imageUrl; |
| 167 |
|
| 168 |
schema['aggregateRating'] = { |
| 169 |
'@type': 'AggregateRating', |
| 170 |
'ratingValue': String(ratingValue), |
| 171 |
'reviewCount': String(ratingCount), |
| 172 |
'bestRating': String(bestRating), |
| 173 |
'worstRating': String(worstRating) |
| 174 |
}; |
| 175 |
|
| 176 |
if (reviews.length > 0) { |
| 177 |
schema['review'] = reviews.map(function (r) { |
| 178 |
return { |
| 179 |
'@type': 'Review', |
| 180 |
'author': { '@type': 'Person', 'name': r.author || '' }, |
| 181 |
'datePublished': r.date || '', |
| 182 |
'reviewBody': r.body || '', |
| 183 |
'reviewRating': { |
| 184 |
'@type': 'Rating', |
| 185 |
'ratingValue': String(r.rating || 5), |
| 186 |
'bestRating': String(bestRating), |
| 187 |
'worstRating': String(worstRating) |
| 188 |
} |
| 189 |
}; |
| 190 |
}); |
| 191 |
} |
| 192 |
|
| 193 |
var script = document.createElement('script'); |
| 194 |
script.type = 'application/ld+json'; |
| 195 |
script.textContent = JSON.stringify(schema); |
| 196 |
document.head.appendChild(script); |
| 197 |
} |
| 198 |
}); |
| 199 |
}()); |
| 200 |
|