| 1 |
// Copyright 2011 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Utility methods to deal with CSS3 transitions |
| 17 |
* programmatically. |
| 18 |
* @author [email protected] (Chris Henry) |
| 19 |
*/ |
| 20 |
|
| 21 |
goog.provide('goog.style.transition'); |
| 22 |
goog.provide('goog.style.transition.Css3Property'); |
| 23 |
|
| 24 |
goog.require('goog.array'); |
| 25 |
goog.require('goog.asserts'); |
| 26 |
goog.require('goog.dom'); |
| 27 |
goog.require('goog.dom.TagName'); |
| 28 |
goog.require('goog.dom.safe'); |
| 29 |
goog.require('goog.dom.vendor'); |
| 30 |
goog.require('goog.functions'); |
| 31 |
goog.require('goog.html.SafeHtml'); |
| 32 |
goog.require('goog.style'); |
| 33 |
goog.require('goog.userAgent'); |
| 34 |
|
| 35 |
|
| 36 |
/** |
| 37 |
* A typedef to represent a CSS3 transition property. Duration and delay |
| 38 |
* are both in seconds. Timing is CSS3 timing function string, such as |
| 39 |
* 'easein', 'linear'. |
| 40 |
* |
| 41 |
* Alternatively, specifying string in the form of '[property] [duration] |
| 42 |
* [timing] [delay]' as specified in CSS3 transition is fine too. |
| 43 |
* |
| 44 |
* @typedef { { |
| 45 |
* property: string, |
| 46 |
* duration: number, |
| 47 |
* timing: string, |
| 48 |
* delay: number |
| 49 |
* } | string } |
| 50 |
*/ |
| 51 |
goog.style.transition.Css3Property; |
| 52 |
|
| 53 |
|
| 54 |
/** |
| 55 |
* Sets the element CSS3 transition to properties. |
| 56 |
* @param {Element} element The element to set transition on. |
| 57 |
* @param {goog.style.transition.Css3Property| |
| 58 |
* Array<goog.style.transition.Css3Property>} properties A single CSS3 |
| 59 |
* transition property or array of properties. |
| 60 |
*/ |
| 61 |
goog.style.transition.set = function(element, properties) { |
| 62 |
if (!goog.isArray(properties)) { |
| 63 |
properties = [properties]; |
| 64 |
} |
| 65 |
goog.asserts.assert( |
| 66 |
properties.length > 0, 'At least one Css3Property should be specified.'); |
| 67 |
|
| 68 |
var values = goog.array.map(properties, function(p) { |
| 69 |
if (goog.isString(p)) { |
| 70 |
return p; |
| 71 |
} else { |
| 72 |
goog.asserts.assertObject(p, 'Expected css3 property to be an object.'); |
| 73 |
var propString = |
| 74 |
p.property + ' ' + p.duration + 's ' + p.timing + ' ' + p.delay + 's'; |
| 75 |
goog.asserts.assert( |
| 76 |
p.property && goog.isNumber(p.duration) && p.timing && |
| 77 |
goog.isNumber(p.delay), |
| 78 |
'Unexpected css3 property value: %s', propString); |
| 79 |
return propString; |
| 80 |
} |
| 81 |
}); |
| 82 |
goog.style.transition.setPropertyValue_(element, values.join(',')); |
| 83 |
}; |
| 84 |
|
| 85 |
|
| 86 |
/** |
| 87 |
* Removes any programmatically-added CSS3 transition in the given element. |
| 88 |
* @param {Element} element The element to remove transition from. |
| 89 |
*/ |
| 90 |
goog.style.transition.removeAll = function(element) { |
| 91 |
goog.style.transition.setPropertyValue_(element, ''); |
| 92 |
}; |
| 93 |
|
| 94 |
|
| 95 |
/** |
| 96 |
* @return {boolean} Whether CSS3 transition is supported. |
| 97 |
*/ |
| 98 |
goog.style.transition.isSupported = goog.functions.cacheReturnValue(function() { |
| 99 |
// Since IE would allow any attribute, we need to explicitly check the |
| 100 |
// browser version here instead. |
| 101 |
if (goog.userAgent.IE) { |
| 102 |
return goog.userAgent.isVersionOrHigher('10.0'); |
| 103 |
} |
| 104 |
|
| 105 |
// We create a test element with style=-vendor-transition |
| 106 |
// We then detect whether those style properties are recognized and |
| 107 |
// available from js. |
| 108 |
var el = goog.dom.createElement(goog.dom.TagName.DIV); |
| 109 |
var transition = 'opacity 1s linear'; |
| 110 |
var vendorPrefix = goog.dom.vendor.getVendorPrefix(); |
| 111 |
var style = {'transition': transition}; |
| 112 |
if (vendorPrefix) { |
| 113 |
style[vendorPrefix + '-transition'] = transition; |
| 114 |
} |
| 115 |
goog.dom.safe.setInnerHtml( |
| 116 |
el, goog.html.SafeHtml.create('div', {'style': style})); |
| 117 |
|
| 118 |
var testElement = /** @type {Element} */ (el.firstChild); |
| 119 |
goog.asserts.assert(testElement.nodeType == Node.ELEMENT_NODE); |
| 120 |
|
| 121 |
return goog.style.getStyle(testElement, 'transition') != ''; |
| 122 |
}); |
| 123 |
|
| 124 |
|
| 125 |
/** |
| 126 |
* Sets CSS3 transition property value to the given value. |
| 127 |
* @param {Element} element The element to set transition on. |
| 128 |
* @param {string} transitionValue The CSS3 transition property value. |
| 129 |
* @private |
| 130 |
*/ |
| 131 |
goog.style.transition.setPropertyValue_ = function(element, transitionValue) { |
| 132 |
goog.style.setStyle(element, 'transition', transitionValue); |
| 133 |
}; |