/*! Local Favorites — v1.1.9 (hotfix: title trim regex) */ (function(){ var LS_KEY = "wpfp_favorites"; var LS_META = "wpfp_favorites_meta"; var LS_SORT = "wpfp_favorites_sort"; var HEART_EMPTY = "♡ "; var HEART_FULL = "❤ "; var lastPostsCache = null; function getFavs(){ try{var raw=localStorage.getItem(LS_KEY);if(!raw)return[];var arr=JSON.parse(raw);if(!Array.isArray(arr))return[];return arr.map(function(x){return +x;}).filter(function(n){return Number.isInteger(n)&&n>0;});}catch(e){return[];} } function getMeta(){ try{var raw=localStorage.getItem(LS_META);return raw?JSON.parse(raw):{};}catch(e){return {};} } function setMeta(meta){ try{ localStorage.setItem(LS_META, JSON.stringify(meta)); }catch(e){} } function getSort(){ var s=localStorage.getItem(LS_SORT); return (s==='alpha'||s==='oldest'||s==='recent')?s:'recent'; } function setSort(s){ try{ localStorage.setItem(LS_SORT, s); }catch(e){} } function saveFavs(arr){ try{ var uniq=Array.from(new Set(arr.map(function(x){return +x;}))).filter(function(n){return Number.isInteger(n)&&n>0;}).slice(0,500); localStorage.setItem(LS_KEY,JSON.stringify(uniq)); return uniq; }catch(e){return arr;} } function isFav(id){return getFavs().indexOf(+id)!==-1;} function addFav(id){ var a=getFavs(); if(a.indexOf(+id)===-1){ a.push(+id); saveFavs(a); } var meta=getMeta(); meta[id]=meta[id]||{}; meta[id].addedAt=Date.now(); setMeta(meta); return a; } function removeFav(id){ var a=getFavs().filter(function(x){return x!==+id;}); saveFavs(a); var meta=getMeta(); if(meta[id]){ delete meta[id]; setMeta(meta); } return a; } function updateCounts(){ var n=getFavs().length; document.querySelectorAll(".wpfp-count").forEach(function(el){ if(n>0){ el.textContent=n; el.style.display=""; } else { el.textContent=""; el.style.display="none"; } }); } window.addEventListener("storage", function(ev){ if(ev.key===LS_KEY||ev.key===LS_META){ updateCounts(); } }); function setToggleState(el,fav){ var addText=(window.lfavL10n&&window.lfavL10n.add)||"Lägg till favorit"; var favText=(window.lfavL10n&&window.lfavL10n.favorit)||"Favorit"; if(fav){ el.classList.add("wpfp-favorited"); el.textContent=HEART_FULL+favText; el.setAttribute("aria-pressed","true"); } else { el.classList.remove("wpfp-favorited"); el.textContent=HEART_EMPTY+addText; el.setAttribute("aria-pressed","false"); } } function parsePostIdFromNode(el){ if(el && el.dataset && el.dataset.postId){ return +el.dataset.postId; } var host = el && el.closest && el.closest("[data-post-id],[id^='post-']"); if(host){ if(host.dataset && host.dataset.postId) return +host.dataset.postId; var m=(/post-(\\d+)/).exec(host.id||""); if(m) return +m[1]; } return null; } function hydrateToggles(ctx){ (ctx||document).querySelectorAll(".wpfp-toggle").forEach(function(el){ var id=parsePostIdFromNode(el); if(!id) return; setToggleState(el,isFav(id)); el.dataset.postId=id; }); } document.addEventListener("click", function(ev){ var el = ev.target.closest(".wpfp-toggle, .wpfp-remove, .wpfp-sort-btn"); if(!el) return; if(el.classList.contains("wpfp-sort-btn")){ ev.preventDefault(); var mode = el.dataset.sort; if(mode && (mode==='alpha' || mode==='recent' || mode==='oldest')){ setSort(mode); renderFavList(true); } return; } ev.preventDefault(); var id=parsePostIdFromNode(el); if(!id) return; if(el.classList.contains("wpfp-toggle")){ var fav=isFav(id); if(fav){ removeFav(id); setToggleState(el,false); } else { addFav(id); setToggleState(el,true); } updateCounts(); return; } if(el.classList.contains("wpfp-remove")){ if(isFav(id)){ removeFav(id); var li=el.closest(".wpfp-item"); if(li){ li.classList.add("lfav-removing"); setTimeout(function(){ li.remove(); if(!getFavs().length){ var box=document.getElementById("wpfp-favorites-client"); if(box) box.innerHTML="
"+((window.lfavL10n&&lfavL10n.empty)||"Du har inga favoriter sparade ännu.")+"
"; } }, 250); } } updateCounts(); } }, {passive:false}); function placeholderSVG(){ return 'data:image/svg+xml;utf8,'+encodeURIComponent(''); } function escapeForRegex(s){ return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); } function trimTitleSuffix(title){ try{ var services = Array.isArray(window.lfavServices)? window.lfavServices : []; var dash = "[\\-\\u2012\\u2013\\u2014]"; for(var i=0;iInga program inlagda än
"; // Använd din specifika text här return; } // ÄNDRING 2: Visa "Laddar programmen" under laddningstiden if(!fromSortToggle) { // Visa endast laddningsmeddelandet när listan laddas första gången eller vid en fullständig uppdatering, inte bara vid sortering box.innerHTML="Laddar programmen
"; // Använd din specifika text här } if(fromSortToggle && lastPostsCache && Array.isArray(lastPostsCache)){ var postsSorted = sortPosts(lastPostsCache, getSort()); box.innerHTML = buildListHTML(postsSorted); return; } try{ var base=(window.wpApiSettings&&window.wpApiSettings.root)?window.wpApiSettings.root:"/wp-json/"; var url=base+"wp/v2/posts"; var qs="?include="+ids.join(",")+"&_fields=id,link,title,featured_media,content&_embed=1&per_page=100"; var res=await fetch(url+qs,{credentials:"same-origin"}); if(!res.ok) throw new Error("HTTP "+res.status); var posts=await res.json(); for (var i=0;iKunde inte ladda favoriter just nu.
"; console.error("[LocalFavorites] renderFavList error:", e); } } function rehydrateAll(){ hydrateToggles(); renderFavList(false); updateCounts(); } document.addEventListener("turbolinks:load", rehydrateAll); document.addEventListener("turbo:load", rehydrateAll); document.addEventListener("pjax:end", rehydrateAll); document.addEventListener("swup:contentReplaced", rehydrateAll); document.addEventListener("barba:ready", rehydrateAll); document.addEventListener("barba:after", rehydrateAll); var mo=new MutationObserver(function(){ clearTimeout(window.__lfav_ob_to); window.__lfav_ob_to=setTimeout(function(){ hydrateToggles(); updateCounts(); },50); }); try{ mo.observe(document.documentElement,{childList:true,subtree:true}); }catch(e){} document.addEventListener("DOMContentLoaded", function(){ hydrateToggles(); renderFavList(false); updateCounts(); }); })();