|
1 | | -(function() { |
2 | | - function displaySearchResults(results, store) { |
3 | | - var searchResults = document.getElementById('search-results'); |
4 | | - |
5 | | - if (results.length) { |
6 | | - var appendString = ''; |
7 | | - |
8 | | - for (var i = 0; i < results.length; i++) { |
9 | | - var item = store[results[i].ref]; |
10 | | - appendString += '<h3><a href="' + item.url + '">' + item.title + '</a></h3>'; |
11 | | - appendString += '<p>' + item.content.substring(0, 150) + '...</p>'; |
12 | | - } |
13 | | - |
14 | | - searchResults.innerHTML = appendString; |
15 | | - } else { |
16 | | - searchResults.innerHTML = '<h3>No results found</h3>'; |
17 | | - } |
| 1 | +document.addEventListener('DOMContentLoaded', function () { |
| 2 | + const searchForm = document.getElementById('search-form'); |
| 3 | + const searchOverlay = document.getElementById('search-overlay'); |
| 4 | + const closeBtn = document.getElementById('close-search'); |
| 5 | + const searchFrame = document.getElementById('search-frame'); |
| 6 | + |
| 7 | + if (searchForm) { |
| 8 | + searchForm.addEventListener('submit', function () { |
| 9 | + searchOverlay.style.display = 'flex'; |
| 10 | + }); |
18 | 11 | } |
19 | 12 |
|
20 | | - function getQueryVariable(variable) { |
21 | | - var query = window.location.search.substring(1); |
22 | | - var vars = query.split('&'); |
23 | | - |
24 | | - for (var i = 0; i < vars.length; i++) { |
25 | | - var pair = vars[i].split('='); |
26 | | - |
27 | | - if (pair[0] === variable) { |
28 | | - return decodeURIComponent(pair[1].replace(/\+/g, '%20')); |
29 | | - } |
30 | | - } |
| 13 | + if (closeBtn) { |
| 14 | + closeBtn.addEventListener('click', function () { |
| 15 | + searchOverlay.style.display = 'none'; |
| 16 | + // Optional: reset frame |
| 17 | + // searchFrame.src = "about:blank"; |
| 18 | + }); |
31 | 19 | } |
32 | 20 |
|
33 | | - var searchTerm = getQueryVariable('q'); |
34 | | - |
35 | | - if (searchTerm) { |
36 | | - document.getElementById('search-results').innerHTML = 'Searching...'; |
37 | | - |
38 | | - // In a real implementation, you might want to fetch the JSON first |
39 | | - fetch('/search.json') |
40 | | - .then(response => response.json()) |
41 | | - .then(data => { |
42 | | - var idx = lunr(function () { |
43 | | - this.field('id'); |
44 | | - this.field('title', { boost: 10 }); |
45 | | - this.field('content'); |
46 | | - |
47 | | - for (var key in data) { // In this simple version, data is array |
48 | | - this.add({ |
49 | | - 'id': key, |
50 | | - 'title': data[key].title, |
51 | | - 'content': data[key].content |
52 | | - }); |
53 | | - } |
54 | | - }); |
| 21 | + // Close on click outside content |
| 22 | + searchOverlay.addEventListener('click', function (e) { |
| 23 | + if (e.target === searchOverlay) { |
| 24 | + searchOverlay.style.display = 'none'; |
| 25 | + } |
| 26 | + }); |
55 | 27 |
|
56 | | - var results = idx.search(searchTerm); |
57 | | - // Map results back to the original data |
58 | | - // The ref in results is the index in the data array |
59 | | - // We need to pass the data store to display function |
60 | | - // But formatting explicitly: |
61 | | - var enrichedResults = results.map(function(result) { |
62 | | - return { |
63 | | - ref: result.ref, // This is the index (0, 1, 2...) |
64 | | - score: result.score |
65 | | - }; |
66 | | - }); |
67 | | - |
68 | | - displaySearchResults(enrichedResults, data); |
69 | | - }) |
70 | | - .catch(err => { |
71 | | - console.error("Error fetching search data: ", err); |
72 | | - document.getElementById('search-results').innerHTML = 'Error loading search.'; |
73 | | - }); |
74 | | - } |
75 | | -})(); |
| 28 | + // Close on Escape key |
| 29 | + document.addEventListener('keydown', function (e) { |
| 30 | + if (e.key === 'Escape' && searchOverlay.style.display === 'flex') { |
| 31 | + searchOverlay.style.display = 'none'; |
| 32 | + } |
| 33 | + }); |
| 34 | +}); |
0 commit comments