| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Common effects related helpers. | 6 * Common effects related helpers. |
| 7 */ | 7 */ |
| 8 class FxUtil { | 8 class FxUtil { |
| 9 /** On transition end event. */ | 9 /** On transition end event. */ |
| 10 static final TRANSITION_END_EVENT = 'webkitTransitionEnd'; | 10 static final TRANSITION_END_EVENT = 'webkitTransitionEnd'; |
| 11 | 11 |
| 12 /** The translate3d transform function. */ | 12 /** The translate3d transform function. */ |
| 13 static final TRANSLATE_3D = 'translate3d'; | 13 static final TRANSLATE_3D = 'translate3d'; |
| 14 | 14 |
| 15 /** The rotate transform function. */ | 15 /** The rotate transform function. */ |
| 16 static final ROTATE = 'rotate'; | 16 static final ROTATE = 'rotate'; |
| 17 | 17 |
| 18 /** The scale transform function. */ | 18 /** The scale transform function. */ |
| 19 static final SCALE = 'scale'; | 19 static final SCALE = 'scale'; |
| 20 | 20 |
| 21 /** | 21 /** |
| 22 * Apply a -webkit-transition on an element using the [duration] of this | 22 * Apply a -webkit-transition on an element using the [duration] of this |
| 23 * animation in ms. | 23 * animation in ms. |
| 24 */ | 24 */ |
| 25 static void setWebkitTransition( | 25 static void setWebkitTransition( |
| 26 Element el, num duration, | 26 Element el, num duration, |
| 27 [String property = StyleUtil.TRANSFORM_STYLE, | 27 [String property = StyleUtil.TRANSFORM_STYLE, |
| 28 String timingFunction = TransitionTimingFunction.EASE_IN_OUT]) { | 28 String timingFunction = TransitionTimingFunction.EASE_IN_OUT]) { |
| 29 Css.setTransition(el.style, '${property} ${duration}ms ${timingFunction}'); | 29 el.style.transition = '${property} ${duration}ms ${timingFunction}'; |
| 30 } | 30 } |
| 31 | 31 |
| 32 /** Stops and clears the transition on an element. */ | 32 /** Stops and clears the transition on an element. */ |
| 33 static void clearWebkitTransition(Element el) { | 33 static void clearWebkitTransition(Element el) { |
| 34 Css.setTransition(el.style, ''); | 34 el.style.transition = ''; |
| 35 } | 35 } |
| 36 | 36 |
| 37 static void setPosition(Element el, Coordinate point) { | 37 static void setPosition(Element el, Coordinate point) { |
| 38 num x = point.x; | 38 num x = point.x; |
| 39 num y = point.y; | 39 num y = point.y; |
| 40 Css.setTransform(el.style, '${TRANSLATE_3D}(${x}px,${y}px,0px)'); | 40 el.style.transform = '${TRANSLATE_3D}(${x}px,${y}px,0px)'; |
| 41 } | 41 } |
| 42 | 42 |
| 43 /** Apply a transform using translate3d to an HTML element. */ | 43 /** Apply a transform using translate3d to an HTML element. */ |
| 44 static void setTranslate(Element el, num x, num y, num z) { | 44 static void setTranslate(Element el, num x, num y, num z) { |
| 45 Css.setTransform(el.style, '${TRANSLATE_3D}(${x}px,${y}px,${z}px)'); | 45 el.style.transform = '${TRANSLATE_3D}(${x}px,${y}px,${z}px)'; |
| 46 } | 46 } |
| 47 | 47 |
| 48 /** Apply a -webkit-transform using translate3d to an HTML element. */ | 48 /** Apply a -webkit-transform using translate3d to an HTML element. */ |
| 49 static void setWebkitTransform( | 49 static void setWebkitTransform( |
| 50 Element el, num x, num y, [num z = 0, | 50 Element el, num x, num y, [num z = 0, |
| 51 num rotation = null, num scale = null, | 51 num rotation = null, num scale = null, |
| 52 num originX = null, num originY = null]) { | 52 num originX = null, num originY = null]) { |
| 53 final style = el.style; | 53 final style = el.style; |
| 54 // TODO(jacobr): create a helper class that simplifies building | 54 // TODO(jacobr): create a helper class that simplifies building |
| 55 // transformation matricies that will be set as CSS styles. We should | 55 // transformation matricies that will be set as CSS styles. We should |
| 56 // consider using CSSMatrix although that may be overkill. | 56 // consider using CSSMatrix although that may be overkill. |
| 57 String transform = '${TRANSLATE_3D}(${x}px,${y}px,${z}px)'; | 57 String transform = '${TRANSLATE_3D}(${x}px,${y}px,${z}px)'; |
| 58 if (rotation !== null) { | 58 if (rotation !== null) { |
| 59 transform = transform.concat(' ${ROTATE}(${rotation}deg)'); | 59 transform = transform.concat(' ${ROTATE}(${rotation}deg)'); |
| 60 } | 60 } |
| 61 if (scale !== null) { | 61 if (scale !== null) { |
| 62 transform = transform.concat(' ${SCALE}(${scale})'); | 62 transform = transform.concat(' ${SCALE}(${scale})'); |
| 63 } | 63 } |
| 64 Css.setTransform(style, transform); | 64 style.transform = transform; |
| 65 if (originX !== null || originY !== null) { | 65 if (originX !== null || originY !== null) { |
| 66 assert(originX !== null && originY !== null); | 66 assert(originX !== null && originY !== null); |
| 67 Css.setTransformOrigin(style, '${originX}px ${originY}px'); | 67 style.transformOrigin = '${originX}px ${originY}px'; |
| 68 } | 68 } |
| 69 } | 69 } |
| 70 | 70 |
| 71 /** | 71 /** |
| 72 * Determine the position of an [element] relative to a [target] element. | 72 * Determine the position of an [element] relative to a [target] element. |
| 73 * Moving the [element] to be a child of [target] and setting the | 73 * Moving the [element] to be a child of [target] and setting the |
| 74 * [element]'s top and left values to the returned coordinate should result | 74 * [element]'s top and left values to the returned coordinate should result |
| 75 * in the [element]'s position remaining unchanged while its parent is | 75 * in the [element]'s position remaining unchanged while its parent is |
| 76 * changed. | 76 * changed. |
| 77 */ | 77 */ |
| 78 static Coordinate computeRelativePosition(Element element, Element target) { | 78 static Coordinate computeRelativePosition(Element element, Element target) { |
| 79 final testPoint = new Point(0, 0); | 79 final testPoint = new Point(0, 0); |
| 80 final pagePoint = | 80 final pagePoint = |
| 81 window.webkitConvertPointFromNodeToPage(element, testPoint); | 81 window.webkitConvertPointFromNodeToPage(element, testPoint); |
| 82 final pointRelativeToTarget = | 82 final pointRelativeToTarget = |
| 83 window.webkitConvertPointFromPageToNode(target, pagePoint); | 83 window.webkitConvertPointFromPageToNode(target, pagePoint); |
| 84 return new Coordinate(pointRelativeToTarget.x, pointRelativeToTarget.y); | 84 return new Coordinate(pointRelativeToTarget.x, pointRelativeToTarget.y); |
| 85 } | 85 } |
| 86 | 86 |
| 87 /** Clear a -webkit-transform from an element. */ | 87 /** Clear a -webkit-transform from an element. */ |
| 88 static void clearWebkitTransform(Element el) { | 88 static void clearWebkitTransform(Element el) { |
| 89 Css.setTransform(el.style, ''); | 89 el.style.transform = ''; |
| 90 } | 90 } |
| 91 | 91 |
| 92 /** | 92 /** |
| 93 * Checks whether an element has a translate3d webkit transform applied. | 93 * Checks whether an element has a translate3d webkit transform applied. |
| 94 */ | 94 */ |
| 95 static bool hasWebkitTransform(Element el) { | 95 static bool hasWebkitTransform(Element el) { |
| 96 return Css.getTransform(el.style).indexOf(TRANSLATE_3D, 0) != -1; | 96 return el.style.transform.indexOf(TRANSLATE_3D, 0) != -1; |
| 97 } | 97 } |
| 98 | 98 |
| 99 /** | 99 /** |
| 100 * Translates [el], an HTML element that has a relative CSS | 100 * Translates [el], an HTML element that has a relative CSS |
| 101 * position, by setting its left and top CSS styles. | 101 * position, by setting its left and top CSS styles. |
| 102 */ | 102 */ |
| 103 static void setLeftAndTop(Element el, num x, num y) { | 103 static void setLeftAndTop(Element el, num x, num y) { |
| 104 final style = el.style; | 104 final style = el.style; |
| 105 Css.setLeft(style, '${x}px'); | 105 style.left = '${x}px'; |
| 106 Css.setTop(style, '${y}px'); | 106 style.top = '${y}px'; |
| 107 } | 107 } |
| 108 } | 108 } |
| 109 | 109 |
| 110 | 110 |
| 111 /** | 111 /** |
| 112 * Utilities for element styles. | 112 * Utilities for element styles. |
| 113 */ | 113 */ |
| 114 class StyleUtil { | 114 class StyleUtil { |
| 115 // TODO(jacobr): Need a good way to set this to -moz, or -o names. | 115 // TODO(jacobr): Need a good way to set this to -moz, or -o names. |
| 116 /** Style name for transform. */ | 116 /** Style name for transform. */ |
| 117 static final TRANSFORM_STYLE = '-webkit-transform'; | 117 static final TRANSFORM_STYLE = '-webkit-transform'; |
| 118 | 118 |
| 119 /** | 119 /** |
| 120 * Retrieves a computed style value of a node. | 120 * Retrieves a computed style value of a node. |
| 121 * | 121 * |
| 122 * Limitations: | 122 * Limitations: |
| 123 * - Does not report border styles correctly in Webkit. | 123 * - Does not report border styles correctly in Webkit. |
| 124 */ | 124 */ |
| 125 static Css getComputedStyle(Element element) { | 125 static CSSStyleDeclaration getComputedStyle(Element element) { |
| 126 // TODO(jmesserly): last param should be null, see b/5045788 | 126 // TODO(jmesserly): last param should be null, see b/5045788 |
| 127 return new Css(window.getComputedStyle(element, '')); | 127 return window.getComputedStyle(element, ''); |
| 128 } | 128 } |
| 129 | 129 |
| 130 /** Retrieves the current transform of an element. */ | 130 /** Retrieves the current transform of an element. */ |
| 131 static CSSMatrix getCurrentTransformMatrix(Element element) { | 131 static CSSMatrix getCurrentTransformMatrix(Element element) { |
| 132 // TODO(jacobr): when poossible switch back to | 132 // TODO(jacobr): when poossible switch back to |
| 133 // return new CSSMatrix(transform); | 133 // return new CSSMatrix(transform); |
| 134 return new CSSMatrix( | 134 return new CSSMatrix( |
| 135 getComputedStyle(element).transform); | 135 getComputedStyle(element).transform); |
| 136 } | 136 } |
| 137 } | 137 } |
| 138 | 138 |
| 139 class TransitionTimingFunction { | 139 class TransitionTimingFunction { |
| 140 static final EASE_IN = 'ease-in'; | 140 static final EASE_IN = 'ease-in'; |
| 141 static final EASE_OUT = 'ease-out'; | 141 static final EASE_OUT = 'ease-out'; |
| 142 static final EASE_IN_OUT = 'ease-in-out'; | 142 static final EASE_IN_OUT = 'ease-in-out'; |
| 143 } | 143 } |
| OLD | NEW |