-
Notifications
You must be signed in to change notification settings - Fork 7
/
animation.js
881 lines (788 loc) · 27.4 KB
/
animation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
/**
* Set of actors that expose the Web Animations API to devtools protocol
* clients.
*
* The |Animations| actor is the main entry point. It is used to discover
* animation players on given nodes.
* There should only be one instance per debugger server.
*
* The |AnimationPlayer| actor provides attributes and methods to inspect an
* animation as well as pause/resume/seek it.
*
* The Web Animation spec implementation is ongoing in Gecko, and so this set
* of actors should evolve when the implementation progresses.
*
* References:
* - WebAnimation spec:
* http://w3c.github.io/web-animations/
* - WebAnimation WebIDL files:
* /dom/webidl/Animation*.webidl
*/
const {Cu} = require("devtools/sham/chrome");
const promise = require("devtools/sham/promise");
const {Task} = require("devtools/sham/task");
const protocol = require("devtools/server/protocol");
const {ActorClass, Actor, FrontClass, Front,
Arg, method, RetVal, types} = protocol;
// Make sure the nodeActor type is know here.
const {NodeActor} = require("devtools/server/actors/inspector");
const events = require("sdk/event/core");
// Types of animations.
const ANIMATION_TYPES = {
CSS_ANIMATION: "cssanimation",
CSS_TRANSITION: "csstransition",
UNKNOWN: "unknown"
};
/**
* The AnimationPlayerActor provides information about a given animation: its
* startTime, currentTime, current state, etc.
*
* Since the state of a player changes as the animation progresses it is often
* useful to call getCurrentState at regular intervals to get the current state.
*
* This actor also allows playing, pausing and seeking the animation.
*/
var AnimationPlayerActor = ActorClass({
typeName: "animationplayer",
events: {
"changed": {
type: "changed",
state: Arg(0, "json")
}
},
/**
* @param {AnimationsActor} The main AnimationsActor instance
* @param {AnimationPlayer} The player object returned by getAnimationPlayers
* @param {Number} Temporary work-around used to retrieve duration and
* iteration count from computed-style rather than from waapi. This is needed
* to know which duration to get, in case there are multiple css animations
* applied to the same node.
*/
initialize: function(animationsActor, player, playerIndex) {
Actor.prototype.initialize.call(this, animationsActor.conn);
this.onAnimationMutation = this.onAnimationMutation.bind(this);
this.tabActor = animationsActor.tabActor;
this.player = player;
this.node = player.effect.target;
this.playerIndex = playerIndex;
let win = this.node.ownerDocument.defaultView;
this.styles = win.getComputedStyle(this.node);
// Listen to animation mutations on the node to alert the front when the
// current animation changes.
this.observer = new win.MutationObserver(this.onAnimationMutation);
this.observer.observe(this.node, {animations: true});
},
destroy: function() {
// Only try to disconnect the observer if it's not already dead (i.e. if the
// container view hasn't navigated since).
if (this.observer && !Cu.isDeadWrapper(this.observer)) {
this.observer.disconnect();
}
this.tabActor = this.player = this.node = this.styles = this.observer = null;
Actor.prototype.destroy.call(this);
},
/**
* Release the actor, when it isn't needed anymore.
* Protocol.js uses this release method to call the destroy method.
*/
release: method(function() {}, {release: true}),
form: function(detail) {
if (detail === "actorid") {
return this.actorID;
}
let data = this.getCurrentState();
data.actor = this.actorID;
return data;
},
isAnimation: function(player=this.player) {
return player instanceof this.tabActor.window.CSSAnimation;
},
isTransition: function(player=this.player) {
return player instanceof this.tabActor.window.CSSTransition;
},
getType: function() {
if (this.isAnimation()) {
return ANIMATION_TYPES.CSS_ANIMATION;
} else if (this.isTransition()) {
return ANIMATION_TYPES.CSS_TRANSITION;
}
return ANIMATION_TYPES.UNKNOWN;
},
/**
* Some of the player's properties are retrieved from the node's
* computed-styles because the Web Animations API does not provide them yet.
* But the computed-styles may contain multiple animations for a node and so
* we need to know which is the index of the current animation in the style.
* @return {Number}
*/
getPlayerIndex: function() {
let names = this.styles.animationName;
if (names === "none") {
names = this.styles.transitionProperty;
}
// If we still don't have a name, let's fall back to the provided index
// which may, by now, be wrong, but it's the best we can do until the waapi
// gives us a way to get duration, delay, ... directly.
if (!names || names === "none") {
return this.playerIndex;
}
// If there's only one name.
if (names.includes(",") === -1) {
return 0;
}
// If there are several names, retrieve the index of the animation name in
// the list.
let playerName = this.getName();
names = names.split(",").map(n => n.trim());
for (let i = 0; i < names.length; i++) {
if (names[i] === playerName) {
return i;
}
}
},
/**
* Get the name associated with the player. This is used to match
* up the player with values in the computed animation-name or
* transition-property property.
* @return {String}
*/
getName: function() {
if (this.isAnimation()) {
return this.player.animationName;
} else if (this.isTransition()) {
return this.player.transitionProperty;
}
return "";
},
/**
* Get the animation duration from this player, in milliseconds.
* Note that the Web Animations API doesn't yet offer a way to retrieve this
* directly from the AnimationPlayer object, so for now, a duration is only
* returned if found in the node's computed styles.
* @return {Number}
*/
getDuration: function() {
let durationText;
if (this.styles.animationDuration !== "0s") {
durationText = this.styles.animationDuration;
} else if (this.styles.transitionDuration !== "0s") {
durationText = this.styles.transitionDuration;
} else {
return null;
}
// If the computed duration has multiple entries, we need to find the right
// one.
if (durationText.indexOf(",") !== -1) {
durationText = durationText.split(",")[this.getPlayerIndex()];
}
return parseFloat(durationText) * 1000;
},
/**
* Get the animation delay from this player, in milliseconds.
* Note that the Web Animations API doesn't yet offer a way to retrieve this
* directly from the AnimationPlayer object, so for now, a delay is only
* returned if found in the node's computed styles.
* @return {Number}
*/
getDelay: function() {
let delayText;
if (this.styles.animationDelay !== "0s") {
delayText = this.styles.animationDelay;
} else if (this.styles.transitionDelay !== "0s") {
delayText = this.styles.transitionDelay;
} else {
return 0;
}
if (delayText.indexOf(",") !== -1) {
delayText = delayText.split(",")[this.getPlayerIndex()];
}
return parseFloat(delayText) * 1000;
},
/**
* Get the animation iteration count for this player. That is, how many times
* is the animation scheduled to run.
* Note that the Web Animations API doesn't yet offer a way to retrieve this
* directly from the AnimationPlayer object, so for now, check for
* animationIterationCount in the node's computed styles, and return that.
* This style property defaults to 1 anyway.
* @return {Number}
*/
getIterationCount: function() {
let iterationText = this.styles.animationIterationCount;
if (iterationText.indexOf(",") !== -1) {
iterationText = iterationText.split(",")[this.getPlayerIndex()];
}
return iterationText === "infinite"
? null
: parseInt(iterationText, 10);
},
/**
* Return the current start of the Animation.
* @return {Object}
*/
getState: function() {
// Remember the startTime each time getState is called, it may be useful
// when animations get paused. As in, when an animation gets paused, its
// startTime goes back to null, but the front-end might still be interested
// in knowing what the previous startTime was. So everytime it is set,
// remember it and send it along with the newState.
if (this.player.startTime) {
this.previousStartTime = this.player.startTime;
}
// Note that if you add a new property to the state object, make sure you
// add the corresponding property in the AnimationPlayerFront' initialState
// getter.
return {
type: this.getType(),
// startTime is null whenever the animation is paused or waiting to start.
startTime: this.player.startTime,
previousStartTime: this.previousStartTime,
currentTime: this.player.currentTime,
playState: this.player.playState,
playbackRate: this.player.playbackRate,
name: this.getName(),
duration: this.getDuration(),
delay: this.getDelay(),
iterationCount: this.getIterationCount(),
// animation is hitting the fast path or not. Returns false whenever the
// animation is paused as it is taken off the compositor then.
isRunningOnCompositor: this.player.isRunningOnCompositor,
// The document timeline's currentTime is being sent along too. This is
// not strictly related to the node's animationPlayer, but is useful to
// know the current time of the animation with respect to the document's.
documentCurrentTime: this.node.ownerDocument.timeline.currentTime
};
},
/**
* Get the current state of the AnimationPlayer (currentTime, playState, ...).
* Note that the initial state is returned as the form of this actor when it
* is initialized.
* This protocol method only returns a trimed down version of this state in
* case some properties haven't changed since last time (since the front can
* reconstruct those). If you want the full state, use the getState method.
* @return {Object}
*/
getCurrentState: method(function() {
let newState = this.getState();
// If we've saved a state before, compare and only send what has changed.
// It's expected of the front to also save old states to re-construct the
// full state when an incomplete one is received.
// This is to minimize protocol traffic.
let sentState = {};
if (this.currentState) {
for (let key in newState) {
if (typeof this.currentState[key] === "undefined" ||
this.currentState[key] !== newState[key]) {
sentState[key] = newState[key];
}
}
} else {
sentState = newState;
}
this.currentState = newState;
return sentState;
}, {
request: {},
response: {
data: RetVal("json")
}
}),
/**
* Executed when the current animation changes, used to emit the new state
* the the front.
*/
onAnimationMutation: function(mutations) {
let isCurrentAnimation = animation => animation === this.player;
let hasCurrentAnimation = animations => animations.some(isCurrentAnimation);
let hasChanged = false;
for (let {removedAnimations, changedAnimations} of mutations) {
if (hasCurrentAnimation(removedAnimations)) {
// Reset the local copy of the state on removal, since the animation can
// be kept on the client and re-added, its state needs to be sent in
// full.
this.currentState = null;
}
if (hasCurrentAnimation(changedAnimations)) {
// Only consider the state has having changed if any of delay, duration
// or iterationcount has changed (for now at least).
let newState = this.getState();
let oldState = this.currentState;
hasChanged = newState.delay !== oldState.delay ||
newState.iterationCount !== oldState.iterationCount ||
newState.duration !== oldState.duration;
break;
}
}
if (hasChanged) {
events.emit(this, "changed", this.getCurrentState());
}
},
/**
* Pause the player.
*/
pause: method(function() {
this.player.pause();
return this.player.ready;
}, {
request: {},
response: {}
}),
/**
* Play the player.
* This method only returns when the animation has left its pending state.
*/
play: method(function() {
this.player.play();
return this.player.ready;
}, {
request: {},
response: {}
}),
/**
* Simply exposes the player ready promise.
*
* When an animation is created/paused then played, there's a short time
* during which its playState is pending, before being set to running.
*
* If you either created a new animation using the Web Animations API or
* paused/played an existing one, and then want to access the playState, you
* might be interested to call this method.
* This is especially important for tests.
*/
ready: method(function() {
return this.player.ready;
}, {
request: {},
response: {}
}),
/**
* Set the current time of the animation player.
*/
setCurrentTime: method(function(currentTime) {
this.player.currentTime = currentTime * this.player.playbackRate;
}, {
request: {
currentTime: Arg(0, "number")
},
response: {}
}),
/**
* Set the playback rate of the animation player.
*/
setPlaybackRate: method(function(playbackRate) {
this.player.playbackRate = playbackRate;
}, {
request: {
currentTime: Arg(0, "number")
},
response: {}
}),
/**
* Get data about the keyframes of this animation player.
* @return {Object} Returns a list of frames, each frame containing the list
* animated properties as well as the frame's offset.
*/
getFrames: method(function() {
return this.player.effect.getFrames();
}, {
request: {},
response: {
frames: RetVal("json")
}
})
});
var AnimationPlayerFront = FrontClass(AnimationPlayerActor, {
initialize: function(conn, form, detail, ctx) {
Front.prototype.initialize.call(this, conn, form, detail, ctx);
this.state = {};
},
form: function(form, detail) {
if (detail === "actorid") {
this.actorID = form;
return;
}
this._form = form;
this.state = this.initialState;
},
destroy: function() {
Front.prototype.destroy.call(this);
},
/**
* Getter for the initial state of the player. Up to date states can be
* retrieved by calling the getCurrentState method.
*/
get initialState() {
return {
type: this._form.type,
startTime: this._form.startTime,
previousStartTime: this._form.previousStartTime,
currentTime: this._form.currentTime,
playState: this._form.playState,
playbackRate: this._form.playbackRate,
name: this._form.name,
duration: this._form.duration,
delay: this._form.delay,
iterationCount: this._form.iterationCount,
isRunningOnCompositor: this._form.isRunningOnCompositor,
documentCurrentTime: this._form.documentCurrentTime
};
},
/**
* Executed when the AnimationPlayerActor emits a "changed" event. Used to
* update the local knowledge of the state.
*/
onChanged: protocol.preEvent("changed", function(partialState) {
let {state} = this.reconstructState(partialState);
this.state = state;
}),
/**
* Refresh the current state of this animation on the client from information
* found on the server. Doesn't return anything, just stores the new state.
*/
refreshState: Task.async(function*() {
let data = yield this.getCurrentState();
if (this.currentStateHasChanged) {
this.state = data;
}
}),
/**
* getCurrentState interceptor re-constructs incomplete states since the actor
* only sends the values that have changed.
*/
getCurrentState: protocol.custom(function() {
this.currentStateHasChanged = false;
return this._getCurrentState().then(partialData => {
let {state, hasChanged} = this.reconstructState(partialData);
this.currentStateHasChanged = hasChanged;
return state;
});
}, {
impl: "_getCurrentState"
}),
reconstructState: function(data) {
let hasChanged = false;
for (let key in this.state) {
if (typeof data[key] === "undefined") {
data[key] = this.state[key];
} else if (data[key] !== this.state[key]) {
hasChanged = true;
}
}
return {state: data, hasChanged};
}
});
/**
* Sent with the 'mutations' event as part of an array of changes, used to
* inform fronts of the type of change that occured.
*/
types.addDictType("animationMutationChange", {
// The type of change ("added" or "removed").
type: "string",
// The changed AnimationPlayerActor.
player: "animationplayer"
});
/**
* The Animations actor lists animation players for a given node.
*/
var AnimationsActor = exports.AnimationsActor = ActorClass({
typeName: "animations",
events: {
"mutations": {
type: "mutations",
changes: Arg(0, "array:animationMutationChange")
}
},
initialize: function(conn, tabActor) {
Actor.prototype.initialize.call(this, conn);
this.tabActor = tabActor;
this.onWillNavigate = this.onWillNavigate.bind(this);
this.onNavigate = this.onNavigate.bind(this);
this.onAnimationMutation = this.onAnimationMutation.bind(this);
this.allAnimationsPaused = false;
events.on(this.tabActor, "will-navigate", this.onWillNavigate);
events.on(this.tabActor, "navigate", this.onNavigate);
},
destroy: function() {
Actor.prototype.destroy.call(this);
events.off(this.tabActor, "will-navigate", this.onWillNavigate);
events.off(this.tabActor, "navigate", this.onNavigate);
this.stopAnimationPlayerUpdates();
this.tabActor = this.observer = this.actors = null;
},
/**
* Since AnimationsActor doesn't have a protocol.js parent actor that takes
* care of its lifetime, implementing disconnect is required to cleanup.
*/
disconnect: function() {
this.destroy();
},
/**
* Retrieve the list of AnimationPlayerActor actors for currently running
* animations on a node and its descendants.
* @param {NodeActor} nodeActor The NodeActor as defined in
* /devtools/server/actors/inspector
*/
getAnimationPlayersForNode: method(function(nodeActor) {
let animations = [
...nodeActor.rawNode.getAnimations(),
...this.getAllAnimations(nodeActor.rawNode)
];
// No care is taken here to destroy the previously stored actors because it
// is assumed that the client is responsible for lifetimes of actors.
this.actors = [];
for (let i = 0; i < animations.length; i++) {
// XXX: for now the index is passed along as the AnimationPlayerActor uses
// it to retrieve animation information from CSS.
let actor = AnimationPlayerActor(this, animations[i], i);
this.actors.push(actor);
}
// When a front requests the list of players for a node, start listening
// for animation mutations on this node to send updates to the front, until
// either getAnimationPlayersForNode is called again or
// stopAnimationPlayerUpdates is called.
this.stopAnimationPlayerUpdates();
let win = nodeActor.rawNode.ownerDocument.defaultView;
this.observer = new win.MutationObserver(this.onAnimationMutation);
this.observer.observe(nodeActor.rawNode, {
animations: true,
subtree: true
});
return this.actors;
}, {
request: {
actorID: Arg(0, "domnode")
},
response: {
players: RetVal("array:animationplayer")
}
}),
onAnimationMutation: function(mutations) {
let eventData = [];
let readyPromises = [];
for (let {addedAnimations, removedAnimations} of mutations) {
for (let player of removedAnimations) {
// Note that animations are reported as removed either when they are
// actually removed from the node (e.g. css class removed) or when they
// are finished and don't have forwards animation-fill-mode.
// In the latter case, we don't send an event, because the corresponding
// animation can still be seeked/resumed, so we want the client to keep
// its reference to the AnimationPlayerActor.
if (player.playState !== "idle") {
continue;
}
let index = this.actors.findIndex(a => a.player === player);
if (index !== -1) {
eventData.push({
type: "removed",
player: this.actors[index]
});
this.actors.splice(index, 1);
}
}
for (let player of addedAnimations) {
// If the added player already exists, it means we previously filtered
// it out when it was reported as removed. So filter it out here too.
if (this.actors.find(a => a.player === player)) {
continue;
}
// If the added player has the same name and target node as a player we
// already have, it means it's a transition that's re-starting. So send
// a "removed" event for the one we already have.
let index = this.actors.findIndex(a => {
let isSameType = a.player.constructor === player.constructor;
let isSameName = (a.isAnimation() &&
a.player.animationName === player.animationName) ||
(a.isTransition() &&
a.player.transitionProperty === player.transitionProperty);
let isSameNode = a.player.effect.target === player.effect.target;
return isSameType && isSameNode && isSameName;
});
if (index !== -1) {
eventData.push({
type: "removed",
player: this.actors[index]
});
this.actors.splice(index, 1);
}
let actor = AnimationPlayerActor(
this, player, player.effect.target.getAnimations().indexOf(player));
this.actors.push(actor);
eventData.push({
type: "added",
player: actor
});
readyPromises.push(player.ready);
}
}
if (eventData.length) {
// Let's wait for all added animations to be ready before telling the
// front-end.
Promise.all(readyPromises).then(() => {
events.emit(this, "mutations", eventData);
});
}
},
/**
* After the client has called getAnimationPlayersForNode for a given DOM
* node, the actor starts sending animation mutations for this node. If the
* client doesn't want this to happen anymore, it should call this method.
*/
stopAnimationPlayerUpdates: method(function() {
if (this.observer && !Cu.isDeadWrapper(this.observer)) {
this.observer.disconnect();
}
}, {
request: {},
response: {}
}),
/**
* Iterates through all nodes below a given rootNode (optionally also in
* nested frames) and finds all existing animation players.
* @param {DOMNode} rootNode The root node to start iterating at. Animation
* players will *not* be reported for this node.
* @param {Boolean} traverseFrames Whether we should iterate through nested
* frames too.
* @return {Array} An array of AnimationPlayer objects.
*/
getAllAnimations: function(rootNode, traverseFrames) {
let animations = [];
// These loops shouldn't be as bad as they look.
// Typically, there will be very few nested frames, and getElementsByTagName
// is really fast even on large DOM trees.
for (let element of rootNode.getElementsByTagNameNS("*", "*")) {
if (traverseFrames && element.contentWindow) {
animations = [
...animations,
...this.getAllAnimations(element.contentWindow.document, traverseFrames)
];
} else {
animations = [
...animations,
...element.getAnimations()
];
}
}
return animations;
},
onWillNavigate: function({isTopLevel}) {
if (isTopLevel) {
this.stopAnimationPlayerUpdates();
}
},
onNavigate: function({isTopLevel}) {
if (isTopLevel) {
this.allAnimationsPaused = false;
}
},
/**
* Pause all animations in the current tabActor's frames.
*/
pauseAll: method(function() {
let readyPromises = [];
// Until the WebAnimations API provides a way to play/pause via the document
// timeline, we have to iterate through the whole DOM to find all players.
for (let player of
this.getAllAnimations(this.tabActor.window.document, true)) {
player.pause();
readyPromises.push(player.ready);
}
this.allAnimationsPaused = true;
return promise.all(readyPromises);
}, {
request: {},
response: {}
}),
/**
* Play all animations in the current tabActor's frames.
* This method only returns when animations have left their pending states.
*/
playAll: method(function() {
let readyPromises = [];
// Until the WebAnimations API provides a way to play/pause via the document
// timeline, we have to iterate through the whole DOM to find all players.
for (let player of
this.getAllAnimations(this.tabActor.window.document, true)) {
player.play();
readyPromises.push(player.ready);
}
this.allAnimationsPaused = false;
return promise.all(readyPromises);
}, {
request: {},
response: {}
}),
toggleAll: method(function() {
if (this.allAnimationsPaused) {
return this.playAll();
}
return this.pauseAll();
}, {
request: {},
response: {}
}),
/**
* Toggle (play/pause) several animations at the same time.
* @param {Array} players A list of AnimationPlayerActor objects.
* @param {Boolean} shouldPause If set to true, the players will be paused,
* otherwise they will be played.
*/
toggleSeveral: method(function(players, shouldPause) {
return promise.all(players.map(player => {
return shouldPause ? player.pause() : player.play();
}));
}, {
request: {
players: Arg(0, "array:animationplayer"),
shouldPause: Arg(1, "boolean")
},
response: {}
}),
/**
* Set the current time of several animations at the same time.
* @param {Array} players A list of AnimationPlayerActor.
* @param {Number} time The new currentTime.
* @param {Boolean} shouldPause Should the players be paused too.
*/
setCurrentTimes: method(function(players, time, shouldPause) {
return promise.all(players.map(player => {
let pause = shouldPause ? player.pause() : promise.resolve();
return pause.then(() => player.setCurrentTime(time));
}));
}, {
request: {
players: Arg(0, "array:animationplayer"),
time: Arg(1, "number"),
shouldPause: Arg(2, "boolean")
},
response: {}
}),
/**
* Set the playback rate of several animations at the same time.
* @param {Array} players A list of AnimationPlayerActor.
* @param {Number} rate The new rate.
*/
setPlaybackRates: method(function(players, rate) {
for (let player of players) {
player.setPlaybackRate(rate);
}
}, {
request: {
players: Arg(0, "array:animationplayer"),
rate: Arg(1, "number")
},
response: {}
})
});
var AnimationsFront = exports.AnimationsFront = FrontClass(AnimationsActor, {
initialize: function(client, {animationsActor}) {
Front.prototype.initialize.call(this, client, {actor: animationsActor});
this.manage(this);
},
destroy: function() {
Front.prototype.destroy.call(this);
}
});