Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
feat(animation): Annotate for closure compiler
Browse files Browse the repository at this point in the history
Resolves #332
  • Loading branch information
traviskaufman committed Apr 20, 2017
1 parent 079da92 commit dac4814
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 33 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,7 @@
"path": "./node_modules/cz-conventional-changelog"
}
},
"closureWhitelist": []
"closureWhitelist": [
"mdc-animation"
]
}
86 changes: 54 additions & 32 deletions packages/mdc-animation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,72 @@
* limitations under the License.
*/

/**
* @typedef {{
* noPrefix: string,
* webkitPrefix: string
* }}
*/
let VendorPropertyMapType;

/** @const {Object<string, !VendorPropertyMapType>} */
const eventTypeMap = {
animationstart: {
'animationstart': {
noPrefix: 'animationstart',
webkitPrefix: 'webkitAnimationStart',
},
animationend: {
'animationend': {
noPrefix: 'animationend',
webkitPrefix: 'webkitAnimationEnd',
},
animationiteration: {
'animationiteration': {
noPrefix: 'animationiteration',
webkitPrefix: 'webkitAnimationIteration',
},
transitionend: {
'transitionend': {
noPrefix: 'transitionend',
webkitPrefix: 'webkitTransitionEnd',
},
};

/** @const {Object<string, !VendorPropertyMapType>} */
const cssPropertyMap = {
animation: {
'animation': {
noPrefix: 'animation',
webkitPrefix: '-webkit-animation',
},
transform: {
'transform': {
noPrefix: 'transform',
webkitPrefix: '-webkit-transform',
},
transition: {
'transition': {
noPrefix: 'transition',
webkitPrefix: '-webkit-transition',
},
};

/**
* @param {!Object} windowObj
* @return {boolean}
*/
function hasProperShape(windowObj) {
return (windowObj.document !== undefined && typeof windowObj.document.createElement === 'function');
return (windowObj['document'] !== undefined && typeof windowObj['document']['createElement'] === 'function');
}

/**
* @param {string} eventType
* @return {boolean}
*/
function eventFoundInMaps(eventType) {
return (eventType in eventTypeMap || eventType in cssPropertyMap);
}

// If 'animation' or 'transition' exist as style property, webkit prefix isn't necessary. Since we are unable to
// see the event types on the element, we must rely on the corresponding style properties.
/**
* @param {string} eventType
* @param {!Object<string, !VendorPropertyMapType>} map
* @param {!Element} el
* @return {string}
*/
function getJavaScriptEventName(eventType, map, el) {
switch (eventType) {
case 'animationstart':
Expand All @@ -71,24 +93,22 @@ function getJavaScriptEventName(eventType, map, el) {
}
}

// Helper function to determine browser prefix for CSS3 animation events
// and property names
//
// Parameters:
// windowObject: Object -- Contains Document with a `createElement()` method
// eventType: string -- The type of animation
//
// returns the value of the event as a string, prefixed if necessary.
// If proper arguments are not supplied, this function will return
// the property or event type without webkit prefix.
//
/**
* Helper function to determine browser prefix for CSS3 animation events
* and property names.
* @param {!Object} windowObj
* @param {string} eventType
* @return {string}
*/
function getAnimationName(windowObj, eventType) {
if (!hasProperShape(windowObj) || !eventFoundInMaps(eventType)) {
return eventType;
}

const map = eventType in eventTypeMap ? eventTypeMap : cssPropertyMap;
const el = windowObj.document.createElement('div');
const map = /** @type {!Object<string, !VendorPropertyMapType>} */ (
eventType in eventTypeMap ? eventTypeMap : cssPropertyMap
);
const el = windowObj['document']['createElement']('div');
let eventName = '';

if (map === eventTypeMap) {
Expand All @@ -102,19 +122,21 @@ function getAnimationName(windowObj, eventType) {

// Public functions to access getAnimationName() for JavaScript events or CSS
// property names.
//
// Parameters:
// windowObject: Object -- Contains Document with a `createElement()` method
// eventType: string -- The type of animation
//
// returns the value of the event as a string, prefixed if necessary.
// If proper arguments are not supplied, this function will return
// the property or event type without webkit prefix.
//

/**
* @param {!Object} windowObj
* @param {string} eventType
* @return {string}
*/
export function getCorrectEventName(windowObj, eventType) {
return getAnimationName(windowObj, eventType);
}

/**
* @param {!Object} windowObj
* @param {string} eventType
* @return {string}
*/
export function getCorrectPropertyName(windowObj, eventType) {
return getAnimationName(windowObj, eventType);
}

0 comments on commit dac4814

Please sign in to comment.