lg-autoplay.umd.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*!
  2. * lightgallery | 2.4.0-beta.0 | December 12th 2021
  3. * http://www.lightgalleryjs.com/
  4. * Copyright (c) 2020 Sachin Neravath;
  5. * @license GPLv3
  6. */
  7. (function (global, factory) {
  8. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  9. typeof define === 'function' && define.amd ? define(factory) :
  10. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.lgAutoplay = factory());
  11. }(this, (function () { 'use strict';
  12. /*! *****************************************************************************
  13. Copyright (c) Microsoft Corporation.
  14. Permission to use, copy, modify, and/or distribute this software for any
  15. purpose with or without fee is hereby granted.
  16. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  17. REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  18. AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  19. INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  20. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  21. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  22. PERFORMANCE OF THIS SOFTWARE.
  23. ***************************************************************************** */
  24. var __assign = function() {
  25. __assign = Object.assign || function __assign(t) {
  26. for (var s, i = 1, n = arguments.length; i < n; i++) {
  27. s = arguments[i];
  28. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
  29. }
  30. return t;
  31. };
  32. return __assign.apply(this, arguments);
  33. };
  34. /**
  35. * List of lightGallery events
  36. * All events should be documented here
  37. * Below interfaces are used to build the website documentations
  38. * */
  39. var lGEvents = {
  40. afterAppendSlide: 'lgAfterAppendSlide',
  41. init: 'lgInit',
  42. hasVideo: 'lgHasVideo',
  43. containerResize: 'lgContainerResize',
  44. updateSlides: 'lgUpdateSlides',
  45. afterAppendSubHtml: 'lgAfterAppendSubHtml',
  46. beforeOpen: 'lgBeforeOpen',
  47. afterOpen: 'lgAfterOpen',
  48. slideItemLoad: 'lgSlideItemLoad',
  49. beforeSlide: 'lgBeforeSlide',
  50. afterSlide: 'lgAfterSlide',
  51. posterClick: 'lgPosterClick',
  52. dragStart: 'lgDragStart',
  53. dragMove: 'lgDragMove',
  54. dragEnd: 'lgDragEnd',
  55. beforeNextSlide: 'lgBeforeNextSlide',
  56. beforePrevSlide: 'lgBeforePrevSlide',
  57. beforeClose: 'lgBeforeClose',
  58. afterClose: 'lgAfterClose',
  59. rotateLeft: 'lgRotateLeft',
  60. rotateRight: 'lgRotateRight',
  61. flipHorizontal: 'lgFlipHorizontal',
  62. flipVertical: 'lgFlipVertical',
  63. autoplay: 'lgAutoplay',
  64. autoplayStart: 'lgAutoplayStart',
  65. autoplayStop: 'lgAutoplayStop',
  66. };
  67. var autoplaySettings = {
  68. autoplay: true,
  69. slideShowAutoplay: false,
  70. slideShowInterval: 5000,
  71. progressBar: true,
  72. forceSlideShowAutoplay: false,
  73. autoplayControls: true,
  74. appendAutoplayControlsTo: '.lg-toolbar',
  75. autoplayPluginStrings: { toggleAutoplay: 'Toggle Autoplay' },
  76. };
  77. /**
  78. * Creates the autoplay plugin.
  79. * @param {object} element - lightGallery element
  80. */
  81. var Autoplay = /** @class */ (function () {
  82. function Autoplay(instance) {
  83. this.core = instance;
  84. // extend module default settings with lightGallery core settings
  85. this.settings = __assign(__assign({}, autoplaySettings), this.core.settings);
  86. return this;
  87. }
  88. Autoplay.prototype.init = function () {
  89. var _this = this;
  90. if (!this.settings.autoplay) {
  91. return;
  92. }
  93. this.interval = false;
  94. // Identify if slide happened from autoplay
  95. this.fromAuto = true;
  96. // Identify if autoplay canceled from touch/drag
  97. this.pausedOnTouchDrag = false;
  98. this.pausedOnSlideChange = false;
  99. // append autoplay controls
  100. if (this.settings.autoplayControls) {
  101. this.controls();
  102. }
  103. // Create progress bar
  104. if (this.settings.progressBar) {
  105. this.core.outer.append('<div class="lg-progress-bar"><div class="lg-progress"></div></div>');
  106. }
  107. // Start autoplay
  108. if (this.settings.slideShowAutoplay) {
  109. this.core.LGel.once(lGEvents.slideItemLoad + ".autoplay", function () {
  110. _this.startAutoPlay();
  111. });
  112. }
  113. // cancel interval on touchstart and dragstart
  114. this.core.LGel.on(lGEvents.dragStart + ".autoplay touchstart.lg.autoplay", function () {
  115. if (_this.interval) {
  116. _this.stopAutoPlay();
  117. _this.pausedOnTouchDrag = true;
  118. }
  119. });
  120. // restore autoplay if autoplay canceled from touchstart / dragstart
  121. this.core.LGel.on(lGEvents.dragEnd + ".autoplay touchend.lg.autoplay", function () {
  122. if (!_this.interval && _this.pausedOnTouchDrag) {
  123. _this.startAutoPlay();
  124. _this.pausedOnTouchDrag = false;
  125. }
  126. });
  127. this.core.LGel.on(lGEvents.beforeSlide + ".autoplay", function () {
  128. _this.showProgressBar();
  129. if (!_this.fromAuto && _this.interval) {
  130. _this.stopAutoPlay();
  131. _this.pausedOnSlideChange = true;
  132. }
  133. else {
  134. _this.pausedOnSlideChange = false;
  135. }
  136. _this.fromAuto = false;
  137. });
  138. // restore autoplay if autoplay canceled from touchstart / dragstart
  139. this.core.LGel.on(lGEvents.afterSlide + ".autoplay", function () {
  140. if (_this.pausedOnSlideChange &&
  141. !_this.interval &&
  142. _this.settings.forceSlideShowAutoplay) {
  143. _this.startAutoPlay();
  144. _this.pausedOnSlideChange = false;
  145. }
  146. });
  147. // set progress
  148. this.showProgressBar();
  149. };
  150. Autoplay.prototype.showProgressBar = function () {
  151. var _this = this;
  152. if (this.settings.progressBar && this.fromAuto) {
  153. var _$progressBar_1 = this.core.outer.find('.lg-progress-bar');
  154. var _$progress_1 = this.core.outer.find('.lg-progress');
  155. if (this.interval) {
  156. _$progress_1.removeAttr('style');
  157. _$progressBar_1.removeClass('lg-start');
  158. setTimeout(function () {
  159. _$progress_1.css('transition', 'width ' +
  160. (_this.core.settings.speed +
  161. _this.settings.slideShowInterval) +
  162. 'ms ease 0s');
  163. _$progressBar_1.addClass('lg-start');
  164. }, 20);
  165. }
  166. }
  167. };
  168. // Manage autoplay via play/stop buttons
  169. Autoplay.prototype.controls = function () {
  170. var _this = this;
  171. var _html = "<button aria-label=\"" + this.settings.autoplayPluginStrings['toggleAutoplay'] + "\" type=\"button\" class=\"lg-autoplay-button lg-icon\"></button>";
  172. // Append autoplay controls
  173. this.core.outer
  174. .find(this.settings.appendAutoplayControlsTo)
  175. .append(_html);
  176. this.core.outer
  177. .find('.lg-autoplay-button')
  178. .first()
  179. .on('click.lg.autoplay', function () {
  180. if (_this.core.outer.hasClass('lg-show-autoplay')) {
  181. _this.stopAutoPlay();
  182. }
  183. else {
  184. if (!_this.interval) {
  185. _this.startAutoPlay();
  186. }
  187. }
  188. });
  189. };
  190. // Autostart gallery
  191. Autoplay.prototype.startAutoPlay = function () {
  192. var _this = this;
  193. this.core.outer
  194. .find('.lg-progress')
  195. .css('transition', 'width ' +
  196. (this.core.settings.speed +
  197. this.settings.slideShowInterval) +
  198. 'ms ease 0s');
  199. this.core.outer.addClass('lg-show-autoplay');
  200. this.core.outer.find('.lg-progress-bar').addClass('lg-start');
  201. this.core.LGel.trigger(lGEvents.autoplayStart, {
  202. index: this.core.index,
  203. });
  204. this.interval = setInterval(function () {
  205. if (_this.core.index + 1 < _this.core.galleryItems.length) {
  206. _this.core.index++;
  207. }
  208. else {
  209. _this.core.index = 0;
  210. }
  211. _this.core.LGel.trigger(lGEvents.autoplay, {
  212. index: _this.core.index,
  213. });
  214. _this.fromAuto = true;
  215. _this.core.slide(_this.core.index, false, false, 'next');
  216. }, this.core.settings.speed + this.settings.slideShowInterval);
  217. };
  218. // cancel Autostart
  219. Autoplay.prototype.stopAutoPlay = function () {
  220. if (this.interval) {
  221. this.core.LGel.trigger(lGEvents.autoplayStop, {
  222. index: this.core.index,
  223. });
  224. this.core.outer.find('.lg-progress').removeAttr('style');
  225. this.core.outer.removeClass('lg-show-autoplay');
  226. this.core.outer.find('.lg-progress-bar').removeClass('lg-start');
  227. }
  228. clearInterval(this.interval);
  229. this.interval = false;
  230. };
  231. Autoplay.prototype.closeGallery = function () {
  232. this.stopAutoPlay();
  233. };
  234. Autoplay.prototype.destroy = function () {
  235. if (this.settings.autoplay) {
  236. this.core.outer.find('.lg-progress-bar').remove();
  237. }
  238. // Remove all event listeners added by autoplay plugin
  239. this.core.LGel.off('.lg.autoplay');
  240. this.core.LGel.off('.autoplay');
  241. };
  242. return Autoplay;
  243. }());
  244. return Autoplay;
  245. })));
  246. //# sourceMappingURL=lg-autoplay.umd.js.map