search.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Query handling.
  3. */
  4. const params = new Proxy(new URLSearchParams(window.location.search), {
  5. get: (searchParams, prop) => searchParams.get(prop),
  6. });
  7. const qstring = params.q;
  8. if (qstring != undefined) {
  9. (async () => {
  10. // Re-fill query string in form field.
  11. const search_field = document.getElementById("qry_f");
  12. search_field.value = qstring;
  13. const fuse = await fuse_init();
  14. const results = fuse.search(qstring);
  15. console.log(results);
  16. const res_el = document.getElementById('result_list');
  17. results.forEach(sres => {
  18. const rsrc = sres.item;
  19. const res_tn = document.createElement("img");
  20. res_tn.setAttribute("src", window.webroot + rsrc.tn);
  21. res_tn.setAttribute("alt", rsrc.Title);
  22. res_txt = document.createTextNode(rsrc.Title);
  23. const res_link = document.createElement("a");
  24. res_link.setAttribute("href", window.webroot + rsrc.href);
  25. res_link.appendChild(res_tn);
  26. res_link.appendChild(res_txt);
  27. const res_li = document.createElement("li");
  28. res_li.setAttribute("id", "sres-" + rsrc.id);
  29. res_li.appendChild(res_link);
  30. res_el.appendChild(res_li);
  31. });
  32. // Reveal section after populating.
  33. const res_sec = document.getElementById('search_results');
  34. res_sec.classList.remove("hidden");
  35. })();
  36. }