text: "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var _createClass=function(){function r(e,t){for(var i=0;i<t.length;i++){var r=t[i];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(e,t,i){return t&&r(e.prototype,t),i&&r(e,i),e}}(),_get=function(e,t,i){for(var r=!0;r;){var n=e,o=t,s=i,r=!1;null===n&&(n=Function.prototype);var a=Object.getOwnPropertyDescriptor(n,o);if(void 0!==a){if("value"in a)return a.value;var l=a.get;if(void 0===l)return;return l.call(s)}var p=Object.getPrototypeOf(n);if(null===p)return;e=p,t=o,i=s,r=!0,a=p=void 0}};function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _inherits(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}var _react=require("react"),_react2=_interopRequireDefault(_react),_lodashThrottle=require("lodash.throttle"),_lodashThrottle2=_interopRequireDefault(_lodashThrottle),_propTypes=require("prop-types"),_propTypes2=_interopRequireDefault(_propTypes),ScrollAnimation=function(){function t(e){_classCallCheck(this,t),_get(Object.getPrototypeOf(t.prototype),"constructor",this).call(this,e),this.serverSide="undefined"==typeof window,this.listener=(0,_lodashThrottle2.default)(this.handleScroll.bind(this),50),this.visibility={onScreen:!1,inViewport:!1},this.state={classes:"animated",style:{animationDuration:this.props.duration+"s",opacity:this.props.initiallyVisible?1:0}}}return _inherits(t,_react.Component),_createClass(t,[{key:"getElementTop",value:function(e){for(var t=0;e&&void 0!==e.offsetTop&&void 0!==e.clientTop;)t+=e.offsetTop+e.clientTop,e=e.offsetParent;return t}},{key:"getScrollPos",value:function(){return void 0!==this.scrollableParent.pageYOffset?this.scrollableParent.pageYOffset:this.scrollableParent.scrollTop}},{key:"getScrollableParentHeight",value:function(){return void 0!==this.scrollableParent.innerHeight?this.scrollableParent.innerHeight:this.scrollableParent.clientHeight}},{key:"getViewportTop",value:function(){return this.getScrollPos()+this.props.offset}},{key:"getViewportBottom",value:function(){return this.getScrollPos()+this.getScrollableParentHeight()-this.props.offset}},{key:"isInViewport",value:function(e){return e>=this.getViewportTop()&&e<=this.getViewportBottom()}},{key:"isAboveViewport",value:function(e){return e<this.getViewportTop()}},{key:"isBelowViewport",value:function(e){return e>this.getViewportBottom()}},{key:"inViewport",value:function(e,t){return this.isInViewport(e)||this.isInViewport(t)||this.isAboveViewport(e)&&this.isBelowViewport(t)}},{key:"onScreen",value:function(e,t){return!this.isAboveScreen(t)&&!this.isBelowScreen(e)}},{key:"isAboveScreen",value:function(e){return e<this.getScrollPos()}},{key:"isBelowScreen",value:function(e){return e>this.getScrollPos()+this.getScrollableParentHeight()}},{key:"getVisibility",value:function(){var e=this.getElementTop(this.node)-this.getElementTop(this.scrollableParent),t=e+this.node.clientHeight;return{inViewport:this.inViewport(e,t),onScreen:this.onScreen(e,t)}}},{key:"componentDidMount",value:function(){var e;this.serverSide||(e=this.props.scrollableParentSelector,this.scrollableParent=e?document.querySelector(e):window,this.scrollableParent&&this.scrollableParent.addEventListener?this.scrollableParent.addEventListener("scroll",this.listener):console.warn("Cannot find element by locator: "+this.props.scrollableParentSelector),this.props.animatePreScroll&&this.handleScroll())}},{key:"componentWillUnmount",value:function(){clearTimeout(this.delayedAnimationTimeout),clearTimeout(this.callbackTimeout),this.scrollableParent&&this.scrollableParent.addEventListener&&this.scrollableParent.removeEventListener("scroll",this.listener)}},{key:"visibilityHasChanged",value:function(e,t){return e.inViewport!==t.inViewport||e.onScreen!==t.onScreen}},{key:"animate",value:function(e,t){var i=this;this.delayedAnimationTimeout=setTimeout(function(){i.animating=!0,i.setState({classes:"animated "+e,style:{animationDuration:i.props.duration+"s"}}),i.callbackTimeout=setTimeout(t,1e3*i.props.duration)},this.props.delay)}},{key:"animateIn",value:function(t){var i=this;this.animate(this.props.animateIn,function(){i.props.animateOnce||(i.setState({style:{animationDuration:i.props.duration+"s",opacity:1}}),i.animating=!1);var e=i.getVisibility();t&&t(e)})}},{key:"animateOut",value:function(t){var i=this;this.animate(this.props.animateOut,function(){i.setState({classes:"animated",style:{animationDuration:i.props.duration+"s",opacity:0}});var e=i.getVisibility();e.inViewport&&i.props.animateIn?i.animateIn(i.props.afterAnimatedIn):i.animating=!1,t&&t(e)})}},{key:"handleScroll",value:function(){var e;this.animating||(e=this.getVisibility(),this.visibilityHasChanged(this.visibility,e)&&(clearTimeout(this.delayedAnimationTimeout),e.onScreen?e.inViewport&&this.props.animateIn?this.animateIn(this.props.afterAnimatedIn):e.onScreen&&this.visibility.inViewport&&this.props.animateOut&&1===this.state.style.opacity&&this.animateOut(this.props.afterAnimatedOut):this.setState({classes:"animated",style:{animationDuration:this.props.duration+"s",opacity:this.props.initiallyVisible?1:0}}),this.visibility=e))}},{key:"render",value:function(){var t=this,e=this.props.className?this.props.className+" "+this.state.classes:this.state.classes;return _react2.default.createElement("div",{ref:function(e){t.node=e},className:e,style:Object.assign({},this.state.style,this.props.style)},this.props.children)}}]),t}();(exports.default=ScrollAnimation).defaultProps={offset:150,duration:1,initiallyVisible:!1,delay:0,animateOnce:!1,animatePreScroll:!0},ScrollAnimation.propTypes={animateIn:_propTypes2.default.string,animateOut:_propTypes2.default.string,offset:_propTypes2.default.number,duration:_propTypes2.default.number,delay:_propTypes2.default.number,initiallyVisible:_propTypes2.default.bool,animateOnce:_propTypes2.default.bool,style:_propTypes2.default.object,scrollableParentSelector:_propTypes2.default.string,className:_propTypes2.default.string,animatePreScroll:_propTypes2.default.bool},module.exports=exports.default;