Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Counter used to give webkit animations unique names. | 5 // Counter used to give webkit animations unique names. |
| 6 var animationCounter = 0; | 6 var animationCounter = 0; |
| 7 | 7 |
| 8 function addAnimation(code) { | 8 function addAnimation(code) { |
| 9 var name = 'anim' + animationCounter; | 9 var name = 'anim' + animationCounter; |
| 10 animationCounter++; | 10 animationCounter++; |
| 11 var rules = document.createTextNode( | 11 var rules = document.createTextNode( |
| 12 '@-webkit-keyframes ' + name + ' {' + code + '}'); | 12 '@-webkit-keyframes ' + name + ' {' + code + '}'); |
| 13 var el = document.createElement('style'); | 13 var el = document.createElement('style'); |
| 14 el.type = 'text/css'; | 14 el.type = 'text/css'; |
| 15 el.appendChild(rules); | 15 el.appendChild(rules); |
| 16 el.setAttribute('id', name); | 16 el.setAttribute('id', name); |
| 17 document.body.appendChild(el); | 17 document.body.appendChild(el); |
| 18 | 18 |
| 19 return name; | 19 return name; |
| 20 } | 20 } |
| 21 | 21 |
| 22 /** | 22 /** |
| 23 * Generates css code for fading in an element by animating the height. | |
| 24 * @param {number} targetHeight The desired height in pixels after the animation | |
| 25 * ends. | |
| 26 * @return {string} The css code for the fade in animation. | |
| 27 */ | |
| 28 function getFadeInAnimationCode(targetHeight) { | |
| 29 return '0% { opacity: 0; height: 0; } ' + | |
| 30 '80% { height: ' + (targetHeight + 4) + 'px; }' + | |
| 31 '100% { opacity: 1; height: ' + targetHeight + 'px; }'; | |
| 32 } | |
| 33 | |
| 34 /** | |
| 35 * Generates css code for fading out an element by animating the height. | |
| 36 * @param {number} startingHeight The desired height in pixels before the | |
| 37 * animation starts. | |
| 38 * @return {string} The css code for the fade in animation. | |
| 39 */ | |
| 40 function getFadeOutAnimationCode(startingHeight) { | |
| 41 return '0% { opacity: 1; height: ' + startingHeight + 'px; }' + | |
| 42 '100% { opacity: 1; height: 0; }'; | |
| 43 } | |
| 44 | |
| 45 /** | |
| 23 * Fades in an element. Used for both printing options and error messages | 46 * Fades in an element. Used for both printing options and error messages |
| 24 * appearing underneath the textfields. | 47 * appearing underneath the textfields. |
| 25 * @param {HTMLElement} el The element to be faded in. | 48 * @param {HTMLElement} el The element to be faded in. |
| 26 */ | 49 */ |
| 27 function fadeInElement(el) { | 50 function fadeInElement(el) { |
| 28 if (el.classList.contains('visible')) | 51 if (el.classList.contains('visible')) |
| 29 return; | 52 return; |
| 30 el.classList.remove('closing'); | 53 el.classList.remove('closing'); |
| 54 el.hidden = false; | |
| 31 el.style.height = 'auto'; | 55 el.style.height = 'auto'; |
| 32 var height = el.offsetHeight; | 56 var height = el.offsetHeight; |
| 33 el.style.height = height + 'px'; | 57 el.style.height = height + 'px'; |
| 34 var animName = addAnimation( | 58 var animName = addAnimation(getFadeInAnimationCode(height)); |
| 35 '0% { opacity: 0; height: 0; } ' + | 59 var eventTracker = new EventTracker(); |
| 36 '80% { height: ' + (height + 4) + 'px; }' + | 60 eventTracker.add(el, 'webkitAnimationEnd', |
| 37 '100% { opacity: 1; height: ' + height + 'px; }'); | 61 onFadeInAnimationEnd.bind(el, animName, eventTracker), |
| 62 false ); | |
| 38 el.style.webkitAnimationName = animName; | 63 el.style.webkitAnimationName = animName; |
| 39 el.classList.add('visible'); | 64 el.classList.add('visible'); |
| 40 el.addEventListener('webkitAnimationEnd', function() { | |
| 41 el.style.height = ''; | |
| 42 el.style.webkitAnimationName = ''; | |
| 43 fadeInOutCleanup(animName); | |
| 44 el.removeEventListener('webkitAnimationEnd', arguments.callee, false); | |
| 45 }, false ); | |
| 46 } | 65 } |
| 47 | 66 |
| 48 /** | 67 /** |
| 49 * Fades out an element. Used for both printing options and error messages | 68 * Fades out an element. Used for both printing options and error messages |
| 50 * appearing underneath the textfields. | 69 * appearing underneath the textfields. |
| 51 * @param {HTMLElement} el The element to be faded out. | 70 * @param {HTMLElement} el The element to be faded out. |
| 52 */ | 71 */ |
| 53 function fadeOutElement(el) { | 72 function fadeOutElement(el) { |
| 54 if (!el.classList.contains('visible')) | 73 if (!el.classList.contains('visible')) |
| 55 return; | 74 return; |
| 56 el.style.height = 'auto'; | 75 el.style.height = 'auto'; |
| 57 var height = el.offsetHeight; | 76 var height = el.offsetHeight; |
| 58 el.style.height = height + 'px'; | 77 el.style.height = height + 'px'; |
| 59 var animName = addAnimation( | 78 var animName = addAnimation(getFadeOutAnimationCode(height)); |
| 60 '0% { opacity: 1; height: ' + height + 'px; }' + | 79 var eventTracker = new EventTracker(); |
| 61 '100% { opacity: 1; height: 0; }'); | 80 eventTracker.add(el, 'webkitTransitionEnd', |
|
Evan Stade
2011/11/16 04:16:31
shuoldn't this be Animation not Transition
dpapad
2011/11/16 19:37:28
No, it is transition on purpose, since the fading
| |
| 81 onFadeOutAnimationEnd.bind(el, animName, eventTracker), | |
| 82 false ); | |
| 62 el.style.webkitAnimationName = animName; | 83 el.style.webkitAnimationName = animName; |
| 63 el.classList.add('closing'); | 84 el.classList.add('closing'); |
| 64 el.classList.remove('visible'); | 85 el.classList.remove('visible'); |
| 65 el.addEventListener('webkitAnimationEnd', function() { | |
| 66 fadeInOutCleanup(animName); | |
| 67 el.removeEventListener('webkitAnimationEnd', arguments.callee, false); | |
| 68 }, false ); | |
| 69 } | 86 } |
| 70 | 87 |
| 71 /** | 88 /** |
| 89 * Executes when a fade out animation ends. | |
| 90 * @param {string} animationName The name of the animation to be removed. | |
| 91 * @param{EventTracker} The |EventTracker| object that was used for adding this | |
| 92 * listener. | |
| 93 * @param {WebkitTransitionEvent} event The event that triggered this listener. | |
| 94 */ | |
| 95 function onFadeOutAnimationEnd(animationName, eventTracker, event) { | |
| 96 fadeInOutCleanup(animationName); | |
| 97 eventTracker.remove(event.target, 'webkitTransitionEnd'); | |
| 98 event.target.hidden = true; | |
| 99 } | |
| 100 | |
| 101 /** | |
| 102 * Executes when a fade in animation ends. | |
| 103 * @param {string} animationName The name of the animation to be removed. | |
| 104 * @param{EventTracker} The |EventTracker| object that was used for adding this | |
| 105 * listener. | |
| 106 * @param {WebkitAnimationEvent} event The event that triggered this listener. | |
| 107 */ | |
| 108 function onFadeInAnimationEnd(animationName, eventTracker, event) { | |
| 109 event.target.style.height = ''; | |
| 110 event.target.style.webkitAnimationName = ''; | |
| 111 fadeInOutCleanup(animationName); | |
| 112 eventTracker.remove(event.target, 'webkitAnimationEnd'); | |
| 113 } | |
| 114 | |
| 115 /** | |
| 72 * Removes the <style> element corrsponding to |animationName| from the DOM. | 116 * Removes the <style> element corrsponding to |animationName| from the DOM. |
| 73 * @param {string} animationName The name of the animation to be removed. | 117 * @param {string} animationName The name of the animation to be removed. |
| 74 */ | 118 */ |
| 75 function fadeInOutCleanup(animationName) { | 119 function fadeInOutCleanup(animationName) { |
| 76 animEl = document.getElementById(animationName); | 120 animEl = document.getElementById(animationName); |
| 77 if (animEl) | 121 if (animEl) |
| 78 animEl.parentNode.removeChild(animEl); | 122 animEl.parentNode.removeChild(animEl); |
| 79 } | 123 } |
| 80 | 124 |
| 81 /** | 125 /** |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 128 while (el.childNodes.length > 0) | 172 while (el.childNodes.length > 0) |
| 129 div.appendChild(el.firstChild); | 173 div.appendChild(el.firstChild); |
| 130 el.appendChild(div); | 174 el.appendChild(div); |
| 131 } | 175 } |
| 132 | 176 |
| 133 div.className = ''; | 177 div.className = ''; |
| 134 div.classList.add('collapsible'); | 178 div.classList.add('collapsible'); |
| 135 for (var i = 0; i < classes.length; i++) | 179 for (var i = 0; i < classes.length; i++) |
| 136 div.classList.add(classes[i]); | 180 div.classList.add(classes[i]); |
| 137 } | 181 } |
| OLD | NEW |