` } else { imgString = `` }; let titleString = ''; if(result.name) { titleString = articleNameReplace(result.name); }; let descriptionString = ''; if(result.summary) { descriptionString = result.summary; } else if(result.description) { descriptionString = result.description; }; if (descriptionString.length > 50) { descriptionString = descriptionString.slice(0, 50) + '...'; }; let markup = ` ${imgString} ${titleString} ${descriptionString} `; resultsContainer.insertAdjacentHTML('beforeend', markup); }); } else { let markup = "No results found."; resultsContainer.insertAdjacentHTML('beforeend', markup); } } let prevScrollpos = window.pageYOffset; window.addEventListener('scroll', (e) => { let headerSpacer = document.querySelector('.header-spacer'); if(headerSpacer) { let spaceFromTop = headerSpacer.getBoundingClientRect().top; window.onscroll = function() { let headerEl = document.querySelector('.header'); if(spaceFromTop < -300) { let menucheckbox = document.querySelector('.side-menu'); if(!menucheckbox.checked) { var currentScrollPos = window.pageYOffset; if (prevScrollpos > currentScrollPos) { headerEl.style.top = "0"; } else { headerEl.style.top = "-80px"; } prevScrollpos = currentScrollPos; } } } }; }); var keys = {37: 1, 38: 1, 39: 1, 40: 1}; function preventDefault(e) { e.preventDefault(); }; function preventDefaultForScrollKeys(e) { if (keys[e.keyCode]) { preventDefault(e); return false; }; }; /* modern Chrome requires { passive: false } when adding event */ var supportsPassive = false; try { window.addEventListener("test", null, Object.defineProperty({}, 'passive', { get: function () { supportsPassive = true; } })); } catch(e) {}; var wheelOpt = supportsPassive ? { passive: false } : false; var wheelEvent = 'onwheel' in document.createElement('div') ? 'wheel' : 'mousewheel'; const toggleScroll = () => { let menucheckbox = document.querySelector('.side-menu'); if(menucheckbox.checked) { disableScroll(); } else { enableScroll(); }; }; /* call this to Disable */ function disableScroll() { window.addEventListener('DOMMouseScroll', preventDefault, false); window.addEventListener(wheelEvent, preventDefault, wheelOpt); window.addEventListener('touchmove', preventDefault, wheelOpt); window.addEventListener('keydown', preventDefaultForScrollKeys, false); }; /* call this to Enable */ function enableScroll() { window.removeEventListener('DOMMouseScroll', preventDefault, false); window.removeEventListener(wheelEvent, preventDefault, wheelOpt); window.removeEventListener('touchmove', preventDefault, wheelOpt); window.removeEventListener('keydown', preventDefaultForScrollKeys, false); }; (function () { const cartNote = document.querySelector(".cartNote"); function updateCartNote(value) { if (value) { cartNote.textContent = value; } else { } } updateCartNote(localStorage.getItem("ubx_basketItems")); const localStorageSetItem = localStorage.setItem; localStorage.setItem = function (key, value) { const event = new Event("localStorageSetItem"); event.key = key; event.newValue = value; window.dispatchEvent(event); localStorageSetItem.apply(this, arguments); }; window.addEventListener("localStorageSetItem", function (event) { if (event.key === "ubx_basketItems") { updateCartNote(event.newValue); } }); })(); ; const links = document.getElementsByTagName('a'); for (let i = 0; i < links.length; i++) { const link = links[i]; if (link.getAttribute('href') === '') { link.removeAttribute('href'); link.removeEventListener('click', preventClick); } } function preventClick(event) { event.preventDefault(); };document.addEventListener('DOMContentLoaded', () => { function getPageNumberFromURL() { const urlParams = new URLSearchParams(window.location.search); const pageNumber = urlParams.get('page'); return pageNumber ? parseInt(pageNumber) : 1; } function getCategoryFromURL() { const urlParams = new URLSearchParams(window.location.search); return urlParams.get('category') || 'All'; } function updateURL(pageNumber, categoryAlias) { const urlParams = new URLSearchParams(window.location.search); urlParams.set('page', pageNumber); urlParams.set('category', categoryAlias); const baseUrl = window.location.href.split('?')[0]; const newURL = baseUrl + '?' + urlParams.toString(); history.pushState({}, '', newURL); } function articleNameReplace(name) { let articleName = name.replace('u2019', "'"); articleName = articleName.replace('u2018', "'"); articleName = articleName.replace('u2013', '-'); articleName = articleName.replace('u2026', '...'); articleName = articleName.replace('u00a3', '£'); return articleName; } const loadArticles = async ( path, articlesListClass, perPage, pageNumber = 1, currentCategory = 'All' ) => { const randomString = Math.random().toString(36).substring(7); const newPath = path + '?id=' + randomString; const res = await fetch(newPath); const superArticles = await res.json(); let articles = superArticles; const allCategories = new Set(); articles.sort((a, b) => b.publishedAt > a.publishedAt ? 1 : b.publishedAt < a.publishedAt ? -1 : 0 ); articles.forEach((article) => { if (Array.isArray(article.categories)) { article.categories.forEach((category) => { allCategories.add(category.name); }); } }); let page = getPageNumberFromURL() - 1; let totalPages = Math.ceil(articles.length / perPage); // Define totalPages here const filterContainer = document.querySelector('.articleFilters'); let filterMarkup = ''; filterMarkup += `All`; allCategories.forEach((category) => { filterMarkup += `${category}`; }); filterContainer.insertAdjacentHTML('beforeend', filterMarkup); const filterArticles = async (categoryFilter) => { currentCategory = categoryFilter; articles = superArticles; if (categoryFilter !== 'All') { articles = articles.filter( (item) => item.categories && Array.isArray(item.categories) && item.categories.some( (category) => category.name === categoryFilter ) ); } page = 0; totalPages = Math.ceil(articles.length / perPage); // Recalculate totalPages insertArticles(); updateURL(1, categoryFilter); }; window.filterArticles = filterArticles; const insertArticles = () => { const articleListEl = document.querySelector(`.${articlesListClass}`); let startFrom = page * perPage; const endAt = startFrom + perPage; const totalArticles = articles.length; articleListEl.innerHTML = ''; for (let i = startFrom; i < endAt && i < totalArticles; i++) { const article = articles[i]; const parts = article.publishedAt.split(/[- :]/); const publishedDate = new Date( Date.UTC( parts[0], parts[1] - 1, parts[2], parts[3], parts[4], parts[5] ) ); const year = publishedDate.getUTCFullYear(); const month = (publishedDate.getUTCMonth() + 1) .toString() .padStart(2, "0"); const day = publishedDate.getUTCDate().toString().padStart(2, "0"); const formattedDate = `${day}/${month}/${year}`; const markup = `Published ${formattedDate}${articleNameReplace( article.name )}Read Article`; articleListEl.insertAdjacentHTML("beforeend", markup); } insertPagination(); }; const insertPagination = () => { const paginationContainer = document.querySelector('.pagination'); let paginationMarkup = ''; if (page !== 0) { paginationMarkup += `|<`; paginationMarkup += `<<`; } for (let i = Math.max(0, page - 3); i < page; i++) { paginationMarkup += `${ i + 1 }`; } paginationMarkup += `${ page + 1 }`; for (let i = page + 1; i < Math.min(totalPages, page + 4); i++) { paginationMarkup += `${ i + 1 }`; } if (page < totalPages - 1) { paginationMarkup += `>>`; paginationMarkup += `>|`; } paginationContainer.innerHTML = paginationMarkup; }; const changePage = (newPage) => { page = newPage; insertArticles(); const articleListTop = document.querySelector(`.${articlesListClass}`); articleListTop.scrollIntoView({ behavior: 'smooth' }); updateURL(page + 1, currentCategory); }; window.changePage = changePage; insertArticles(); }; const pageNumber = getPageNumberFromURL(); const category = getCategoryFromURL(); var randomNumber = Math.floor(Math.random() * 10000) + 1; loadArticles('/public/articles.json?v=' + randomNumber, 'article-list', 12, pageNumber, category); }); function generateSessionID() { return Math.random().toString(36).substring(2, 15); } function setSessionIDCookie(sessionID) { const expirationDate = new Date(); expirationDate.setDate(expirationDate.getDate() + 7); document.cookie = `sessionID=${sessionID}; expires=${expirationDate.toUTCString()}; path=/; domain=brightwayz.co.uk`; } function getSessionID() { const cookieString = document.cookie; const cookieArray = cookieString.split('; '); for (const cookie of cookieArray) { const [name, value] = cookie.split('='); if (name === 'sessionID') { return value; } } return null; } const sessionID = generateSessionID(); setSessionIDCookie(sessionID); const retrievedSessionID = getSessionID(); document.addEventListener('DOMContentLoaded', function() { var cookieDismissed = localStorage.getItem('cookieDismissed'); if (cookieDismissed) { var cookiePopup = document.querySelector('#cookie-popup'); cookiePopup.style.display = 'none'; } var okButton = document.querySelector('#cookie-popup button'); okButton.addEventListener('click', function() { var cookiePopup = document.querySelector('#cookie-popup'); cookiePopup.style.display = 'none'; localStorage.setItem('cookieDismissed', true); }); });
${titleString}
${descriptionString}
Published ${formattedDate}