-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
160 lines (144 loc) · 5.68 KB
/
app.js
File metadata and controls
160 lines (144 loc) · 5.68 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// on load render html and set current selection
let currentPlanet = 'mercury';
let currentSection = 'overview';
renderHtml(currentPlanet);
// construct variables for main DOM elements
const mainText = document.getElementById('js__main-text');
const mainImage = document.getElementById('js__main-image');
const navbarPrimary = document.getElementById('js__navbar-primary');
const navbarSecondaryMobile = document.getElementById('js__navbar-secondary--mobile');
const navbarSecondaryDesktop = document.getElementById('js__navbar-secondary--desktop');
const footer = document.getElementById('js__footer');
navbarSecondaryDesktop.querySelector('li').style.backgroundColor = returnPlanetColor();
// add event listener logic to primary navigation links
document.querySelectorAll('.navbar--mobile__planets ~ a')
.forEach((e) => {
e.addEventListener('click', () => {
// render data for planet requested
renderHtml(e.innerHTML);
// close the navbar only mobile version
const elementToHide = navbarPrimary.querySelector('.collapse')
if (elementToHide.classList.contains('show')) {
elementToHide.classList.remove('show');
};
// remove active class from previous planet
navbarPrimary.querySelector(`.${currentPlanet} ~ a`).classList.toggle('active');
// add active class to new planet
e.classList.toggle('active');
// set current planet to new planet
currentPlanet = e.innerHTML.toLowerCase();
// reset current section to 'overview'
currentSection = 'overview';
// resets secondary menu to 'overview' section
setSecondary('#overview');
});
});
// add event listener to secondary navigation links
navbarSecondaryMobile.querySelectorAll('a')
.forEach((e) => {
e.addEventListener('click', () => {
let sectionName = e.innerHTML.toLowerCase();
if (sectionName === 'surface') {
sectionName = 'geology';
}
// render data for section requested
renderHtml(currentPlanet, sectionName);
// sets mobile and desktop secondary menus to section requested
setSecondary(`#${sectionName}`);
})
})
navbarSecondaryDesktop.querySelectorAll('a')
.forEach((e) => {
e.addEventListener('click', () => {
let sectionName = e.innerHTML.toLowerCase();
if (sectionName.includes('overview')) {
sectionName = 'overview';
} else if (sectionName.includes('structure')) {
sectionName = 'structure';
} else if (sectionName.includes('geology')) {
sectionName = 'geology';
}
// render data for section requested
renderHtml(currentPlanet, sectionName);
// sets mobile and desktop secondary menus to section requested
setSecondary(`#${sectionName}`);
})
})
// sets mobile and desktop secondary menus to section requested
function setSecondary(sectionRequested) {
if (sectionRequested != currentSection) {
// sets secondary menu (mobile) to sectionRequested
navbarSecondaryMobile.querySelectorAll('a').forEach((element) => {
if (element.classList.contains('active')) {
element.classList.remove('active');
}
})
navbarSecondaryMobile.querySelector(sectionRequested).classList.add('active');
// sets secondary menu (desktop) to sectionRequested
navbarSecondaryDesktop.querySelectorAll('li').forEach((element) => {
if (element.style.backgroundColor != 'transparent') {
element.style.backgroundColor = 'transparent';
};
});
navbarSecondaryDesktop.querySelector(sectionRequested).parentElement.style.backgroundColor = returnPlanetColor();
}
}
// returns colour using currentPlanet
function returnPlanetColor() {
return `var(--${currentPlanet}`;
}
// read data from json file and return as objects
async function readJsonFile() {
const data = await fetch('./data.json')
return data.json();
}
// search through array of objects using
// return single object where key name matches planet name
async function searchJson(key, value) {
const json = await readJsonFile();
for (let i = 0; i < json.length; i++) {
if (json[i][key].toLowerCase() === value.toLowerCase()) {
return json[i];
};
};
};
// refactor planetObject to match placeholders in mustache template
// returns newObject where data matches section called by click
function processObject(section, object) {
let image = section === 'structure' ? 'internal' : 'planet';
const {name, [section]: {content, source}, images, height} = object;
return {
name,
content: content,
source: source,
imageSource: images[`${image}`],
imageHeight: height
};
}
// fetch mustache template from file using fileName
// convert to text and return
async function renderTemplate(templateName) {
const response = await fetch(`./${templateName}.mustache`);
const template = await response.text();
return template;
}
// use template and object to modify innerHTML
async function renderHtml(planetName, sectionName = 'overview') {
const data = await searchJson('name', planetName);
const refactoredObject = processObject(sectionName, data);
mainText.innerHTML = Mustache.render(await renderTemplate('main_text'), refactoredObject);
mainImage.innerHTML = Mustache.render(await renderTemplate('main_image'), refactoredObject);
footer.innerHTML = Mustache.render(await renderTemplate('footer'), data);
if (sectionName === 'geology') {
const {images: {[sectionName]: imageUrl}} = data;
appendImage(imageUrl)
}
};
// append image to the main image using specific image url
function appendImage(imageUrl) {
const imageContainer = mainImage.querySelector('div');
const imageElement = document.createElement('img');
imageElement.setAttribute('src', imageUrl);
imageElement.setAttribute('class', "position-absolute top-50 h-50");
imageContainer.appendChild(imageElement);
}