Skip to content

Commit

Permalink
loading main page content on load function
Browse files Browse the repository at this point in the history
intHomeContent function is to generate home page content directly
  • Loading branch information
HamzaElkotp committed Sep 22, 2022
1 parent 34c02a9 commit cd71405
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 20,002 deletions.
5 changes: 3 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
i created a meta tag "SPAname" to can access them and add events easly
also to get their content from JSON using this meta tag
-->

</ul>
</aside>

<div class="" id="content">

<div class='column is-10' id="SPAoutContent">
</div>
</div>

Expand Down
12 changes: 12 additions & 0 deletions main.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ html,body {
body{
position: sticky;
top: 0;
margin: 0 10px !important;
}
*{
box-sizing: border-box !important;
}

:root{
Expand All @@ -16,6 +20,14 @@ body{
.sideBar{
margin-bottom: 0;
height: 100vh;
top: 0;
position: sticky;
}

.link {
display: block;
font-size: 20px;
color: #00d1b2;
}

.columns:not(:last-child){
Expand Down
36 changes: 32 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
function jsonRead(jsonpages, metatagArr = ["SPAname", "SPAurl", "SPAtitle"]){
let contentBox = document.getElementById("SPAoutContent");

function intContent(jsonpages, metatagArr = ["SPAname", "SPAurl", "SPAtitle"]){

let xhr = fetch(jsonpages);
xhr.then((res)=>{
Expand All @@ -19,15 +21,25 @@ function jsonRead(jsonpages, metatagArr = ["SPAname", "SPAurl", "SPAtitle"]){
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");
})
Expand All @@ -40,7 +52,23 @@ function jsonRead(jsonpages, metatagArr = ["SPAname", "SPAurl", "SPAtitle"]){
});
}

jsonRead("spa.json",["SPAname1", "SPAurl1", "SPAtitle1"]);

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;
}
}
})
}

// You have a problem, many requests, to window.history.pushState because looping on every element
intContent("spa.json",["SPAname1", "SPAurl1", "SPAtitle1"],true);
intHomeContent("spa.json","home","SPAname1");
Loading

0 comments on commit cd71405

Please sign in to comment.