christianermann.dev-hugo

The Hugo source for my website
git clone git://git.christianermann.dev/christianermann.dev-hugo
Log | Files | Refs | Submodules | README

web-loop.js (1131B)


      1 
      2 const WEB_LOOP_DATA_URL = `https://raw.githubusercontent.com/c2000e/web-loops/main/test-loop.json`
      3 
      4 window.onload = function() {
      5 
      6     const webLoop = document.getElementById("web-loop");
      7     const thisSite = webLoop.getAttribute("site");
      8 
      9     fetch(WEB_LOOP_DATA_URL)
     10       .then((response) => response.json())
     11       .then((sites) => {
     12           const thisSiteIndex = sites.findIndex(
     13               (site) => site.name === thisSite
     14           );
     15 
     16           let prevSiteIndex = thisSiteIndex - 1;
     17           if (prevSiteIndex < 0) prevSiteIndex = sites.length - 1;
     18 
     19           let nextSiteIndex = thisSiteIndex + 1;
     20           if (nextSiteIndex > sites.length - 1) nextSiteIndex = 0;
     21 
     22           const mainLink = webLoop.querySelector("#main");
     23           const prevLink = webLoop.querySelector("#prev");
     24           const nextLink = webLoop.querySelector("#next");
     25 
     26           console.log(thisSiteIndex);
     27           console.log(prevSiteIndex);
     28           console.log(nextSiteIndex);
     29 
     30           mainLink.href = sites[0].url;
     31           prevLink.href = sites[prevSiteIndex].url;
     32           nextLink.href = sites[nextSiteIndex].url;
     33       });
     34 }
     35