-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
100 lines (81 loc) · 2.85 KB
/
Copy pathscript.js
File metadata and controls
100 lines (81 loc) · 2.85 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
let dataset = [];
async function loadData(){
try{
const res = await fetch('data.json');
dataset = await res.json();
}catch(e){
console.error('Failed to load data.json', e);
}
}
function renderResults(items){
const container = document.getElementById('results');
container.innerHTML = '';
if(!items || items.length === 0){
container.innerHTML = '<p class="no-results">No results found.</p>';
return;
}
const grid = document.createElement('div');
grid.className = 'grid';
items.forEach(it => {
const card = document.createElement('div');
card.className = 'card';
const img = document.createElement('img');
img.src = it.image;
img.alt = it.title;
const body = document.createElement('div');
body.className = 'card-body';
const title = document.createElement('h2');
title.textContent = it.title;
const desc = document.createElement('p');
desc.textContent = it.description;
const cat = document.createElement('p');
cat.className = 'cat';
cat.textContent = it.category;
body.appendChild(title);
body.appendChild(desc);
body.appendChild(cat);
card.appendChild(img);
card.appendChild(body);
grid.appendChild(card);
});
container.appendChild(grid);
}
function matchCategoryQuery(q){
q = q.toLowerCase();
if(q.includes('beach')) return 'Beaches';
if(q.includes('beaches')) return 'Beaches';
if(q.includes('temple')) return 'Temples';
if(q.includes('temples')) return 'Temples';
if(q.includes('country') || q.includes('countries')) return 'Countries';
return null;
}
function search(query){
if(!dataset || dataset.length === 0) return renderResults([]);
query = (query || '').trim();
if(query === '') return renderResults([]);
const q = query.toLowerCase();
const cat = matchCategoryQuery(q);
let results = [];
if(cat){
results = dataset.filter(d => d.category.toLowerCase() === cat.toLowerCase());
} else {
results = dataset.filter(d =>
d.title.toLowerCase().includes(q) ||
d.description.toLowerCase().includes(q) ||
d.category.toLowerCase().includes(q)
);
}
// ensure we show at least two suggestions when possible
if(results.length === 1){
const extra = dataset.find(d => d.category.toLowerCase() === results[0].category.toLowerCase() && d.title !== results[0].title);
if(extra) results.push(extra);
}
renderResults(results.slice(0, 10));
}
document.addEventListener('DOMContentLoaded', async ()=>{
await loadData();
const searchInput = document.getElementById('searchInput');
document.getElementById('searchBtn').addEventListener('click', ()=> search(searchInput.value));
document.getElementById('clearBtn').addEventListener('click', ()=>{ searchInput.value = ''; renderResults([]); });
searchInput.addEventListener('keydown', (e)=>{ if(e.key === 'Enter') search(searchInput.value); });
});