]> git.ipfire.org Git - thirdparty/foundation/foundation-sites.git/commitdiff
refactor: move SmoothScroll click event handling to its own function
authorNicolas Coden <nicolas@ncoden.fr>
Fri, 28 Sep 2018 20:47:22 +0000 (22:47 +0200)
committerNicolas Coden <nicolas@ncoden.fr>
Fri, 28 Sep 2018 21:24:27 +0000 (23:24 +0200)
js/foundation.smoothScroll.js

index 3ec58c84a15e2b2179f94c5218649e7d05843996..9d5f9c656b5a522d5447fad7430aff7ce5d82672 100644 (file)
@@ -39,24 +39,30 @@ class SmoothScroll extends Plugin {
      * @private
      */
     _events() {
-        const handleLinkClick = (e) => {
-            // Follow the link it does not point to an anchor.
-            if (!$(e.currentTarget).is('a[href^="#"]')) return;
+        this.$element.on('click.zf.smoothScroll', this._handleLinkClick)
+        this.$element.on('click.zf.smoothScroll', 'a[href^="#"]', this._handleLinkClick);
+    }
 
-            const arrival = e.currentTarget.getAttribute('href');
+    /**
+     * Handle the given event to smoothly scroll to the anchor pointed by the event target.
+     * @param {*} e - event
+     * @function
+     * @private
+     */
+    _handleLinkClick(e) {
+        // Follow the link it does not point to an anchor.
+        if (!$(e.currentTarget).is('a[href^="#"]')) return;
 
-            this._inTransition = true;
+        const arrival = e.currentTarget.getAttribute('href');
 
-            SmoothScroll.scrollToLoc(arrival, this.options, () => {
-                this._inTransition = false;
-            });
+        this._inTransition = true;
 
-            e.preventDefault();
-        };
+        SmoothScroll.scrollToLoc(arrival, this.options, () => {
+            this._inTransition = false;
+        });
 
-        this.$element.on('click.zf.smoothScroll', handleLinkClick)
-        this.$element.on('click.zf.smoothScroll', 'a[href^="#"]', handleLinkClick);
-    }
+        e.preventDefault();
+    };
 
     /**
      * Function to scroll to a given location on the page.