2019-06-04 11:11:58 -06:00
|
|
|
import { DISPLAY_AD_EVERY } from '../constants.js';
|
2019-06-06 11:25:08 -06:00
|
|
|
import ads from '../../ads.json';
|
2019-06-04 11:11:58 -06:00
|
|
|
|
|
|
|
export function setupAds() {
|
2019-06-06 11:25:08 -06:00
|
|
|
const shuffle = (a, b) => Math.random() > 0.5 ? 1 : -1;
|
|
|
|
const priority = ads.filter(ad => isActive(ad) && ad.isPriority).sort(shuffle);
|
|
|
|
const regular = ads.filter(ad => isActive(ad) && !ad.isPriority).sort(shuffle);
|
|
|
|
window.ads = [...priority, ...regular];
|
2019-06-04 11:11:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function isActive(ad) {
|
|
|
|
const date = new Date();
|
|
|
|
return date >= new Date(ad.start) && date < new Date(ad.end);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function renderAd(wordPosition) {
|
|
|
|
if (window.hasOwnProperty('ads') && window.ads) {
|
|
|
|
if (wordPosition % DISPLAY_AD_EVERY === 3) {
|
|
|
|
const adIndex = Math.floor(wordPosition / DISPLAY_AD_EVERY) % window.ads.length;
|
|
|
|
const ad = window.ads[adIndex];
|
|
|
|
return `<article class="entry">
|
|
|
|
<header>
|
|
|
|
<h4 class="word">${ad.header}</h4>
|
|
|
|
</header>
|
|
|
|
<dl>
|
|
|
|
<dt class="definition">${ad.body}</dt>
|
|
|
|
<dd class="details">
|
|
|
|
<a href="${ad.link}" target="_blank" class="button">${ad.cta}</a>
|
|
|
|
</dd>
|
|
|
|
</dl>
|
|
|
|
</article>`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|