| 1 |
function typoCssVarsForEl(typo, prefix, el) { |
| 2 |
if (!typo || typeof typo !== 'object') return; |
| 3 |
var map = { |
| 4 |
family: 'font-family', weight: 'font-weight', style: 'font-style', |
| 5 |
transform: 'text-transform', decoration: 'text-decoration', |
| 6 |
sizeDesktop: 'font-size-d', sizeTablet: 'font-size-t', sizeMobile: 'font-size-m', |
| 7 |
sizeUnit: null, |
| 8 |
lineHeightDesktop: 'line-height-d', lineHeightTablet: 'line-height-t', lineHeightMobile: 'line-height-m', |
| 9 |
lineHeightUnit: null, |
| 10 |
letterSpacingDesktop: 'letter-spacing-d', letterSpacingTablet: 'letter-spacing-t', letterSpacingMobile: 'letter-spacing-m', |
| 11 |
letterSpacingUnit: null, |
| 12 |
wordSpacingDesktop: 'word-spacing-d', wordSpacingTablet: 'word-spacing-t', wordSpacingMobile: 'word-spacing-m', |
| 13 |
wordSpacingUnit: null |
| 14 |
}; |
| 15 |
var sizeU = typo.sizeUnit || 'px'; |
| 16 |
var lhU = typo.lineHeightUnit || ''; |
| 17 |
var lsU = typo.letterSpacingUnit || 'px'; |
| 18 |
var wsU = typo.wordSpacingUnit || 'px'; |
| 19 |
Object.keys(map).forEach(function (k) { |
| 20 |
var css = map[k]; if (!css) return; |
| 21 |
var v = typo[k]; if (v === undefined || v === '' || v === null) return; |
| 22 |
if (/size|spacing/i.test(k)) { |
| 23 |
var u = /letterSpacing/.test(k) ? lsU : /wordSpacing/.test(k) ? wsU : /lineHeight/.test(k) ? lhU : sizeU; |
| 24 |
v = v + u; |
| 25 |
} |
| 26 |
el.style.setProperty(prefix + css, v); |
| 27 |
}); |
| 28 |
} |
| 29 |
|
| 30 |
(function () { |
| 31 |
var SVG_NS = 'http://www.w3.org/2000/svg'; |
| 32 |
|
| 33 |
function pad2(n) { return String(Math.max(0, n)).padStart(2, '0'); } |
| 34 |
|
| 35 |
function parseEnd(endDate) { |
| 36 |
if (endDate) { |
| 37 |
var d = new Date(endDate); |
| 38 |
if (!isNaN(d)) return d; |
| 39 |
} |
| 40 |
var fallback = new Date(); |
| 41 |
fallback.setDate(fallback.getDate() + 14); |
| 42 |
return fallback; |
| 43 |
} |
| 44 |
|
| 45 |
function diffParts(end) { |
| 46 |
var now = Date.now(); |
| 47 |
var total = Math.max(0, end.getTime() - now); |
| 48 |
var days = Math.floor(total / 86400000); |
| 49 |
var hours = Math.floor((total % 86400000) / 3600000); |
| 50 |
var mins = Math.floor((total % 3600000) / 60000); |
| 51 |
var secs = Math.floor((total % 60000) / 1000); |
| 52 |
return { days: days, hours: hours, mins: mins, secs: secs, total: total }; |
| 53 |
} |
| 54 |
|
| 55 |
/* SVG ring helpers */ |
| 56 |
function makeSvgRing(radius, strokeWidth, ringBg, ringColor) { |
| 57 |
var cx = radius + strokeWidth, cy = radius + strokeWidth; |
| 58 |
var size = cx * 2; |
| 59 |
var svg = document.createElementNS(SVG_NS, 'svg'); |
| 60 |
svg.setAttribute('width', size); |
| 61 |
svg.setAttribute('height', size); |
| 62 |
svg.setAttribute('viewBox', '0 0 ' + size + ' ' + size); |
| 63 |
|
| 64 |
var track = document.createElementNS(SVG_NS, 'circle'); |
| 65 |
track.setAttribute('cx', cx); track.setAttribute('cy', cy); |
| 66 |
track.setAttribute('r', radius); |
| 67 |
track.setAttribute('stroke', ringBg); |
| 68 |
track.setAttribute('stroke-width', strokeWidth); |
| 69 |
track.setAttribute('fill', 'none'); |
| 70 |
track.setAttribute('stroke-linecap', 'round'); |
| 71 |
svg.appendChild(track); |
| 72 |
|
| 73 |
var fill = document.createElementNS(SVG_NS, 'circle'); |
| 74 |
fill.setAttribute('cx', cx); fill.setAttribute('cy', cy); |
| 75 |
fill.setAttribute('r', radius); |
| 76 |
fill.setAttribute('stroke', ringColor); |
| 77 |
fill.setAttribute('stroke-width', strokeWidth); |
| 78 |
fill.setAttribute('fill', 'none'); |
| 79 |
fill.setAttribute('stroke-linecap', 'round'); |
| 80 |
fill.style.transform = 'rotate(-90deg)'; |
| 81 |
fill.style.transformOrigin = 'center'; |
| 82 |
svg.appendChild(fill); |
| 83 |
|
| 84 |
return { svg: svg, fill: fill, circumference: 2 * Math.PI * radius }; |
| 85 |
} |
| 86 |
|
| 87 |
function updateRing(fillEl, circumference, pct) { |
| 88 |
var dash = pct * circumference; |
| 89 |
fillEl.setAttribute('stroke-dasharray', circumference); |
| 90 |
fillEl.setAttribute('stroke-dashoffset', circumference - dash); |
| 91 |
} |
| 92 |
|
| 93 |
/* Unit constructors */ |
| 94 |
function makeRingUnit(o, unit) { |
| 95 |
var wrap = document.createElement('div'); |
| 96 |
wrap.className = 'bkbg-cdp-ring-wrap'; |
| 97 |
|
| 98 |
var ring = makeSvgRing(o.radius, o.strokeWidth, o.ringBg || '#1e293b', o.ringColor || '#6366f1'); |
| 99 |
wrap.appendChild(ring.svg); |
| 100 |
|
| 101 |
var labels = document.createElement('div'); |
| 102 |
labels.className = 'bkbg-cdp-ring-labels'; |
| 103 |
labels.style.color = o.numColor || '#fff'; |
| 104 |
|
| 105 |
var numEl = document.createElement('span'); |
| 106 |
numEl.className = 'bkbg-cdp-num'; |
| 107 |
numEl.textContent = '--'; |
| 108 |
labels.appendChild(numEl); |
| 109 |
|
| 110 |
if (o.showLabels) { |
| 111 |
var unitEl = document.createElement('span'); |
| 112 |
unitEl.className = 'bkbg-cdp-unit'; |
| 113 |
unitEl.style.color = o.labelColor || '#94a3b8'; |
| 114 |
unitEl.textContent = unit; |
| 115 |
labels.appendChild(unitEl); |
| 116 |
} |
| 117 |
|
| 118 |
wrap.appendChild(labels); |
| 119 |
return { wrap: wrap, numEl: numEl, fill: ring.fill, circumference: ring.circumference }; |
| 120 |
} |
| 121 |
|
| 122 |
function makeBarUnit(o, unit) { |
| 123 |
var wrap = document.createElement('div'); |
| 124 |
wrap.className = 'bkbg-cdp-bar-unit'; |
| 125 |
|
| 126 |
var numEl = document.createElement('div'); |
| 127 |
numEl.className = 'bkbg-cdp-bar-num'; |
| 128 |
numEl.style.color = o.numColor || '#fff'; |
| 129 |
numEl.textContent = '--'; |
| 130 |
wrap.appendChild(numEl); |
| 131 |
|
| 132 |
if (o.showLabels) { |
| 133 |
var labelEl = document.createElement('div'); |
| 134 |
labelEl.className = 'bkbg-cdp-bar-label'; |
| 135 |
labelEl.style.color = o.labelColor || '#94a3b8'; |
| 136 |
labelEl.textContent = unit; |
| 137 |
wrap.appendChild(labelEl); |
| 138 |
} |
| 139 |
|
| 140 |
var track = document.createElement('div'); |
| 141 |
track.className = 'bkbg-cdp-bar-track'; |
| 142 |
track.style.background = o.barBg || o.ringBg || '#1e293b'; |
| 143 |
|
| 144 |
var fill = document.createElement('div'); |
| 145 |
fill.className = 'bkbg-cdp-bar-fill'; |
| 146 |
fill.style.background = o.barColor || o.ringColor || '#6366f1'; |
| 147 |
fill.style.width = '0%'; |
| 148 |
track.appendChild(fill); |
| 149 |
wrap.appendChild(track); |
| 150 |
|
| 151 |
return { wrap: wrap, numEl: numEl, fill: fill }; |
| 152 |
} |
| 153 |
|
| 154 |
function init() { |
| 155 |
document.querySelectorAll('.bkbg-cdp-app').forEach(function (el) { |
| 156 |
if (el.dataset.rendered) return; |
| 157 |
el.dataset.rendered = '1'; |
| 158 |
|
| 159 |
var opts; |
| 160 |
try { opts = JSON.parse(el.dataset.opts || '{}'); } catch (e) { opts = {}; } |
| 161 |
|
| 162 |
var o = Object.assign({ |
| 163 |
heading: 'Launching In', subtext: 'Don\'t miss out — get notified when we go live.', |
| 164 |
endDate: '', style: 'ring', showDays: true, showHours: true, showMins: true, showSecs: true, |
| 165 |
showLabels: true, radius: 60, strokeWidth: 10, maxWidth: 900, paddingTop: 80, paddingBottom: 80, |
| 166 |
bgColor: '#0f172a', headingColor: '#ffffff', subColor: '#94a3b8', |
| 167 |
ringColor: '#6366f1', ringBg: '#1e293b', numColor: '#ffffff', labelColor: '#94a3b8', |
| 168 |
barColor: '#6366f1', barBg: '#1e293b' |
| 169 |
}, opts); |
| 170 |
|
| 171 |
el.parentElement && (el.parentElement.style.background = o.bgColor); |
| 172 |
|
| 173 |
typoCssVarsForEl(o.typoHeading, '--bkbg-cdp-h-', el); |
| 174 |
typoCssVarsForEl(o.typoSub, '--bkbg-cdp-sub-', el); |
| 175 |
typoCssVarsForEl(o.typoNum, '--bkbg-cdp-num-', el); |
| 176 |
typoCssVarsForEl(o.typoUnit, '--bkbg-cdp-unit-', el); |
| 177 |
|
| 178 |
var inner = document.createElement('div'); |
| 179 |
inner.className = 'bkbg-cdp-inner'; |
| 180 |
inner.style.cssText = 'max-width:' + o.maxWidth + 'px;margin:0 auto;padding:' + o.paddingTop + 'px 24px ' + o.paddingBottom + 'px;text-align:center;'; |
| 181 |
|
| 182 |
var h2 = document.createElement('h2'); |
| 183 |
h2.className = 'bkbg-cdp-heading'; |
| 184 |
h2.style.color = o.headingColor; |
| 185 |
h2.innerHTML = o.heading; |
| 186 |
|
| 187 |
var sub = document.createElement('p'); |
| 188 |
sub.className = 'bkbg-cdp-sub'; |
| 189 |
sub.style.color = o.subColor; |
| 190 |
sub.innerHTML = o.subtext; |
| 191 |
|
| 192 |
inner.appendChild(h2); |
| 193 |
inner.appendChild(sub); |
| 194 |
|
| 195 |
var endDate = parseEnd(o.endDate); |
| 196 |
|
| 197 |
/* Build unit widgets */ |
| 198 |
var unitDefs = [ |
| 199 |
{ key: 'days', show: o.showDays, label: 'Days', max: 30 }, |
| 200 |
{ key: 'hours', show: o.showHours, label: 'Hours', max: 24 }, |
| 201 |
{ key: 'mins', show: o.showMins, label: 'Mins', max: 60 }, |
| 202 |
{ key: 'secs', show: o.showSecs, label: 'Secs', max: 60 } |
| 203 |
]; |
| 204 |
|
| 205 |
var unitsRow = document.createElement('div'); |
| 206 |
unitsRow.className = 'bkbg-cdp-units style-' + o.style; |
| 207 |
inner.appendChild(unitsRow); |
| 208 |
|
| 209 |
var units = []; |
| 210 |
unitDefs.forEach(function (def) { |
| 211 |
if (!def.show) return; |
| 212 |
var u; |
| 213 |
if (o.style === 'ring') { |
| 214 |
u = makeRingUnit(o, def.label); |
| 215 |
unitsRow.appendChild(u.wrap); |
| 216 |
} else { |
| 217 |
u = makeBarUnit(o, def.label); |
| 218 |
unitsRow.appendChild(u.wrap); |
| 219 |
} |
| 220 |
u.key = def.key; |
| 221 |
u.max = def.max; |
| 222 |
units.push(u); |
| 223 |
}); |
| 224 |
|
| 225 |
el.appendChild(inner); |
| 226 |
|
| 227 |
function tick() { |
| 228 |
var parts = diffParts(endDate); |
| 229 |
units.forEach(function (u) { |
| 230 |
var val = parts[u.key]; |
| 231 |
var pct = val / u.max; |
| 232 |
u.numEl.textContent = pad2(val); |
| 233 |
if (o.style === 'ring') { |
| 234 |
updateRing(u.fill, u.circumference, pct); |
| 235 |
} else { |
| 236 |
u.fill.style.width = (pct * 100) + '%'; |
| 237 |
} |
| 238 |
}); |
| 239 |
if (parts.total <= 0) { clearInterval(timer); } |
| 240 |
} |
| 241 |
|
| 242 |
tick(); |
| 243 |
var timer = setInterval(tick, 1000); |
| 244 |
}); |
| 245 |
} |
| 246 |
|
| 247 |
if (document.readyState !== 'loading') { init(); } |
| 248 |
else { document.addEventListener('DOMContentLoaded', init); } |
| 249 |
})(); |
| 250 |
|