Home | History | Annotate | Download | only in neon-animation
      1 <!--
      2 @license
      3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
      4 This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
      5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
      6 The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
      7 Code distributed by Google as part of the polymer project is also
      8 subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
      9 -->
     10 <link rel="import" href="../polymer/polymer.html">
     11 <link rel="import" href="../iron-meta/iron-meta.html">
     12 <link rel="import" href="neon-animatable-behavior.html">
     13 
     14 <script>
     15 
     16   /**
     17    * `Polymer.NeonAnimationRunnerBehavior` adds a method to run animations.
     18    *
     19    * @polymerBehavior Polymer.NeonAnimationRunnerBehavior
     20    */
     21   Polymer.NeonAnimationRunnerBehaviorImpl = {
     22 
     23     properties: {
     24 
     25       /** @type {?Object} */
     26       _player: {
     27         type: Object
     28       }
     29 
     30     },
     31 
     32     _configureAnimationEffects: function(allConfigs) {
     33       var allAnimations = [];
     34       if (allConfigs.length > 0) {
     35         for (var config, index = 0; config = allConfigs[index]; index++) {
     36           var animation = document.createElement(config.name);
     37           // is this element actually a neon animation?
     38           if (animation.isNeonAnimation) {
     39             var effect = animation.configure(config);
     40             if (effect) {
     41               allAnimations.push({
     42                 animation: animation,
     43                 config: config,
     44                 effect: effect
     45               });
     46             }
     47           } else {
     48             console.warn(this.is + ':', config.name, 'not found!');
     49           }
     50         }
     51       }
     52       return allAnimations;
     53     },
     54 
     55     _runAnimationEffects: function(allEffects) {
     56       return document.timeline.play(new GroupEffect(allEffects));
     57     },
     58 
     59     _completeAnimations: function(allAnimations) {
     60       for (var animation, index = 0; animation = allAnimations[index]; index++) {
     61         animation.animation.complete(animation.config);
     62       }
     63     },
     64 
     65     /**
     66      * Plays an animation with an optional `type`.
     67      * @param {string=} type
     68      * @param {!Object=} cookie
     69      */
     70     playAnimation: function(type, cookie) {
     71       var allConfigs = this.getAnimationConfig(type);
     72       if (!allConfigs) {
     73         return;
     74       }
     75       try {
     76         var allAnimations = this._configureAnimationEffects(allConfigs);
     77         var allEffects = allAnimations.map(function(animation) {
     78           return animation.effect;
     79         });
     80 
     81         if (allEffects.length > 0) {
     82           this._player = this._runAnimationEffects(allEffects);
     83           this._player.onfinish = function() {
     84             this._completeAnimations(allAnimations);
     85 
     86             if (this._player) {
     87               this._player.cancel();
     88               this._player = null;
     89             }
     90 
     91             this.fire('neon-animation-finish', cookie, {bubbles: false});
     92           }.bind(this);
     93           return;
     94         }
     95       } catch (e) {
     96         console.warn('Couldnt play', '(', type, allConfigs, ').', e);
     97       }
     98       this.fire('neon-animation-finish', cookie, {bubbles: false});
     99     },
    100 
    101     /**
    102      * Cancels the currently running animation.
    103      */
    104     cancelAnimation: function() {
    105       if (this._player) {
    106         this._player.cancel();
    107       }
    108     }
    109   };
    110 
    111   /** @polymerBehavior Polymer.NeonAnimationRunnerBehavior */
    112   Polymer.NeonAnimationRunnerBehavior = [
    113     Polymer.NeonAnimatableBehavior,
    114     Polymer.NeonAnimationRunnerBehaviorImpl
    115   ];
    116 </script>
    117