-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrestaurants.js
155 lines (114 loc) · 4.84 KB
/
restaurants.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
const entityMap = {
"&": "&",
"<": "<",
">": ">",
'"': '"',
"'": ''',
"/": '/'
};
function escapeHtml(string) {
return String(string).replace(/[&<>"'\/]/g, function (s) {
return entityMap[s];
});
}
let slideBars = document.querySelectorAll(".slideBar")
const searchInput = document.getElementById("searchbar")
searchInput.placeholder = "Search for Restaurants"
function fixSlideBar(e){
let min = parseInt(document.querySelector(".slideBar.min").value,10)
let max = parseInt(document.querySelector(".slideBar.max").value,10)
if(min>=max-14){
if(e.target.classList.contains("min")){
min=max-15
e.target.value=min
}
else{
max=min+15
e.target.value=max
}
}
document.querySelector(".range").style.width = (max-min)+"%"
document.querySelector(".range").style.marginLeft = min+"%"
document.querySelector(".minScore").style.marginLeft = min+"%"
document.querySelector(".minScore").innerHTML=min
document.querySelector(".maxScore").style.marginLeft = max+"%"
document.querySelector(".maxScore").innerHTML=max
//spaghetti code to center text with button
document.querySelector(".maxScore").style.right= (-(100-max)*0.005+0.4*(Math.floor(Math.log(max)/Math.log(10))+1))+"em"
document.querySelector(".minScore").style.right= (min*0.005+0.4*(Math.floor(Math.log(min)/Math.log(10))))+"em"
}
for(const slideBar of slideBars){
slideBar.addEventListener('change',async function(){
updatePage()
})
slideBar.addEventListener('input',function(e){
fixSlideBar(e)
})
}
let priceButtons = document.querySelectorAll(".price")
for(const priceButton of priceButtons){
priceButton.addEventListener('click',async function(e){
const removeSelected = e.target.classList.contains("checked")
for(const checkedButtons of document.querySelectorAll(".price.checked")){
checkedButtons.classList.remove("checked")
}
if(!removeSelected)
priceButton.classList.add("checked")
updatePage()
})
}
let sortRadios = document.querySelectorAll(".sort")
for(const sortRadio of sortRadios){
sortRadio.addEventListener('click',async function(){
updatePage()
})
}
async function updatePage(){
let checkedPrice = document.querySelector(".price.checked")??-1
if(checkedPrice!==-1)
checkedPrice = checkedPrice.classList[1]
const response = await fetch('api_restaurants.php?search='+(searchInput.value??"")+ '&minScore=' +
(document.querySelector(".slideBar.min").value??0) + '&maxScore='+(document.querySelector(".slideBar.max").value??100)+
'&priceMagnitude='+checkedPrice + '&sort='+document.querySelector(".sort:checked").classList[1])
const response_json = await response.json()
generateRestaurantsHTML(response_json[0],response_json[1])
}
function generateRestaurantsHTML(restaurants,restaurantsWithImages){
const listOfCategoryRestaurants = document.querySelector('#restaurants')
listOfCategoryRestaurants.innerHTML = ''
for (const category of Object.keys(restaurants).filter(x=>(x !== null && x !== undefined && x!==""))) {
const restaurantCategory = document.createElement('div')
restaurantCategory.classList.add("Category")
const categoryName = document.createElement('h1')
categoryName.innerHTML = "Category: " + escapeHtml(category)
restaurantCategory.appendChild(categoryName)
const categoryRestaurants = document.createElement('ul')
for(const restaurantInfo of restaurants[category]){
const img = document.createElement('img')
if(restaurantsWithImages.includes(restaurantInfo.idRestaurant))
img.src = 'imgs/restaurants/'+restaurantInfo.idRestaurant+'/header.jpg'
else
img.src = 'imgs/restaurants_background.png'
img.alt = escapeHtml(restaurantInfo.name)
const restaurant = document.createElement('li')
const nameAndAddress = document.createElement('p')
nameAndAddress.innerHTML= escapeHtml(restaurantInfo.name)+"<br>"+"Address: "+ escapeHtml(restaurantInfo.address)
const link = document.createElement('a')
link.href = 'restaurant.php?id=' + restaurantInfo.idRestaurant
link.appendChild(img)
link.appendChild(nameAndAddress)
restaurant.appendChild(link)
categoryRestaurants.appendChild(restaurant)
}
restaurantCategory.appendChild(categoryRestaurants)
listOfCategoryRestaurants.appendChild(restaurantCategory)
}
}
if(searchInput){
searchInput.addEventListener("input", async function() {
updatePage()
})
}
window.addEventListener('load', async function(){
updatePage()
});