1// Copyright 2014 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5//     You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//     See the License for the specific language governing permissions and
13// limitations under the License.
14
15
16(function(shared, scope, testing) {
17
18  scope.AnimationTimeline = function() {
19    this._players = [];
20    this.currentTime = undefined;
21  };
22
23  scope.AnimationTimeline.prototype = {
24    // FIXME: This needs to return the wrapped players in Web Animations Next
25    // TODO: Does this need to be sorted?
26    // TODO: Do we need to consider needsRetick?
27    getAnimationPlayers: function() {
28      this._discardPlayers();
29      return this._players.slice();
30    },
31    _discardPlayers: function() {
32      this._players = this._players.filter(function(player) {
33        return player.playState != 'finished' && player.playState != 'idle';
34      });
35    },
36    play: function(source) {
37      var player = new scope.Player(source);
38      this._players.push(player);
39      scope.restartWebAnimationsNextTick();
40      player.play();
41      return player;
42    },
43  };
44
45  var ticking = false;
46
47  scope.restartWebAnimationsNextTick = function() {
48    if (!ticking) {
49      ticking = true;
50      requestAnimationFrame(webAnimationsNextTick);
51    }
52  };
53
54  function webAnimationsNextTick(t) {
55    var timeline = window.document.timeline;
56    timeline.currentTime = t;
57    timeline._discardPlayers();
58    if (timeline._players.length == 0)
59      ticking = false;
60    else
61      requestAnimationFrame(webAnimationsNextTick);
62  }
63
64  var timeline = new scope.AnimationTimeline();
65  scope.timeline = timeline;
66
67  try {
68    Object.defineProperty(window.document, 'timeline', {
69      configurable: true,
70      get: function() { return timeline; }
71    });
72  } catch (e) { }
73  try {
74    window.document.timeline = timeline;
75  } catch (e) { }
76
77})(webAnimationsShared, webAnimationsNext, webAnimationsTesting);
78