OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 /** | |
6 * Adds convenience functions to control native Web Animations. The | |
7 * implementation details may change as Chrome support evolves. | |
8 * @polymerBehavior | |
9 */ | |
10 var TransitionBehavior = { | |
11 ready: function() { | |
12 /** | |
13 * @type {!Object<!Animation>} | |
14 * Map of running animations by animation name. Animation names are | |
15 * arbitrary but used to prevent multiple animations of the same type (e.g., | |
16 * expand/collapse section) from running simultaneously. | |
17 */ | |
18 this.animations = {}; | |
19 | |
20 /** | |
21 * @private {!Object<!Promise>} | |
22 * Map of Promises for each running animation. Key names and existence | |
23 * should correspond with |animations|. | |
24 */ | |
25 this.promises_ = {}; | |
26 }, | |
27 | |
28 /** | |
29 * Calls el.animate(keyframes, options). Returns a Promise which is resolved | |
30 * when the transition ends, or rejected when the transition is canceled. | |
31 * If an animation with the same name is in progress, that animation is | |
32 * finished immediately before creating the new animation. | |
33 * @param {string} name Name of the animation, used to finish this animation | |
34 * when playing the same type of animation again. | |
35 * @param {!HTMLElement} el The element to animate. | |
36 * @param {!Array|!Object} keyframes Keyframes, as in Element.animate. | |
37 * @param {number|!Object} options Options, as in Element.animate. | |
38 * @return {!Promise} A promise which is resolved when the animation finishes | |
39 * or rejected if the animation is canceled. | |
40 */ | |
41 animateElement: function(name, el, keyframes, options) { | |
42 if (this.animations[name]) | |
43 this.animations[name].finish(); | |
44 | |
45 var animation = el.animate(keyframes, options); | |
46 this.animations[name] = animation; | |
47 | |
48 return this.promises_[name] = new Promise(function(resolve, reject) { | |
Dan Beam
2016/03/04 19:24:24
this does work, I was just trying to be brief
doi
michaelpg
2016/03/14 18:57:44
Done.
| |
49 animation.addEventListener('finish', function() { | |
50 this.animations[name] = undefined; | |
Dan Beam
2016/03/04 19:24:24
note: be care about using `in` with animations as
michaelpg
2016/03/14 18:57:44
Noted. There shouldn't be a need to.
| |
51 resolve(); | |
52 }.bind(this)); | |
53 | |
54 animation.addEventListener('cancel', function() { | |
55 this.animations[name] = undefined; | |
56 reject(); | |
57 }.bind(this)); | |
58 }.bind(this)); | |
59 }, | |
60 | |
61 /** | |
62 * Finishes the ongoing named animation, waits for the animation's Promise | |
63 * to be resolved, then calls the callback. | |
64 * @param {string} name Name of the animation passed to animateElement. | |
65 * @param {function()=} opt_callback Called once the animation finishes. | |
66 */ | |
67 finishAnimation: function(name, opt_callback) { | |
68 if (opt_callback) | |
69 this.promises_[name].then(opt_callback); | |
70 this.animations[name].finish(); | |
71 }, | |
72 | |
73 /** | |
74 * Cancels the ongoing named animation, waits for the animation's Promise | |
75 * to be rejected, then calls the callback. | |
76 * @param {string} name Name of the animation passed to animateElement. | |
77 * @param {function()=} opt_callback Called once the animation cancels. | |
78 */ | |
79 cancelAnimation: function(name, opt_callback) { | |
80 if (opt_callback) | |
81 this.promises_[name].catch(opt_callback); | |
82 this.animations[name].cancel(); | |
83 }, | |
84 }; | |
OLD | NEW |