From e4a962ba9bb173efcbb92540efc13ceb423e4ae8 Mon Sep 17 00:00:00 2001 From: Antonio Laguna Date: Sun, 12 Mar 2017 23:01:54 +0100 Subject: [PATCH] Allowing to stop/play if focused element is interactive #51 --- src/js/plugins/autoslide.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/js/plugins/autoslide.js b/src/js/plugins/autoslide.js index b510399..e3e282f 100644 --- a/src/js/plugins/autoslide.js +++ b/src/js/plugins/autoslide.js @@ -19,8 +19,28 @@ export default class AutoSlide { * @private */ this.interval_ = null; + /** + * Internal stored time. + * @type {?number} + */ + this.time = this.ws_.options.autoslide; - DOM.once(wsInstance.el, 'ws:init', this.play.bind(this)); + if (this.time) { + DOM.once(wsInstance.el, 'ws:init', this.play.bind(this)); + document.body.addEventListener('focus', this.onFocus.bind(this)); + } + } + + /** + * On focus handler. Will decide if stops/play depending on the focused + * element if autoslide is active. + */ + onFocus() { + if (DOM.isFocusableElement()) { + this.stop(); + } else if (this.interval_ === null) { + this.play(); + } } /** @@ -30,7 +50,8 @@ export default class AutoSlide { * automatically. */ play(time) { - time = time || this.ws_.options.autoslide; + time = time || this.time; + this.time = time; if (!this.interval_ && typeof time === 'number' && time > 0) { this.interval_ = setInterval(this.ws_.goNext.bind(this.ws_), time);