display.html 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <link rel="stylesheet" href="/currency/style">
  6. <title>Lee Morgan</title>
  7. </head>
  8. <body>
  9. <div id="locations">
  10. <button id="allLocations" class="locationBtn selected">ALL</button>
  11. </div>
  12. <div id="container"></div>
  13. <template id="currency">
  14. <div class="currency">
  15. <h1 class="location"></h1>
  16. <p class="info"></p>
  17. <img class="frontImage">
  18. <img class="backImage">
  19. <p class="comment"></p>
  20. </div>
  21. </template>
  22. <script>
  23. let filterLocations = (location, currencies)=>{
  24. let container = document.getElementById("container");
  25. let template = document.getElementById("currency").content.children[0];
  26. while(container.children.length > 0){
  27. container.removeChild(container.firstChild);
  28. }
  29. for(let i = 0; i < currencies.length; i++){
  30. if(currencies[i].location.toLowerCase() === location || location === "all"){
  31. let currency = template.cloneNode(true);
  32. currency.querySelector(".location").innerText = currencies[i].location.toUpperCase();
  33. currency.querySelector(".info").innerText = `${currencies[i].value} ${currencies[i].name} (${currencies[i].year})`;
  34. currency.querySelector(".frontImage").src = currencies[i].frontImage;
  35. currency.querySelector(".backImage").src = currencies[i].backImage;
  36. currency.querySelector(".comment").innerText = currencies[i].comment;
  37. container.appendChild(currency);
  38. }
  39. }
  40. }
  41. fetch("/currency/data")
  42. .then(r=>r.json())
  43. .then((response)=>{
  44. document.getElementById("allLocations").onclick = ()=>{filterLocations("all", response)};
  45. let container = document.getElementById("container");
  46. let locationsContainer = document.getElementById("locations");
  47. let template = document.getElementById("currency").content.children[0];
  48. let locations = [];
  49. for(let i = 0; i < response.length; i++){
  50. let currency = template.cloneNode(true);
  51. currency.querySelector(".location").innerText = response[i].location.toUpperCase();
  52. currency.querySelector(".info").innerText = `${response[i].value} ${response[i].name} (${response[i].year})`;
  53. currency.querySelector(".frontImage").src = response[i].frontImage;
  54. currency.querySelector(".backImage").src = response[i].backImage;
  55. currency.querySelector(".comment").innerText = response[i].comment;
  56. container.appendChild(currency);
  57. if(!locations.includes(response[i].location.toLowerCase())){
  58. locations.push(response[i].location.toLowerCase());
  59. let button = document.createElement("button");
  60. button.classList.add("locationBtn");
  61. button.innerText = response[i].location.toUpperCase();
  62. button.onclick = ()=>{
  63. let pLocations = document.querySelectorAll(".locationBtn");
  64. for(let j = 0; j < pLocations.length; j++){
  65. pLocations[j].classList.remove("selected");
  66. }
  67. button.classList.add("selected");
  68. filterLocations(response[i].location.toLowerCase(), response);
  69. };
  70. locationsContainer.appendChild(button);
  71. }
  72. }
  73. })
  74. .catch((err)=>{});
  75. </script>
  76. </body>
  77. </html>