-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.js
171 lines (147 loc) · 5.84 KB
/
menu.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
/* Pagination */
const itemCardTemplate = document.querySelector("[data-item-template]");
const itemCardContainer = document.querySelector("[data-item-cards-container]");
const paginationContainer = document.getElementById('pagination');
const categorySelect = document.querySelector('.category-select');
const itemsPerPage = 12;
let currentPage = 1;
let menuData = [];
let filteredData = [];
// Fetch menu data and initialize
fetch('menu.json')
.then(res => res.json())
.then(data => {
menuData = data;
filteredData = menuData; // Initially display all items
displayMenu(currentPage);
setupPagination(filteredData.length);
})
.catch(error => console.error('Error fetching menu:', error));
// Function to filter items by category
function filterByCategory(category) {
const reminder = document.querySelector(".none-reminder")
if (category === 'All') {
filteredData = menuData;
} else {
filteredData = menuData.filter(item => item.category === category);
}
if (filteredData.length == 0){
reminder.innerText = "Sorry, we currently don't have any item in this category."
reminder.style.height = '200px';
reminder.style.padding = '2rem';
} else {
reminder.innerText = ""
reminder.style.height = '0';
reminder.style.padding = '0';
}
currentPage = 1; // Reset to first page
setupPagination(filteredData.length);
displayMenu(currentPage);
}
// Function to display menu items for a given page
function displayMenu(page) {
itemCardContainer.innerHTML = '';
const startIndex = (page - 1) * itemsPerPage;
const endIndex = startIndex + itemsPerPage;
const paginatedItems = filteredData.slice(startIndex, endIndex);
paginatedItems.forEach(item => {
const card = itemCardTemplate.content.cloneNode(true).children[0];
const name = card.querySelector("[data-header]");
const price = card.querySelector("[data-price]");
const image = card.querySelector("[data-image]");
name.textContent = item.name;
price.textContent = item.price;
if (item.image) {
image.src = item.image; // Assuming `item.image` is a URL to the image
image.alt = item.name;
} else {
image.style.display = 'none'; // Hide the image element if the URL is empty
}
itemCardContainer.appendChild(card);
});
}
// Function to setup pagination controls
function setupPagination(totalItems) {
paginationContainer.innerHTML = '';
const totalPages = Math.ceil(totalItems / itemsPerPage);
for (let i = 1; i <= totalPages; i++) {
const pageButton = document.createElement('button');
pageButton.innerText = i;
if(i === currentPage){
pageButton.classList.toggle('page-selected');
}
pageButton.addEventListener('click', () => {
currentPage = i;
displayMenu(currentPage);
updatePaginationActiveState(totalPages);
});
paginationContainer.appendChild(pageButton);
}
}
function updatePaginationActiveState(totalPages) {
const pageButtons = paginationContainer.querySelectorAll('button');
pageButtons.forEach((button,index) => {
if(index+1=== currentPage){
button.classList.add('page-selected');
} else{
button.classList.remove('page-selected');
}
});
}
// search bar, hide whatever doesn't match, case insensitive
/* dropdown menu */
// get all dropdowns from the document
const dropdowns = document.querySelectorAll('.dropdown');
//loop through all dropdown elements
dropdowns.forEach(dropdown => {
//get inner elements from each dropdown
const select = dropdown.querySelector('.select');
const caret = dropdown.querySelector('.caret');
const menu = dropdown.querySelector('.menu');
const options = dropdown.querySelectorAll('.menu li');
const selected = dropdown.querySelector('.category-selected');
// add click event to the select element
select.addEventListener('click', () => {
//add clicked select styles to the select element
select.classList.toggle('select-clicked');
//add the rotate styles to the caret element
caret.classList.toggle('caret-rotate');
//add the open style to the menu element
menu.classList.toggle('menu-open');
})
//loop through all option elements
options.forEach(option => {
// add click event to the option element
option.addEventListener('click',() =>{
// change selected inner text to clicked option inner text
selected.innerText = option.innerText;
filterByCategory(option.innerText);
// remove the clicked select styles to the select element
select.classList.remove('select-clicked');
// remove the rotate style to the caret element
caret.classList.remove('caret-rotate');
// remove the open style to the menu element
menu.classList.remove('menu-open');
// remove active class from all option elements
options.forEach(option => {
option.classList.remove('active');
})
// add active class to clicked option element
option.classList.add('active');
})
})
})
// Close dropdowns if clicked outside
window.addEventListener('click', (e) => {
dropdowns.forEach(dropdown => {
const select = dropdown.querySelector('.select');
const caret = dropdown.querySelector('.caret');
const menu = dropdown.querySelector('.menu');
if (!dropdown.contains(e.target)) {
select.classList.remove('select-clicked');
caret.classList.remove('caret-rotate');
menu.classList.remove('menu-open');
}
});
});
/* Dropdown menu selection */