/*! lightgallery - v1.2.0 - 2015-08-26 * http://sachinchoolur.github.io/lightgallery/ * copyright (c) 2015 sachin n; licensed apache 2.0 */ /** * autoplay plugin * @version 1.2.0 * @author sachin n - @sachinchoolur * @license mit license (mit) */ (function($, window, document, undefined) { 'use strict'; var defaults = { autoplay: false, pause: 5000, progressbar: true, fourceautoplay: false, autoplaycontrols: true, appendautoplaycontrolsto: '.lg-toolbar' }; /** * creates the autoplay plugin. * @param {object} element - lightgallery element */ var autoplay = function(element) { this.core = $(element).data('lightgallery'); this.$el = $(element); // exicute only if items are above 1 if (this.core.$items.length < 2) { return false; } this.core.s = $.extend({}, defaults, this.core.s); this.interval = false; // identify if slide happened from autoplay this.fromauto = true; // identify if autoplay canceled from touch/drag this.canceledontouch = false; // save fourceautoplay value this.fourceautoplaytemp = this.core.s.fourceautoplay; // do not allow progress bar if browser does not support css3 transitions if (!this.core.docss()) { this.core.s.progressbar = false; } this.init(); return this; }; autoplay.prototype.init = function() { var _this = this; // append autoplay controls if (_this.core.s.autoplaycontrols) { _this.controls(); } // create progress bar if (_this.core.s.progressbar) { _this.core.$outer.find('.lg').append('
'); } // set progress _this.progress(); // start autoplay if (_this.core.s.autoplay) { _this.startlauto(); } // cancel interval on touchstart and dragstart _this.$el.on('ondragstart.lg.tm touchstart.lg.tm', function() { if (_this.interval) { _this.cancelauto(); _this.canceledontouch = true; } }); // restore autoplay if autoplay canceled from touchstart / dragstart _this.$el.on('ondragend.lg.tm touchend.lg.tm onslideclick.lg.tm', function() { if (!_this.interval && _this.canceledontouch) { _this.startlauto(); _this.canceledontouch = false; } }); }; autoplay.prototype.progress = function() { var _this = this; var _$progressbar; var _$progress; _this.$el.on('onbeforeslide.lg.tm', function() { // start progress bar animation if (_this.core.s.progressbar && _this.fromauto) { _$progressbar = _this.core.$outer.find('.lg-progress-bar'); _$progress = _this.core.$outer.find('.lg-progress'); if (_this.interval) { _$progress.removeattr('style'); _$progressbar.removeclass('lg-start'); settimeout(function() { _$progress.css('transition', 'width ' + (_this.core.s.speed + _this.core.s.pause) + 'ms ease 0s'); _$progressbar.addclass('lg-start'); }, 20); } } // remove setinterval if slide is trigered manualy and fourceautoplay is false if (!_this.fromauto && !_this.core.s.fourceautoplay) { _this.cancelauto(); } _this.fromauto = false; }); }; // manage autoplay via play/stop buttons autoplay.prototype.controls = function() { var _this = this; var _html = ''; // append autoplay controls $(this.core.s.appendautoplaycontrolsto).append(_html); _this.core.$outer.find('.lg-autoplay-button').on('click.lg', function() { if ($(_this.core.$outer).hasclass('lg-show-autoplay')) { _this.cancelauto(); _this.core.s.fourceautoplay = false; } else { if (!_this.interval) { _this.startlauto(); _this.core.s.fourceautoplay = _this.fourceautoplaytemp; } } }); }; // autostart gallery autoplay.prototype.startlauto = function() { var _this = this; _this.core.$outer.find('.lg-progress').css('transition', 'width ' + (_this.core.s.speed + _this.core.s.pause) + 'ms ease 0s'); _this.core.$outer.addclass('lg-show-autoplay'); _this.core.$outer.find('.lg-progress-bar').addclass('lg-start'); _this.interval = setinterval(function() { if (_this.core.index + 1 < _this.core.$items.length) { _this.core.index = _this.core.index; } else { _this.core.index = -1; } _this.core.index++; _this.fromauto = true; _this.core.slide(_this.core.index, false, false); }, _this.core.s.speed + _this.core.s.pause); }; // cancel autostart autoplay.prototype.cancelauto = function() { clearinterval(this.interval); this.interval = false; this.core.$outer.find('.lg-progress').removeattr('style'); this.core.$outer.removeclass('lg-show-autoplay'); this.core.$outer.find('.lg-progress-bar').removeclass('lg-start'); }; autoplay.prototype.destroy = function() { this.cancelauto(); this.core.$outer.find('.lg-progress-bar').remove(); }; $.fn.lightgallery.modules.autoplay = autoplay; })(jquery, window, document);