UNPKG

lisn.js

Version:

Simply handle user gestures and actions. Includes widgets.

1 lines 10.6 kB
{"version":3,"file":"normalize-wheel.cjs","names":["_settings","require","normalizeWheel","event","spinX","spinY","pixelX","deltaX","pixelY","deltaY","LINE","settings","deltaLineHeight","detail","undefined","wheelDelta","wheelDeltaY","wheelDeltaX","deltaMode","deltaPageWidth","deltaPageHeight","exports"],"sources":["../../../src/ts/utils/normalize-wheel.ts"],"sourcesContent":["/**\n * @module\n * @ignore\n * @internal\n *\n * FULL CREDIT FOR THIS GOES TO\n * https://github.com/facebookarchive/fixed-data-table/blob/master/src/vendor_upstream/dom/normalizeWheel.js\n *\n * ADAPTED FROM THE ABOVE SOURCE\n *\n * ORIGINAL COPYRIGHT IN FILE PRESERVED:\n *\n * Copyright (c) 2015, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under the BSD-style license found in the\n * LICENSE file in the root directory of this source tree. An additional grant\n * of patent rights can be found in the PATENTS file in the same directory.\n *\n * ORIGINAL LICENSE\n *\n * BSD License\n *\n * For FixedDataTable software\n *\n * Copyright (c) 2015, Facebook, Inc. All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without modification,\n * are permitted provided that the following conditions are met:\n *\n * * Redistributions of source code must retain the above copyright notice, this\n * list of conditions and the following disclaimer.\n *\n * * Redistributions in binary form must reproduce the above copyright notice,\n * this list of conditions and the following disclaimer in the documentation\n * and/or other materials provided with the distribution.\n *\n * * Neither the name Facebook nor the names of its contributors may be used to\n * endorse or promote products derived from this software without specific\n * prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND\n * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR\n * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\n * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\n * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\nimport { settings } from \"@lisn/globals/settings\";\n\n/**\n * ORIGINAL DEVELOPER COMMENT PRESERVED\n *\n * Mouse wheel (and 2-finger trackpad) support on the web sucks. It is\n * complicated, thus this doc is long and (hopefully) detailed enough to answer\n * your questions.\n *\n * If you need to react to the mouse wheel in a predictable way, this code is\n * like your bestest friend. * hugs *\n *\n * As of today, there are 4 DOM event types you can listen to:\n *\n * 'wheel' -- Chrome(31+), FF(17+), IE(9+)\n * 'mousewheel' -- Chrome, IE(6+), Opera, Safari\n * 'MozMousePixelScroll' -- FF(3.5 only!) (2010-2013) -- don't bother!\n * 'DOMMouseScroll' -- FF(0.9.7+) since 2003\n *\n * So what to do? The is the best:\n *\n * normalizeWheel.getEventType();\n *\n * In your event callback, use this code to get sane interpretation of the\n * deltas. This code will return an object with properties:\n *\n * spinX -- normalized spin speed (use for zoom) - x plane\n * spinY -- \" - y plane\n * pixelX -- normalized distance (to pixels) - x plane\n * pixelY -- \" - y plane\n *\n * Wheel values are provided by the browser assuming you are using the wheel to\n * scroll a web page by a number of lines or pixels (or pages). Values can vary\n * significantly on different platforms and browsers, forgetting that you can\n * scroll at different speeds. Some devices (like trackpads) emit more events\n * at smaller increments with fine granularity, and some emit massive jumps with\n * linear speed or acceleration.\n *\n * This code does its best to normalize the deltas for you:\n *\n * - spin is trying to normalize how far the wheel was spun (or trackpad\n * dragged). This is super useful for zoom support where you want to\n * throw away the chunky scroll steps on the PC and make those equal to\n * the slow and smooth tiny steps on the Mac. Key data: This code tries to\n * resolve a single slow step on a wheel to 1.\n *\n * - pixel is normalizing the desired scroll delta in pixel units. You'll\n * get the crazy differences between browsers, but at least it'll be in\n * pixels!\n *\n * - positive value indicates scrolling DOWN/RIGHT, negative UP/LEFT. This\n * should translate to positive value zooming IN, negative zooming OUT.\n * This matches the newer 'wheel' event.\n *\n * Why are there spinX, spinY (or pixels)?\n *\n * - spinX is a 2-finger side drag on the trackpad, and a shift + wheel turn\n * with a mouse. It results in side-scrolling in the browser by default.\n *\n * - spinY is what you expect -- it's the classic axis of a mouse wheel.\n *\n * - I dropped spinZ/pixelZ. It is supported by the DOM 3 'wheel' event and\n * probably is by browsers in conjunction with fancy 3D controllers .. but\n * you know.\n *\n * Implementation info:\n *\n * Examples of 'wheel' event if you scroll slowly (down) by one step with an\n * average mouse:\n *\n * OS X + Chrome (mouse) - 4 pixel delta (wheelDelta -120)\n * OS X + Safari (mouse) - N/A pixel delta (wheelDelta -12)\n * OS X + Firefox (mouse) - 0.1 line delta (wheelDelta N/A)\n * Win8 + Chrome (mouse) - 100 pixel delta (wheelDelta -120)\n * Win8 + Firefox (mouse) - 3 line delta (wheelDelta -120)\n *\n * On the trackpad:\n *\n * OS X + Chrome (trackpad) - 2 pixel delta (wheelDelta -6)\n * OS X + Firefox (trackpad) - 1 pixel delta (wheelDelta N/A)\n *\n * On other/older browsers.. it's more complicated as there can be multiple and\n * also missing delta values.\n *\n * The 'wheel' event is more standard:\n *\n * http://www.w3.org/TR/DOM-Level-3-Events/#events-wheelevents\n *\n * The basics is that it includes a unit, deltaMode (pixels, lines, pages), and\n * deltaX, deltaY and deltaZ. Some browsers provide other values to maintain\n * backward compatibility with older events. Those other values help us\n * better normalize spin speed. Example of what the browsers provide:\n *\n * | event.wheelDelta | event.detail\n * ------------------+------------------+--------------\n * Safari v5/OS X | -120 | 0\n * Safari v5/Win7 | -120 | 0\n * Chrome v17/OS X | -120 | 0\n * Chrome v17/Win7 | -120 | 0\n * IE9/Win7 | -120 | undefined\n * Firefox v4/OS X | undefined | 1\n * Firefox v4/Win7 | undefined | 3\n */\nexport const normalizeWheel = (event: LegacyWheelEvent): WheelData => {\n let spinX = 0,\n spinY = 0,\n pixelX = event.deltaX,\n pixelY = event.deltaY;\n\n const LINE = settings.deltaLineHeight;\n\n // Legacy\n if (event.detail !== undefined) {\n spinY = event.detail;\n }\n if (event.wheelDelta !== undefined) {\n spinY = -event.wheelDelta / 120;\n }\n if (event.wheelDeltaY !== undefined) {\n spinY = -event.wheelDeltaY / 120;\n }\n if (event.wheelDeltaX !== undefined) {\n spinX = -event.wheelDeltaX / 120;\n }\n\n if ((pixelX || pixelY) && event.deltaMode) {\n if (event.deltaMode === 1) {\n // delta in LINE units\n pixelX *= LINE;\n pixelY *= LINE;\n } else {\n // delta in PAGE units\n pixelX *= settings.deltaPageWidth;\n pixelY *= settings.deltaPageHeight;\n }\n }\n\n // Fall-back if spin cannot be determined\n if (pixelX && !spinX) {\n spinX = pixelX < 1 ? -1 : 1;\n }\n if (pixelY && !spinY) {\n spinY = pixelY < 1 ? -1 : 1;\n }\n\n return { spinX, spinY, pixelX, pixelY };\n};\n\nexport type WheelData = {\n spinX: number;\n spinY: number;\n pixelX: number;\n pixelY: number;\n};\n\n// --------------------\n\ntype LegacyWheelEvent = WheelEvent & {\n wheelDelta?: number;\n wheelDeltaX?: number;\n wheelDeltaY?: number;\n};\n"],"mappings":";;;;;;AAqDA,IAAAA,SAAA,GAAAC,OAAA;AArDA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMC,cAAc,GAAIC,KAAuB,IAAgB;EACpE,IAAIC,KAAK,GAAG,CAAC;IACXC,KAAK,GAAG,CAAC;IACTC,MAAM,GAAGH,KAAK,CAACI,MAAM;IACrBC,MAAM,GAAGL,KAAK,CAACM,MAAM;EAEvB,MAAMC,IAAI,GAAGC,kBAAQ,CAACC,eAAe;;EAErC;EACA,IAAIT,KAAK,CAACU,MAAM,KAAKC,SAAS,EAAE;IAC9BT,KAAK,GAAGF,KAAK,CAACU,MAAM;EACtB;EACA,IAAIV,KAAK,CAACY,UAAU,KAAKD,SAAS,EAAE;IAClCT,KAAK,GAAG,CAACF,KAAK,CAACY,UAAU,GAAG,GAAG;EACjC;EACA,IAAIZ,KAAK,CAACa,WAAW,KAAKF,SAAS,EAAE;IACnCT,KAAK,GAAG,CAACF,KAAK,CAACa,WAAW,GAAG,GAAG;EAClC;EACA,IAAIb,KAAK,CAACc,WAAW,KAAKH,SAAS,EAAE;IACnCV,KAAK,GAAG,CAACD,KAAK,CAACc,WAAW,GAAG,GAAG;EAClC;EAEA,IAAI,CAACX,MAAM,IAAIE,MAAM,KAAKL,KAAK,CAACe,SAAS,EAAE;IACzC,IAAIf,KAAK,CAACe,SAAS,KAAK,CAAC,EAAE;MACzB;MACAZ,MAAM,IAAII,IAAI;MACdF,MAAM,IAAIE,IAAI;IAChB,CAAC,MAAM;MACL;MACAJ,MAAM,IAAIK,kBAAQ,CAACQ,cAAc;MACjCX,MAAM,IAAIG,kBAAQ,CAACS,eAAe;IACpC;EACF;;EAEA;EACA,IAAId,MAAM,IAAI,CAACF,KAAK,EAAE;IACpBA,KAAK,GAAGE,MAAM,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC;EAC7B;EACA,IAAIE,MAAM,IAAI,CAACH,KAAK,EAAE;IACpBA,KAAK,GAAGG,MAAM,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC;EAC7B;EAEA,OAAO;IAAEJ,KAAK;IAAEC,KAAK;IAAEC,MAAM;IAAEE;EAAO,CAAC;AACzC,CAAC;;AASD;AAAAa,OAAA,CAAAnB,cAAA,GAAAA,cAAA","ignoreList":[]}