2016-06-23 09:47:54 -07: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-06-08 16:38:52 -07:00
|
|
|
import {AUTO_STYLE, BaseException} from '@angular/core';
|
2016-05-25 12:46:22 -07:00
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
import {AnimationDriver, AnimationKeyframe, AnimationPlayer, AnimationStyles, NoOpAnimationPlayer} from '../../core_private';
|
|
|
|
import {StringMapWrapper} from '../facade/collection';
|
|
|
|
import {StringWrapper, isNumber, isPresent} from '../facade/lang';
|
2016-05-25 12:46:22 -07:00
|
|
|
|
|
|
|
import {getDOM} from './dom_adapter';
|
2016-06-08 22:50:52 -07:00
|
|
|
import {DomAnimatePlayer} from './dom_animate_player';
|
|
|
|
import {dashCaseToCamelCase} from './util';
|
2016-06-08 16:38:52 -07:00
|
|
|
import {WebAnimationsPlayer} from './web_animations_player';
|
2016-05-25 12:46:22 -07:00
|
|
|
|
|
|
|
export class WebAnimationsDriver implements AnimationDriver {
|
2016-06-08 16:38:52 -07:00
|
|
|
animate(
|
|
|
|
element: any, startingStyles: AnimationStyles, keyframes: AnimationKeyframe[],
|
|
|
|
duration: number, delay: number, easing: string): AnimationPlayer {
|
2016-05-25 12:46:22 -07:00
|
|
|
var anyElm = <any>element;
|
|
|
|
|
2016-06-08 22:50:52 -07:00
|
|
|
var formattedSteps: {[key: string]: string | number}[] = [];
|
2016-06-08 16:38:52 -07:00
|
|
|
var startingStyleLookup: {[key: string]: string | number} = {};
|
2016-05-25 12:46:22 -07:00
|
|
|
if (isPresent(startingStyles) && startingStyles.styles.length > 0) {
|
|
|
|
startingStyleLookup = _populateStyles(anyElm, startingStyles, {});
|
|
|
|
startingStyleLookup['offset'] = 0;
|
|
|
|
formattedSteps.push(startingStyleLookup);
|
|
|
|
}
|
|
|
|
|
|
|
|
keyframes.forEach((keyframe: AnimationKeyframe) => {
|
|
|
|
let data = _populateStyles(anyElm, keyframe.styles, startingStyleLookup);
|
2016-06-08 22:50:52 -07:00
|
|
|
data['offset'] = keyframe.offset;
|
2016-05-25 12:46:22 -07:00
|
|
|
formattedSteps.push(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
// this is a special case when only styles are applied as an
|
|
|
|
// animation. When this occurs we want to animate from start to
|
|
|
|
// end with the same values. Removing the offset and having only
|
|
|
|
// start/end values is suitable enough for the web-animations API
|
|
|
|
if (formattedSteps.length == 1) {
|
|
|
|
var start = formattedSteps[0];
|
2016-06-08 22:50:52 -07:00
|
|
|
start['offset'] = null;
|
2016-05-25 12:46:22 -07:00
|
|
|
formattedSteps = [start, start];
|
|
|
|
}
|
|
|
|
|
2016-06-17 16:49:05 -07:00
|
|
|
var playerOptions = {
|
|
|
|
'duration': duration,
|
|
|
|
'delay': delay,
|
2016-06-22 23:57:49 -07:00
|
|
|
'easing': easing,
|
2016-06-17 16:49:05 -07:00
|
|
|
'fill': 'both' // we use `both` because it allows for styling at 0% to work with `delay`
|
|
|
|
};
|
|
|
|
|
|
|
|
var player = this._triggerWebAnimation(anyElm, formattedSteps, playerOptions);
|
2016-05-25 12:46:22 -07:00
|
|
|
|
|
|
|
return new WebAnimationsPlayer(player, duration);
|
|
|
|
}
|
2016-06-08 22:50:52 -07:00
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
_triggerWebAnimation(elm: any, keyframes: any[], options: any): DomAnimatePlayer {
|
|
|
|
return elm.animate(keyframes, options);
|
|
|
|
}
|
2016-05-25 12:46:22 -07:00
|
|
|
}
|
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
function _populateStyles(
|
2016-06-08 22:50:52 -07:00
|
|
|
element: any, styles: AnimationStyles,
|
|
|
|
defaultStyles: {[key: string]: string | number}): {[key: string]: string | number} {
|
|
|
|
var data: {[key: string]: string | number} = {};
|
2016-05-25 12:46:22 -07:00
|
|
|
styles.styles.forEach((entry) => {
|
2016-06-08 22:50:52 -07:00
|
|
|
StringMapWrapper.forEach(entry, (val: any, prop: string) => {
|
|
|
|
var formattedProp = dashCaseToCamelCase(prop);
|
|
|
|
data[formattedProp] = val == AUTO_STYLE ?
|
|
|
|
_computeStyle(element, formattedProp) :
|
|
|
|
val.toString() + _resolveStyleUnit(val, prop, formattedProp);
|
2016-05-25 12:46:22 -07:00
|
|
|
});
|
|
|
|
});
|
2016-06-08 22:50:52 -07:00
|
|
|
StringMapWrapper.forEach(defaultStyles, (value: string, prop: string) => {
|
|
|
|
if (!isPresent(data[prop])) {
|
|
|
|
data[prop] = value;
|
|
|
|
}
|
|
|
|
});
|
2016-05-25 12:46:22 -07:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2016-06-08 22:50:52 -07:00
|
|
|
function _resolveStyleUnit(
|
|
|
|
val: string | number, userProvidedProp: string, formattedProp: string): string {
|
2016-05-25 12:46:22 -07:00
|
|
|
var unit = '';
|
2016-06-08 22:50:52 -07:00
|
|
|
if (_isPixelDimensionStyle(formattedProp) && val != 0 && val != '0') {
|
2016-05-25 12:46:22 -07:00
|
|
|
if (isNumber(val)) {
|
|
|
|
unit = 'px';
|
|
|
|
} else if (_findDimensionalSuffix(val.toString()).length == 0) {
|
2016-06-08 22:50:52 -07:00
|
|
|
throw new BaseException(
|
|
|
|
'Please provide a CSS unit value for ' + userProvidedProp + ':' + val);
|
2016-05-25 12:46:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return unit;
|
|
|
|
}
|
|
|
|
|
|
|
|
const _$0 = 48;
|
|
|
|
const _$9 = 57;
|
|
|
|
const _$PERIOD = 46;
|
|
|
|
|
|
|
|
function _findDimensionalSuffix(value: string): string {
|
|
|
|
for (var i = 0; i < value.length; i++) {
|
|
|
|
var c = StringWrapper.charCodeAt(value, i);
|
|
|
|
if ((c >= _$0 && c <= _$9) || c == _$PERIOD) continue;
|
|
|
|
return value.substring(i, value.length);
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
function _isPixelDimensionStyle(prop: string): boolean {
|
|
|
|
switch (prop) {
|
|
|
|
case 'width':
|
|
|
|
case 'height':
|
2016-06-08 22:50:52 -07:00
|
|
|
case 'minWidth':
|
|
|
|
case 'minHeight':
|
|
|
|
case 'maxWidth':
|
|
|
|
case 'maxHeight':
|
2016-05-25 12:46:22 -07:00
|
|
|
case 'left':
|
|
|
|
case 'top':
|
|
|
|
case 'bottom':
|
|
|
|
case 'right':
|
2016-06-08 22:50:52 -07:00
|
|
|
case 'fontSize':
|
|
|
|
case 'outlineWidth':
|
|
|
|
case 'outlineOffset':
|
|
|
|
case 'paddingTop':
|
|
|
|
case 'paddingLeft':
|
|
|
|
case 'paddingBottom':
|
|
|
|
case 'paddingRight':
|
|
|
|
case 'marginTop':
|
|
|
|
case 'marginLeft':
|
|
|
|
case 'marginBottom':
|
|
|
|
case 'marginRight':
|
|
|
|
case 'borderRadius':
|
|
|
|
case 'borderWidth':
|
|
|
|
case 'borderTopWidth':
|
|
|
|
case 'borderLeftWidth':
|
|
|
|
case 'borderRightWidth':
|
|
|
|
case 'borderBottomWidth':
|
|
|
|
case 'textIndent':
|
2016-05-25 12:46:22 -07:00
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _computeStyle(element: any, prop: string): string {
|
|
|
|
return getDOM().getComputedStyle(element)[prop];
|
|
|
|
}
|