|
|
import './style.scss'; |
|
|
|
|
|
( function ( $, document ) { |
|
|
const $document = $( document ); |
|
|
|
|
|
$( function () { |
|
|
|
|
|
const $menu = $( '.x-menu' ); |
|
|
const $menuContent = $menu.find( '.x-menu-content' ); |
|
|
const $menuButton = $menu.find( '.x-menu-button' ); |
|
|
|
|
|
|
|
|
const $menuTrigger = $( '.x-nav-link--menu' ); |
|
|
|
|
|
|
|
|
const $menuContentItems = $menuContent.find( '[role=menuitem]:visible' ); |
|
|
const menuContentItemLength = $menuContentItems.length; |
|
|
|
|
|
|
|
|
let widgetActive = false; |
|
|
|
|
|
|
|
|
let currentState = false; |
|
|
let currentKeyboard = false; |
|
|
let currentKeyboardIndex = false; |
|
|
|
|
|
function activateWidget( delay ) { |
|
|
setTimeout( function () { |
|
|
if ( widgetActive ) { |
|
|
return; |
|
|
} |
|
|
$menu.addClass( 'x-menu--active' ); |
|
|
widgetActive = true; |
|
|
}, delay || 0 ); |
|
|
} |
|
|
|
|
|
function moveItemIndex( increment ) { |
|
|
let index = 0; |
|
|
|
|
|
if ( $.isNumeric( currentKeyboardIndex ) ) { |
|
|
index = currentKeyboardIndex; |
|
|
} else if ( increment > 0 ) { |
|
|
index = -1; |
|
|
} |
|
|
|
|
|
index = ( index + increment + menuContentItemLength ) % menuContentItemLength; |
|
|
const element = $menuContentItems[ index ]; |
|
|
|
|
|
if ( element ) { |
|
|
element.focus(); |
|
|
} |
|
|
currentKeyboardIndex = index; |
|
|
} |
|
|
|
|
|
function captureArrows() { |
|
|
if ( currentKeyboard ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
currentKeyboard = true; |
|
|
currentKeyboardIndex = false; |
|
|
|
|
|
$document.on( 'keydown.x-menu', function ( $event ) { |
|
|
const triggerActive = $menuTrigger.get( 0 ) === document.activeElement; |
|
|
|
|
|
|
|
|
switch ( $event.which ) { |
|
|
case 38: |
|
|
case 37: |
|
|
$event.preventDefault(); |
|
|
moveItemIndex( -1 ); |
|
|
break; |
|
|
case 40: |
|
|
case 39: |
|
|
$event.preventDefault(); |
|
|
moveItemIndex( 1 ); |
|
|
break; |
|
|
case 27: |
|
|
if ( ! triggerActive ) { |
|
|
$menuTrigger.trigger( 'focus.x-menu' ); |
|
|
} |
|
|
setCurrentState( false ); |
|
|
break; |
|
|
case 9: |
|
|
if ( ! triggerActive ) { |
|
|
$event.preventDefault(); |
|
|
$menuTrigger.trigger( 'focus.x-menu' ); |
|
|
} |
|
|
setCurrentState( false ); |
|
|
break; |
|
|
|
|
|
} |
|
|
} ); |
|
|
} |
|
|
|
|
|
function releaseArrows() { |
|
|
currentKeyboard = false; |
|
|
$document.off( 'keydown.x-menu' ); |
|
|
} |
|
|
|
|
|
function setCurrentState( state ) { |
|
|
if ( ! widgetActive || state === currentState ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
currentState = state; |
|
|
|
|
|
if ( state ) { |
|
|
captureArrows(); |
|
|
} else { |
|
|
releaseArrows(); |
|
|
} |
|
|
|
|
|
$menuTrigger.xAria( 'expanded', state ); |
|
|
$menu.xAria( 'hidden', ! state ); |
|
|
$menu.toggleClass( 'x-menu--open', state ); |
|
|
} |
|
|
|
|
|
activateWidget(); |
|
|
|
|
|
|
|
|
$menuTrigger.on( 'click.x-menu', function ( $event ) { |
|
|
if ( ! widgetActive ) { |
|
|
return; |
|
|
} |
|
|
$event.stopPropagation(); |
|
|
$menuTrigger.blur(); |
|
|
setCurrentState( true ); |
|
|
} ); |
|
|
|
|
|
$menuButton.on( 'click.x-menu', function () { |
|
|
if ( ! widgetActive ) { |
|
|
return; |
|
|
} |
|
|
$menuButton.blur(); |
|
|
setCurrentState( false ); |
|
|
} ); |
|
|
|
|
|
$menuContent.on( 'click.x-menu touchstart.x-menu', function ( $event ) { |
|
|
if ( ! widgetActive ) { |
|
|
return; |
|
|
} |
|
|
$event.stopPropagation(); |
|
|
} ); |
|
|
|
|
|
|
|
|
$document.on( 'click.x-menu touchstart.x-menu', function () { |
|
|
if ( ! widgetActive ) { |
|
|
return; |
|
|
} |
|
|
setCurrentState( false ); |
|
|
} ); |
|
|
} ); |
|
|
} )( window.jQuery, document ); |
|
|
|
|
|
( function ( $, window, document ) { |
|
|
const $window = $( window ); |
|
|
const $document = $( document ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$.fn.xToArray = function () { |
|
|
return this.toArray().map( function ( element ) { |
|
|
return $( element ); |
|
|
} ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$.fn.xAria = function ( name, value ) { |
|
|
value = Boolean( value ); |
|
|
this.attr( 'aria-' + name, value ); |
|
|
|
|
|
if ( name === 'hidden' && value ) { |
|
|
|
|
|
this.find( '[tabindex], button, input, object, select, textarea' ).each( |
|
|
function ( _, element ) { |
|
|
element.blur(); |
|
|
} |
|
|
); |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$.fn.xGroupByData = function ( key ) { |
|
|
const group = {}; |
|
|
let data; |
|
|
|
|
|
this.xToArray().forEach( function ( $element ) { |
|
|
data = $element.data( key ); |
|
|
if ( data ) { |
|
|
group[ data ] = $element; |
|
|
} |
|
|
} ); |
|
|
|
|
|
return group; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$.xCloneObject = function ( object ) { |
|
|
return $.extend( {}, object ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$.fn.xOpacity = function ( value ) { |
|
|
return this.each( function () { |
|
|
this.style.opacity = value; |
|
|
} ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$.xMax = function ( items ) { |
|
|
if ( $.isPlainObject( items ) ) { |
|
|
items = $.xObjectValues( items ); |
|
|
} |
|
|
|
|
|
return items.reduce( function ( memo, value ) { |
|
|
return Math.max( memo, value ); |
|
|
}, 0 ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$.fn.xTransform = function ( x, y, z, scale ) { |
|
|
const coordinates = [ x, y, z ].map( function ( value ) { |
|
|
return value ? value + 'px' : 0; |
|
|
} ); |
|
|
|
|
|
let value = 'translate3d(' + coordinates.join( ', ' ) + ')'; |
|
|
if ( $.isNumeric( scale ) ) { |
|
|
value += ' scale(' + scale + ')'; |
|
|
} |
|
|
|
|
|
return this.each( function () { |
|
|
this.style.transform = value; |
|
|
} ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$.xFlattenObject = function xFlattenObject( object ) { |
|
|
const result = {}; |
|
|
|
|
|
$.each( object, function ( key, value ) { |
|
|
if ( ! $.isPlainObject( value ) ) { |
|
|
result[ key ] = value; |
|
|
return; |
|
|
} |
|
|
|
|
|
$.each( xFlattenObject( value ), function ( _key, _value ) { |
|
|
result[ key + '.' + _key ] = _value; |
|
|
} ); |
|
|
} ); |
|
|
|
|
|
return result; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$.xMapPlainObject = function ( items, callback ) { |
|
|
const isValue = ! $.isFunction( callback ); |
|
|
|
|
|
return Object.keys( items ).reduce( function ( memo, key ) { |
|
|
const copy = {}; |
|
|
copy[ key ] = isValue ? callback : callback( key, items[ key ], items ); |
|
|
return $.extend( {}, memo, copy ); |
|
|
}, {} ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$.xObjectValues = function ( object ) { |
|
|
return Object.keys( object ).map( function ( key ) { |
|
|
return object[ key ]; |
|
|
} ); |
|
|
}; |
|
|
|
|
|
$( function () { |
|
|
|
|
|
const $dropdown = $( '.x-dropdown' ); |
|
|
|
|
|
|
|
|
const $dropdownBackground = $dropdown.find( '.x-dropdown-bottom' ); |
|
|
const $dropdownBackgroundFill = $dropdownBackground.find( '.x-dropdown-bottom-fill' ); |
|
|
|
|
|
|
|
|
const $dropdownContents = $dropdown.find( '.x-dropdown-content' ); |
|
|
const dropdownContents = $dropdownContents.xGroupByData( 'dropdown-name' ); |
|
|
const dropdownContentKeys = Object.keys( dropdownContents ); |
|
|
|
|
|
|
|
|
const dropdownContentItems = $.xMapPlainObject( dropdownContents, function ( _, $element ) { |
|
|
return $element.find( '[role=menuitem]:visible' ); |
|
|
} ); |
|
|
|
|
|
|
|
|
const $dropdownTriggers = $( '[data-dropdown-trigger]' ); |
|
|
const dropdownTriggers = $dropdownTriggers.xGroupByData( 'dropdown-trigger' ); |
|
|
|
|
|
|
|
|
const dropdownHiddenOffset = -6; |
|
|
const dropdownHiddenScale = 0.85; |
|
|
|
|
|
|
|
|
let dropdownWidth = 0; |
|
|
let dropdownBackgroundHeight = 0; |
|
|
let dropdownContentHeights = {}; |
|
|
let dropdownContentOffsets = {}; |
|
|
|
|
|
function resizeElements() { |
|
|
|
|
|
dropdownWidth = $dropdown.outerWidth(); |
|
|
dropdownContentHeights = $.xMapPlainObject( dropdownContents, function ( _, $element ) { |
|
|
return $element.outerHeight(); |
|
|
} ); |
|
|
dropdownBackgroundHeight = $.xMax( dropdownContentHeights ); |
|
|
dropdownContentOffsets = $.xMapPlainObject( dropdownTriggers, function ( _, $element ) { |
|
|
const width = $element.outerWidth(); |
|
|
const offset = $element.offset(); |
|
|
return Math.round( offset.left + ( width - dropdownWidth ) / 2 ); |
|
|
} ); |
|
|
|
|
|
|
|
|
$dropdownBackground.height( dropdownBackgroundHeight ); |
|
|
$dropdownBackgroundFill.height( dropdownBackgroundHeight ); |
|
|
} |
|
|
|
|
|
|
|
|
let currentName; |
|
|
let currentDropdownOpacity; |
|
|
const currentDropdownContentOpacity = $.xMapPlainObject( dropdownContents, 0 ); |
|
|
let currentDropdownTransform = {}; |
|
|
let currentDropdownHeight; |
|
|
let currentAnimation; |
|
|
let currentKeyboardIndex; |
|
|
|
|
|
function setCurrentName( name ) { |
|
|
currentName = name; |
|
|
currentKeyboardIndex = false; |
|
|
$.each( dropdownContents, function ( key, $element ) { |
|
|
$element.xAria( 'hidden', key !== name ); |
|
|
} ); |
|
|
} |
|
|
|
|
|
function broadcastEvent( type, name, data ) { |
|
|
data = $.extend( data || {}, { name: name } ); |
|
|
$document.trigger( 'x-dropdown.' + type, data ); |
|
|
} |
|
|
|
|
|
|
|
|
function updateDropdownOpacity( value, forceUpdate ) { |
|
|
if ( ! forceUpdate && value === currentDropdownOpacity ) { |
|
|
return; |
|
|
} |
|
|
$dropdown.xOpacity( value ); |
|
|
currentDropdownOpacity = value; |
|
|
} |
|
|
|
|
|
function updateDropdownContentOpacity( values, forceUpdate ) { |
|
|
dropdownContentKeys.forEach( function ( key ) { |
|
|
const value = values[ 'contents.opacity.' + key ]; |
|
|
if ( ! forceUpdate && value === currentDropdownContentOpacity[ key ] ) { |
|
|
return; |
|
|
} |
|
|
dropdownContents[ key ].xOpacity( value ); |
|
|
currentDropdownContentOpacity[ key ] = value; |
|
|
} ); |
|
|
} |
|
|
|
|
|
function updateDropdownTransform( values, forceUpdate ) { |
|
|
const x = values.x || 0; |
|
|
const y = values.y || 0; |
|
|
const scale = values.scale; |
|
|
const current = currentDropdownTransform; |
|
|
|
|
|
if ( ! forceUpdate && current.x === x && current.y === y && current.scale === scale ) { |
|
|
return; |
|
|
} |
|
|
$dropdown.xTransform( x, y, 0, scale ); |
|
|
currentDropdownTransform = { x: x, y: y, scale: scale }; |
|
|
} |
|
|
|
|
|
function updateDropdownHeight( value, forceUpdate ) { |
|
|
value = Math.min( value, dropdownBackgroundHeight ); |
|
|
if ( ! forceUpdate && value === currentDropdownHeight ) { |
|
|
return; |
|
|
} |
|
|
$dropdownBackgroundFill.xTransform( 0, value - dropdownBackgroundHeight ); |
|
|
currentDropdownHeight = value; |
|
|
} |
|
|
|
|
|
function updateDropdownProperties( values, forceUpdate ) { |
|
|
updateDropdownOpacity( values[ 'dropdown.opacity' ], forceUpdate ); |
|
|
updateDropdownContentOpacity( values, forceUpdate ); |
|
|
updateDropdownTransform( |
|
|
{ |
|
|
x: values[ 'dropdown.transform.x' ], |
|
|
y: values[ 'dropdown.transform.y' ], |
|
|
scale: values[ 'dropdown.transform.scale' ], |
|
|
}, |
|
|
forceUpdate |
|
|
); |
|
|
updateDropdownHeight( values[ 'dropdown.height' ], forceUpdate ); |
|
|
} |
|
|
|
|
|
function show( name, animate ) { |
|
|
|
|
|
if ( dropdownContentKeys.indexOf( name ) < 0 ) { |
|
|
name = false; |
|
|
} |
|
|
|
|
|
|
|
|
if ( currentName === name ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
if ( currentAnimation ) { |
|
|
currentAnimation.stop(); |
|
|
} |
|
|
|
|
|
|
|
|
const isHidden = ! currentName && ! ( currentAnimation && currentAnimation.running ); |
|
|
const isHiding = ! name; |
|
|
|
|
|
|
|
|
const animationFrom = { |
|
|
dropdown: isHidden |
|
|
? { |
|
|
height: dropdownContentHeights[ name ], |
|
|
opacity: currentDropdownOpacity, |
|
|
transform: { |
|
|
x: dropdownContentOffsets[ name ], |
|
|
y: dropdownHiddenOffset, |
|
|
scale: dropdownHiddenScale, |
|
|
}, |
|
|
} |
|
|
: { |
|
|
height: currentDropdownHeight, |
|
|
opacity: currentDropdownOpacity, |
|
|
transform: currentDropdownTransform, |
|
|
}, |
|
|
contents: { |
|
|
opacity: isHidden |
|
|
? $.xMapPlainObject( currentDropdownContentOpacity, 0 ) |
|
|
: currentDropdownContentOpacity, |
|
|
}, |
|
|
}; |
|
|
|
|
|
const animationTo = { |
|
|
dropdown: isHiding |
|
|
? { |
|
|
height: currentDropdownHeight, |
|
|
opacity: 0, |
|
|
transform: { |
|
|
x: currentDropdownTransform.x, |
|
|
y: dropdownHiddenOffset, |
|
|
scale: dropdownHiddenScale, |
|
|
}, |
|
|
} |
|
|
: { |
|
|
height: dropdownContentHeights[ name ], |
|
|
opacity: 1, |
|
|
transform: { |
|
|
x: dropdownContentOffsets[ name ], |
|
|
y: 0, |
|
|
scale: 1, |
|
|
}, |
|
|
}, |
|
|
contents: { |
|
|
opacity: isHiding |
|
|
? currentDropdownContentOpacity |
|
|
: $.xMapPlainObject( currentDropdownContentOpacity, function ( key ) { |
|
|
return name === key ? 1 : 0; |
|
|
} ), |
|
|
}, |
|
|
}; |
|
|
|
|
|
const animationProperties = { |
|
|
from: $.xFlattenObject( animationFrom ), |
|
|
to: $.xFlattenObject( animationTo ), |
|
|
duration: isHiding ? 150 : 450, |
|
|
ease: isHiding ? 'quadraticOut' : 'quinticOut', |
|
|
}; |
|
|
|
|
|
setCurrentName( name ); |
|
|
|
|
|
if ( ! animate ) { |
|
|
updateDropdownProperties( animationProperties.to ); |
|
|
} else { |
|
|
currentAnimation = $.xTween( animationProperties ).start( { |
|
|
update: updateDropdownProperties, |
|
|
complete: function () { |
|
|
currentAnimation = false; |
|
|
}, |
|
|
} ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function handleResize() { |
|
|
show( false ); |
|
|
resizeElements(); |
|
|
} |
|
|
|
|
|
function handleLoad() { |
|
|
resizeElements(); |
|
|
} |
|
|
|
|
|
function handleScroll() { |
|
|
show( false, true ); |
|
|
} |
|
|
|
|
|
handleResize(); |
|
|
$window.on( 'resize', handleResize ); |
|
|
$window.on( 'load', handleLoad ); |
|
|
$window.on( 'scroll', handleScroll ); |
|
|
$window.on( 'x-detected.dynamic-type', handleResize ); |
|
|
|
|
|
|
|
|
$document.on( 'x-trigger.select', function ( _, data ) { |
|
|
show( data.name, true ); |
|
|
} ); |
|
|
|
|
|
$document.on( 'x-trigger.arrow-down', function () { |
|
|
if ( $.isNumeric( currentKeyboardIndex ) ) { |
|
|
currentKeyboardIndex += 1; |
|
|
} else { |
|
|
currentKeyboardIndex = 0; |
|
|
} |
|
|
} ); |
|
|
|
|
|
$document.on( 'x-trigger.arrow-up', function () { |
|
|
if ( $.isNumeric( currentKeyboardIndex ) ) { |
|
|
currentKeyboardIndex -= 1; |
|
|
} else { |
|
|
currentKeyboardIndex = -1; |
|
|
} |
|
|
} ); |
|
|
|
|
|
|
|
|
$document.on( 'x-trigger.arrow-up x-trigger.arrow-down', function () { |
|
|
const items = dropdownContentItems[ currentName ]; |
|
|
const length = items.length; |
|
|
const index = ( currentKeyboardIndex + length ) % length; |
|
|
const element = items[ index ]; |
|
|
if ( element ) { |
|
|
element.focus(); |
|
|
} |
|
|
currentKeyboardIndex = index; |
|
|
} ); |
|
|
|
|
|
|
|
|
$.each( dropdownContents, function ( key, $element ) { |
|
|
[ 'mouseenter', 'mouseleave' ].forEach( function ( type ) { |
|
|
$element.on( type, function () { |
|
|
broadcastEvent( type, key ); |
|
|
} ); |
|
|
} ); |
|
|
} ); |
|
|
} ); |
|
|
|
|
|
$( function () { |
|
|
|
|
|
const $triggers = $( '[data-dropdown-trigger]' ); |
|
|
const triggers = $triggers.xGroupByData( 'dropdown-trigger' ); |
|
|
|
|
|
|
|
|
let currentName; |
|
|
let currentTimeout; |
|
|
let currentKeyboard; |
|
|
|
|
|
function setCurrentName( name ) { |
|
|
currentName = name; |
|
|
$.each( triggers, function ( key, $element ) { |
|
|
$element.xAria( 'expanded', key === name ); |
|
|
} ); |
|
|
} |
|
|
|
|
|
function broadcastEvent( type, name, data ) { |
|
|
data = $.extend( data || {}, { name: name } ); |
|
|
$document.trigger( 'x-trigger.' + type, data ); |
|
|
} |
|
|
|
|
|
function captureArrows() { |
|
|
if ( currentKeyboard ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
currentKeyboard = true; |
|
|
|
|
|
$document.on( 'keydown.x-trigger', function ( $event ) { |
|
|
const $trigger = triggers[ currentName ]; |
|
|
const triggerActive = $trigger && $trigger.get( 0 ) === document.activeElement; |
|
|
|
|
|
|
|
|
switch ( $event.which ) { |
|
|
case 38: |
|
|
case 37: |
|
|
$event.preventDefault(); |
|
|
broadcastEvent( 'arrow-up', currentName ); |
|
|
break; |
|
|
case 40: |
|
|
case 39: |
|
|
$event.preventDefault(); |
|
|
broadcastEvent( 'arrow-down', currentName ); |
|
|
break; |
|
|
case 27: |
|
|
if ( ! triggerActive ) { |
|
|
$trigger.trigger( 'focus.x-trigger' ); |
|
|
} |
|
|
show( 0, false, { keyboard: true } ); |
|
|
break; |
|
|
case 9: |
|
|
if ( ! triggerActive ) { |
|
|
$event.preventDefault(); |
|
|
$trigger.trigger( 'focus.x-trigger' ); |
|
|
} |
|
|
show( 0, false, { keyboard: true } ); |
|
|
break; |
|
|
|
|
|
} |
|
|
} ); |
|
|
} |
|
|
|
|
|
function releaseArrows() { |
|
|
currentKeyboard = false; |
|
|
$document.off( 'keydown.x-trigger' ); |
|
|
} |
|
|
|
|
|
function show( delay, name, data ) { |
|
|
const callback = function () { |
|
|
setCurrentName( name ); |
|
|
broadcastEvent( 'select', name, data ); |
|
|
|
|
|
if ( name ) { |
|
|
captureArrows(); |
|
|
} else { |
|
|
releaseArrows(); |
|
|
} |
|
|
}; |
|
|
|
|
|
clearTimeout( currentTimeout ); |
|
|
currentTimeout = setTimeout( callback, delay ); |
|
|
} |
|
|
|
|
|
|
|
|
$.each( triggers, function ( key, $element ) { |
|
|
$element.on( 'click.x-trigger touchstart.x-trigger', function ( $event ) { |
|
|
$event.stopPropagation(); |
|
|
$event.target.blur(); |
|
|
show( 0, key ); |
|
|
} ); |
|
|
|
|
|
$element.on( 'focus.x-trigger', function () { |
|
|
show( 0, key, { keyboard: true } ); |
|
|
} ); |
|
|
|
|
|
$element.on( 'mouseenter.x-trigger', function () { |
|
|
show( currentName ? 0 : 150, key ); |
|
|
} ); |
|
|
|
|
|
$element.on( 'mouseleave.x-trigger', function () { |
|
|
show( 50, false ); |
|
|
} ); |
|
|
} ); |
|
|
|
|
|
|
|
|
$document.on( 'x-dropdown.mouseenter', function ( _, data ) { |
|
|
show( 0, data.name ); |
|
|
} ); |
|
|
|
|
|
$document.on( 'x-dropdown.mouseleave', function () { |
|
|
show( 350, false ); |
|
|
} ); |
|
|
|
|
|
|
|
|
$document.on( 'click.x-trigger', function () { |
|
|
show( 50, false ); |
|
|
} ); |
|
|
|
|
|
|
|
|
function handleResize() { |
|
|
show( 0, false ); |
|
|
} |
|
|
|
|
|
handleResize(); |
|
|
$window.on( 'resize', handleResize ); |
|
|
} ); |
|
|
} )( window.jQuery, window, document ); |
|
|
|
|
|
( function ( $ ) { |
|
|
const scheduleFrame = window.requestAnimationFrame; |
|
|
|
|
|
|
|
|
const EASING_FUNCTIONS = { |
|
|
linear: function ( k ) { |
|
|
return k; |
|
|
}, |
|
|
quadraticOut: function ( k ) { |
|
|
return k * ( 2 - k ); |
|
|
}, |
|
|
quinticOut: function ( k ) { |
|
|
|
|
|
return --k * k * k * k * k + 1; |
|
|
}, |
|
|
}; |
|
|
|
|
|
function Tween( properties ) { |
|
|
this.running = false; |
|
|
|
|
|
this._valuesFrom = $.xCloneObject( properties.from ); |
|
|
this._valuesTo = $.xCloneObject( properties.to ); |
|
|
|
|
|
this._duration = properties.duration; |
|
|
this._ease = EASING_FUNCTIONS[ properties.ease ] || EASING_FUNCTIONS.linear; |
|
|
|
|
|
return this; |
|
|
} |
|
|
|
|
|
Tween.prototype.start = function ( properties ) { |
|
|
this._onUpdateCallback = properties.update; |
|
|
this._onEndCallback = properties.end; |
|
|
|
|
|
if ( ! $.isFunction( this._onUpdateCallback ) ) { |
|
|
this._onUpdateCallback = $.noop; |
|
|
} |
|
|
if ( ! $.isFunction( this._onEndCallback ) ) { |
|
|
this._onEndCallback = $.noop; |
|
|
} |
|
|
|
|
|
this._startAnimation(); |
|
|
return this; |
|
|
}; |
|
|
|
|
|
Tween.prototype.stop = function () { |
|
|
if ( ! this.running ) { |
|
|
return this; |
|
|
} |
|
|
|
|
|
this._interrupted = true; |
|
|
this._endAnimation(); |
|
|
return this; |
|
|
}; |
|
|
|
|
|
Tween.prototype._startAnimation = function () { |
|
|
this.running = true; |
|
|
this._interrupted = false; |
|
|
this._start = performance.now(); |
|
|
scheduleFrame( this._runAnimation.bind( this ) ); |
|
|
}; |
|
|
|
|
|
Tween.prototype._runAnimation = function () { |
|
|
if ( ! this.running ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
this._nextFrame(); |
|
|
scheduleFrame( this._runAnimation.bind( this ) ); |
|
|
}; |
|
|
|
|
|
Tween.prototype._updateAnimation = function () { |
|
|
const currentValues = this._getInterpolatedValues(); |
|
|
this._onUpdateCallback( currentValues, this._progress ); |
|
|
}; |
|
|
|
|
|
Tween.prototype._endAnimation = function () { |
|
|
this.running = false; |
|
|
|
|
|
if ( ! this._interrupted ) { |
|
|
this._onUpdateCallback( $.xCloneObject( this._valuesTo ), 1 ); |
|
|
} |
|
|
|
|
|
this._onEndCallback(); |
|
|
}; |
|
|
|
|
|
Tween.prototype._nextFrame = function () { |
|
|
this._progress = ( performance.now() - this._start ) / this._duration; |
|
|
|
|
|
if ( this._progress < 1 ) { |
|
|
this._updateAnimation(); |
|
|
} else { |
|
|
this._endAnimation(); |
|
|
} |
|
|
}; |
|
|
|
|
|
Tween.prototype._getInterpolatedValues = function () { |
|
|
const values = {}; |
|
|
|
|
|
Object.keys( this._valuesFrom ).forEach( |
|
|
function ( key ) { |
|
|
let valueFrom = this._valuesFrom[ key ]; |
|
|
let valueTo = this._valuesTo[ key ]; |
|
|
|
|
|
if ( ! $.isNumeric( valueFrom ) ) { |
|
|
valueFrom = 0; |
|
|
} |
|
|
if ( ! $.isNumeric( valueTo ) ) { |
|
|
valueTo = 0; |
|
|
} |
|
|
|
|
|
values[ key ] = this._ease( this._progress ) * ( valueTo - valueFrom ) + valueFrom; |
|
|
}.bind( this ) |
|
|
); |
|
|
|
|
|
return values; |
|
|
}; |
|
|
|
|
|
|
|
|
$.xTween = function ( properties ) { |
|
|
return new Tween( properties ); |
|
|
}; |
|
|
} )( window.jQuery, window, document ); |
|
|
|