search.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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.label);
  22. res_tn.classList.add("result_tn");
  23. res_tn.classList.add("float-left");
  24. const res_icon = document.createElement("img");
  25. res_icon.setAttribute("src", rsrc.icon);
  26. res_icon.setAttribute("alt", rsrc.content_type);
  27. res_icon.classList.add("body_icon");
  28. const res_title_txt = document.createTextNode(rsrc.label || rsrc.fname);
  29. const res_link = document.createElement("a");
  30. res_link.setAttribute("href", window.webroot + rsrc.href);
  31. res_link.appendChild(res_icon);
  32. res_link.appendChild(res_title_txt);
  33. const res_title = document.createElement("h4");
  34. res_title.appendChild(res_link);
  35. const res_id_txt = document.createTextNode("ID: " + rsrc.id);
  36. res_id = document.createElement("p");
  37. res_id.appendChild(res_id_txt);
  38. const res_date_txt = document.createTextNode("Submitted: " + rsrc.submitted);
  39. res_date = document.createElement("p");
  40. res_date.appendChild(res_date_txt);
  41. const res_box = document.createElement("div");
  42. res_box.classList.add("column");
  43. res_box.classList.add("search_item");
  44. res_box.appendChild(res_title);
  45. res_box.appendChild(res_id);
  46. res_box.appendChild(res_date);
  47. const res_li = document.createElement("li");
  48. res_li.setAttribute("id", "sres-" + rsrc.id);
  49. res_li.classList.add("row");
  50. res_li.classList.add("clearfix");
  51. res_li.appendChild(res_tn);
  52. res_li.appendChild(res_box);
  53. res_el.appendChild(res_li);
  54. });
  55. // Reveal section after populating.
  56. const res_sec = document.getElementById('search_results');
  57. res_sec.classList.remove("hidden");
  58. })();
  59. }