UNPKG

react-spy-scroll

Version:
293 lines (236 loc) 11 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.__RewireAPI__ = exports.__ResetDependency__ = exports.__set__ = exports.__Rewire__ = exports.__GetDependency__ = exports.__get__ = undefined; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /** * Class for executing and maintaining a smooth scroll-animation using TweenFunctions for easing */ var _tweenFunctions = require('tween-functions'); var _tweenFunctions2 = _interopRequireDefault(_tweenFunctions); var _requestAnimationFrame = require('./request-animation-frame'); var _requestAnimationFrame2 = _interopRequireDefault(_requestAnimationFrame); var _utils = require('./utils'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var fn = function fn(value) { return typeof value === 'function' ? value : function () { return value; }; }; var AnimateScroll = function () { function AnimateScroll(options) { _classCallCheck(this, AnimateScroll); this._setup(options); } _createClass(AnimateScroll, [{ key: '_setup', value: function _setup(opt) { var options = opt || this._options; this._start = null; this._cancel = false; this._duration = options.duration ? _get__('fn')(options.duration) : _get__('fn')(500); this._container = options.container || document.body || document.documentElement; this._startPosition = _get__('getScrollYPosition')(this._container); this._options = options; this._id = null; this._component = null; } }, { key: '_setupListeners', value: function _setupListeners() { var _this = this; this._listener = this.cancel.bind(this); _get__('AnimateScroll').cancelEvent.forEach(function (e) { return _this._container.addEventListener(e, _this._listener); }); } }, { key: '_teardownListeners', value: function _teardownListeners() { var _this2 = this; _get__('AnimateScroll').cancelEvent.forEach(function (e) { return _this2._container.removeEventListener(e, _this2._listener); }); } }, { key: 'start', value: function start(id, component, targetPosition) { this._targetPosition = targetPosition; this._duration = this._duration(this._targetPosition - this._startPosition); this._id = id; this._component = component; this._setupListeners(); _get__('requestAnimationFrame')(this.animate.bind(this)); } }, { key: 'animate', value: function animate(time) { if (this._start === null) { this._start = time; if (this._options.events && this._options.events.start) { this._options.events.start(this._id, this._component); } } if (this._cancel) { return; } var deltaT = time - this._start; var currentPosition = this._targetPosition; if (deltaT < this._duration) { currentPosition = _get__('AnimateScroll').easing(deltaT, this._startPosition, this._targetPosition, this._duration); } if (this._container === document.body) { window.scrollTo(0, currentPosition); } else { this._container.scrollTop = currentPosition; } if (deltaT < this._duration) { _get__('requestAnimationFrame')(this.animate.bind(this)); } else { this._teardownListeners(); if (this._options.events && this._options.events.end) { this._options.events.end(this._id, this._component); } } } }, { key: 'cancel', value: function cancel() { this._cancel = true; } }]); return AnimateScroll; }(); _get__('AnimateScroll').easing = _get__('TweenFunctions').easeInOutQuad; _get__('AnimateScroll').cancelEvent = ['mousedown', 'mousewheel', 'touchmove', 'keydown']; exports.default = _get__('AnimateScroll'); var _RewiredData__ = Object.create(null); var INTENTIONAL_UNDEFINED = '__INTENTIONAL_UNDEFINED__'; var _RewireAPI__ = {}; (function () { function addPropertyToAPIObject(name, value) { Object.defineProperty(_RewireAPI__, name, { value: value, enumerable: false, configurable: true }); } addPropertyToAPIObject('__get__', _get__); addPropertyToAPIObject('__GetDependency__', _get__); addPropertyToAPIObject('__Rewire__', _set__); addPropertyToAPIObject('__set__', _set__); addPropertyToAPIObject('__reset__', _reset__); addPropertyToAPIObject('__ResetDependency__', _reset__); addPropertyToAPIObject('__with__', _with__); })(); function _get__(variableName) { if (_RewiredData__ === undefined || _RewiredData__[variableName] === undefined) { return _get_original__(variableName); } else { var value = _RewiredData__[variableName]; if (value === INTENTIONAL_UNDEFINED) { return undefined; } else { return value; } } } function _get_original__(variableName) { switch (variableName) { case 'fn': return fn; case 'getScrollYPosition': return _utils.getScrollYPosition; case 'AnimateScroll': return AnimateScroll; case 'requestAnimationFrame': return _requestAnimationFrame2.default; case 'TweenFunctions': return _tweenFunctions2.default; } return undefined; } function _assign__(variableName, value) { if (_RewiredData__ === undefined || _RewiredData__[variableName] === undefined) { return _set_original__(variableName, value); } else { return _RewiredData__[variableName] = value; } } function _set_original__(variableName, _value) { switch (variableName) {} return undefined; } function _update_operation__(operation, variableName, prefix) { var oldValue = _get__(variableName); var newValue = operation === '++' ? oldValue + 1 : oldValue - 1; _assign__(variableName, newValue); return prefix ? newValue : oldValue; } function _set__(variableName, value) { if ((typeof variableName === 'undefined' ? 'undefined' : _typeof(variableName)) === 'object') { Object.keys(variableName).forEach(function (name) { _RewiredData__[name] = variableName[name]; }); } else { if (value === undefined) { _RewiredData__[variableName] = INTENTIONAL_UNDEFINED; } else { _RewiredData__[variableName] = value; } return function () { _reset__(variableName); }; } } function _reset__(variableName) { delete _RewiredData__[variableName]; } function _with__(object) { var rewiredVariableNames = Object.keys(object); var previousValues = {}; function reset() { rewiredVariableNames.forEach(function (variableName) { _RewiredData__[variableName] = previousValues[variableName]; }); } return function (callback) { rewiredVariableNames.forEach(function (variableName) { previousValues[variableName] = _RewiredData__[variableName]; _RewiredData__[variableName] = object[variableName]; }); var result = callback(); if (!!result && typeof result.then == 'function') { result.then(reset).catch(reset); } else { reset(); } return result; }; } var _typeOfOriginalExport = typeof AnimateScroll === 'undefined' ? 'undefined' : _typeof(AnimateScroll); function addNonEnumerableProperty(name, value) { Object.defineProperty(AnimateScroll, name, { value: value, enumerable: false, configurable: true }); } if ((_typeOfOriginalExport === 'object' || _typeOfOriginalExport === 'function') && Object.isExtensible(AnimateScroll)) { addNonEnumerableProperty('__get__', _get__); addNonEnumerableProperty('__GetDependency__', _get__); addNonEnumerableProperty('__Rewire__', _set__); addNonEnumerableProperty('__set__', _set__); addNonEnumerableProperty('__reset__', _reset__); addNonEnumerableProperty('__ResetDependency__', _reset__); addNonEnumerableProperty('__with__', _with__); addNonEnumerableProperty('__RewireAPI__', _RewireAPI__); } exports.__get__ = _get__; exports.__GetDependency__ = _get__; exports.__Rewire__ = _set__; exports.__set__ = _set__; exports.__ResetDependency__ = _reset__; exports.__RewireAPI__ = _RewireAPI__;