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, 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(''); |
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 onFadeOutTransitionEnd.bind(el, animName, eventTracker), |
| 71 false ); |
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 |
| 80 * listener. |
| 81 * @param {WebkitTransitionEvent} event The event that triggered this listener. |
| 82 */ |
| 83 function onFadeOutTransitionEnd(animationName, eventTracker, event) { |
| 84 if (event.propertyName != 'height') |
| 85 return; |
| 86 fadeInOutCleanup(animationName); |
| 87 eventTracker.remove(this, 'webkitTransitionEnd'); |
| 88 this.hidden = true; |
| 89 } |
| 90 |
| 91 /** |
| 92 * Executes when a fade in animation ends. |
| 93 * @param{EventTracker} The |EventTracker| object that was used for adding this |
| 94 * listener. |
| 95 * @param {WebkitAnimationEvent} event The event that triggered this listener. |
| 96 */ |
| 97 function onFadeInAnimationEnd(eventTracker, event) { |
| 98 this.style.height = ''; |
| 99 this.style.webkitAnimationName = ''; |
| 100 fadeInOutCleanup(event.animationName); |
| 101 eventTracker.remove(this, 'webkitAnimationEnd'); |
| 102 } |
| 103 |
| 104 /** |
72 * Removes the <style> element corrsponding to |animationName| from the DOM. | 105 * Removes the <style> element corrsponding to |animationName| from the DOM. |
73 * @param {string} animationName The name of the animation to be removed. | 106 * @param {string} animationName The name of the animation to be removed. |
74 */ | 107 */ |
75 function fadeInOutCleanup(animationName) { | 108 function fadeInOutCleanup(animationName) { |
76 animEl = document.getElementById(animationName); | 109 animEl = document.getElementById(animationName); |
77 if (animEl) | 110 if (animEl) |
78 animEl.parentNode.removeChild(animEl); | 111 animEl.parentNode.removeChild(animEl); |
79 } | 112 } |
80 | 113 |
81 /** | 114 /** |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 while (el.childNodes.length > 0) | 161 while (el.childNodes.length > 0) |
129 div.appendChild(el.firstChild); | 162 div.appendChild(el.firstChild); |
130 el.appendChild(div); | 163 el.appendChild(div); |
131 } | 164 } |
132 | 165 |
133 div.className = ''; | 166 div.className = ''; |
134 div.classList.add('collapsible'); | 167 div.classList.add('collapsible'); |
135 for (var i = 0; i < classes.length; i++) | 168 for (var i = 0; i < classes.length; i++) |
136 div.classList.add(classes[i]); | 169 div.classList.add(classes[i]); |
137 } | 170 } |
OLD | NEW |