Ads in Business Opportunities - Farangmart
Ads in Business Opportunities - Farangmart
'+
'
Recently Viewed
'+
'
Pick up where you left off.
'+
'
'+
'
'+
'
'+
''+
'';
return section;
}
function insertComponent(section){
if(surface==="home"){
var home=document.getElementById("fmv36-home");
if(home){
var finalCta=
home.querySelector(".fm-final") ||
home.querySelector(".fm-final-cta") ||
home.lastElementChild;
if(finalCta&&finalCta.parentNode===home){
home.insertBefore(section,finalCta);
}else{
home.appendChild(section);
}
return true;
}
}
if(surface==="archive"){
var archive=document.getElementById("fm-archive-v11");
var marketplace=
(archive&&archive.querySelector(".fma11-marketplace")) ||
document.querySelector(".fma11-marketplace");
if(marketplace){
var shell=marketplace.closest(".fma11-shell");
if(shell){
shell.insertAdjacentElement("afterend",section);
}else{
marketplace.insertAdjacentElement("afterend",section);
}
return true;
}
}
if(surface==="dashboard"){
var dashboard=document.querySelector("body.page-id-11 .content-wrap.dashboard-listings");
var workspace=document.getElementById("fmd160-workspace");
if(dashboard&&workspace){
workspace.insertBefore(section,dashboard);
return true;
}
if(dashboard&&dashboard.parentNode){
dashboard.parentNode.insertBefore(section,dashboard);
return true;
}
}
return false;
}
function cardMarkup(item){
var media=item.image
? '
'
: 'FarangMart';
var meta=[];
if(item.category){meta.push(item.category);}
if(item.location){meta.push(item.location);}
var metaHtml=meta.map(function(value){
return ""+escapeHtml(value)+"";
}).join("");
var price=item.price
? ''+escapeHtml(item.price)+'
'
: "";
return (
''+
''+media+'
'+
''+
(metaHtml?'
'+metaHtml+'
':"")+
'
'+escapeHtml(item.title)+'
'+
price+
'
'+
''
);
}
function removeUnavailable(history,items){
var allowed=new Set(items.map(function(item){return item.id;}));
var cleaned=history.filter(function(entry){
return allowed.has(entry.id);
});
if(cleaned.length!==history.length){
writeHistory(cleaned);
}
}
function hydrate(section,history){
if(!endpoint||!history.length){return;}
var ids=history.map(function(item){return item.id;}).join(",");
var requestUrl=endpoint+(endpoint.indexOf("?")===-1?"?":"&")+"ids="+encodeURIComponent(ids);
window.fetch(requestUrl,{
method:"GET",
credentials:"same-origin",
headers:{
"Accept":"application/json"
}
})
.then(function(response){
if(!response.ok){
throw new Error("Recently viewed request failed");
}
return response.json();
})
.then(function(payload){
var items=Array.isArray(payload&&payload.items)
? payload.items.map(normaliseItem).filter(function(item){
return item.id&&item.title&&item.url;
})
: [];
removeUnavailable(history,items);
if(!items.length){
section.remove();
return;
}
var track=section.querySelector(".fmrv10-track");
if(!track){return;}
track.innerHTML=items.map(cardMarkup).join("");
section.hidden=false;
})
.catch(function(){
section.remove();
});
}
function initialiseDisplay(){
if(surface==="single"){return;}
/*
* V1.0.3 DOM guard.
* Never insert a second Recently Viewed component on the same page.
*/
if(document.querySelector(".fmrv10")){
return;
}
var history=readHistory();
if(!history.length){
return;
}
var section=component();
if(!insertComponent(section)){
return;
}
var clear=section.querySelector(".fmrv10-clear");
if(clear){
clear.addEventListener("click",function(){
writeHistory([]);
section.remove();
});
}
hydrate(section,history);
}
function boot(){
if(!surface){return;}
captureCurrent();
window.setTimeout(initialiseDisplay,40);
}
if(document.readyState==="loading"){
document.addEventListener("DOMContentLoaded",boot,{once:true});
}else{
boot();
}
})();
(function(){
"use strict";
/*
* V1.0.3 duplicate-runtime guard.
* Prevents a second execution from creating another component.
*/
if(window.FMRecentlyViewedRuntimeLoaded){
return;
}
window.FMRecentlyViewedRuntimeLoaded=true;
var config=window.FMRecentlyViewedConfig||{};
var storageKey=config.storage||"fm_recently_viewed_v1";
var maxItems=Number(config.maxItems||15);
var endpoint=String(config.endpoint||"");
var surface=String(config.surface||"");
var currentId=Number(config.currentId||0);
function readHistory(){
try{
var raw=window.localStorage.getItem(storageKey);
if(!raw){return [];}
var parsed=JSON.parse(raw);
if(!Array.isArray(parsed)){return [];}
return parsed
.map(function(item){
if(typeof item==="number"||typeof item==="string"){
return {id:Number(item),viewed:0};
}
return {
id:Number(item&&item.id||0),
viewed:Number(item&&item.viewed||0)
};
})
.filter(function(item){
return item.id>0;
})
.slice(0,maxItems);
}catch(error){
return [];
}
}
function writeHistory(items){
try{
window.localStorage.setItem(
storageKey,
JSON.stringify(items.slice(0,maxItems))
);
}catch(error){}
}
function captureCurrent(){
if(surface!=="single"||!currentId){return;}
var history=readHistory().filter(function(item){
return item.id!==currentId;
});
history.unshift({
id:currentId,
viewed:Date.now()
});
writeHistory(history);
}
function escapeHtml(value){
return String(value||"")
.replace(/&/g,"&")
.replace(/,"<")
.replace(/>/g,">")
.replace(/"/g,""")
.replace(/'/g,"'");
}
function normaliseItem(item){
return {
id:Number(item&&item.id||0),
title:String(item&&item.title||"").trim(),
url:String(item&&item.url||"").trim(),
price:String(item&&item.price||"").trim(),
location:String(item&&item.location||"").trim(),
category:String(item&&item.category||"").trim(),
image:String(item&&item.image||"").trim()
};
}
function component(){
var section=document.createElement("section");
section.className="fmrv10";
section.hidden=true;
section.setAttribute("aria-labelledby","fmrv10-title");
section.innerHTML=
''+
'
'+
'
'+
'
Your browsing history
'+
'
Recently Viewed
'+
'
Pick up where you left off.
'+
'
'+
'
'+
'
'+
'
'+
'
';
return section;
}
function insertComponent(section){
if(surface==="home"){
var home=document.getElementById("fmv36-home");
if(home){
var finalCta=
home.querySelector(".fm-final") ||
home.querySelector(".fm-final-cta") ||
home.lastElementChild;
if(finalCta&&finalCta.parentNode===home){
home.insertBefore(section,finalCta);
}else{
home.appendChild(section);
}
return true;
}
}
if(surface==="archive"){
var archive=document.getElementById("fm-archive-v11");
var marketplace=
(archive&&archive.querySelector(".fma11-marketplace")) ||
document.querySelector(".fma11-marketplace");
if(marketplace){
var shell=marketplace.closest(".fma11-shell");
if(shell){
shell.insertAdjacentElement("afterend",section);
}else{
marketplace.insertAdjacentElement("afterend",section);
}
return true;
}
}
if(surface==="dashboard"){
var dashboard=document.querySelector("body.page-id-11 .content-wrap.dashboard-listings");
var workspace=document.getElementById("fmd160-workspace");
if(dashboard&&workspace){
workspace.insertBefore(section,dashboard);
return true;
}
if(dashboard&&dashboard.parentNode){
dashboard.parentNode.insertBefore(section,dashboard);
return true;
}
}
return false;
}
function cardMarkup(item){
var media=item.image
? '
'
: 'FarangMart';
var meta=[];
if(item.category){meta.push(item.category);}
if(item.location){meta.push(item.location);}
var metaHtml=meta.map(function(value){
return ""+escapeHtml(value)+"";
}).join("");
var price=item.price
? ''+escapeHtml(item.price)+'
'
: "";
return (
''+
''+media+'
'+
''+
(metaHtml?'
'+metaHtml+'
':"")+
'
'+escapeHtml(item.title)+'
'+
price+
'
'+
''
);
}
function removeUnavailable(history,items){
var allowed=new Set(items.map(function(item){return item.id;}));
var cleaned=history.filter(function(entry){
return allowed.has(entry.id);
});
if(cleaned.length!==history.length){
writeHistory(cleaned);
}
}
function hydrate(section,history){
if(!endpoint||!history.length){return;}
var ids=history.map(function(item){return item.id;}).join(",");
var requestUrl=endpoint+(endpoint.indexOf("?")===-1?"?":"&")+"ids="+encodeURIComponent(ids);
window.fetch(requestUrl,{
method:"GET",
credentials:"same-origin",
headers:{
"Accept":"application/json"
}
})
.then(function(response){
if(!response.ok){
throw new Error("Recently viewed request failed");
}
return response.json();
})
.then(function(payload){
var items=Array.isArray(payload&&payload.items)
? payload.items.map(normaliseItem).filter(function(item){
return item.id&&item.title&&item.url;
})
: [];
removeUnavailable(history,items);
if(!items.length){
section.remove();
return;
}
var track=section.querySelector(".fmrv10-track");
if(!track){return;}
track.innerHTML=items.map(cardMarkup).join("");
section.hidden=false;
})
.catch(function(){
section.remove();
});
}
function initialiseDisplay(){
if(surface==="single"){return;}
/*
* V1.0.3 DOM guard.
* Never insert a second Recently Viewed component on the same page.
*/
if(document.querySelector(".fmrv10")){
return;
}
var history=readHistory();
if(!history.length){
return;
}
var section=component();
if(!insertComponent(section)){
return;
}
var clear=section.querySelector(".fmrv10-clear");
if(clear){
clear.addEventListener("click",function(){
writeHistory([]);
section.remove();
});
}
hydrate(section,history);
}
function boot(){
if(!surface){return;}
captureCurrent();
window.setTimeout(initialiseDisplay,40);
}
if(document.readyState==="loading"){
document.addEventListener("DOMContentLoaded",boot,{once:true});
}else{
boot();
}
})();
//# sourceURL=farangmart-recently-viewed-js-after