diff --git a/lethargy.coffee b/lethargy.coffee index 376efee..54099a9 100644 --- a/lethargy.coffee +++ b/lethargy.coffee @@ -1,48 +1,38 @@ -root = exports ? this - -class root.Lethargy - constructor: (stability, sensitivity, tolerance, delay) -> - - # Stability is how many records to use to calculate the average +umd = (root, factory, exportsName) -> + if typeof define is 'function' and define.amd + define([], () -> + root[exportsName] = factory + ) + else if typeof module is 'object' && module.exports + module.exports = factory + else + root[exportsName]= factory + +class Lethargy + constructor: (stability, sensitivity, tolerance) -> @stability = if stability? then Math.abs stability else 8 - - # The wheelDelta threshold. If an event has a wheelDelta below this value, it will not register @sensitivity = if sensitivity? then 1 + Math.abs sensitivity else 100 - - # How much the old rolling average have to differ from the new rolling average for it to be deemed significant @tolerance = if tolerance? then 1 + Math.abs tolerance else 1.1 - - # Threshold for the amount of time between mousewheel events for them to be deemed separate - @delay = if delay? then delay else 150 - - # Used internally and should not be manipulated @lastUpDeltas = (null for [1..(@stability * 2)]) @lastDownDeltas = (null for [1..(@stability * 2)]) @deltasTimestamp = (null for [1..(@stability * 2)]) - # Checks whether the mousewheel event is an intent check: (e) -> - # Use jQuery's e.originalEvent if available - e = e.originalEvent || e + lastDelta + if e.originalEvent.wheelDelta? + lastDelta = e.originalEvent.wheelDelta + else if e.originalEvent.deltaY? + lastDelta = e.originalEvent.deltaY * -40 + else if (e.originalEvent.detail? or e.originalEvent.detail == 0) + lastDelta = e.originalEvent.detail * -40 - # Standardise wheelDelta values for different browsers - if e.wheelDelta? - lastDelta = e.wheelDelta - else if e.deltaY? - lastDelta = e.deltaY * -40 - else if (e.detail? or e.detail == 0) - lastDelta = e.detail * -40 - - # Add the new event timestamp to deltasTimestamp array, and remove the oldest entry @deltasTimestamp.push(Date.now()) @deltasTimestamp.shift() - # If lastDelta is positive, it means the user scrolled up if (lastDelta > 0) @lastUpDeltas.push(lastDelta) @lastUpDeltas.shift() return @isInertia(1) - # Otherwise, the user scrolled down else @lastDownDeltas.push(lastDelta) @lastDownDeltas.shift() @@ -50,18 +40,11 @@ class root.Lethargy false; isInertia: (direction) -> - # Get the relevant last*Delta array lastDeltas = if direction == -1 then @lastDownDeltas else @lastUpDeltas - - # If the array is not filled up yet, we cannot compare averages, so assume the scroll event to be intentional if lastDeltas[0] == null return direction - - # If the last mousewheel occurred within the specified delay of the penultimate one, and their values are the same. We will assume that this is a trackpad with a constant profile, and will return false - if @deltasTimestamp[(this.stability * 2) - 2] + @delay > Date.now() and lastDeltas[0] == lastDeltas[(@stability * 2) - 1] + if @deltasTimestamp[(this.stability * 2) - 2] + 150 > Date.now() and lastDeltas[0] == lastDeltas[(@stability * 2) - 1] return false - - # Check to see if the new rolling average (based on the last half of the lastDeltas array) is significantly higher than the old rolling average. If so return direction, else false lastDeltasOld = lastDeltas.slice(0, @stability) lastDeltasNew = lastDeltas.slice(@stability, (@stability * 2)) @@ -80,4 +63,6 @@ class root.Lethargy return @lastUpDeltas showLastDownDeltas: -> - return @lastDownDeltas \ No newline at end of file + return @lastDownDeltas + +umd window || this, Lethargy, 'Lethargy' diff --git a/lethargy.js b/lethargy.js index 6634df5..3d88ab3 100644 --- a/lethargy.js +++ b/lethargy.js @@ -1,15 +1,24 @@ -// Generated by CoffeeScript 1.9.2 +// Generated by CoffeeScript (function() { - var root; + var Lethargy, umd; - root = typeof exports !== "undefined" && exports !== null ? exports : this; + umd = function(root, factory, exportsName) { + if (typeof define === 'function' && define.amd) { + return define([], function() { + return root[exportsName] = factory; + }); + } else if (typeof module === 'object' && module.exports) { + return module.exports = factory; + } else { + return root[exportsName] = factory; + } + }; - root.Lethargy = (function() { - function Lethargy(stability, sensitivity, tolerance, delay) { + Lethargy = (function() { + function Lethargy(stability, sensitivity, tolerance) { this.stability = stability != null ? Math.abs(stability) : 8; this.sensitivity = sensitivity != null ? 1 + Math.abs(sensitivity) : 100; this.tolerance = tolerance != null ? 1 + Math.abs(tolerance) : 1.1; - this.delay = delay != null ? delay : 150; this.lastUpDeltas = (function() { var i, ref, results; results = []; @@ -37,14 +46,14 @@ } Lethargy.prototype.check = function(e) { + lastDelta; var lastDelta; - e = e.originalEvent || e; - if (e.wheelDelta != null) { - lastDelta = e.wheelDelta; - } else if (e.deltaY != null) { - lastDelta = e.deltaY * -40; - } else if ((e.detail != null) || e.detail === 0) { - lastDelta = e.detail * -40; + if (e.originalEvent.wheelDelta != null) { + lastDelta = e.originalEvent.wheelDelta; + } else if (e.originalEvent.deltaY != null) { + lastDelta = e.originalEvent.deltaY * -40; + } else if ((e.originalEvent.detail != null) || e.originalEvent.detail === 0) { + lastDelta = e.originalEvent.detail * -40; } this.deltasTimestamp.push(Date.now()); this.deltasTimestamp.shift(); @@ -66,7 +75,7 @@ if (lastDeltas[0] === null) { return direction; } - if (this.deltasTimestamp[(this.stability * 2) - 2] + this.delay > Date.now() && lastDeltas[0] === lastDeltas[(this.stability * 2) - 1]) { + if (this.deltasTimestamp[(this.stability * 2) - 2] + 150 > Date.now() && lastDeltas[0] === lastDeltas[(this.stability * 2) - 1]) { return false; } lastDeltasOld = lastDeltas.slice(0, this.stability); @@ -98,4 +107,6 @@ })(); + umd(window || this, Lethargy, 'Lethargy'); + }).call(this); diff --git a/lethargy.min.js b/lethargy.min.js index 018e7a0..313989e 100644 --- a/lethargy.min.js +++ b/lethargy.min.js @@ -1 +1 @@ -(function(){var t;t="undefined"!=typeof exports&&null!==exports?exports:this,t.Lethargy=function(){function t(t,s,i,l){this.stability=null!=t?Math.abs(t):8,this.sensitivity=null!=s?1+Math.abs(s):100,this.tolerance=null!=i?1+Math.abs(i):1.1,this.delay=null!=l?l:150,this.lastUpDeltas=function(){var t,s,i;for(i=[],t=1,s=2*this.stability;s>=1?s>=t:t>=s;s>=1?t++:t--)i.push(null);return i}.call(this),this.lastDownDeltas=function(){var t,s,i;for(i=[],t=1,s=2*this.stability;s>=1?s>=t:t>=s;s>=1?t++:t--)i.push(null);return i}.call(this),this.deltasTimestamp=function(){var t,s,i;for(i=[],t=1,s=2*this.stability;s>=1?s>=t:t>=s;s>=1?t++:t--)i.push(null);return i}.call(this)}return t.prototype.check=function(t){var s;return t=t.originalEvent||t,null!=t.wheelDelta?s=t.wheelDelta:null!=t.deltaY?s=-40*t.deltaY:(null!=t.detail||0===t.detail)&&(s=-40*t.detail),this.deltasTimestamp.push(Date.now()),this.deltasTimestamp.shift(),s>0?(this.lastUpDeltas.push(s),this.lastUpDeltas.shift(),this.isInertia(1)):(this.lastDownDeltas.push(s),this.lastDownDeltas.shift(),this.isInertia(-1))},t.prototype.isInertia=function(t){var s,i,l,a,e,n,h;return s=-1===t?this.lastDownDeltas:this.lastUpDeltas,null===s[0]?t:this.deltasTimestamp[2*this.stability-2]+this.delay>Date.now()&&s[0]===s[2*this.stability-1]?!1:(l=s.slice(0,this.stability),i=s.slice(this.stability,2*this.stability),h=l.reduce(function(t,s){return t+s}),e=i.reduce(function(t,s){return t+s}),n=h/l.length,a=e/i.length,Math.abs(n)=1?i>=t:t>=i;i>=1?t++:t--)s.push(null);return s}.call(this),this.lastDownDeltas=function(){var t,i,s;for(s=[],t=1,i=2*this.stability;i>=1?i>=t:t>=i;i>=1?t++:t--)s.push(null);return s}.call(this),this.deltasTimestamp=function(){var t,i,s;for(s=[],t=1,i=2*this.stability;i>=1?i>=t:t>=i;i>=1?t++:t--)s.push(null);return s}.call(this)}return t.prototype.check=function(t){var i;return null!=t.originalEvent.wheelDelta?i=t.originalEvent.wheelDelta:null!=t.originalEvent.deltaY?i=-40*t.originalEvent.deltaY:(null!=t.originalEvent.detail||0===t.originalEvent.detail)&&(i=-40*t.originalEvent.detail),this.deltasTimestamp.push(Date.now()),this.deltasTimestamp.shift(),i>0?(this.lastUpDeltas.push(i),this.lastUpDeltas.shift(),this.isInertia(1)):(this.lastDownDeltas.push(i),this.lastDownDeltas.shift(),this.isInertia(-1))},t.prototype.isInertia=function(t){var i,s,e,l,n,a,h;return i=-1===t?this.lastDownDeltas:this.lastUpDeltas,null===i[0]?t:this.deltasTimestamp[2*this.stability-2]+150>Date.now()&&i[0]===i[2*this.stability-1]?!1:(e=i.slice(0,this.stability),s=i.slice(this.stability,2*this.stability),h=e.reduce(function(t,i){return t+i}),n=s.reduce(function(t,i){return t+i}),a=h/e.length,l=n/s.length,Math.abs(a)