|
| 1 | +/*! lg-autoplay - v1.0.4 - 2017-03-28 |
| 2 | +* http://sachinchoolur.github.io/lightGallery |
| 3 | +* Copyright (c) 2017 Sachin N; Licensed GPLv3 */ |
| 4 | + |
| 5 | +(function (root, factory) { |
| 6 | + if (typeof define === 'function' && define.amd) { |
| 7 | + // AMD. Register as an anonymous module unless amdModuleId is set |
| 8 | + define(['jquery'], function (a0) { |
| 9 | + return (factory(a0)); |
| 10 | + }); |
| 11 | + } else if (typeof exports === 'object') { |
| 12 | + // Node. Does not work with strict CommonJS, but |
| 13 | + // only CommonJS-like environments that support module.exports, |
| 14 | + // like Node. |
| 15 | + module.exports = factory(require('jquery')); |
| 16 | + } else { |
| 17 | + factory(jQuery); |
| 18 | + } |
| 19 | +}(this, function ($) { |
| 20 | + |
| 21 | + |
| 22 | +(function() { |
| 23 | + |
| 24 | + 'use strict'; |
| 25 | + |
| 26 | + var defaults = { |
| 27 | + autoplay: false, |
| 28 | + pause: 5000, |
| 29 | + progressBar: true, |
| 30 | + fourceAutoplay: false, |
| 31 | + autoplayControls: true, |
| 32 | + appendAutoplayControlsTo: '.lg-toolbar' |
| 33 | + }; |
| 34 | + |
| 35 | + /** |
| 36 | + * Creates the autoplay plugin. |
| 37 | + * @param {object} element - lightGallery element |
| 38 | + */ |
| 39 | + var Autoplay = function(element) { |
| 40 | + |
| 41 | + this.core = $(element).data('lightGallery'); |
| 42 | + |
| 43 | + this.$el = $(element); |
| 44 | + |
| 45 | + // Execute only if items are above 1 |
| 46 | + if (this.core.$items.length < 2) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + this.core.s = $.extend({}, defaults, this.core.s); |
| 51 | + this.interval = false; |
| 52 | + |
| 53 | + // Identify if slide happened from autoplay |
| 54 | + this.fromAuto = true; |
| 55 | + |
| 56 | + // Identify if autoplay canceled from touch/drag |
| 57 | + this.canceledOnTouch = false; |
| 58 | + |
| 59 | + // save fourceautoplay value |
| 60 | + this.fourceAutoplayTemp = this.core.s.fourceAutoplay; |
| 61 | + |
| 62 | + // do not allow progress bar if browser does not support css3 transitions |
| 63 | + if (!this.core.doCss()) { |
| 64 | + this.core.s.progressBar = false; |
| 65 | + } |
| 66 | + |
| 67 | + this.init(); |
| 68 | + |
| 69 | + return this; |
| 70 | + }; |
| 71 | + |
| 72 | + Autoplay.prototype.init = function() { |
| 73 | + var _this = this; |
| 74 | + |
| 75 | + // append autoplay controls |
| 76 | + if (_this.core.s.autoplayControls) { |
| 77 | + _this.controls(); |
| 78 | + } |
| 79 | + |
| 80 | + // Create progress bar |
| 81 | + if (_this.core.s.progressBar) { |
| 82 | + _this.core.$outer.find('.lg').append('<div class="lg-progress-bar"><div class="lg-progress"></div></div>'); |
| 83 | + } |
| 84 | + |
| 85 | + // set progress |
| 86 | + _this.progress(); |
| 87 | + |
| 88 | + // Start autoplay |
| 89 | + if (_this.core.s.autoplay) { |
| 90 | + _this.$el.one('onSlideItemLoad.lg.tm', function() { |
| 91 | + _this.startlAuto(); |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + // cancel interval on touchstart and dragstart |
| 96 | + _this.$el.on('onDragstart.lg.tm touchstart.lg.tm', function() { |
| 97 | + if (_this.interval) { |
| 98 | + _this.cancelAuto(); |
| 99 | + _this.canceledOnTouch = true; |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + // restore autoplay if autoplay canceled from touchstart / dragstart |
| 104 | + _this.$el.on('onDragend.lg.tm touchend.lg.tm onSlideClick.lg.tm', function() { |
| 105 | + if (!_this.interval && _this.canceledOnTouch) { |
| 106 | + _this.startlAuto(); |
| 107 | + _this.canceledOnTouch = false; |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + }; |
| 112 | + |
| 113 | + Autoplay.prototype.progress = function() { |
| 114 | + |
| 115 | + var _this = this; |
| 116 | + var _$progressBar; |
| 117 | + var _$progress; |
| 118 | + |
| 119 | + _this.$el.on('onBeforeSlide.lg.tm', function() { |
| 120 | + |
| 121 | + // start progress bar animation |
| 122 | + if (_this.core.s.progressBar && _this.fromAuto) { |
| 123 | + _$progressBar = _this.core.$outer.find('.lg-progress-bar'); |
| 124 | + _$progress = _this.core.$outer.find('.lg-progress'); |
| 125 | + if (_this.interval) { |
| 126 | + _$progress.removeAttr('style'); |
| 127 | + _$progressBar.removeClass('lg-start'); |
| 128 | + setTimeout(function() { |
| 129 | + _$progress.css('transition', 'width ' + (_this.core.s.speed + _this.core.s.pause) + 'ms ease 0s'); |
| 130 | + _$progressBar.addClass('lg-start'); |
| 131 | + }, 20); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // Remove setinterval if slide is triggered manually and fourceautoplay is false |
| 136 | + if (!_this.fromAuto && !_this.core.s.fourceAutoplay) { |
| 137 | + _this.cancelAuto(); |
| 138 | + } |
| 139 | + |
| 140 | + _this.fromAuto = false; |
| 141 | + |
| 142 | + }); |
| 143 | + }; |
| 144 | + |
| 145 | + // Manage autoplay via play/stop buttons |
| 146 | + Autoplay.prototype.controls = function() { |
| 147 | + var _this = this; |
| 148 | + var _html = '<span class="lg-autoplay-button lg-icon"></span>'; |
| 149 | + |
| 150 | + // Append autoplay controls |
| 151 | + $(this.core.s.appendAutoplayControlsTo).append(_html); |
| 152 | + |
| 153 | + _this.core.$outer.find('.lg-autoplay-button').on('click.lg', function() { |
| 154 | + if ($(_this.core.$outer).hasClass('lg-show-autoplay')) { |
| 155 | + _this.cancelAuto(); |
| 156 | + _this.core.s.fourceAutoplay = false; |
| 157 | + } else { |
| 158 | + if (!_this.interval) { |
| 159 | + _this.startlAuto(); |
| 160 | + _this.core.s.fourceAutoplay = _this.fourceAutoplayTemp; |
| 161 | + } |
| 162 | + } |
| 163 | + }); |
| 164 | + }; |
| 165 | + |
| 166 | + // Autostart gallery |
| 167 | + Autoplay.prototype.startlAuto = function() { |
| 168 | + var _this = this; |
| 169 | + |
| 170 | + _this.core.$outer.find('.lg-progress').css('transition', 'width ' + (_this.core.s.speed + _this.core.s.pause) + 'ms ease 0s'); |
| 171 | + _this.core.$outer.addClass('lg-show-autoplay'); |
| 172 | + _this.core.$outer.find('.lg-progress-bar').addClass('lg-start'); |
| 173 | + |
| 174 | + _this.interval = setInterval(function() { |
| 175 | + if (_this.core.index + 1 < _this.core.$items.length) { |
| 176 | + _this.core.index++; |
| 177 | + } else { |
| 178 | + _this.core.index = 0; |
| 179 | + } |
| 180 | + |
| 181 | + _this.fromAuto = true; |
| 182 | + _this.core.slide(_this.core.index, false, false, 'next'); |
| 183 | + }, _this.core.s.speed + _this.core.s.pause); |
| 184 | + }; |
| 185 | + |
| 186 | + // cancel Autostart |
| 187 | + Autoplay.prototype.cancelAuto = function() { |
| 188 | + clearInterval(this.interval); |
| 189 | + this.interval = false; |
| 190 | + this.core.$outer.find('.lg-progress').removeAttr('style'); |
| 191 | + this.core.$outer.removeClass('lg-show-autoplay'); |
| 192 | + this.core.$outer.find('.lg-progress-bar').removeClass('lg-start'); |
| 193 | + }; |
| 194 | + |
| 195 | + Autoplay.prototype.destroy = function() { |
| 196 | + |
| 197 | + this.cancelAuto(); |
| 198 | + this.core.$outer.find('.lg-progress-bar').remove(); |
| 199 | + }; |
| 200 | + |
| 201 | + $.fn.lightGallery.modules.autoplay = Autoplay; |
| 202 | + |
| 203 | +})(); |
| 204 | + |
| 205 | + |
| 206 | +})); |
0 commit comments