-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.html
More file actions
302 lines (253 loc) · 13.6 KB
/
Copy pathcode.html
File metadata and controls
302 lines (253 loc) · 13.6 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<title>Eventnet: A Coding Challange</title>
<!-- Google Fonts -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link
href="https://fonts.googleapis.com/css2?family=Allan:wght@400;700&family=Bebas+Neue&family=Fira+Sans:ital,wght@0,100;0,200;0,300;0,400;1,100;1,200;1,300;1,400&family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap" rel="stylesheet">
<!-- Bootstrap -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<!-- Bootstrap Icons -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.3/font/bootstrap-icons.css">
<!-- Devicons -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/devicons/devicon@v2.15.1/devicon.min.css">
<!-- Prism CSS -->
<link rel="stylesheet" href="/css/prism.css">
<!-- Custom CSS -->
<link rel="stylesheet" href="/css/site.css">
</head>
<body>
<header>
<nav id="mainNav" class="navbar navbar-expand-lg navbar-dark">
<div class="container">
<a class="navbar-brand" href="/index.html">
<img src="/img/Base Logo Vector Files Blue Red.svg" height="50" alt="">
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon-mrb">
<i class="bi bi-list"></i>
</span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ms-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link" aria-current="page" href="/index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/app.html">App</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="/code.html">The Code</a>
</li>
<li class="nav-item">
<!-- Change This Before Publishing -->
<a class="nav-link" href="https://github.com/MattBridges/Eventnet">The Repo</a>
</li>
<li class="nav-item">
<a class="nav-link" href="https://mattbridgesportfolio.netlify.app/">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Blog</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<main class="content container">
<div class="row">
<h1 class="border-bottom botder-2 border-dark">The Code</h1>
<!-- The Source Code -->
<div class="col-12 col-lg-8">
<pre class="line-numbers">
<code class="language-js">
//Test Data
var events = [{...}];
function BuildDropDown() {
//Get the dropdown menu from the page
let dropdownMenu = document.getElementById('eventDropDown');
//Empty the dropdown innerHTML to ensure theres nothing in it
dropdownMenu.innerHTML = '';
//Get our events
let currEvents = GetEventData();
//Pull out just the city names
let eventCities = currEvents.map((event) => event.city);
//Filter the cites to on distinct city names
let distinctCities = [...new Set(eventCities)];
//Get the template from the page
const template = document.getElementById('dropdownItemTemplate');
//Copy the template
let dropdownTemplateNode = document.importNode(template.content, true);
//Get the a tag from the template copy
let menuItem = dropdownTemplateNode.querySelector('a');
//change the text
menuItem.textContent = 'All Cities';
menuItem.setAttribute("data-string", "All");
//Add item to the page
dropdownMenu.appendChild(dropdownTemplateNode);
for (let i = 0; i < distinctCities.length; i++) {
let cityMenuItem = document.importNode(template.content, true);
let cityBtn = cityMenuItem.querySelector('a')
cityBtn.textContent = distinctCities[i];
cityBtn.setAttribute("data-string", distinctCities[i]);
dropdownMenu.appendChild(cityMenuItem);
}
DisplayStats(currEvents);
DisplayEventData(currEvents);
}
function DisplayStats(eventsArray) {
let calculatedValues = CalculateStats(eventsArray);
document.getElementById('total').textContent = calculatedValues.total.toLocaleString();
document.getElementById('average').textContent = calculatedValues.average.toLocaleString("en-US", { maximumFractionDigits: 0, minimumFractionDigits: 0 });
document.getElementById('most').textContent = calculatedValues.most.toLocaleString();
document.getElementById('least').textContent = calculatedValues.least.toLocaleString();
}
function CalculateStats(eventsArray) {
let finalValues = {
total: 0,
average: 0,
most: eventsArray[0].attendance,
least: eventsArray[0].attendance
}
for (let i = 0; i < eventsArray.length; i++) {
let currentEvent = eventsArray[i];
//Calculate total and update the object
finalValues.total += currentEvent.attendance;
//test for most and update the object
finalValues.most = finalValues.most < currentEvent.attendance ? currentEvent.attendance : finalValues.most;
//test for least and update the object
finalValues.least = finalValues.least > currentEvent.attendance ? currentEvent.attendance : finalValues.least;
}
//Calculate the average
finalValues.average = finalValues.total / eventsArray.length;
return finalValues;
}
function DisplayEventData(eventsArray) {
let tableBody = document.getElementById('eventTableBody');
const tableRowTemplate = document.getElementById('eventTableRowTemplate');
tableBody.innerHTML = '';
for (let i = 0; i < eventsArray.length; i++) {
let eventRow = document.importNode(tableRowTemplate.content, true);
let currentEvent = eventsArray[i];
let tableCells = eventRow.querySelectorAll('td');
tableCells[0].textContent = currentEvent.event;
tableCells[1].textContent = currentEvent.city;
tableCells[2].textContent = currentEvent.state;
tableCells[3].textContent = currentEvent.attendance.toLocaleString();
tableCells[4].textContent = currentEvent.date;
tableBody.appendChild(eventRow);
}
}
function GetEventData(){
let currentEvents = JSON.parse(localStorage.getItem('eventnetEventData'));
if(currentEvents == null){
currentEvents = events;
localStorage.setItem('eventnetEventData', JSON.stringify(currentEvents));
}
return currentEvents;
}
function GetEvents(element){
let currentEvents = GetEventData();
let cityName = element.getAttribute('data-string');
let filteredEvents = currentEvents;
if(cityName != 'All'){
filteredEvents = currentEvents.filter(
function(event){
if(cityName == event.city){
return event;
}}
);
}
document.getElementById('statsHeader').textContent = cityName;
DisplayStats(filteredEvents);
DisplayEventData(filteredEvents);
}
function SaveEventData(){
let eventName = document.getElementById('newEventName').value;
let cityName = document.getElementById('newEventCity').value;
let eventAttendance = parseInt(document.getElementById('newEventAttendance').value);
let eventDate = document.getElementById('newEventDate').value;
eventDate = `${eventDate} 00:00`;
eventDate = new Date(eventDate).toLocaleDateString();
let stateSelect = document.getElementById('newEventState')
let state = stateSelect.options[stateSelect.selectedIndex].text;
let newEvent = {
event: eventName,
city: cityName,
state: state,
attendance: eventAttendance,
date: eventDate,
}
let currentEvents = GetEventData();
currentEvents.push(newEvent);
localStorage.setItem("eventnetEventData",JSON.stringify(currentEvents));
BuildDropDown();
document.getElementById('statsHeader').textContent = 'All';
document.getElementById('newEventForm').requestFullscreen();
}
</code>
</pre>
</div>
<div class="col-12 col-lg-4">
<p></p>
<h5><code class="code-writeup">BuildDropDown()</code></h5>
<p>To build our dropdown at the top of the page we iterate through all of our sample data, and pull each distinct city name out. Once we have our city list, we grab the dropdown button template from html and create a button for each one. Once we have finished building our dropdown we then call our display functions for the stats section and event data table.</p>
<h5><code class="code-writeup">CalculateStats()</code></h5>
<p>To calculate the information for our stats area we pass in our array of event data. After intintializing an object to store all of our calculated information, we iterate through the event data an make the appropriate calculations for each value in the finalValues object. We then return the final values object to the <code>DisplayStats()</code> function.</p>
<h5><code class="code-writeup">DisplayStats()</code></h5>
<p>To display the stats information at the top left we set the textcontent of each element to its corespinding value from the finalValues object we passed in</p>
<h5><code class="code-writeup">GetEventData()</code></h5>
<p>To initialize the event data we first check if there is any current events data stored localy on the users machine. If there isnt, we initialze the events from our test data, stringify that with JSON and save it to local storage.</p>
<h5><code class="code-writeup">DisplayEventData()</code></h5>
<p>To get the table information onto the page, we grab the tableBody and tableRowTemplate elements from html. We then clear the table to make sure there is not any data to show and iterate through our events array using the template to build each row, then appending that row to the bottom of our table body.</p>
<h5><code class="code-writeup">GetEvents()</code></h5>
<p>This function lets us filter through our events and pull out each one based on the cityname data-string attribute.</p>
<h5><code class="code-writeup">SaveEventData()</code></h5>
<p>Finally to wrap this up we include a save data funtion allowing the user to locally save any data that they have entered themselves. </p>
</div>
</div>
</main>
<footer class="container-fluid py-3">
<div class="container">
<div class="row row-cols-1 row-cols-lg-3 align-items-center order-last order-lg-first">
<div class="col text-center text-lg-start ">©2023 Matt Bridges</div>
<div class="col text-center">
<img src="/img/Light_Grey_Signature_Long.svg" height="40" alt="">
</div>
<div class="col d-flex justify-content-center justify-content-lg-end my-2">
<a href="https://www.linkedin.com/in/themattbridges/" target="_blank" class="socialicons"><i
class="bi bi-linkedin"></i></a>
<a href="https://twitter.com/MattBridgesDev" target="_blank" class="socialicons"><i
class="bi bi-twitter"></i></a>
<a href="https://www.instagram.com/mattbridgesdev/" target="_blank" class="socialicons"><i
class="bi bi-instagram"></i></a>
<a href="https://github.com/MattBridges" target="_blank" class="socialicons"><i
class="bi bi-github"></i></a>
</div>
</div>
</div>
</footer>
<!-- Scripts -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"
integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN"
crossorigin="anonymous"></script>
<!-- Prism Script -->
<script src="/js/prism.js"></script>
<script>
Prism.plugins.NormalizeWhitespace.setDefaults({
'remove-trailing': true,
'remove-indent': true,
'left-trim': true,
'right-trim': true
})
</script>
</body>
</html>