showcase.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. const ShowCase=function(showCaseEl){
  2. console.log("Init showcase for",showCaseEl);
  3. const mode=showCaseEl.getAttribute("mode");
  4. this.getYoutubeVideos=()=>{
  5. const els = showCaseEl.querySelectorAll("iframe.showcaseElement.youtubeVid");
  6. return els;
  7. };
  8. this.getVideos=()=>{
  9. const els = showCaseEl.querySelectorAll("video.showcaseElement");
  10. return els;
  11. };
  12. this.getImages=()=>{
  13. const els = showCaseEl.querySelectorAll(".cover:not(.blur)");
  14. return els;
  15. };
  16. this.playCurrent=()=>{
  17. if(mode=="gallery"){
  18. this.getImages().forEach(el=>el.classList.add("gallery"));
  19. }else{
  20. this.getImages().forEach(el=>el.classList.remove("gallery"));
  21. }
  22. lazyLoad(showCaseEl);
  23. this.getVideos().forEach(el=>{
  24. if(isVisible(el)){
  25. if(mode=="gallery"){
  26. el.classList.add("gallery");
  27. el.setAttribute("controls", "");
  28. el.removeAttribute("loop");
  29. el.removeAttribute("autoplay");
  30. el.removeAttribute("muted");
  31. el.volume=1.0;
  32. el.muted = false;
  33. }else{
  34. el.play();
  35. el.removeAttribute("controls", "");
  36. el.setAttribute("loop");
  37. el.setAttribute("muted");
  38. el.setAttribute("autoplay");
  39. el.classList.remove("gallery");
  40. el.muted = true;
  41. }
  42. }else{
  43. el.pause();
  44. el.currentTime=0;
  45. }
  46. });
  47. this.ytReloadInc=0;
  48. this.getYoutubeVideos().forEach(el=>{
  49. let osrc=el.getAttribute("osrc");
  50. if(!osrc){
  51. const src=el.getAttribute("src");
  52. if(!src)return;
  53. osrc=src;
  54. el.setAttribute("osrc",osrc);
  55. }
  56. if(isVisible(el)){
  57. if(mode=="gallery"){
  58. el.src = osrc+"?controls=1&autoplay=0&mute=0&modestbranding=1&disablekb=1&"+(this.ytReloadInc++);
  59. }else{
  60. el.src = osrc+"?controls=0&autoplay=1&mute=1&modestbranding=1&disablekb=1&"+(this.ytReloadInc++);
  61. }
  62. }else{
  63. el.src = osrc+"?"+(this.ytReloadInc++);
  64. }
  65. });
  66. };
  67. this.currentShowcaseElement = 0;
  68. this.cycleShowCase = (direction) => {
  69. const els = showCaseEl.querySelectorAll(".showcaseElement");
  70. els.forEach(el=>el.style.display = "none");
  71. this.currentShowcaseElement+=direction;
  72. console.log("Select", this.currentShowcaseElement);
  73. if (this.currentShowcaseElement < 0) this.currentShowcaseElement = 0;
  74. else if (this.currentShowcaseElement > els.length - 1) this.currentShowcaseElement = els.length - 1;
  75. const el = els[this.currentShowcaseElement];
  76. el.style.display = "block";
  77. this.playCurrent();
  78. }
  79. if(mode=="gallery"){
  80. const showCasePrev=document.createElement("i");
  81. const showCaseNext=document.createElement("i");
  82. showCasePrev.setAttribute("id","showCasePrev");
  83. showCasePrev.classList.add("fas");
  84. showCasePrev.classList.add("fa-chevron-left");
  85. showCaseNext.setAttribute("id","showCaseNext");
  86. showCaseNext.classList.add("fas");
  87. showCaseNext.classList.add("fa-chevron-right");
  88. showCaseEl.append(showCasePrev);
  89. showCaseEl.append(showCaseNext);
  90. showCasePrev.addEventListener("click", (ev) => {
  91. ev.stopPropagation();
  92. this.cycleShowCase(-1)
  93. });
  94. showCaseNext.addEventListener("click", (ev) => {
  95. ev.stopPropagation();
  96. this.cycleShowCase(1)
  97. });
  98. showCaseEl.addEventListener("click", () => showCaseEl.requestFullscreen());
  99. }
  100. this.playCurrent();
  101. }
  102. window.addEventListener("DOMContentLoaded", function () {
  103. document.querySelectorAll("#showcase").forEach(el=>new ShowCase(el));
  104. });