galleries.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Lee Morgan -gallery-</title>
  6. <link rel="icon" type="img/ico" href="/images/favicon">
  7. <link rel="stylesheet" href="/gallery/style">
  8. </head>
  9. <body id="galleryBody">
  10. <h1>Gallery</h1>
  11. <div id="tags"></div>
  12. <div id="container"></div>
  13. <template id="card">
  14. <a class="card">
  15. <h2></h2>
  16. <img>
  17. </a>
  18. </template>
  19. <script>
  20. let container = document.getElementById("container");
  21. let template = document.getElementById("card").content.children[0];
  22. let populateGalleries = (galleries)=>{
  23. while(container.children.length > 0){
  24. container.removeChild(container.firstChild);
  25. }
  26. for(let i = 0; i < galleries.length; i++){
  27. let card = template.cloneNode(true);
  28. card.href = `/gallery/${galleries[i]._id}`;
  29. card.children[0].innerText = galleries[i].title;
  30. card.children[1].src = galleries[i].images[0];
  31. card.children[1].alt = `${galleries[i].title} thumbnail`;
  32. container.appendChild(card);
  33. }
  34. }
  35. let searchTags = (galleries, tag)=>{
  36. let galleryList = [];
  37. for(let i = 0; i < galleries.length; i++){
  38. for(let j = 0; j < galleries[i].tags.length; j++){
  39. if(galleries[i].tags[j].toLowerCase() === tag.toLowerCase()){
  40. galleryList.push(galleries[i]);
  41. break;
  42. }
  43. }
  44. }
  45. return galleryList;
  46. }
  47. fetch("/gallery/retrieve")
  48. .then(response => response.json())
  49. .then((response)=>{
  50. let tagsContainer = document.getElementById("tags");
  51. let tags = [];
  52. populateGalleries(response);
  53. let tag = document.createElement("button");
  54. tag.innerText = "All";
  55. tag.onclick = ()=>{populateGalleries(response)};
  56. tagsContainer.appendChild(tag);
  57. for(let i = 0; i < response.length; i++){
  58. for(let j = 0; j < response[i].tags.length; j++){
  59. if(tags.includes(response[i].tags[j].toLowerCase())) continue;
  60. tags.push(response[i].tags[j].toLowerCase());
  61. let tag = document.createElement("button");
  62. tag.innerText = response[i].tags[j];
  63. tag.onclick = ()=>{populateGalleries(searchTags(response, response[i].tags[j]))};
  64. tagsContainer.appendChild(tag);
  65. }
  66. }
  67. })
  68. .catch((err)=>{});
  69. </script>
  70. </body>
  71. </html>