ReactWheelHandler.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * Copyright (c) 2013-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * This is utility that hanlds onWheel events and calls provided wheel
  8. * callback with correct frame rate.
  9. *
  10. * @typechecks
  11. */
  12. 'use strict';
  13. var emptyFunction = require("./emptyFunction");
  14. var normalizeWheel = require("./normalizeWheel");
  15. var requestAnimationFramePolyfill = require("./requestAnimationFramePolyfill");
  16. var ReactWheelHandler =
  17. /*#__PURE__*/
  18. function () {
  19. /**
  20. * onWheel is the callback that will be called with right frame rate if
  21. * any wheel events happened
  22. * onWheel should is to be called with two arguments: deltaX and deltaY in
  23. * this order
  24. */
  25. function ReactWheelHandler(
  26. /*function*/
  27. onWheel,
  28. /*boolean|function*/
  29. handleScrollX,
  30. /*boolean|function*/
  31. handleScrollY,
  32. /*?boolean|?function*/
  33. stopPropagation) {
  34. this._animationFrameID = null;
  35. this._deltaX = 0;
  36. this._deltaY = 0;
  37. this._didWheel = this._didWheel.bind(this);
  38. if (typeof handleScrollX !== 'function') {
  39. handleScrollX = handleScrollX ? emptyFunction.thatReturnsTrue : emptyFunction.thatReturnsFalse;
  40. }
  41. if (typeof handleScrollY !== 'function') {
  42. handleScrollY = handleScrollY ? emptyFunction.thatReturnsTrue : emptyFunction.thatReturnsFalse;
  43. }
  44. if (typeof stopPropagation !== 'function') {
  45. stopPropagation = stopPropagation ? emptyFunction.thatReturnsTrue : emptyFunction.thatReturnsFalse;
  46. }
  47. this._handleScrollX = handleScrollX;
  48. this._handleScrollY = handleScrollY;
  49. this._stopPropagation = stopPropagation;
  50. this._onWheelCallback = onWheel;
  51. this.onWheel = this.onWheel.bind(this);
  52. }
  53. var _proto = ReactWheelHandler.prototype;
  54. _proto.onWheel = function onWheel(
  55. /*object*/
  56. event) {
  57. var normalizedEvent = normalizeWheel(event);
  58. var deltaX = this._deltaX + normalizedEvent.pixelX;
  59. var deltaY = this._deltaY + normalizedEvent.pixelY;
  60. var handleScrollX = this._handleScrollX(deltaX, deltaY);
  61. var handleScrollY = this._handleScrollY(deltaY, deltaX);
  62. if (!handleScrollX && !handleScrollY) {
  63. return;
  64. }
  65. this._deltaX += handleScrollX ? normalizedEvent.pixelX : 0;
  66. this._deltaY += handleScrollY ? normalizedEvent.pixelY : 0;
  67. event.preventDefault();
  68. var changed;
  69. if (this._deltaX !== 0 || this._deltaY !== 0) {
  70. if (this._stopPropagation()) {
  71. event.stopPropagation();
  72. }
  73. changed = true;
  74. }
  75. if (changed === true && this._animationFrameID === null) {
  76. this._animationFrameID = requestAnimationFramePolyfill(this._didWheel);
  77. }
  78. };
  79. _proto._didWheel = function _didWheel() {
  80. this._animationFrameID = null;
  81. this._onWheelCallback(this._deltaX, this._deltaY);
  82. this._deltaX = 0;
  83. this._deltaY = 0;
  84. };
  85. return ReactWheelHandler;
  86. }();
  87. module.exports = ReactWheelHandler;