-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
195 lines (170 loc) · 6 KB
/
app.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
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
'use strict';
var allLocations = [ ];
var hoods = [
'Downtown',
'Belltown',
'Pioneer Square',
'Capitol Hill',
'First Hill',
'SLU',
'U District',
'Uptown',
'Greenlake',
'SODO'
];
//Object constructor for locations
var Location = function(obj) {
this.name = obj.name;
this.hood = obj.hood;
this.address = obj.address;
this.mealType = obj.meal;
this.daysOpen = obj.days;
this.openTime = obj.openTime;
this.closeTime = obj.closeTime;
this.restrictions = obj.restrictions;
this.reservations = obj.reservations || 0;
allLocations.push(this);
};
function instantiateLocations() {
for(var i = 0; i < breakfastLocationData.length; i++) {
new Location(breakfastLocationData[i]);
new Location(lunchLocationData[i]);
new Location(dinnerLocationData[i]);
}
};
instantiateLocations();
var map;
function googleMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 47.6050, lng: -122.3344},
zoom: 14
});
};
// start filtering meal type locations IF CLICKED. saving for now just in case
// var breakfast = document.getElementById("breakfast");
// var lunch = document.getElementById("lunch");
// var dinner = document.getElementById("dinner");
//display all Breakfast locations when clicked
// function clickBreakfast(event) {
// for(var i = 0; i < breakfastLocationData.length; i++) {
// alert(breakfastLocationData[i].name);
// }
// }
// breakfast.addEventListener("click", clickBreakfast);
// ^ end filtering click event for meal types ^
//helper function to create table elements
function makeAnElementWithText(element, textContent, parent) {
var childEl = document.createElement(element);
childEl.textContent = textContent;
parent.appendChild(childEl);
};
function setClassOfAddressCells(element, textContent, parent) {
var childEl = document.createElement(element);
childEl.setAttribute('class', 'address');
childEl.textContent = textContent;
parent.appendChild(childEl);
};
//helper function to create reservation button
function makeAReservationButton(idName, parent) {
var buttonEl = document.createElement('button');
buttonEl.setAttribute('id', idName);
buttonEl.textContent = 'Make a Reservation';
parent.appendChild(buttonEl);
};
//helper function to create table rows
function createRow(idName, rowElement, El, tC1, tC2, tC3, tC4, tC5) {
var tableEl = document.getElementById(idName);
var rowEl = document.createElement(rowElement);
makeAnElementWithText(El, tC1, rowEl);
makeAnElementWithText(El, tC2, rowEl);
setClassOfAddressCells(El, tC3, rowEl);
makeAnElementWithText(El, tC4, rowEl);
makeAnElementWithText(El, tC5, rowEl);
makeAReservationButton(tC1, rowEl);
tableEl.appendChild(rowEl);
};
//loop to create table with object instances in allLocations array
function populateTable() {
var tableEl = document.getElementById('foodTable');
tableEl.innerHTML = ' ';
var rowEl = document.createElement('thead');
makeAnElementWithText('th', 'Location Name', rowEl);
makeAnElementWithText('th', 'Neighborhood', rowEl);
makeAnElementWithText('th', 'Address', rowEl);
makeAnElementWithText('th', 'Requirements', rowEl);
makeAnElementWithText('th', 'Meal', rowEl);
tableEl.appendChild(rowEl);
for (var i = 0; i < allLocations.length; i++) {
createRow('foodTable', 'tr', 'td', allLocations[i].name, allLocations[i].hood, allLocations[i].address, allLocations[i].restrictions, allLocations[i].mealType);
}
};
populateTable();
function filterTheTableByMeal(event) {
populateTable();
makeReservationEventListeners();
showAddressMarker();
var tableEl = document.getElementById('foodTable');
var tableCells = tableEl.getElementsByTagName('td');
for (var i = 0; i < tableCells.length; i++) {
if (tableCells[i].textContent === 'breakfast' || tableCells[i].textContent === 'lunch' || tableCells[i].textContent === 'dinner') {
if (tableCells[i].textContent !== event.target.value) {
tableCells[i].parentNode.setAttribute('class', 'hidden');
}
}
};
};
function eventListenerForMealSelection() {
var radioForm = document.getElementsByName('meal');
for (var i = 0; i < radioForm.length; i++) {
radioForm[i].addEventListener('change', filterTheTableByMeal);
}
};
eventListenerForMealSelection();
function filterTheTableByHood(event) {
populateTable();
makeReservationEventListeners();
showAddressMarker();
var tableEl = document.getElementById('foodTable');
var tableCells = tableEl.getElementsByTagName('td');
for (var i = 0; i < tableCells.length; i++) {
for (var j = 0; j < hoods.length; j++) {
if (tableCells[i].textContent === hoods[j]) {
if (tableCells[i].textContent !== event.target.value) {
tableCells[i].parentNode.setAttribute('class', 'hidden');
}
}
};
};
};
function eventListenerForHoodSelection() {
var dropDownList = document.getElementsByName('hoods');
dropDownList[0].addEventListener('change', filterTheTableByHood);
};
eventListenerForHoodSelection();
function reservationForm(event) {
var reservationClick = event.target.id;
var reservedLocation = [];
var thisLocation;
for (var i = 0; i < allLocations.length; i++) {
if (reservationClick === allLocations[i].name) {
allLocations[i].reservations += 1;
reservedLocation.push(allLocations[i]);
thisLocation = allLocations[i];
}
};
localStorage.setItem('reservation', JSON.stringify(reservedLocation));
localStorage.setItem('allLocations', JSON.stringify(allLocations));
localStorage.setItem('thisReservation', '');
localStorage.setItem('thisReservation', JSON.stringify(thisLocation));
window.location.assign('reservations.html');
};
function makeReservationEventListeners() {
for (var i = 0; i < allLocations.length; i++) {
document.getElementById(allLocations[i].name).addEventListener('click', reservationForm);
}
};
makeReservationEventListeners();
function newLocationButtonClick() {
window.location.assign('newLocation.html');
};
document.getElementById('newLocation').addEventListener('click', newLocationButtonClick);