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 /** | |
| 23 * Fades in an element. Used for both printing options and error messages | 35 * Fades in an element. Used for both printing options and error messages |
| 24 * appearing underneath the textfields. | 36 * appearing underneath the textfields. |
| 25 * @param {HTMLElement} el The element to be faded in. | 37 * @param {HTMLElement} el The element to be faded in. |
| 26 */ | 38 */ |
| 27 function fadeInElement(el) { | 39 function fadeInElement(el) { |
| 28 if (el.classList.contains('visible')) | 40 if (el.classList.contains('visible')) |
| 29 return; | 41 return; |
| 30 el.classList.remove('closing'); | 42 el.classList.remove('closing'); |
| 43 el.hidden = false; | |
| 31 el.style.height = 'auto'; | 44 el.style.height = 'auto'; |
| 32 var height = el.offsetHeight; | 45 var height = el.offsetHeight; |
| 33 el.style.height = height + 'px'; | 46 el.style.height = height + 'px'; |
| 34 var animName = addAnimation( | 47 var animName = addAnimation(getFadeInAnimationCode(height)); |
| 35 '0% { opacity: 0; height: 0; } ' + | 48 var eventTracker = new EventTracker(); |
| 36 '80% { height: ' + (height + 4) + 'px; }' + | 49 eventTracker.add(el, 'webkitAnimationEnd', |
| 37 '100% { opacity: 1; height: ' + height + 'px; }'); | 50 onFadeInAnimationEnd.bind(el, animName, eventTracker), |
| 51 false ); | |
| 38 el.style.webkitAnimationName = animName; | 52 el.style.webkitAnimationName = animName; |
| 39 el.classList.add('visible'); | 53 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 } | 54 } |
| 47 | 55 |
| 48 /** | 56 /** |
| 49 * Fades out an element. Used for both printing options and error messages | 57 * Fades out an element. Used for both printing options and error messages |
| 50 * appearing underneath the textfields. | 58 * appearing underneath the textfields. |
| 51 * @param {HTMLElement} el The element to be faded out. | 59 * @param {HTMLElement} el The element to be faded out. |
| 52 */ | 60 */ |
| 53 function fadeOutElement(el) { | 61 function fadeOutElement(el) { |
| 54 if (!el.classList.contains('visible')) | 62 if (!el.classList.contains('visible')) |
| 55 return; | 63 return; |
| 56 el.style.height = 'auto'; | 64 el.style.height = 'auto'; |
| 57 var height = el.offsetHeight; | 65 var height = el.offsetHeight; |
| 58 el.style.height = height + 'px'; | 66 el.style.height = height + 'px'; |
| 59 var animName = addAnimation( | 67 var animName = addAnimation(''); |
|
dpapad
2011/11/16 19:37:28
Removed some unneeded animation code for the fade
| |
| 60 '0% { opacity: 1; height: ' + height + 'px; }' + | 68 var eventTracker = new EventTracker(); |
| 61 '100% { opacity: 1; height: 0; }'); | 69 eventTracker.add(el, 'webkitTransitionEnd', |
| 62 el.style.webkitAnimationName = animName; | 70 onFadeOutAnimationEnd.bind(el, animName, eventTracker), |
| 71 false ); | |
|
Evan Stade
2011/11/16 20:04:23
false);
dpapad
2011/11/16 21:09:25
Done.
| |
| 63 el.classList.add('closing'); | 72 el.classList.add('closing'); |
| 64 el.classList.remove('visible'); | 73 el.classList.remove('visible'); |
| 65 el.addEventListener('webkitAnimationEnd', function() { | |
| 66 fadeInOutCleanup(animName); | |
| 67 el.removeEventListener('webkitAnimationEnd', arguments.callee, false); | |
| 68 }, false ); | |
| 69 } | 74 } |
| 70 | 75 |
| 71 /** | 76 /** |
| 77 * Executes when a fade out animation ends. | |
| 78 * @param {string} animationName The name of the animation to be removed. | |
| 79 * @param{EventTracker} The |EventTracker| object that was used for adding this | |
|
Evan Stade
2011/11/16 20:04:23
param space
dpapad
2011/11/16 21:09:25
Done.
| |
| 80 * listener. | |
| 81 * @param {WebkitTransitionEvent} event The event that triggered this listener. | |
| 82 */ | |
| 83 function onFadeOutAnimationEnd(animationName, eventTracker, event) { | |
|
Evan Stade
2011/11/16 20:04:23
call it onFadeOutTransitionEnd
Evan Stade
2011/11/16 20:04:23
you should make sure the property that is done tra
dpapad
2011/11/16 21:09:25
Done.
dpapad
2011/11/16 21:09:25
Done.
| |
| 84 fadeInOutCleanup(animationName); | |
| 85 eventTracker.remove(event.target, 'webkitTransitionEnd'); | |
|
Evan Stade
2011/11/16 20:04:23
use this instead of event.target
dpapad
2011/11/16 21:09:25
Done.
| |
| 86 event.target.hidden = true; | |
| 87 } | |
| 88 | |
| 89 /** | |
| 90 * Executes when a fade in animation ends. | |
| 91 * @param {string} animationName The name of the animation to be removed. | |
| 92 * @param{EventTracker} The |EventTracker| object that was used for adding this | |
| 93 * listener. | |
| 94 * @param {WebkitAnimationEvent} event The event that triggered this listener. | |
| 95 */ | |
| 96 function onFadeInAnimationEnd(animationName, eventTracker, event) { | |
| 97 event.target.style.height = ''; | |
| 98 event.target.style.webkitAnimationName = ''; | |
| 99 fadeInOutCleanup(animationName); | |
| 100 eventTracker.remove(event.target, 'webkitAnimationEnd'); | |
| 101 } | |
| 102 | |
| 103 /** | |
| 72 * Removes the <style> element corrsponding to |animationName| from the DOM. | 104 * Removes the <style> element corrsponding to |animationName| from the DOM. |
| 73 * @param {string} animationName The name of the animation to be removed. | 105 * @param {string} animationName The name of the animation to be removed. |
| 74 */ | 106 */ |
| 75 function fadeInOutCleanup(animationName) { | 107 function fadeInOutCleanup(animationName) { |
| 76 animEl = document.getElementById(animationName); | 108 animEl = document.getElementById(animationName); |
| 77 if (animEl) | 109 if (animEl) |
| 78 animEl.parentNode.removeChild(animEl); | 110 animEl.parentNode.removeChild(animEl); |
| 79 } | 111 } |
| 80 | 112 |
| 81 /** | 113 /** |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 128 while (el.childNodes.length > 0) | 160 while (el.childNodes.length > 0) |
| 129 div.appendChild(el.firstChild); | 161 div.appendChild(el.firstChild); |
| 130 el.appendChild(div); | 162 el.appendChild(div); |
| 131 } | 163 } |
| 132 | 164 |
| 133 div.className = ''; | 165 div.className = ''; |
| 134 div.classList.add('collapsible'); | 166 div.classList.add('collapsible'); |
| 135 for (var i = 0; i < classes.length; i++) | 167 for (var i = 0; i < classes.length; i++) |
| 136 div.classList.add(classes[i]); | 168 div.classList.add(classes[i]); |
| 137 } | 169 } |
| OLD | NEW |