From: Nicolas Coden Date: Fri, 28 Sep 2018 20:46:23 +0000 (+0200) Subject: refactor: move SmoothScroll to ES6+ and fix linting issues X-Git-Tag: v6.6.0~3^2~89^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7d717bfb2db219aac07e38572a8ae2a3d59e6869;p=thirdparty%2Ffoundation%2Ffoundation-sites.git refactor: move SmoothScroll to ES6+ and fix linting issues --- diff --git a/js/foundation.smoothScroll.js b/js/foundation.smoothScroll.js index a90e0965a..3ec58c84a 100644 --- a/js/foundation.smoothScroll.js +++ b/js/foundation.smoothScroll.js @@ -1,5 +1,3 @@ -'use strict'; - import $ from 'jquery'; import { GetYoDigits } from './foundation.core.utils'; import { Plugin } from './foundation.core.plugin'; @@ -30,11 +28,8 @@ class SmoothScroll extends Plugin { * @private */ _init() { - var id = this.$element[0].id || GetYoDigits(6, 'smooth-scroll'); - var _this = this; - this.$element.attr({ - 'id': id - }); + const id = this.$element[0].id || GetYoDigits(6, 'smooth-scroll'); + this.$element.attr({ id }); this._events(); } @@ -44,19 +39,16 @@ class SmoothScroll extends Plugin { * @private */ _events() { - var _this = this; - - // click handler function. - var handleLinkClick = function(e) { + const handleLinkClick = (e) => { // Follow the link it does not point to an anchor. - if (!$(this).is('a[href^="#"]')) return; + if (!$(e.currentTarget).is('a[href^="#"]')) return; - var arrival = this.getAttribute('href'); + const arrival = e.currentTarget.getAttribute('href'); - _this._inTransition = true; + this._inTransition = true; - SmoothScroll.scrollToLoc(arrival, _this.options, function() { - _this._inTransition = false; + SmoothScroll.scrollToLoc(arrival, this.options, () => { + this._inTransition = false; }); e.preventDefault(); @@ -75,19 +67,19 @@ class SmoothScroll extends Plugin { * @function */ static scrollToLoc(loc, options = SmoothScroll.defaults, callback) { + const $loc = $(loc); + // Do nothing if target does not exist to prevent errors - if (!$(loc).length) { - return false; - } + if (!$loc.length) return false; - var scrollPos = Math.round($(loc).offset().top - options.threshold / 2 - options.offset); + var scrollPos = Math.round($loc.offset().top - options.threshold / 2 - options.offset); $('html, body').stop(true).animate( { scrollTop: scrollPos }, options.animationDuration, options.animationEasing, - function() { - if(callback && typeof callback == "function"){ + () => { + if (typeof callback === 'function'){ callback(); } }