From 5c15a053eeed249d26657cae654e38007cff242d Mon Sep 17 00:00:00 2001 From: Corey Snyder Date: Wed, 11 Jan 2017 11:03:45 -0500 Subject: [PATCH] Add eventsHandler() slim down the code --- js/foundation.sticky.js | 54 ++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/js/foundation.sticky.js b/js/foundation.sticky.js index e643ea112..ed3cc74b9 100644 --- a/js/foundation.sticky.js +++ b/js/foundation.sticky.js @@ -129,44 +129,38 @@ class Sticky { this.$element.off('resizeme.zf.trigger') .on('resizeme.zf.trigger', function(e, el) { - _this._setSizes(function() { - _this._calc(false); - if (_this.canStick) { - if (!_this.isOn) { - _this._events(id); - } - } else if (_this.isOn) { - _this._pauseListeners(scrollListener); - } - }); + _this._eventsHandler(id); }); this.$element.on('mutateme.zf.trigger', function (e, el) { - _this._setSizes(function () { - _this._calc(false); - if (_this.canStick) { - if (!_this.isOn) { - _this._events(id); - } - } else if (_this.isOn) { - _this._pauseListeners(scrollListener); - } - }); + _this._eventsHandler(id); }); this.$anchor.on('mutateme.zf.trigger', function (e, el) { - _this._setSizes(function () { - _this._calc(false); - if (_this.canStick) { - if (!_this.isOn) { - _this._events(id); - } - } else if (_this.isOn) { - _this._pauseListeners(scrollListener); - } - }); + _this._eventsHandler(id); }); } + + /** + * Handler for events. + * @private + * @param {String} id - psuedo-random id for unique scroll event listener. + */ + _eventsHandler(id) { + var _this = this, + scrollListener = this.scrollListener = `scroll.zf.${id}`; + + _this._setSizes(function() { + _this._calc(false); + if (_this.canStick) { + if (!_this.isOn) { + _this._events(id); + } + } else if (_this.isOn) { + _this._pauseListeners(scrollListener); + } + }); + } /** * Removes event handlers for scroll and change events on anchor. -- 2.47.2