-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSparling.js
72 lines (58 loc) · 2.42 KB
/
Sparling.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
let contentBox = document.getElementById("SPAoutContent");
function intContent(jsonpages, metatagArr = ["SPAname", "SPAurl", "SPAtitle"]){
let xhr = fetch(jsonpages);
xhr.then((res)=>{
return res.json()
}).then((data)=>{
return data
}).then((data)=>{
function evenAdder(){
// to make No need to stick to static "page title" like: "SPAtitle", "page url" like: "SPAurl" or "page name" like: "SPAname", in your json file, name them as you want but be carefull to their order
let SPAname = metatagArr[0];
let SPAurl = metatagArr[1];
let SPAtitle = metatagArr[2];
let elements = document.querySelectorAll('[SPAname]');
// Loop through "SPAname" meta tag elemnts to add to them 'click' event
elements.forEach((ele, index)=>{
ele.addEventListener('click',()=>{
let eleIndex = ""
// change title, url regardless of pages or json file objects order
for(let dataI = 0; dataI<data.length ; dataI++){
if(data[dataI][SPAname] == ele.getAttribute("SPAname")){
document.title = data[dataI][SPAtitle];
window.history.pushState("","",data[dataI][SPAurl]);
eleIndex = dataI;
break
}
}
contentBox.innerHTML = ``;
for (i in data[eleIndex]["Body"]){
contentBox.innerHTML += `${data[eleIndex]["Body"][i]}`
}
elements.forEach((eles)=>{
eles.classList.remove("is-active", "is-primary");
})
ele.classList.add("is-active", "is-primary")
})
})
}
evenAdder();
});
}
function intHomeContent (jsonpages, homePageSPAname, keyName){
let xhr = fetch(jsonpages);
xhr.then((res)=>{
return res.json()
}).then((data)=>{
return data
}).then((data)=>{
for(let ind = 0; ind<data.length; ind++){
if(data[ind][keyName] == homePageSPAname){
for (item in data[ind]["Body"]){
contentBox.innerHTML += `${data[ind]["Body"][item]}`;
}
break;
}
}
})
}