showcase.js 3.6 KB

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