UNPKG

animated

Version:

Declarative Animations Library for React and React Native

212 lines (105 loc) 4.63 kB
'use strict';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;};}();function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}function _possibleConstructorReturn(self,call){if(!self){throw new ReferenceError("this hasn't been initialised - super() hasn't been called");}return call&&(typeof call==="object"||typeof call==="function")?call:self;}function _inherits(subClass,superClass){if(typeof superClass!=="function"&&superClass!==null){throw new TypeError("Super expression must either be null or a function, not "+typeof superClass);}subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,enumerable:false,writable:true,configurable:true}});if(superClass)Object.setPrototypeOf?Object.setPrototypeOf(subClass,superClass):subClass.__proto__=superClass;} var AnimatedWithChildren=require('./AnimatedWithChildren'); var InteractionManager=require('./injectable/InteractionManager'); var AnimatedInterpolation=require('./AnimatedInterpolation'); var Interpolation=require('./Interpolation'); var Animation=require('./Animation'); var guid=require('./guid'); var Set=global.Set||require('./SetPolyfill'); function _flush(rootNode){ var animatedStyles=new Set(); function findAnimatedStyles(node){ if(typeof node.update==='function'){ animatedStyles.add(node); }else{ node.__getChildren().forEach(findAnimatedStyles); } } findAnimatedStyles(rootNode); animatedStyles.forEach(function(animatedStyle){return animatedStyle.update();}); }var AnimatedValue=function(_AnimatedWithChildren){_inherits(AnimatedValue,_AnimatedWithChildren); function AnimatedValue(value){_classCallCheck(this,AnimatedValue);var _this=_possibleConstructorReturn(this,(AnimatedValue.__proto__||Object.getPrototypeOf(AnimatedValue)).call(this)); _this._value=value; _this._offset=0; _this._animation=null; _this._listeners={};return _this; }_createClass(AnimatedValue,[{key:'__detach',value:function __detach() { this.stopAnimation(); }},{key:'__getValue',value:function __getValue() { return this._value+this._offset; }},{key:'setValue',value:function setValue( value){ if(this._animation){ this._animation.stop(); this._animation=null; } this._updateValue(value); }},{key:'setOffset',value:function setOffset( offset){ this._offset=offset; }},{key:'flattenOffset',value:function flattenOffset() { this._value+=this._offset; this._offset=0; }},{key:'addListener',value:function addListener( callback){ var id=guid(); this._listeners[id]=callback; return id; }},{key:'removeListener',value:function removeListener( id){ delete this._listeners[id]; }},{key:'removeAllListeners',value:function removeAllListeners() { this._listeners={}; }},{key:'stopAnimation',value:function stopAnimation( callback){ this.stopTracking(); this._animation&&this._animation.stop(); this._animation=null; callback&&callback(this.__getValue()); }},{key:'interpolate',value:function interpolate( config){ return new AnimatedInterpolation(this,Interpolation.create(config)); }},{key:'animate',value:function animate( animation,callback){var _this2=this; var handle=null; if(animation.__isInteraction){ handle=InteractionManager.current.createInteractionHandle(); } var previousAnimation=this._animation; this._animation&&this._animation.stop(); this._animation=animation; animation.start( this._value, function(value){ _this2._updateValue(value); }, function(result){ _this2._animation=null; if(handle!==null){ InteractionManager.current.clearInteractionHandle(handle); } callback&&callback(result); }, previousAnimation); }},{key:'stopTracking',value:function stopTracking() { this._tracking&&this._tracking.__detach(); this._tracking=null; }},{key:'track',value:function track( tracking){ this.stopTracking(); this._tracking=tracking; }},{key:'_updateValue',value:function _updateValue( value){ this._value=value; _flush(this); for(var key in this._listeners){ this._listeners[key]({value:this.__getValue()}); } }}]);return AnimatedValue;}(AnimatedWithChildren); module.exports=AnimatedValue;