Update 'main.js'

This commit is contained in:
Dominik V. Salonen 2018-03-14 07:16:21 +00:00
parent 9f8d406c58
commit 9831b9fc8d
1 changed files with 69 additions and 52 deletions

121
main.js
View File

@ -1,53 +1,70 @@
// I suck at JS okay, don't judge. I know this is horrendous. // I suck at JS okay, don't judge. I know this is horrendous. Heavy copy/paste junk.
// Heavy copy/paste junk. // Code is commented more than it needs to be because I'm still learning and the comments are basically post-it notes to help me remember.
// Ajax is entirely pointless for this site, but I did it to learn.
var button1 = document.getElementById('button1');
var button2 = document.getElementById('button2'); // Find all our buttons
var button3 = document.getElementById('button3'); const button1 = document.getElementById('button1');
const button2 = document.getElementById('button2');
contsrc = "file:///H:/Side projects/Site/"; const button3 = document.getElementById('button3');
var payload1 = "Hellow"; // Make the base URL a variable because it's easier if I ever move the site
//var payload2 = new Promise(function(resolve) { const baseUrl = "file:///H:/Side projects/Site/";
// setTimeout(resolve(httpGet(contsrc + "pages/about.html")), 100, 'foo')});
var payload2 = "Ayy"; // Create variables that will get populated by loadContent() later
var payload3 = "lmao3"; var payload1;
var payload2;
function resetButtons() { var payload3;
button1.className = "nav-button";
button2.className = "nav-button"; // Very bad function to get rid of the "active" class wherever it is atm
button3.className = "nav-button"; function resetButtons() {
} button1.className = "nav-button";
button2.className = "nav-button";
function httpGet(myUrl) { button3.className = "nav-button";
var request = new XMLHttpRequest(); }
request.open('GET', myUrl, true); // Fetch all the page contents
request.send(null); async function loadContent(baseUrl) {
const http1 = fetch(baseUrl + "pages/about.html");
if (request.status == 200) { const http2 = fetch(baseUrl + "pages/about.html");
return request.responseText; const http3 = fetch(baseUrl + "pages/about.html");
}
} const requests = [http1, http2, http3];
function setContent(navbutt, payload) { const responses = await Promise.all(requests);
document.getElementById("page").innerHTML = payload;
resetButtons(); for (let response of responses) {
navbutt.className += " active"; console.log("Loaded data")
} }
button1.addEventListener('click', function() { return responses
setContent(button1, payload1); }
}, false);
function setContent(navbutt, payload) {
button2.addEventListener('click', function() { document.getElementById("page").innerHTML = payload;
setContent(button2, payload2); resetButtons();
}, false); navbutt.className += " active";
}
button3.addEventListener('click', function() {
setContent(button3, payload3); function initialContent() {
}, false);
// Get the HTML we need
function initialContent() { var contents = loadContent();
setContent(button1, payload1);
payload1 = contents[0];
payload2 = contents[1];
payload3 = contents[2];
// Slap on all the event listeners
button1.addEventListener('click', function() {
setContent(button1, payload1);
}, false);
button2.addEventListener('click', function() {
setContent(button2, payload2);
}, false);
button3.addEventListener('click', function() {
setContent(button3, payload3);
}, false);
// And finally enable the first tab
setContent(button1, payload1);
} }