|
| 1 | +import { |
| 2 | + DateWrapper, |
| 3 | + StringWrapper, |
| 4 | + RegExpWrapper, |
| 5 | + NumberWrapper |
| 6 | +} from 'angular2/src/core/facade/lang'; |
| 7 | +import {Math} from 'angular2/src/core/facade/math'; |
| 8 | +import {camelCaseToDashCase} from 'angular2/src/core/render/dom/util'; |
| 9 | +import {StringMapWrapper} from 'angular2/src/core/facade/collection'; |
| 10 | +import {DOM} from 'angular2/src/core/dom/dom_adapter'; |
| 11 | + |
| 12 | +import {BrowserDetails} from './browser_details'; |
| 13 | +import {CssAnimationOptions} from './css_animation_options'; |
| 14 | + |
| 15 | +export class Animation { |
| 16 | + /** functions to be called upon completion */ |
| 17 | + callbacks: Function[] = []; |
| 18 | + |
| 19 | + /** the duration (ms) of the animation (whether from CSS or manually set) */ |
| 20 | + computedDuration: number; |
| 21 | + |
| 22 | + /** the animation delay (ms) (whether from CSS or manually set) */ |
| 23 | + computedDelay: number; |
| 24 | + |
| 25 | + /** timestamp of when the animation started */ |
| 26 | + startTime: number; |
| 27 | + |
| 28 | + /** functions for removing event listeners */ |
| 29 | + eventClearFunctions: Function[] = []; |
| 30 | + |
| 31 | + /** flag used to track whether or not the animation has finished */ |
| 32 | + completed: boolean = false; |
| 33 | + |
| 34 | + /** total amount of time that the animation should take including delay */ |
| 35 | + get totalTime(): number { |
| 36 | + let delay = this.computedDelay != null ? this.computedDelay : 0; |
| 37 | + let duration = this.computedDuration != null ? this.computedDuration : 0; |
| 38 | + return delay + duration; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Stores the start time and starts the animation |
| 43 | + * @param element |
| 44 | + * @param data |
| 45 | + * @param browserDetails |
| 46 | + */ |
| 47 | + constructor(public element: HTMLElement, public data: CssAnimationOptions, |
| 48 | + public browserDetails: BrowserDetails) { |
| 49 | + this.startTime = DateWrapper.toMillis(DateWrapper.now()); |
| 50 | + this.setup(); |
| 51 | + this.wait(timestamp => this.start()); |
| 52 | + } |
| 53 | + |
| 54 | + wait(callback: Function) { |
| 55 | + // Firefox requires 2 frames for some reason |
| 56 | + this.browserDetails.raf(callback, 2); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Sets up the initial styles before the animation is started |
| 61 | + */ |
| 62 | + setup(): void { |
| 63 | + if (this.data.fromStyles != null) this.applyStyles(this.data.fromStyles); |
| 64 | + if (this.data.duration != null) |
| 65 | + this.applyStyles({'transitionDuration': this.data.duration.toString() + 'ms'}); |
| 66 | + if (this.data.delay != null) |
| 67 | + this.applyStyles({'transitionDelay': this.data.delay.toString() + 'ms'}); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * After the initial setup has occurred, this method adds the animation styles |
| 72 | + */ |
| 73 | + start(): void { |
| 74 | + this.addClasses(this.data.classesToAdd); |
| 75 | + this.addClasses(this.data.animationClasses); |
| 76 | + this.removeClasses(this.data.classesToRemove); |
| 77 | + if (this.data.toStyles != null) this.applyStyles(this.data.toStyles); |
| 78 | + var computedStyles = DOM.getComputedStyle(this.element); |
| 79 | + this.computedDelay = |
| 80 | + Math.max(this.parseDurationString(computedStyles.getPropertyValue('transition-delay')), |
| 81 | + this.parseDurationString(this.element.style.getPropertyValue('transition-delay'))); |
| 82 | + this.computedDuration = Math.max( |
| 83 | + this.parseDurationString(computedStyles.getPropertyValue('transition-duration')), |
| 84 | + this.parseDurationString(this.element.style.getPropertyValue('transition-duration'))); |
| 85 | + this.addEvents(); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Applies the provided styles to the element |
| 90 | + * @param styles |
| 91 | + */ |
| 92 | + applyStyles(styles: StringMap<string, any>): void { |
| 93 | + StringMapWrapper.forEach(styles, (value, key) => { |
| 94 | + DOM.setStyle(this.element, camelCaseToDashCase(key), value.toString()); |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Adds the provided classes to the element |
| 100 | + * @param classes |
| 101 | + */ |
| 102 | + addClasses(classes: string[]): void { |
| 103 | + for (let i = 0, len = classes.length; i < len; i++) DOM.addClass(this.element, classes[i]); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Removes the provided classes from the element |
| 108 | + * @param classes |
| 109 | + */ |
| 110 | + removeClasses(classes: string[]): void { |
| 111 | + for (let i = 0, len = classes.length; i < len; i++) DOM.removeClass(this.element, classes[i]); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Adds events to track when animations have finished |
| 116 | + */ |
| 117 | + addEvents(): void { |
| 118 | + if (this.totalTime > 0) { |
| 119 | + this.eventClearFunctions.push(DOM.onAndCancel( |
| 120 | + this.element, 'transitionend', (event: any) => this.handleAnimationEvent(event))); |
| 121 | + } else { |
| 122 | + this.handleAnimationCompleted(); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + handleAnimationEvent(event: any): void { |
| 127 | + let elapsedTime = Math.round(event.elapsedTime * 1000); |
| 128 | + if (!this.browserDetails.elapsedTimeIncludesDelay) elapsedTime += this.computedDelay; |
| 129 | + event.stopPropagation(); |
| 130 | + if (elapsedTime >= this.totalTime) this.handleAnimationCompleted(); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Runs all animation callbacks and removes temporary classes |
| 135 | + */ |
| 136 | + handleAnimationCompleted(): void { |
| 137 | + this.removeClasses(this.data.animationClasses); |
| 138 | + this.callbacks.forEach(callback => callback()); |
| 139 | + this.callbacks = []; |
| 140 | + this.eventClearFunctions.forEach(fn => fn()); |
| 141 | + this.eventClearFunctions = []; |
| 142 | + this.completed = true; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Adds animation callbacks to be called upon completion |
| 147 | + * @param callback |
| 148 | + * @returns {Animation} |
| 149 | + */ |
| 150 | + onComplete(callback: Function): Animation { |
| 151 | + if (this.completed) { |
| 152 | + callback(); |
| 153 | + } else { |
| 154 | + this.callbacks.push(callback); |
| 155 | + } |
| 156 | + return this; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Converts the duration string to the number of milliseconds |
| 161 | + * @param duration |
| 162 | + * @returns {number} |
| 163 | + */ |
| 164 | + parseDurationString(duration: string): number { |
| 165 | + var maxValue = 0; |
| 166 | + // duration must have at least 2 characters to be valid. (number + type) |
| 167 | + if (duration == null || duration.length < 2) { |
| 168 | + return maxValue; |
| 169 | + } else if (duration.substring(duration.length - 2) == 'ms') { |
| 170 | + let value = NumberWrapper.parseInt(this.stripLetters(duration), 10); |
| 171 | + if (value > maxValue) maxValue = value; |
| 172 | + } else if (duration.substring(duration.length - 1) == 's') { |
| 173 | + let ms = NumberWrapper.parseFloat(this.stripLetters(duration)) * 1000; |
| 174 | + let value = Math.floor(ms); |
| 175 | + if (value > maxValue) maxValue = value; |
| 176 | + } |
| 177 | + return maxValue; |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Strips the letters from the duration string |
| 182 | + * @param str |
| 183 | + * @returns {string} |
| 184 | + */ |
| 185 | + stripLetters(str: string): string { |
| 186 | + return StringWrapper.replaceAll(str, RegExpWrapper.create('[^0-9]+$', ''), ''); |
| 187 | + } |
| 188 | +} |
0 commit comments