my-portfolio/dist/blog/js/index.js

1 line
29 KiB
JavaScript
Raw Normal View History

const scrollLimit=150;function goToURL(t){let e=t.split("/");if("accepted"===localStorage.getItem("cookiePopup")&&document.querySelector("#cookiePopup").classList.add("hidden"),"/blog/"!==t&&"/blog"!==t)if("post"!==e[2]){if("category"===e[2])return e[3]?void loadPostsByCategory(e[e.length-1]):void loadAllCategories().catch((t=>console.log(t)));if("search"===e[2]&&e[3])loadSearchResults(e[e.length-1]);else{if("policy"===e[2]){if("privacy"===e[3])return void loadPrivacyPolicy();if("cookie"===e[3])return void loadCookiePolicy()}show404()}}else loadIndividualPost(e[e.length-1]).catch((t=>console.log(t)));else loadHomeContent()}function submitNewsletter(){fetch(`/api/blog/newsletter/${document.querySelector("#email").value}`,{method:"POST",headers:{"Content-Type":"application/json"}}).then((t=>t.json().then((e=>(document.querySelector("#newsletterMessage").classList.remove("hidden"),e.message.includes("exists")?(document.querySelector("#newsletterMessage").classList.add("error"),document.querySelector("#newsletterMessage").classList.remove("success"),void(document.querySelector("#newsletterMessage div").innerHTML="You've already signed up you silly goose!")):t.ok?(document.querySelector("#newsletterMessage div").innerHTML=e.message,void document.querySelector("#newsletterMessage").classList.add("success")):(document.querySelector("#newsletterMessage").classList.add("error"),document.querySelector("#newsletterMessage").classList.remove("success"),void(document.querySelector("#newsletterMessage div").innerHTML=e.error)))))))}function createFormattedDate(t){return new Date(t).toLocaleDateString("en-GB",{day:"2-digit",month:"short",year:"numeric"})}function createLargePost(t){let e=document.createElement("div");e.classList.add("outerContent");let o=document.createElement("img");o.className="banner",o.src=t.headerImg,o.alt=t.title,e.appendChild(o);let n=document.createElement("div");n.classList.add("content");let a=document.createElement("div");a.classList.add("postContent");let i="";t.categories.split(", ").forEach((e=>{i+=`<a href="/blog/category/${e}" class="link">${e}</a>`,t.categories.split(", ").length>1&&(i+=", ")})),i.endsWith(", ")&&(i=i.substring(0,i.length-2));let r=createFormattedDate(t.dateModified);return a.innerHTML=`\n <h2>${t.title}</h2>\n <h3>Last updated: ${r} | ${i}</h3>\n <p>${t.abstract}</p>\n <a href="/blog/post/${t.title}" class="btn btnPrimary">See Post</a>\n `,n.appendChild(a),e.appendChild(n),e}function loadHomeContent(){fetch("/api/blog/post").then((t=>t.json().then((t=>{for(let e=0;e<t.length;e++){if(1===t[e].featured){let o=document.createElement("section");o.classList.add("largePost"),o.id="featuredPost";let n=document.createElement("h1");n.innerHTML="featured post",o.appendChild(n);let a=createLargePost(t[e]);o.appendChild(a),document.querySelector("#main").appendChild(o)}if(1===e){let o=document.createElement("section");o.classList.add("largePost"),o.id="latestPost";let n=document.createElement("h1");n.innerHTML="latest post",o.appendChild(n);let a=createLargePost(t[e]);o.appendChild(a),document.querySelector("#main").appendChild(o)}}}))))}async function getLatestAndFeaturedPosts(){return[await fetch("/api/blog/post/latest").then((t=>t.json())),await fetch("/api/blog/post/featured").then((t=>t.json()))]}function csvToArray(t){let e="",o=[""],n=0,a=!0,i=null;for(i of t)'"'===i?(a&&i===e&&(o[n]+=i),a=!a):","===i&&a?i=o[++n]="":"\n"===i&&a?("\r"===e&&(row[n]=row[n].slice(0,-1)),o=o[++r]=[i=""],n=0):o[n]+=i,e=i;return o}async function getCategories(){let t=await fetch("/api/blog/categories").then((t=>t.json())),e=[];return t.forEach((t=>e=e.concat(csvToArray(t.categories.replace(/\s*,\s*/g,","))))),[...new Set(e)]}function createCategories(t){let e="";return t.forEach((t=>e+=`<a href="/blog/category/${t}" class="link">${t}</a>`)),e}function createButtonCategories(t){let e="";return t.forEach((t=>t.forEach((t=>e+=`<a href="/blog/category/${t}" class="btn btnOutline">${t}</a>`)))),e}async function createSideConten