Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / animation / AnimationModel.js
blob09e30fd25b2c5d91cbdc8010c7291169f674bedb
1 // Copyright (c) 2014 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.
6 /**
7 * @constructor
8 * @extends {WebInspector.SDKModel}
9 * @param {!WebInspector.Target} target
11 WebInspector.AnimationModel = function(target)
13 WebInspector.SDKModel.call(this, WebInspector.AnimationModel, target);
14 this._agent = target.animationAgent();
15 target.registerAnimationDispatcher(new WebInspector.AnimationDispatcher(this));
16 /** @type {!Map.<string, !WebInspector.AnimationModel.Animation>} */
17 this._animationsById = new Map();
18 /** @type {!Map.<string, !WebInspector.AnimationModel.AnimationGroup>} */
19 this._animationGroups = new Map();
20 /** @type {!Array.<string>} */
21 this._pendingAnimations = [];
24 WebInspector.AnimationModel.Events = {
25 AnimationCreated: "AnimationCreated",
26 AnimationCanceled: "AnimationCanceled"
29 WebInspector.AnimationModel.prototype = {
30 /**
31 * @param {string} id
33 animationCreated: function(id)
35 this._pendingAnimations.push(id);
38 /**
39 * @param {!AnimationAgent.Animation} payload
41 animationStarted: function(payload)
43 var animation = WebInspector.AnimationModel.Animation.parsePayload(this.target(), payload);
44 this._animationsById.set(animation.id(), animation);
46 for (var id of this._pendingAnimations) {
47 if (!this._animationsById.get(id))
48 return;
51 while (this._pendingAnimations.length) {
52 var group = this._createGroupFromPendingAnimations();
53 this._animationGroups.set(group.id(), group);
54 // TODO(samli): Dispatch single group event.
55 for (var anim of group.animations())
56 this.dispatchEventToListeners(WebInspector.AnimationModel.Events.AnimationCreated, { "player": anim, "resetTimeline": anim.id() === group.id() });
60 /**
61 * @return {!WebInspector.AnimationModel.AnimationGroup}
63 _createGroupFromPendingAnimations: function()
65 console.assert(this._pendingAnimations.length);
66 var groupedAnimations = [this._animationsById.get(this._pendingAnimations.shift())];
67 var remainingAnimations = [];
68 for (var id of this._pendingAnimations) {
69 var anim = this._animationsById.get(id);
70 if (anim.startTime() === groupedAnimations[0].startTime())
71 groupedAnimations.push(anim);
72 else
73 remainingAnimations.push(id);
75 this._pendingAnimations = remainingAnimations;
76 return new WebInspector.AnimationModel.AnimationGroup(this.target(), groupedAnimations[0].id(), groupedAnimations);
79 /**
80 * @param {string} id
82 animationCanceled: function(id)
84 this.dispatchEventToListeners(WebInspector.AnimationModel.Events.AnimationCanceled, { "id": id });
87 /**
88 * @param {number} playbackRate
90 setPlaybackRate: function(playbackRate)
92 this._agent.setPlaybackRate(playbackRate);
95 ensureEnabled: function()
97 if (this._enabled)
98 return;
99 this._agent.enable();
100 this._enabled = true;
103 __proto__: WebInspector.SDKModel.prototype
106 WebInspector.AnimationModel._symbol = Symbol("AnimationModel");
109 * @param {!WebInspector.Target} target
110 * @return {!WebInspector.AnimationModel}
112 WebInspector.AnimationModel.fromTarget = function(target)
114 if (!target[WebInspector.AnimationModel._symbol])
115 target[WebInspector.AnimationModel._symbol] = new WebInspector.AnimationModel(target);
117 return target[WebInspector.AnimationModel._symbol];
121 * @constructor
122 * @extends {WebInspector.SDKObject}
123 * @param {!WebInspector.Target} target
124 * @param {!AnimationAgent.Animation} payload
126 WebInspector.AnimationModel.Animation = function(target, payload)
128 WebInspector.SDKObject.call(this, target);
129 this._payload = payload;
130 this._source = new WebInspector.AnimationModel.AnimationEffect(this.target(), this._payload.source);
134 * @param {!WebInspector.Target} target
135 * @param {!AnimationAgent.Animation} payload
136 * @return {!WebInspector.AnimationModel.Animation}
138 WebInspector.AnimationModel.Animation.parsePayload = function(target, payload)
140 return new WebInspector.AnimationModel.Animation(target, payload);
143 WebInspector.AnimationModel.Animation.prototype = {
145 * @return {!AnimationAgent.Animation}
147 payload: function()
149 return this._payload;
153 * @return {string}
155 id: function()
157 return this._payload.id;
161 * @return {string}
163 name: function()
165 return this.source().name();
169 * @return {boolean}
171 paused: function()
173 return this._payload.pausedState;
177 * @return {string}
179 playState: function()
181 return this._playState || this._payload.playState;
185 * @param {string} playState
187 setPlayState: function(playState)
189 this._playState = playState;
193 * @return {number}
195 playbackRate: function()
197 return this._payload.playbackRate;
201 * @return {number}
203 startTime: function()
205 return this._payload.startTime;
209 * @return {number}
211 endTime: function()
213 if (!this.source().iterations)
214 return Infinity;
215 return this.startTime() + this.source().delay() + this.source().duration() * this.source().iterations() + this.source().endDelay();
219 * @return {number}
221 currentTime: function()
223 return this._payload.currentTime;
227 * @return {!WebInspector.AnimationModel.AnimationEffect}
229 source: function()
231 return this._source;
235 * @return {string}
237 type: function()
239 return this._payload.type;
243 * @param {!WebInspector.AnimationModel.Animation} animation
244 * @return {boolean}
246 overlaps: function(animation)
248 // Infinite animations
249 if (!this.source().iterations() || !animation.source().iterations())
250 return true;
252 var firstAnimation = this.startTime() < animation.startTime() ? this : animation;
253 var secondAnimation = firstAnimation === this ? animation : this;
254 return firstAnimation.endTime() >= secondAnimation.startTime();
257 __proto__: WebInspector.SDKObject.prototype
261 * @constructor
262 * @extends {WebInspector.SDKObject}
263 * @param {!WebInspector.Target} target
264 * @param {!AnimationAgent.AnimationEffect} payload
266 WebInspector.AnimationModel.AnimationEffect = function(target, payload)
268 WebInspector.SDKObject.call(this, target);
269 this._payload = payload;
270 if (payload.keyframesRule)
271 this._keyframesRule = new WebInspector.AnimationModel.KeyframesRule(target, payload.keyframesRule);
272 this._delay = this._payload.delay;
273 this._duration = this._payload.duration;
276 WebInspector.AnimationModel.AnimationEffect.prototype = {
278 * @return {number}
280 delay: function()
282 return this._delay;
286 * @param {number} delay
288 setDelay: function(delay)
290 this._delay = delay;
294 * @return {number}
296 endDelay: function()
298 return this._payload.endDelay;
302 * @return {number}
304 playbackRate: function()
306 return this._payload.playbackRate;
310 * @return {number}
312 iterationStart: function()
314 return this._payload.iterationStart;
318 * @return {number}
320 iterations: function()
322 return this._payload.iterations || Infinity;
326 * @return {number}
328 duration: function()
330 return this._duration;
333 setDuration: function(duration)
335 this._duration = duration;
339 * @return {string}
341 direction: function()
343 return this._payload.direction;
347 * @return {string}
349 fill: function()
351 return this._payload.fill;
355 * @return {string}
357 name: function()
359 return this._payload.name;
363 * @return {!WebInspector.DeferredDOMNode}
365 deferredNode: function()
367 return new WebInspector.DeferredDOMNode(this.target(), this.backendNodeId());
371 * @return {number}
373 backendNodeId: function()
375 return this._payload.backendNodeId;
379 * @return {?WebInspector.AnimationModel.KeyframesRule}
381 keyframesRule: function()
383 return this._keyframesRule;
387 * @return {string}
389 easing: function()
391 return this._payload.easing;
394 __proto__: WebInspector.SDKObject.prototype
398 * @constructor
399 * @extends {WebInspector.SDKObject}
400 * @param {!WebInspector.Target} target
401 * @param {!AnimationAgent.KeyframesRule} payload
403 WebInspector.AnimationModel.KeyframesRule = function(target, payload)
405 WebInspector.SDKObject.call(this, target);
406 this._payload = payload;
407 this._keyframes = this._payload.keyframes.map(function (keyframeStyle) {
408 return new WebInspector.AnimationModel.KeyframeStyle(target, keyframeStyle);
412 WebInspector.AnimationModel.KeyframesRule.prototype = {
414 * @param {!Array.<!AnimationAgent.KeyframeStyle>} payload
416 _setKeyframesPayload: function(payload)
418 this._keyframes = payload.map(function (keyframeStyle) {
419 return new WebInspector.AnimationModel.KeyframeStyle(this._target, keyframeStyle);
424 * @return {string|undefined}
426 name: function()
428 return this._payload.name;
432 * @return {!Array.<!WebInspector.AnimationModel.KeyframeStyle>}
434 keyframes: function()
436 return this._keyframes;
439 __proto__: WebInspector.SDKObject.prototype
443 * @constructor
444 * @extends {WebInspector.SDKObject}
445 * @param {!WebInspector.Target} target
446 * @param {!AnimationAgent.KeyframeStyle} payload
448 WebInspector.AnimationModel.KeyframeStyle = function(target, payload)
450 WebInspector.SDKObject.call(this, target);
451 this._payload = payload;
452 this._offset = this._payload.offset;
455 WebInspector.AnimationModel.KeyframeStyle.prototype = {
457 * @return {string}
459 offset: function()
461 return this._offset;
465 * @param {number} offset
467 setOffset: function(offset)
469 this._offset = offset * 100 + "%";
473 * @return {number}
475 offsetAsNumber: function()
477 return parseFloat(this._offset) / 100;
481 * @return {string}
483 easing: function()
485 return this._payload.easing;
488 __proto__: WebInspector.SDKObject.prototype
492 * @constructor
493 * @extends {WebInspector.SDKObject}
494 * @param {!WebInspector.Target} target
495 * @param {string} id
496 * @param {!Array.<!WebInspector.AnimationModel.Animation>} animations
498 WebInspector.AnimationModel.AnimationGroup = function(target, id, animations)
500 WebInspector.SDKObject.call(this, target);
501 this._id = id;
502 this._animations = animations;
505 WebInspector.AnimationModel.AnimationGroup.prototype = {
507 * @return {string}
509 id: function()
511 return this._id;
515 * @return {!Array.<!WebInspector.AnimationModel.Animation>}
517 animations: function()
519 return this._animations;
522 __proto__: WebInspector.SDKObject.prototype
527 * @constructor
528 * @implements {AnimationAgent.Dispatcher}
530 WebInspector.AnimationDispatcher = function(animationModel)
532 this._animationModel = animationModel;
535 WebInspector.AnimationDispatcher.prototype = {
537 * @override
538 * @param {string} id
540 animationCreated: function(id)
542 this._animationModel.animationCreated(id);
546 * @override
547 * @param {!AnimationAgent.Animation} payload
549 animationStarted: function(payload)
551 this._animationModel.animationStarted(payload);
555 * @override
556 * @param {string} id
558 animationCanceled: function(id)
560 this._animationModel.animationCanceled(id);