Socket
Socket
Sign inDemoInstall

wheel-gestures

Package Overview
Dependencies
0
Maintainers
1
Versions
62
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.2-beta.4 to 1.0.2

dist/wheel-analyzer/wheel-analyzer-types.d.ts

2

dist/events/EventBus.d.ts

@@ -6,3 +6,3 @@ export interface Props {

export declare type Off = () => void;
export default function EventBus<EventMap = EventMapEmpty>({}?: Props): Readonly<{
export default function EventBus<EventMap = EventMapEmpty>(): Readonly<{
on: <EK extends keyof EventMap>(type: EK, listener: EventListener<EventMap[EK]>) => Off;

@@ -9,0 +9,0 @@ off: <EK_1 extends keyof EventMap>(type: EK_1, listener: EventListener<EventMap[EK_1]>) => void;

@@ -1,4 +0,4 @@

export * from './wheel-gestures';
export * from './wheel-gestures.types';
export * from './wheel-analyzer';
export * from './wheel-analyzer.types';
export * from './wheel-gestures/wheel-gestures';
export * from './wheel-gestures/wheel-gestures-types';
export * from './wheel-analyzer/wheel-analyzer';
export * from './wheel-analyzer/wheel-analyzer-types';

@@ -1,3 +0,3 @@

import { WheelAnalyzer } from '../../wheel-analyzer';
import { PhaseData, SubscribeFn, WheelEventData, WheelTypes } from '../../wheel-analyzer.types';
import { WheelAnalyzer } from '../../wheel-analyzer/wheel-analyzer';
import { PhaseData, SubscribeFn, WheelEventData, WheelTypes } from '../../wheel-analyzer/wheel-analyzer-types';
export declare type Range = [number, number];

@@ -17,7 +17,6 @@ export interface PhaseRange {

allPhaseData: {
type: import("../../wheel-analyzer.types").WheelPhase;
debugData: Partial<WheelAnalyzer> | null;
type: import("../../wheel-analyzer/wheel-analyzer-types").WheelPhase;
willEndSoon: boolean;
isMomentum: boolean;
axisDeltas: number[];
axisMovement: number[];
axisVelocity: number[];

@@ -24,0 +23,0 @@ }[];

@@ -39,19 +39,32 @@ 'use strict';

function _objectDestructuringEmpty(obj) {
if (obj == null) throw new TypeError("Cannot destructure undefined");
function EventBus() {
var listeners = {};
function on(type, listener) {
listeners[type] = (listeners[type] || []).concat(listener);
return function () {
return off(type, listener);
};
}
function off(type, listener) {
listeners[type] = (listeners[type] || []).filter(function (l) {
return l !== listener;
});
}
function dispatch(type, data) {
if (!(type in listeners)) return;
listeners[type].forEach(function (l) {
return l(data);
});
}
return Object.freeze({
on: on,
off: off,
dispatch: dispatch
});
}
(function (WheelPhase) {
WheelPhase["ANY_WHEEL_START"] = "ANY_WHEEL_START";
WheelPhase["ANY_WHEEL"] = "ANY_WHEEL";
WheelPhase["ANY_WHEEL_END"] = "ANY_WHEEL_END";
WheelPhase["WHEEL_START"] = "WHEEL_START";
WheelPhase["WHEEL"] = "WHEEL";
WheelPhase["WHEEL_END"] = "WHEEL_END";
WheelPhase["MOMENTUM_WHEEL_START"] = "MOMENTUM_WHEEL_START";
WheelPhase["MOMENTUM_WHEEL"] = "MOMENTUM_WHEEL";
WheelPhase["MOMENTUM_WHEEL_CANCEL"] = "MOMENTUM_WHEEL_CANCEL";
WheelPhase["MOMENTUM_WHEEL_END"] = "MOMENTUM_WHEEL_END";
})(exports.WheelPhase || (exports.WheelPhase = {}));
var LINE_HEIGHT = 16 * 1.125;

@@ -73,11 +86,25 @@ var PAGE_HEIGHT = typeof window !== 'undefined' && window.innerHeight || 800;

var WHEELEVENTS_TO_MERGE = 2;
var WHEELEVENTS_TO_ANALAZE = 5;
var SOON_ENDING_WHEEL_COUNT = 3;
(function (WheelPhase) {
WheelPhase["ANY_WHEEL_START"] = "ANY_WHEEL_START";
WheelPhase["ANY_WHEEL"] = "ANY_WHEEL";
WheelPhase["ANY_WHEEL_END"] = "ANY_WHEEL_END";
WheelPhase["WHEEL_START"] = "WHEEL_START";
WheelPhase["WHEEL"] = "WHEEL";
WheelPhase["WHEEL_END"] = "WHEEL_END";
WheelPhase["MOMENTUM_WHEEL_START"] = "MOMENTUM_WHEEL_START";
WheelPhase["MOMENTUM_WHEEL"] = "MOMENTUM_WHEEL";
WheelPhase["MOMENTUM_WHEEL_CANCEL"] = "MOMENTUM_WHEEL_CANCEL";
WheelPhase["MOMENTUM_WHEEL_END"] = "MOMENTUM_WHEEL_END";
})(exports.WheelPhase || (exports.WheelPhase = {}));
var SOON_ENDING_THRESHOLD = 1.4;
var ACC_FACTOR_MIN = 0.6;
var ACC_FACTOR_MAX = 0.96;
var DELTA_MAX_ABS = 150; // the initial timeout period is pretty long, so even old mouses, which emit wheel events less often, can produce a continuous gesture
// the timeout is automatically adjusted during a gesture
var DELTA_MAX_ABS = 150; // TODO: test next release
/**
* the timeout is automatically adjusted during a gesture
* the initial timeout period is pretty long, so even old mouses, which emit wheel events less often, can produce a continuous gesture
*/
var WILL_END_TIMEOUT_DEFAULT = 400;

@@ -89,5 +116,7 @@ var axes = ['x', 'y'];

};
var WHEELEVENTS_TO_MERGE = 2;
var WHEELEVENTS_TO_ANALAZE = 5;
var SOON_ENDING_WHEEL_COUNT = 3;
var defaults = {
preventWheelAction: 'all',
isDebug: "development" === 'development'
preventWheelAction: 'all'
};

@@ -106,3 +135,3 @@ var WheelAnalyzer = /*#__PURE__*/function () {

this.lastAbsDelta = Infinity;
this.axisDeltas = [0, 0];
this.axisMovement = [0, 0];
this.axisVelocity = [0, 0];

@@ -116,11 +145,4 @@ this.accelerationFactors = [];

this.willEnd = function () {
var willEndId = setTimeout(function () {}, _this.willEndTimeout);
return function () {
clearTimeout(willEndId);
willEndId = setTimeout(_this.end, _this.willEndTimeout);
};
}();
this.observe = function (target) {
// TODO: need to test if passive supported? might throw error otherwise in older browsers?
target.addEventListener('wheel', _this.feedWheel, {

@@ -161,5 +183,13 @@ passive: false

this.publish = function (phase, data) {
if (data === void 0) {
data = _this.getCurrentState(phase);
}
_this.subscriptions.forEach(function (fn) {
return fn(phase, data);
});
};
this.feedWheel = function (wheelEvents) {
if (!wheelEvents) return;
if (Array.isArray(wheelEvents)) {

@@ -174,12 +204,10 @@ wheelEvents.forEach(function (wheelEvent) {

this.publish = function (phase, data) {
if (data === void 0) {
data = _this.getCurrentState(phase);
}
this.willEnd = function () {
var willEndId;
return function () {
clearTimeout(willEndId);
willEndId = setTimeout(_this.end, _this.willEndTimeout);
};
}();
_this.subscriptions.forEach(function (fn) {
return fn(phase, data);
});
};
this.end = function () {

@@ -236,3 +264,3 @@ if (!_this.isStarted) return;

this.debugMessage('unsupported preventWheelAction value: ' + preventWheelAction, 'warn');
console.warn('unsupported preventWheelAction value: ' + preventWheelAction, 'warn');
};

@@ -265,3 +293,3 @@

this.axisDeltas = this.axisDeltas.map(function (prevDelta, i) {
this.axisMovement = this.axisMovement.map(function (prevDelta, i) {
return prevDelta + _this2.clampDelta(normalizedWheel[deltaProp[axes[i]]]);

@@ -271,6 +299,5 @@ });

this.scrollPointsToMerge.push({
currentDelta: currentDelta,
currentAbsDelta: currentAbsDelta,
axisDeltaUnclampt: [normalizedWheel.deltaX, normalizedWheel.deltaY],
timestamp: wheelEvent.timeStamp || Date.now()
timestamp: wheelEvent.timeStamp
});

@@ -280,5 +307,2 @@

var mergedScrollPoint = {
currentDelta: this.scrollPointsToMerge.reduce(function (sum, b) {
return sum + b.currentDelta;
}, 0) / WHEELEVENTS_TO_MERGE,
currentAbsDelta: this.scrollPointsToMerge.reduce(function (sum, b) {

@@ -295,4 +319,2 @@ return sum + b.currentAbsDelta;

}, [0, 0]),
// TODO: should one devide here or not?!
//.map((sum) => sum / WHEELEVENTS_TO_MERGE),
timestamp: this.scrollPointsToMerge.reduce(function (sum, b) {

@@ -316,4 +338,3 @@ return sum + b.timestamp;

this.updateStartVelocity();
} // publish start after all data points have been updated
// TODO: check momentum afterwards
} // publish start after velocity etc. have been updated

@@ -333,11 +354,2 @@

_proto.debugMessage = function debugMessage(msg, level) {
if (level === void 0) {
level = 'log';
}
if (!this.options.isDebug) return;
console[level](msg);
};
_proto.updateStartVelocity = function updateStartVelocity() {

@@ -366,3 +378,3 @@ var latestScrollPoint = this.scrollPointsToMerge[this.scrollPointsToMerge.length - 1];

if (deltaTime <= 0) {
this.debugMessage('invalid deltaTime');
console.warn('invalid deltaTime');
return;

@@ -432,16 +444,8 @@ } // calc the velocity per axes

_proto.getDebugState = function getDebugState() {
var props = _extends({}, this);
return props;
};
_proto.getCurrentState = function getCurrentState(type) {
var debugData = this.options.isDebug ? this.getDebugState() : null;
return {
type: type,
debugData: debugData,
willEndSoon: this.willEndSoon,
isMomentum: this.isMomentum,
axisDeltas: this.axisDeltas,
axisMovement: this.axisMovement,
axisVelocity: this.axisVelocity

@@ -456,3 +460,3 @@ };

this.lastAbsDelta = Infinity;
this.axisDeltas = [0, 0];
this.axisMovement = [0, 0];
this.axisVelocity = [0, 0];

@@ -482,36 +486,2 @@ this.scrollPointsToMerge = [];

function EventBus(_temp) {
var _ref = _temp === void 0 ? {} : _temp;
_objectDestructuringEmpty(_ref);
var listeners = {};
function on(type, listener) {
listeners[type] = (listeners[type] || []).concat(listener);
return function () {
return off(type, listener);
};
}
function off(type, listener) {
listeners[type] = (listeners[type] || []).filter(function (l) {
return l !== listener;
});
}
function dispatch(type, data) {
if (!(type in listeners)) return;
listeners[type].forEach(function (l) {
return l(data);
});
}
return Object.freeze({
on: on,
off: off,
dispatch: dispatch
});
}
(function (WheelReason) {

@@ -541,3 +511,3 @@ WheelReason["USER"] = "USER";

down: false,
delta: [0, 0],
axisMovement: [0, 0],
axisVelocity: [0, 0]

@@ -561,3 +531,3 @@ };

// TODO: why * -1, should this not better be in analyzer or when used?
delta: data.axisDeltas.map(function (d) {
axisMovement: data.axisMovement.map(function (d) {
return d * -1;

@@ -564,0 +534,0 @@ }),

@@ -1,2 +0,2 @@

"use strict";function e(){return(e=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var s=arguments[t];for(var i in s)Object.prototype.hasOwnProperty.call(s,i)&&(e[i]=s[i])}return e}).apply(this,arguments)}var t;Object.defineProperty(exports,"__esModule",{value:!0}),(t=exports.WheelPhase||(exports.WheelPhase={})).ANY_WHEEL_START="ANY_WHEEL_START",t.ANY_WHEEL="ANY_WHEEL",t.ANY_WHEEL_END="ANY_WHEEL_END",t.WHEEL_START="WHEEL_START",t.WHEEL="WHEEL",t.WHEEL_END="WHEEL_END",t.MOMENTUM_WHEEL_START="MOMENTUM_WHEEL_START",t.MOMENTUM_WHEEL="MOMENTUM_WHEEL",t.MOMENTUM_WHEEL_CANCEL="MOMENTUM_WHEEL_CANCEL",t.MOMENTUM_WHEEL_END="MOMENTUM_WHEEL_END";var s,i,n=[1,18,"undefined"!=typeof window&&window.innerHeight||800],r=["x","y"],o={x:"deltaX",y:"deltaY"},a={preventWheelAction:"all",isDebug:!1},l=function(){function t(t){var s,i=this;void 0===t&&(t={}),this.isStarted=!1,this.isStartPublished=!1,this.isMomentum=!1,this.lastAbsDelta=Infinity,this.axisDeltas=[0,0],this.axisVelocity=[0,0],this.accelerationFactors=[],this.scrollPoints=[],this.scrollPointsToMerge=[],this.subscriptions=[],this.targets=[],this.willEndTimeout=400,this.willEnd=(s=setTimeout((function(){}),i.willEndTimeout),function(){clearTimeout(s),s=setTimeout(i.end,i.willEndTimeout)}),this.observe=function(e){return e.addEventListener("wheel",i.feedWheel,{passive:!1}),i.targets.push(e),i.unobserve.bind(i,e)},this.unobserve=function(e){e.removeEventListener("wheel",i.feedWheel),i.targets=i.targets.filter((function(t){return t!==e}))},this.disconnect=function(){i.targets.forEach(i.unobserve)},this.subscribe=function(e){return i.subscriptions.push(e),i.unsubscribe.bind(i,e)},this.unsubscribe=function(e){if(!e)throw new Error("please pass the callback which was used to subscribe");i.subscriptions=i.subscriptions.filter((function(t){return t!==e}))},this.feedWheel=function(e){e&&(Array.isArray(e)?e.forEach((function(e){return i.processWheelEventData(e)})):i.processWheelEventData(e))},this.publish=function(e,t){void 0===t&&(t=i.getCurrentState(e)),i.subscriptions.forEach((function(s){return s(e,t)}))},this.end=function(){i.isStarted&&(i.publish(i.isMomentum?i.willEndSoon?exports.WheelPhase.MOMENTUM_WHEEL_END:exports.WheelPhase.MOMENTUM_WHEEL_CANCEL:exports.WheelPhase.WHEEL_END),i.publish(exports.WheelPhase.ANY_WHEEL_END),i.isMomentum=!1,i.isStarted=!1)},this.options=Object.entries(t).filter((function(e){return void 0!==e[1]})).reduce((function(e,t){var s;return Object.assign(e,((s={})[t[0]]=t[1],s))}),e({},a))}var s,i=t.prototype;return i.shouldPreventDefault=function(e){var t=this.options.preventWheelAction,s=e.deltaX,i=e.deltaY;switch(t){case"all":return!0;case"x":return Math.abs(s)>=Math.abs(i);case"y":return Math.abs(i)>=Math.abs(s)}this.debugMessage("unsupported preventWheelAction value: "+t,"warn")},i.clampDelta=function(e){return Math.min(150,Math.max(-150,e))},i.processWheelEventData=function(e){var t,s=this,i={deltaX:(t=e).deltaX*n[t.deltaMode],deltaY:t.deltaY*n[t.deltaMode],deltaZ:(t.deltaZ||0)*n[t.deltaMode],deltaMode:0,timestamp:t.timeStamp};e.preventDefault&&this.shouldPreventDefault(e)&&e.preventDefault(),this.isStarted||this.start();var a=this.clampDelta(Math.abs(i.deltaY)>Math.abs(i.deltaX)?i.deltaY:i.deltaX),l=Math.abs(a);if(this.isMomentum&&l>this.lastAbsDelta&&(this.end(),this.start()),this.axisDeltas=this.axisDeltas.map((function(e,t){return e+s.clampDelta(i[o[r[t]]])})),this.lastAbsDelta=l,this.scrollPointsToMerge.push({currentDelta:a,currentAbsDelta:l,axisDeltaUnclampt:[i.deltaX,i.deltaY],timestamp:e.timeStamp||Date.now()}),2===this.scrollPointsToMerge.length){var u={currentDelta:this.scrollPointsToMerge.reduce((function(e,t){return e+t.currentDelta}),0)/2,currentAbsDelta:this.scrollPointsToMerge.reduce((function(e,t){return e+t.currentAbsDelta}),0)/2,axisDeltaUnclampt:this.scrollPointsToMerge.reduce((function(e,t){var s=t.axisDeltaUnclampt;return[e[0]+s[0],e[1]+s[1]]}),[0,0]),timestamp:this.scrollPointsToMerge.reduce((function(e,t){return e+t.timestamp}),0)/2};this.scrollPoints.push(u),this.updateVelocity(),this.isMomentum||this.detectMomentum(),this.scrollPointsToMerge=[]}this.scrollPoints.length||this.updateStartVelocity(),this.isStartPublished||(this.publish(exports.WheelPhase.ANY_WHEEL_START),this.publish(exports.WheelPhase.WHEEL_START),this.isStartPublished=!0),this.publish(exports.WheelPhase.ANY_WHEEL),this.publish(this.isMomentum?exports.WheelPhase.MOMENTUM_WHEEL:exports.WheelPhase.WHEEL),this.willEnd()},i.debugMessage=function(e,t){void 0===t&&(t="log"),this.options.isDebug&&console[t](e)},i.updateStartVelocity=function(){this.axisVelocity=this.scrollPointsToMerge[this.scrollPointsToMerge.length-1].axisDeltaUnclampt.map((function(e){return e/400}))},i.updateVelocity=function(){var e=this,t=this.scrollPoints.slice(-2),s=t[0],i=t[1];if(s&&i){var n=i.timestamp-s.timestamp;if(n<=0)this.debugMessage("invalid deltaTime");else{var r=i.axisDeltaUnclampt.map((function(e){return e/n})),o=r.map((function(t,s){return t/(e.axisVelocity[s]||1)}));this.axisVelocity=r,this.accelerationFactors.push(o),this.updateWillEndTimeout(n)}}},i.updateWillEndTimeout=function(e){var t=10*Math.ceil(e/10)*1.2;this.isMomentum||(t=Math.max(100,2*t)),this.willEndTimeout=Math.min(1e3,Math.round(t))},t.accelerationFactorInMomentumRange=function(e){return 0===e||e<=.96&&e>=.6},i.detectMomentum=function(){if(this.accelerationFactors.length<5)return this.isMomentum;var e=this.accelerationFactors.slice(-5),s=e.reduce((function(e,s){if(!e)return!1;var i=!!s.reduce((function(e,t){return e&&e<1&&e===t?1:0})),n=s.filter(t.accelerationFactorInMomentumRange).length===s.length;return i||n}),!0);return this.accelerationFactors=e,s&&!this.isMomentum&&(this.publish(exports.WheelPhase.WHEEL_END),this.isMomentum=!0,this.publish(exports.WheelPhase.MOMENTUM_WHEEL_START)),this.isMomentum},i.getDebugState=function(){return e({},this)},i.getCurrentState=function(e){return{type:e,debugData:this.options.isDebug?this.getDebugState():null,willEndSoon:this.willEndSoon,isMomentum:this.isMomentum,axisDeltas:this.axisDeltas,axisVelocity:this.axisVelocity}},i.start=function(){this.isStarted=!0,this.isStartPublished=!1,this.isMomentum=!1,this.lastAbsDelta=Infinity,this.axisDeltas=[0,0],this.axisVelocity=[0,0],this.scrollPointsToMerge=[],this.scrollPoints=[],this.accelerationFactors=[],this.willEndTimeout=400},(s=[{key:"willEndSoon",get:function(){var e=this.scrollPoints.slice(-3).map((function(e){return e.currentAbsDelta}));return e.reduce((function(e,t){return e+t}),0)/e.length<=1.4}}])&&function(e,t){for(var s=0;s<t.length;s++){var i=t[s];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}(t.prototype,s),t}();(s=exports.WheelReason||(exports.WheelReason={})).USER="USER",s.ANY="ANY";var u=((i={})[exports.WheelReason.USER]={start:exports.WheelPhase.WHEEL_START,wheel:exports.WheelPhase.WHEEL,end:exports.WheelPhase.WHEEL_END},i[exports.WheelReason.ANY]={start:exports.WheelPhase.ANY_WHEEL_START,wheel:exports.WheelPhase.ANY_WHEEL,end:exports.WheelPhase.ANY_WHEEL_END},i);exports.WheelAnalyzer=l,exports.WheelGestures=function(t){var s=void 0===t?{}:t,i=s.axis,n=s.wheelReason,r=void 0===n?exports.WheelReason.USER:n,o={down:!1,delta:[0,0],axisVelocity:[0,0]},a=new l({preventWheelAction:void 0===i?"all":i}),h=a.observe,c=a.unobserve,E=a.disconnect,d=function(e){!function(e){if(null=={})throw new TypeError("Cannot destructure undefined")}();var t={};function s(e,s){t[e]=(t[e]||[]).filter((function(e){return e!==s}))}return Object.freeze({on:function(e,i){return t[e]=(t[e]||[]).concat(i),function(){return s(e,i)}},off:s,dispatch:function(e,s){e in t&&t[e].forEach((function(e){return e(s)}))}})}(),p=d.on,f=d.off,M=d.dispatch;return a.subscribe((function(t,s){switch(o={down:!0,delta:s.axisDeltas.map((function(e){return-1*e})),axisVelocity:[-1*s.axisVelocity[0],-1*s.axisVelocity[1]]},t){case u[r].start:o=e({},o,{down:!0}),M("wheelstart",o);break;case u[r].wheel:o=e({},o,{down:!0}),M("wheelmove",o);break;case u[r].end:o=e({},o,{down:!1}),M("wheelend",o);break;default:return}})),Object.freeze({observe:h,unobserve:c,disconnect:E,on:p,off:f})};
"use strict";function e(){return(e=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var s=arguments[t];for(var i in s)Object.prototype.hasOwnProperty.call(s,i)&&(e[i]=s[i])}return e}).apply(this,arguments)}Object.defineProperty(exports,"__esModule",{value:!0});var t,s=[1,18,"undefined"!=typeof window&&window.innerHeight||800];(t=exports.WheelPhase||(exports.WheelPhase={})).ANY_WHEEL_START="ANY_WHEEL_START",t.ANY_WHEEL="ANY_WHEEL",t.ANY_WHEEL_END="ANY_WHEEL_END",t.WHEEL_START="WHEEL_START",t.WHEEL="WHEEL",t.WHEEL_END="WHEEL_END",t.MOMENTUM_WHEEL_START="MOMENTUM_WHEEL_START",t.MOMENTUM_WHEEL="MOMENTUM_WHEEL",t.MOMENTUM_WHEEL_CANCEL="MOMENTUM_WHEEL_CANCEL",t.MOMENTUM_WHEEL_END="MOMENTUM_WHEEL_END";var i,n,r=["x","y"],o={x:"deltaX",y:"deltaY"},a={preventWheelAction:"all"},l=function(){function t(t){var s,i=this;void 0===t&&(t={}),this.isStarted=!1,this.isStartPublished=!1,this.isMomentum=!1,this.lastAbsDelta=Infinity,this.axisMovement=[0,0],this.axisVelocity=[0,0],this.accelerationFactors=[],this.scrollPoints=[],this.scrollPointsToMerge=[],this.subscriptions=[],this.targets=[],this.willEndTimeout=400,this.observe=function(e){return e.addEventListener("wheel",i.feedWheel,{passive:!1}),i.targets.push(e),i.unobserve.bind(i,e)},this.unobserve=function(e){e.removeEventListener("wheel",i.feedWheel),i.targets=i.targets.filter((function(t){return t!==e}))},this.disconnect=function(){i.targets.forEach(i.unobserve)},this.subscribe=function(e){return i.subscriptions.push(e),i.unsubscribe.bind(i,e)},this.unsubscribe=function(e){if(!e)throw new Error("please pass the callback which was used to subscribe");i.subscriptions=i.subscriptions.filter((function(t){return t!==e}))},this.publish=function(e,t){void 0===t&&(t=i.getCurrentState(e)),i.subscriptions.forEach((function(s){return s(e,t)}))},this.feedWheel=function(e){Array.isArray(e)?e.forEach((function(e){return i.processWheelEventData(e)})):i.processWheelEventData(e)},this.willEnd=function(){clearTimeout(s),s=setTimeout(i.end,i.willEndTimeout)},this.end=function(){i.isStarted&&(i.publish(i.isMomentum?i.willEndSoon?exports.WheelPhase.MOMENTUM_WHEEL_END:exports.WheelPhase.MOMENTUM_WHEEL_CANCEL:exports.WheelPhase.WHEEL_END),i.publish(exports.WheelPhase.ANY_WHEEL_END),i.isMomentum=!1,i.isStarted=!1)},this.options=Object.entries(t).filter((function(e){return void 0!==e[1]})).reduce((function(e,t){var s;return Object.assign(e,((s={})[t[0]]=t[1],s))}),e({},a))}var i,n=t.prototype;return n.shouldPreventDefault=function(e){var t=e.deltaX,s=e.deltaY;switch(this.options.preventWheelAction){case"all":return!0;case"x":return Math.abs(t)>=Math.abs(s);case"y":return Math.abs(s)>=Math.abs(t)}},n.clampDelta=function(e){return Math.min(150,Math.max(-150,e))},n.processWheelEventData=function(e){var t,i=this,n={deltaX:(t=e).deltaX*s[t.deltaMode],deltaY:t.deltaY*s[t.deltaMode],deltaZ:(t.deltaZ||0)*s[t.deltaMode],deltaMode:0,timestamp:t.timeStamp};e.preventDefault&&this.shouldPreventDefault(e)&&e.preventDefault(),this.isStarted||this.start();var a=this.clampDelta(Math.abs(n.deltaY)>Math.abs(n.deltaX)?n.deltaY:n.deltaX),l=Math.abs(a);if(this.isMomentum&&l>this.lastAbsDelta&&(this.end(),this.start()),this.axisMovement=this.axisMovement.map((function(e,t){return e+i.clampDelta(n[o[r[t]]])})),this.lastAbsDelta=l,this.scrollPointsToMerge.push({currentAbsDelta:l,axisDeltaUnclampt:[n.deltaX,n.deltaY],timestamp:e.timeStamp}),2===this.scrollPointsToMerge.length){var h={currentAbsDelta:this.scrollPointsToMerge.reduce((function(e,t){return e+t.currentAbsDelta}),0)/2,axisDeltaUnclampt:this.scrollPointsToMerge.reduce((function(e,t){var s=t.axisDeltaUnclampt;return[e[0]+s[0],e[1]+s[1]]}),[0,0]),timestamp:this.scrollPointsToMerge.reduce((function(e,t){return e+t.timestamp}),0)/2};this.scrollPoints.push(h),this.updateVelocity(),this.isMomentum||this.detectMomentum(),this.scrollPointsToMerge=[]}this.scrollPoints.length||this.updateStartVelocity(),this.isStartPublished||(this.publish(exports.WheelPhase.ANY_WHEEL_START),this.publish(exports.WheelPhase.WHEEL_START),this.isStartPublished=!0),this.publish(exports.WheelPhase.ANY_WHEEL),this.publish(this.isMomentum?exports.WheelPhase.MOMENTUM_WHEEL:exports.WheelPhase.WHEEL),this.willEnd()},n.updateStartVelocity=function(){this.axisVelocity=this.scrollPointsToMerge[this.scrollPointsToMerge.length-1].axisDeltaUnclampt.map((function(e){return e/400}))},n.updateVelocity=function(){var e=this,t=this.scrollPoints.slice(-2),s=t[0],i=t[1];if(s&&i){var n=i.timestamp-s.timestamp;if(!(n<=0)){var r=i.axisDeltaUnclampt.map((function(e){return e/n})),o=r.map((function(t,s){return t/(e.axisVelocity[s]||1)}));this.axisVelocity=r,this.accelerationFactors.push(o),this.updateWillEndTimeout(n)}}},n.updateWillEndTimeout=function(e){var t=10*Math.ceil(e/10)*1.2;this.isMomentum||(t=Math.max(100,2*t)),this.willEndTimeout=Math.min(1e3,Math.round(t))},t.accelerationFactorInMomentumRange=function(e){return 0===e||e<=.96&&e>=.6},n.detectMomentum=function(){if(this.accelerationFactors.length<5)return this.isMomentum;var e=this.accelerationFactors.slice(-5),s=e.reduce((function(e,s){if(!e)return!1;var i=!!s.reduce((function(e,t){return e&&e<1&&e===t?1:0})),n=s.filter(t.accelerationFactorInMomentumRange).length===s.length;return i||n}),!0);return this.accelerationFactors=e,s&&!this.isMomentum&&(this.publish(exports.WheelPhase.WHEEL_END),this.isMomentum=!0,this.publish(exports.WheelPhase.MOMENTUM_WHEEL_START)),this.isMomentum},n.getCurrentState=function(e){return{type:e,willEndSoon:this.willEndSoon,isMomentum:this.isMomentum,axisMovement:this.axisMovement,axisVelocity:this.axisVelocity}},n.start=function(){this.isStarted=!0,this.isStartPublished=!1,this.isMomentum=!1,this.lastAbsDelta=Infinity,this.axisMovement=[0,0],this.axisVelocity=[0,0],this.scrollPointsToMerge=[],this.scrollPoints=[],this.accelerationFactors=[],this.willEndTimeout=400},(i=[{key:"willEndSoon",get:function(){var e=this.scrollPoints.slice(-3).map((function(e){return e.currentAbsDelta}));return e.reduce((function(e,t){return e+t}),0)/e.length<=1.4}}])&&function(e,t){for(var s=0;s<t.length;s++){var i=t[s];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}(t.prototype,i),t}();(i=exports.WheelReason||(exports.WheelReason={})).USER="USER",i.ANY="ANY";var h=((n={})[exports.WheelReason.USER]={start:exports.WheelPhase.WHEEL_START,wheel:exports.WheelPhase.WHEEL,end:exports.WheelPhase.WHEEL_END},n[exports.WheelReason.ANY]={start:exports.WheelPhase.ANY_WHEEL_START,wheel:exports.WheelPhase.ANY_WHEEL,end:exports.WheelPhase.ANY_WHEEL_END},n);exports.WheelAnalyzer=l,exports.WheelGestures=function(t){var s=void 0===t?{}:t,i=s.axis,n=s.wheelReason,r=void 0===n?exports.WheelReason.USER:n,o={down:!1,axisMovement:[0,0],axisVelocity:[0,0]},a=new l({preventWheelAction:void 0===i?"all":i}),u=a.observe,c=a.unobserve,E=a.disconnect,d=function(){var e={};function t(t,s){e[t]=(e[t]||[]).filter((function(e){return e!==s}))}return Object.freeze({on:function(s,i){return e[s]=(e[s]||[]).concat(i),function(){return t(s,i)}},off:t,dispatch:function(t,s){t in e&&e[t].forEach((function(e){return e(s)}))}})}(),p=d.on,f=d.off,m=d.dispatch;return a.subscribe((function(t,s){switch(o={down:!0,axisMovement:s.axisMovement.map((function(e){return-1*e})),axisVelocity:[-1*s.axisVelocity[0],-1*s.axisVelocity[1]]},t){case h[r].start:o=e({},o,{down:!0}),m("wheelstart",o);break;case h[r].wheel:o=e({},o,{down:!0}),m("wheelmove",o);break;case h[r].end:o=e({},o,{down:!1}),m("wheelend",o);break;default:return}})),Object.freeze({observe:u,unobserve:c,disconnect:E,on:p,off:f})};
//# sourceMappingURL=wheel-gestures.cjs.production.min.js.map

@@ -35,21 +35,32 @@ function _defineProperties(target, props) {

function _objectDestructuringEmpty(obj) {
if (obj == null) throw new TypeError("Cannot destructure undefined");
}
function EventBus() {
var listeners = {};
var WheelPhase;
function on(type, listener) {
listeners[type] = (listeners[type] || []).concat(listener);
return function () {
return off(type, listener);
};
}
(function (WheelPhase) {
WheelPhase["ANY_WHEEL_START"] = "ANY_WHEEL_START";
WheelPhase["ANY_WHEEL"] = "ANY_WHEEL";
WheelPhase["ANY_WHEEL_END"] = "ANY_WHEEL_END";
WheelPhase["WHEEL_START"] = "WHEEL_START";
WheelPhase["WHEEL"] = "WHEEL";
WheelPhase["WHEEL_END"] = "WHEEL_END";
WheelPhase["MOMENTUM_WHEEL_START"] = "MOMENTUM_WHEEL_START";
WheelPhase["MOMENTUM_WHEEL"] = "MOMENTUM_WHEEL";
WheelPhase["MOMENTUM_WHEEL_CANCEL"] = "MOMENTUM_WHEEL_CANCEL";
WheelPhase["MOMENTUM_WHEEL_END"] = "MOMENTUM_WHEEL_END";
})(WheelPhase || (WheelPhase = {}));
function off(type, listener) {
listeners[type] = (listeners[type] || []).filter(function (l) {
return l !== listener;
});
}
function dispatch(type, data) {
if (!(type in listeners)) return;
listeners[type].forEach(function (l) {
return l(data);
});
}
return Object.freeze({
on: on,
off: off,
dispatch: dispatch
});
}
var LINE_HEIGHT = 16 * 1.125;

@@ -71,11 +82,27 @@ var PAGE_HEIGHT = typeof window !== 'undefined' && window.innerHeight || 800;

var WHEELEVENTS_TO_MERGE = 2;
var WHEELEVENTS_TO_ANALAZE = 5;
var SOON_ENDING_WHEEL_COUNT = 3;
var WheelPhase;
(function (WheelPhase) {
WheelPhase["ANY_WHEEL_START"] = "ANY_WHEEL_START";
WheelPhase["ANY_WHEEL"] = "ANY_WHEEL";
WheelPhase["ANY_WHEEL_END"] = "ANY_WHEEL_END";
WheelPhase["WHEEL_START"] = "WHEEL_START";
WheelPhase["WHEEL"] = "WHEEL";
WheelPhase["WHEEL_END"] = "WHEEL_END";
WheelPhase["MOMENTUM_WHEEL_START"] = "MOMENTUM_WHEEL_START";
WheelPhase["MOMENTUM_WHEEL"] = "MOMENTUM_WHEEL";
WheelPhase["MOMENTUM_WHEEL_CANCEL"] = "MOMENTUM_WHEEL_CANCEL";
WheelPhase["MOMENTUM_WHEEL_END"] = "MOMENTUM_WHEEL_END";
})(WheelPhase || (WheelPhase = {}));
var SOON_ENDING_THRESHOLD = 1.4;
var ACC_FACTOR_MIN = 0.6;
var ACC_FACTOR_MAX = 0.96;
var DELTA_MAX_ABS = 150; // the initial timeout period is pretty long, so even old mouses, which emit wheel events less often, can produce a continuous gesture
// the timeout is automatically adjusted during a gesture
var DELTA_MAX_ABS = 150; // TODO: test next release
/**
* the timeout is automatically adjusted during a gesture
* the initial timeout period is pretty long, so even old mouses, which emit wheel events less often, can produce a continuous gesture
*/
var WILL_END_TIMEOUT_DEFAULT = 400;

@@ -87,5 +114,8 @@ var axes = ['x', 'y'];

};
var isDev = process.env.NODE_ENV !== 'production';
var WHEELEVENTS_TO_MERGE = 2;
var WHEELEVENTS_TO_ANALAZE = 5;
var SOON_ENDING_WHEEL_COUNT = 3;
var defaults = {
preventWheelAction: 'all',
isDebug: process.env.NODE_ENV === 'development'
preventWheelAction: 'all'
};

@@ -104,3 +134,3 @@ var WheelAnalyzer = /*#__PURE__*/function () {

this.lastAbsDelta = Infinity;
this.axisDeltas = [0, 0];
this.axisMovement = [0, 0];
this.axisVelocity = [0, 0];

@@ -114,11 +144,4 @@ this.accelerationFactors = [];

this.willEnd = function () {
var willEndId = setTimeout(function () {}, _this.willEndTimeout);
return function () {
clearTimeout(willEndId);
willEndId = setTimeout(_this.end, _this.willEndTimeout);
};
}();
this.observe = function (target) {
// TODO: need to test if passive supported? might throw error otherwise in older browsers?
target.addEventListener('wheel', _this.feedWheel, {

@@ -159,5 +182,13 @@ passive: false

this.publish = function (phase, data) {
if (data === void 0) {
data = _this.getCurrentState(phase);
}
_this.subscriptions.forEach(function (fn) {
return fn(phase, data);
});
};
this.feedWheel = function (wheelEvents) {
if (!wheelEvents) return;
if (Array.isArray(wheelEvents)) {

@@ -172,12 +203,10 @@ wheelEvents.forEach(function (wheelEvent) {

this.publish = function (phase, data) {
if (data === void 0) {
data = _this.getCurrentState(phase);
}
this.willEnd = function () {
var willEndId;
return function () {
clearTimeout(willEndId);
willEndId = setTimeout(_this.end, _this.willEndTimeout);
};
}();
_this.subscriptions.forEach(function (fn) {
return fn(phase, data);
});
};
this.end = function () {

@@ -234,3 +263,3 @@ if (!_this.isStarted) return;

this.debugMessage('unsupported preventWheelAction value: ' + preventWheelAction, 'warn');
isDev && console.warn('unsupported preventWheelAction value: ' + preventWheelAction, 'warn');
};

@@ -263,3 +292,3 @@

this.axisDeltas = this.axisDeltas.map(function (prevDelta, i) {
this.axisMovement = this.axisMovement.map(function (prevDelta, i) {
return prevDelta + _this2.clampDelta(normalizedWheel[deltaProp[axes[i]]]);

@@ -269,6 +298,5 @@ });

this.scrollPointsToMerge.push({
currentDelta: currentDelta,
currentAbsDelta: currentAbsDelta,
axisDeltaUnclampt: [normalizedWheel.deltaX, normalizedWheel.deltaY],
timestamp: wheelEvent.timeStamp || Date.now()
timestamp: wheelEvent.timeStamp
});

@@ -278,5 +306,2 @@

var mergedScrollPoint = {
currentDelta: this.scrollPointsToMerge.reduce(function (sum, b) {
return sum + b.currentDelta;
}, 0) / WHEELEVENTS_TO_MERGE,
currentAbsDelta: this.scrollPointsToMerge.reduce(function (sum, b) {

@@ -293,4 +318,2 @@ return sum + b.currentAbsDelta;

}, [0, 0]),
// TODO: should one devide here or not?!
//.map((sum) => sum / WHEELEVENTS_TO_MERGE),
timestamp: this.scrollPointsToMerge.reduce(function (sum, b) {

@@ -314,4 +337,3 @@ return sum + b.timestamp;

this.updateStartVelocity();
} // publish start after all data points have been updated
// TODO: check momentum afterwards
} // publish start after velocity etc. have been updated

@@ -331,11 +353,2 @@

_proto.debugMessage = function debugMessage(msg, level) {
if (level === void 0) {
level = 'log';
}
if (!this.options.isDebug) return;
console[level](msg);
};
_proto.updateStartVelocity = function updateStartVelocity() {

@@ -364,3 +377,3 @@ var latestScrollPoint = this.scrollPointsToMerge[this.scrollPointsToMerge.length - 1];

if (deltaTime <= 0) {
this.debugMessage('invalid deltaTime');
isDev && console.warn('invalid deltaTime');
return;

@@ -430,16 +443,8 @@ } // calc the velocity per axes

_proto.getDebugState = function getDebugState() {
var props = _extends({}, this);
return props;
};
_proto.getCurrentState = function getCurrentState(type) {
var debugData = this.options.isDebug ? this.getDebugState() : null;
return {
type: type,
debugData: debugData,
willEndSoon: this.willEndSoon,
isMomentum: this.isMomentum,
axisDeltas: this.axisDeltas,
axisMovement: this.axisMovement,
axisVelocity: this.axisVelocity

@@ -454,3 +459,3 @@ };

this.lastAbsDelta = Infinity;
this.axisDeltas = [0, 0];
this.axisMovement = [0, 0];
this.axisVelocity = [0, 0];

@@ -480,36 +485,2 @@ this.scrollPointsToMerge = [];

function EventBus(_temp) {
var _ref = _temp === void 0 ? {} : _temp;
_objectDestructuringEmpty(_ref);
var listeners = {};
function on(type, listener) {
listeners[type] = (listeners[type] || []).concat(listener);
return function () {
return off(type, listener);
};
}
function off(type, listener) {
listeners[type] = (listeners[type] || []).filter(function (l) {
return l !== listener;
});
}
function dispatch(type, data) {
if (!(type in listeners)) return;
listeners[type].forEach(function (l) {
return l(data);
});
}
return Object.freeze({
on: on,
off: off,
dispatch: dispatch
});
}
var WheelReason;

@@ -541,3 +512,3 @@

down: false,
delta: [0, 0],
axisMovement: [0, 0],
axisVelocity: [0, 0]

@@ -561,3 +532,3 @@ };

// TODO: why * -1, should this not better be in analyzer or when used?
delta: data.axisDeltas.map(function (d) {
axisMovement: data.axisMovement.map(function (d) {
return d * -1;

@@ -564,0 +535,0 @@ }),

{
"name": "wheel-gestures",
"version": "1.0.2-beta.4",
"version": "1.0.2",
"description": "wheel gestures and momentum detection",

@@ -51,2 +51,4 @@ "main": "dist/index.js",

"bundlewatch": "^0.2.6",
"eslint-plugin-import": "^2.19.1",
"eslint-plugin-simple-import-sort": "^5.0.0",
"husky": "^4.2.5",

@@ -53,0 +55,0 @@ "tsdx": "^0.13.2",

@@ -13,3 +13,3 @@ # ![wheel gestures](./WheelGestures.svg)

**OS & Browsers**k
**OS & Browsers**

@@ -16,0 +16,0 @@ - Mac OS (Chrome, Firefox, Safari, Brave, Edge)

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc