|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import {AnimationPlayer, ɵStyleData} from '@angular/animations'; |
| 9 | + |
| 10 | +import {allowPreviousPlayerStylesMerge, balancePreviousStylesIntoKeyframes, computeStyle} from '../../util'; |
| 11 | +import {AnimationDriver} from '../animation_driver'; |
| 12 | +import {containsElement, invokeQuery, matchesElement, validateStyleProperty} from '../shared'; |
| 13 | + |
| 14 | +import {CssKeyframesPlayer} from './css_keyframes_player'; |
| 15 | +import {DirectStylePlayer} from './direct_style_player'; |
| 16 | + |
| 17 | +const KEYFRAMES_NAME_PREFIX = 'gen_css_kf_'; |
| 18 | +const TAB_SPACE = ' '; |
| 19 | + |
| 20 | +export class CssKeyframesDriver implements AnimationDriver { |
| 21 | + private _count = 0; |
| 22 | + private readonly _head: any = document.querySelector('head'); |
| 23 | + private _warningIssued = false; |
| 24 | + |
| 25 | + validateStyleProperty(prop: string): boolean { return validateStyleProperty(prop); } |
| 26 | + |
| 27 | + matchesElement(element: any, selector: string): boolean { |
| 28 | + return matchesElement(element, selector); |
| 29 | + } |
| 30 | + |
| 31 | + containsElement(elm1: any, elm2: any): boolean { return containsElement(elm1, elm2); } |
| 32 | + |
| 33 | + query(element: any, selector: string, multi: boolean): any[] { |
| 34 | + return invokeQuery(element, selector, multi); |
| 35 | + } |
| 36 | + |
| 37 | + computeStyle(element: any, prop: string, defaultValue?: string): string { |
| 38 | + return (window.getComputedStyle(element) as any)[prop] as string; |
| 39 | + } |
| 40 | + |
| 41 | + buildKeyframeElement(element: any, name: string, keyframes: {[key: string]: any}[]): any { |
| 42 | + keyframes = keyframes.map(kf => hypenatePropsObject(kf)); |
| 43 | + let keyframeStr = `@keyframes ${name} {\n`; |
| 44 | + let tab = ''; |
| 45 | + keyframes.forEach(kf => { |
| 46 | + tab = TAB_SPACE; |
| 47 | + const offset = parseFloat(kf.offset); |
| 48 | + keyframeStr += `${tab}${offset * 100}% {\n`; |
| 49 | + tab += TAB_SPACE; |
| 50 | + Object.keys(kf).forEach(prop => { |
| 51 | + const value = kf[prop]; |
| 52 | + switch (prop) { |
| 53 | + case 'offset': |
| 54 | + return; |
| 55 | + case 'easing': |
| 56 | + if (value) { |
| 57 | + keyframeStr += `${tab}animation-timing-function: ${value};\n`; |
| 58 | + } |
| 59 | + return; |
| 60 | + default: |
| 61 | + keyframeStr += `${tab}${prop}: ${value};\n`; |
| 62 | + return; |
| 63 | + } |
| 64 | + }); |
| 65 | + keyframeStr += `${tab}}\n`; |
| 66 | + }); |
| 67 | + keyframeStr += `}\n`; |
| 68 | + |
| 69 | + const kfElm = document.createElement('style'); |
| 70 | + kfElm.innerHTML = keyframeStr; |
| 71 | + return kfElm; |
| 72 | + } |
| 73 | + |
| 74 | + animate( |
| 75 | + element: any, keyframes: ɵStyleData[], duration: number, delay: number, easing: string, |
| 76 | + previousPlayers: AnimationPlayer[] = [], scrubberAccessRequested?: boolean): AnimationPlayer { |
| 77 | + if (scrubberAccessRequested) { |
| 78 | + this._notifyFaultyScrubber(); |
| 79 | + } |
| 80 | + |
| 81 | + const previousCssKeyframePlayers = <CssKeyframesPlayer[]>previousPlayers.filter( |
| 82 | + player => player instanceof CssKeyframesPlayer); |
| 83 | + |
| 84 | + const previousStyles: {[key: string]: any} = {}; |
| 85 | + |
| 86 | + if (allowPreviousPlayerStylesMerge(duration, delay)) { |
| 87 | + previousCssKeyframePlayers.forEach(player => { |
| 88 | + let styles = player.currentSnapshot; |
| 89 | + Object.keys(styles).forEach(prop => previousStyles[prop] = styles[prop]); |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + keyframes = balancePreviousStylesIntoKeyframes(element, keyframes, previousStyles); |
| 94 | + const finalStyles = flattenKeyframesIntoStyles(keyframes); |
| 95 | + |
| 96 | + // if there is no animation then there is no point in applying |
| 97 | + // styles and waiting for an event to get fired. This causes lag. |
| 98 | + // It's better to just directly apply the styles to the element |
| 99 | + // via the direct styling animation player. |
| 100 | + if (duration == 0) { |
| 101 | + return new DirectStylePlayer(element, finalStyles); |
| 102 | + } |
| 103 | + |
| 104 | + const animationName = `${KEYFRAMES_NAME_PREFIX}${this._count++}`; |
| 105 | + const kfElm = this.buildKeyframeElement(element, animationName, keyframes); |
| 106 | + document.querySelector('head') !.appendChild(kfElm); |
| 107 | + |
| 108 | + const player = new CssKeyframesPlayer( |
| 109 | + element, keyframes, animationName, duration, delay, easing, finalStyles); |
| 110 | + |
| 111 | + player.onDestroy(() => removeElement(kfElm)); |
| 112 | + return player; |
| 113 | + } |
| 114 | + |
| 115 | + private _notifyFaultyScrubber() { |
| 116 | + if (!this._warningIssued) { |
| 117 | + console.warn( |
| 118 | + '@angular/animations: please load the web-animations.js polyfill to allow programmatic access...\n', |
| 119 | + ' visit http://bit.ly/IWukam to learn more about using the web-animation-js polyfill.'); |
| 120 | + this._warningIssued = true; |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +function flattenKeyframesIntoStyles( |
| 126 | + keyframes: null | {[key: string]: any} | {[key: string]: any}[]): {[key: string]: any} { |
| 127 | + let flatKeyframes: {[key: string]: any} = {}; |
| 128 | + if (keyframes) { |
| 129 | + const kfs = Array.isArray(keyframes) ? keyframes : [keyframes]; |
| 130 | + kfs.forEach(kf => { |
| 131 | + Object.keys(kf).forEach(prop => { |
| 132 | + if (prop == 'offset' || prop == 'easing') return; |
| 133 | + flatKeyframes[prop] = kf[prop]; |
| 134 | + }); |
| 135 | + }); |
| 136 | + } |
| 137 | + return flatKeyframes; |
| 138 | +} |
| 139 | + |
| 140 | +function hypenatePropsObject(object: {[key: string]: any}): {[key: string]: any} { |
| 141 | + const newObj: {[key: string]: any} = {}; |
| 142 | + Object.keys(object).forEach(prop => { |
| 143 | + const newProp = prop.replace(/([a-z])([A-Z])/g, '$1-$2'); |
| 144 | + newObj[newProp] = object[prop]; |
| 145 | + }); |
| 146 | + return newObj; |
| 147 | +} |
| 148 | + |
| 149 | +function removeElement(node: any) { |
| 150 | + node.parentNode.removeChild(node); |
| 151 | +} |
0 commit comments