-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
151 lines (138 loc) · 4.04 KB
/
script.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
const search = document.getElementById('search'),
submit = document.getElementById('submit'),
random = document.getElementById('random'),
selectCategory = document.getElementById('select'),
mealsEl = document.getElementById('meals'),
resultHeading = document.getElementById('result-heading'),
single_mealEl = document.getElementById('single-meal'),
Area = document.getElementById('area');
// Search meal and fetch from API
function searchMeal(e) {
e.preventDefault();
// Clear single meal
single_mealEl.innerHTML = '';
//Get search term
const query = search.value;
//check for empty
if (query.trim()) {
fetch(`https://www.themealdb.com/api/json/v1/1/search.php?s=${query}`)
.then(res => res.json())
.then(data => {
// console.log(data);
resultHeading.innerHTML = `<h2><u>Search results for '${query}'</u></h2>`;
if (data.meals === null) {
errorMsg();
} else {
mealsEl.innerHTML = data.meals
.map(
meal => `
<div class="meal">
<img src="${meal.strMealThumb}" alt="${meal.strMeal}" />
<div class="meal-info" data-MEALId=${meal.idMeal}>
<h3>${meal.strMeal}</h3>
</div>
</div>
`
)
.join('');
}
});
} else {
alert('Please enter a search term');
}
}
function errorMsg() {
document.body.innerHTML = `<div id="container">
<div class="content">
<h2>404</h2>
<h4>Oops! Page Not found</h4>
<p align="center">
The page you were looking for doesn't exist.
</p>
<a href="index.html">Back To Home</a>
</div>
</div>`;
var container = document.getElementById('container');
window.onmousemove = function (e) {
var x = -e.clientX / 5,
y = -e.clientY / 5;
container.style.backgroundPositionX = x + 'px';
container.style.backgroundPositionY = y + 'px';
};
}
submit.addEventListener('submit', searchMeal);
mealsEl.addEventListener('click', e => {
// console.log(e);
let val = e.path[0].dataset.mealid;
if (val) {
localStorage.setItem('val', JSON.stringify(val));
window.location.href = './description.html';
}
});
// Event listeners
random.addEventListener('click', () => {
window.location.href = './recipe.html';
});
selectCategory.addEventListener('click', () => {
window.location.href = './category.html';
});
Area.addEventListener('click', () => {
window.location.href = './area.html';
});
// Debounce
var listresult = document.querySelector('.resultsList');
var ignoreClickOnMeElement = document.getElementById('search');
document.addEventListener('click', function (event) {
var isClickInsideElement = ignoreClickOnMeElement.contains(event.target);
if (!isClickInsideElement) {
//Do something click is outside specified element
listresult.innerHTML = '';
}
});
let token = 0;
window.onload = () => {
if (search.value.length == 0) {
listresult.innerHTML = '';
}
search.onkeydown = event => {
// console.log(event);
if (event.Code == 'Backspace' && search.value.length == 0) {
listresult.innerHTML = '';
}
clearTimeout(token);
if (search.value.trim().length === 0) {
return;
}
token = setTimeout(() => {
searchShow(search.value);
}, 250);
};
};
async function searchShow(query) {
try {
var res = await fetch(
`https://www.themealdb.com/api/json/v1/1/search.php?s=${query}`
);
var data = await res.json();
// console.log(data.meals);
Render_results(data.meals);
} catch (error) {
console.log(error);
}
}
function Render_results(result) {
listresult.innerHTML = '';
result.forEach(data => {
const { strMeal, strMealThumb } = data;
const searchEl = document.createElement('div');
searchEl.classList.add('resultsList_items');
searchEl.innerHTML = `<img
src="${strMealThumb}"
alt=""
/>
<div class="Searchinfo">
<h3>${strMeal}</h3>
</div>`;
listresult.append(searchEl);
});
}