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 {!Array<!Animation>} | |
Dan Beam
2016/02/26 19:18:23
wat, Object<!Animation>?
michaelpg
2016/03/01 00:18:28
Done.
| |
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 /** | |
22 * Calls el.animate(keyframes, options). Returns a Promise which is resolved | |
23 * when the transition ends, or rejected when the transition is cancelled. | |
24 * If an animation with the same name is in progress, that animation is | |
25 * finished immediately before creating the new animation. | |
26 * @param {string} name Name of the animation, used to finish this animation | |
27 * when playing the same type of animation again. | |
28 * @param {!HTMLElement} el The element to animate. | |
29 * @param {!Array|!Object} keyframes Keyframes, as in Element.animate. | |
30 * @param {number|!Object} options Options, as in Element.animate. | |
31 * @return {!Promise} A promise which is resolved when the animation finishes | |
32 * or rejected if the animation is cancelled. | |
33 */ | |
34 animateElement: function(name, el, keyframes, options) { | |
35 if (this.animations[name]) | |
36 this.animations[name].finish(); | |
37 | |
38 var animation = el.animate(keyframes, options); | |
39 this.animations[name] = animation; | |
40 | |
41 return new Promise(function(resolve, reject) { | |
Dan Beam
2016/02/26 19:18:23
return this.promises_[name] = new Promise(...);
michaelpg
2016/03/01 00:18:28
Done.
Dan Beam
2016/03/04 19:24:24
thank you, i think the new code is much simpler
| |
42 animation.addEventListener('finish', function() { | |
43 this.animations[name] = undefined; | |
44 resolve(); | |
45 }.bind(this)); | |
46 | |
47 animation.addEventListener('cancel', function() { | |
48 this.animations[name] = undefined; | |
49 reject(); | |
50 }.bind(this)); | |
51 }.bind(this)); | |
52 }, | |
53 | |
54 /** | |
55 * Finishes the ongoing named animation, waits for the animation's Promise | |
56 * to be resolved, then calls the callback. | |
57 * @param {string} name Name of the animation passed to animateElement. | |
58 * @param {function()=} opt_callback Called once the animation finishes. | |
59 */ | |
60 finishAnimation: function(name, opt_callback) { | |
Dan Beam
2016/02/26 19:18:24
this.promises_[name].then(opt_callback);
michaelpg
2016/03/01 00:18:28
Done.
| |
61 if (opt_callback) { | |
62 this.animations[name].addEventListener('finish', function() { | |
63 // Schedule a task with setTimeout to allow the animation's Promise | |
64 // handlers to be run first. | |
65 setTimeout(/** @type {Function} */(opt_callback)); | |
66 }); | |
67 } | |
68 this.animations[name].finish(); | |
69 }, | |
70 | |
71 /** | |
72 * Cancels the ongoing named animation, waits for the animation's Promise | |
73 * to be rejected, then calls the callback. | |
74 * @param {string} name Name of the animation passed to animateElement. | |
75 * @param {function()=} opt_callback Called once the animation cancels. | |
76 */ | |
77 cancelAnimation: function(name, opt_callback) { | |
78 if (opt_callback) { | |
Dan Beam
2016/02/26 19:18:23
this.promise_[name].catch(opt_callback);
michaelpg
2016/03/01 00:18:28
Done.
| |
79 this.animations[name].addEventListener('cancel', function() { | |
80 // Schedule a task with setTimeout to allow the animation's Promise | |
81 // handlers to be run first. | |
82 setTimeout(/** @type {Function} */(opt_callback)); | |
83 }); | |
84 } | |
85 this.animations[name].cancel(); | |
86 }, | |
87 }; | |
OLD | NEW |