2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-07-01 19:01:57 -04:00
|
|
|
import {AUTO_STYLE} from '@angular/core';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {isPresent} from '../facade/lang';
|
2016-08-30 21:07:40 -04:00
|
|
|
import {AnimationPlayer} from '../private_import_core';
|
2016-06-08 19:38:52 -04:00
|
|
|
|
2016-07-01 19:01:57 -04:00
|
|
|
import {getDOM} from './dom_adapter';
|
2016-05-25 15:46:22 -04:00
|
|
|
import {DomAnimatePlayer} from './dom_animate_player';
|
|
|
|
|
|
|
|
export class WebAnimationsPlayer implements AnimationPlayer {
|
2016-08-22 19:39:52 -04:00
|
|
|
private _onDoneFns: Function[] = [];
|
|
|
|
private _onStartFns: Function[] = [];
|
2016-05-25 15:46:22 -04:00
|
|
|
private _finished = false;
|
2016-07-01 19:01:57 -04:00
|
|
|
private _initialized = false;
|
|
|
|
private _player: DomAnimatePlayer;
|
|
|
|
private _started: boolean = false;
|
|
|
|
private _duration: number;
|
|
|
|
|
2016-05-25 15:46:22 -04:00
|
|
|
public parentPlayer: AnimationPlayer = null;
|
|
|
|
|
2016-07-01 19:01:57 -04:00
|
|
|
constructor(
|
|
|
|
public element: any, public keyframes: {[key: string]: string | number}[],
|
|
|
|
public options: {[key: string]: string | number}) {
|
|
|
|
this._duration = <number>options['duration'];
|
2016-05-25 15:46:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private _onFinish() {
|
|
|
|
if (!this._finished) {
|
|
|
|
this._finished = true;
|
|
|
|
if (!isPresent(this.parentPlayer)) {
|
|
|
|
this.destroy();
|
|
|
|
}
|
2016-08-22 19:39:52 -04:00
|
|
|
this._onDoneFns.forEach(fn => fn());
|
|
|
|
this._onDoneFns = [];
|
2016-05-25 15:46:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-01 19:01:57 -04:00
|
|
|
init(): void {
|
|
|
|
if (this._initialized) return;
|
|
|
|
this._initialized = true;
|
|
|
|
|
|
|
|
var keyframes = this.keyframes.map(styles => {
|
|
|
|
var formattedKeyframe: {[key: string]: string | number} = {};
|
2016-10-03 19:46:05 -04:00
|
|
|
Object.keys(styles).forEach(prop => {
|
|
|
|
const value = styles[prop];
|
2016-07-01 19:01:57 -04:00
|
|
|
formattedKeyframe[prop] = value == AUTO_STYLE ? _computeStyle(this.element, prop) : value;
|
|
|
|
});
|
|
|
|
return formattedKeyframe;
|
|
|
|
});
|
|
|
|
|
|
|
|
this._player = this._triggerWebAnimation(this.element, keyframes, this.options);
|
|
|
|
|
|
|
|
// this is required so that the player doesn't start to animate right away
|
|
|
|
this.reset();
|
|
|
|
this._player.onfinish = () => this._onFinish();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
_triggerWebAnimation(element: any, keyframes: any[], options: any): DomAnimatePlayer {
|
|
|
|
return <DomAnimatePlayer>element.animate(keyframes, options);
|
|
|
|
}
|
|
|
|
|
2016-08-22 19:39:52 -04:00
|
|
|
onStart(fn: () => void): void { this._onStartFns.push(fn); }
|
|
|
|
|
|
|
|
onDone(fn: () => void): void { this._onDoneFns.push(fn); }
|
2016-05-25 15:46:22 -04:00
|
|
|
|
2016-07-01 19:01:57 -04:00
|
|
|
play(): void {
|
|
|
|
this.init();
|
2016-08-22 19:39:52 -04:00
|
|
|
if (!this.hasStarted()) {
|
|
|
|
this._onStartFns.forEach(fn => fn());
|
|
|
|
this._onStartFns = [];
|
|
|
|
this._started = true;
|
|
|
|
}
|
2016-07-01 19:01:57 -04:00
|
|
|
this._player.play();
|
|
|
|
}
|
2016-05-25 15:46:22 -04:00
|
|
|
|
2016-07-01 19:01:57 -04:00
|
|
|
pause(): void {
|
|
|
|
this.init();
|
|
|
|
this._player.pause();
|
|
|
|
}
|
2016-05-25 15:46:22 -04:00
|
|
|
|
|
|
|
finish(): void {
|
2016-07-01 19:01:57 -04:00
|
|
|
this.init();
|
2016-05-25 15:46:22 -04:00
|
|
|
this._onFinish();
|
|
|
|
this._player.finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
reset(): void { this._player.cancel(); }
|
|
|
|
|
|
|
|
restart(): void {
|
|
|
|
this.reset();
|
|
|
|
this.play();
|
|
|
|
}
|
|
|
|
|
2016-07-01 19:01:57 -04:00
|
|
|
hasStarted(): boolean { return this._started; }
|
|
|
|
|
2016-05-25 15:46:22 -04:00
|
|
|
destroy(): void {
|
|
|
|
this.reset();
|
|
|
|
this._onFinish();
|
|
|
|
}
|
|
|
|
|
2016-07-01 19:01:57 -04:00
|
|
|
get totalTime(): number { return this._duration; }
|
|
|
|
|
|
|
|
setPosition(p: number): void { this._player.currentTime = p * this.totalTime; }
|
2016-05-25 15:46:22 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
getPosition(): number { return this._player.currentTime / this.totalTime; }
|
2016-05-25 15:46:22 -04:00
|
|
|
}
|
2016-07-01 19:01:57 -04:00
|
|
|
|
|
|
|
function _computeStyle(element: any, prop: string): string {
|
|
|
|
return getDOM().getComputedStyle(element)[prop];
|
|
|
|
}
|