From 2fb528a29836d69cb57712d37925918ffd746a99 Mon Sep 17 00:00:00 2001 From: Milos Sakovic Date: Fri, 21 Oct 2016 01:30:38 +0200 Subject: [PATCH] [Orbit] Detaches events on reInit --- js/foundation.orbit.js | 44 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/js/foundation.orbit.js b/js/foundation.orbit.js index e02724c1d..dd663a6a7 100644 --- a/js/foundation.orbit.js +++ b/js/foundation.orbit.js @@ -43,6 +43,8 @@ class Orbit { * @private */ _init() { + this._detachEvents(); + this.$wrapper = this.$element.find(`.${this.options.containerClass}`); this.$slides = this.$element.find(`.${this.options.slideClass}`); var $images = this.$element.find('img'), @@ -216,7 +218,7 @@ class Orbit { _this.changeSlide(ltr, $slide, idx); }); } - + if (this.options.accessible) { this.$wrapper.add(this.$bullets).on('keydown.zf.orbit', function(e) { // handle keyboard event with keyboard util @@ -238,6 +240,44 @@ class Orbit { } } + /** + * Detaches event listeners + */ + _detachEvents() { + // Don't do anything if there are no slides (first run) + if (typeof this.$slides == "undefined") { + return; + } + + if (this.$slides.length > 1) { + // Remove old events + this.$element.off('.zf.orbit').find('*').off('.zf.orbit') + + // Restart timer if autoPlay is enabled + if (this.options.autoPlay) { + this.timer.restart(); + } + + // Reset all sliddes + this.$slides.each(function(el) { + $(el).removeClass('is-active is-active is-in') + .removeAttr('aria-live') + .hide(); + }); + + // Show the first slide + this.$slides.first().addClass('is-active').show(); + + // Triggers when the slide has finished animating + this.$element.trigger('slidechange.zf.orbit', [this.$slides.first()]); + + // Select first bullet if bullets are present + if (this.options.bullets) { + this._updateBullets(0); + } + } + } + /** * Changes the current slide to a new one. * @function @@ -273,7 +313,7 @@ class Orbit { * @event Orbit#beforeslidechange */ this.$element.trigger('beforeslidechange.zf.orbit', [$curSlide, $newSlide]); - + if (this.options.bullets) { idx = idx || this.$slides.index($newSlide); //grab index to update bullets this._updateBullets(idx); -- 2.47.2