You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
7.8 KiB

  1. var makeBSS = function (el, options) {
  2. var $slideshows = document.querySelectorAll(el), // a collection of all of the slideshow
  3. $slideshow = {},
  4. Slideshow = {
  5. init: function (el, options) {
  6. options = options || {}; // if options object not passed in, then set to empty object
  7. options.auto = options.auto || false; // if options.auto object not passed in, then set to false
  8. this.opts = {
  9. selector: (typeof options.selector === "undefined") ? "figure" : options.selector,
  10. auto: (typeof options.auto === "undefined") ? false : options.auto,
  11. speed: (typeof options.auto.speed === "undefined") ? 1500 : options.auto.speed,
  12. pauseOnHover: (typeof options.auto.pauseOnHover === "undefined") ? false : options.auto.pauseOnHover,
  13. fullScreen: (typeof options.fullScreen === "undefined") ? false : options.fullScreen,
  14. swipe: (typeof options.swipe === "undefined") ? false : options.swipe
  15. };
  16. this.counter = 0; // to keep track of current slide
  17. this.el = el; // current slideshow container
  18. this.$items = el.querySelectorAll(this.opts.selector); // a collection of all of the slides, caching for performance
  19. this.numItems = this.$items.length; // total number of slides
  20. this.$items[0].classList.add('bss-show'); // add show class to first figure
  21. this.injectControls(el);
  22. this.addEventListeners(el);
  23. if (this.opts.auto) {
  24. this.autoCycle(this.el, this.opts.speed, this.opts.pauseOnHover);
  25. }
  26. if (this.opts.fullScreen) {
  27. this.addFullScreen(this.el);
  28. }
  29. if (this.opts.swipe) {
  30. this.addSwipe(this.el);
  31. }
  32. },
  33. showCurrent: function (i) {
  34. // increment or decrement this.counter depending on whether i === 1 or i === -1
  35. if (i > 0) {
  36. this.counter = (this.counter + 1 === this.numItems) ? 0 : this.counter + 1;
  37. } else {
  38. this.counter = (this.counter - 1 < 0) ? this.numItems - 1 : this.counter - 1;
  39. }
  40. // remove .show from whichever element currently has it
  41. // http://stackoverflow.com/a/16053538/2006057
  42. [].forEach.call(this.$items, function (el) {
  43. el.classList.remove('bss-show');
  44. });
  45. // add .show to the one item that's supposed to have it
  46. this.$items[this.counter].classList.add('bss-show');
  47. },
  48. injectControls: function (el) {
  49. // build and inject prev/next controls
  50. // first create all the new elements
  51. var spanPrev = document.createElement("span"),
  52. spanNext = document.createElement("span"),
  53. docFrag = document.createDocumentFragment();
  54. // add classes
  55. spanPrev.classList.add('bss-prev');
  56. spanNext.classList.add('bss-next');
  57. // add contents
  58. spanPrev.innerHTML = '&laquo;';
  59. spanNext.innerHTML = '&raquo;';
  60. // append elements to fragment, then append fragment to DOM
  61. docFrag.appendChild(spanPrev);
  62. docFrag.appendChild(spanNext);
  63. el.appendChild(docFrag);
  64. },
  65. addEventListeners: function (el) {
  66. var that = this;
  67. el.querySelector('.bss-next').addEventListener('click', function () {
  68. that.showCurrent(1); // increment & show
  69. }, false);
  70. el.querySelector('.bss-prev').addEventListener('click', function () {
  71. that.showCurrent(-1); // decrement & show
  72. }, false);
  73. el.onkeydown = function (e) {
  74. e = e || window.event;
  75. if (e.keyCode === 37) {
  76. that.showCurrent(-1); // decrement & show
  77. } else if (e.keyCode === 39) {
  78. that.showCurrent(1); // increment & show
  79. }
  80. };
  81. },
  82. autoCycle: function (el, speed, pauseOnHover) {
  83. var that = this,
  84. interval = window.setInterval(function () {
  85. that.showCurrent(1); // increment & show
  86. }, speed);
  87. if (pauseOnHover) {
  88. el.addEventListener('mouseover', function () {
  89. clearInterval(interval);
  90. interval = null;
  91. }, false);
  92. el.addEventListener('mouseout', function () {
  93. if(!interval) {
  94. interval = window.setInterval(function () {
  95. that.showCurrent(1); // increment & show
  96. }, speed);
  97. }
  98. }, false);
  99. } // end pauseonhover
  100. },
  101. addFullScreen: function(el){
  102. var that = this,
  103. fsControl = document.createElement("span");
  104. fsControl.classList.add('bss-fullscreen');
  105. el.appendChild(fsControl);
  106. el.querySelector('.bss-fullscreen').addEventListener('click', function () {
  107. that.toggleFullScreen(el);
  108. }, false);
  109. },
  110. addSwipe: function(el){
  111. var that = this,
  112. ht = new Hammer(el);
  113. ht.on('swiperight', function(e) {
  114. that.showCurrent(-1); // decrement & show
  115. });
  116. ht.on('swipeleft', function(e) {
  117. that.showCurrent(1); // increment & show
  118. });
  119. },
  120. toggleFullScreen: function(el){
  121. // https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Using_full_screen_mode
  122. if (!document.fullscreenElement && // alternative standard method
  123. !document.mozFullScreenElement && !document.webkitFullscreenElement &&
  124. !document.msFullscreenElement ) { // current working methods
  125. if (document.documentElement.requestFullscreen) {
  126. el.requestFullscreen();
  127. } else if (document.documentElement.msRequestFullscreen) {
  128. el.msRequestFullscreen();
  129. } else if (document.documentElement.mozRequestFullScreen) {
  130. el.mozRequestFullScreen();
  131. } else if (document.documentElement.webkitRequestFullscreen) {
  132. el.webkitRequestFullscreen(el.ALLOW_KEYBOARD_INPUT);
  133. }
  134. } else {
  135. if (document.exitFullscreen) {
  136. document.exitFullscreen();
  137. } else if (document.msExitFullscreen) {
  138. document.msExitFullscreen();
  139. } else if (document.mozCancelFullScreen) {
  140. document.mozCancelFullScreen();
  141. } else if (document.webkitExitFullscreen) {
  142. document.webkitExitFullscreen();
  143. }
  144. }
  145. } // end toggleFullScreen
  146. }; // end Slideshow object .....
  147. // make instances of Slideshow as needed
  148. [].forEach.call($slideshows, function (el) {
  149. $slideshow = Object.create(Slideshow);
  150. $slideshow.init(el, options);
  151. });
  152. };