import TWEEN from '@tweenjs/tween.js' ; export function getScrollTop ( container ? ) { if ( container ) { return container . scrollTop ; } return document . documentElement . scrollTop || window . pageYOffset || document . body . scrollTop ; } export function getScrollHeight ( container ? ) { if ( container ) { return container . scrollHeight ; } return Math . max ( document . documentElement . scrollHeight , document . body . scrollHeight ) ; } export type SetScrollTopOptions = { container ? : any ; animate ? : boolean ; duration ? : number ; complete ? : ( ) => void ; } ; export function setScrollTop ( scrollTop , options = { } ) { const container = options . container ; const animate = options . animate ; const set = ( top ) => { if ( container ) { container . scrollTop = top ; } else { document . documentElement . scrollTop = document . body . scrollTop = top ; } } ; if ( ! animate ) { set ( scrollTop ) ; } else { const duration = options . duration || 500 ; const complete = options . complete ; const top = getScrollTop ( container ) ; const coords = { scrollTop : top } ; const tween = new TWEEN . Tween ( coords ) . to ( { scrollTop } , duration ) . easing ( TWEEN . Easing . Cubic . Out ) . start ( ) . onUpdate ( ( ) => { set ( coords . scrollTop ) ; } ) ; const run = ( ) => { const id = requestAnimationFrame ( run ) ; const res = tween . update ( ) ; if ( ! res ) { complete && complete ( ) ; cancelAnimationFrame ( id ) ; } } ; run ( ) ; } } export function getOffsetTop ( node ) { let t = node . offsetTop ; while ( node . offsetParent ) { node = node . offsetParent ; t += node . offsetTop ; } return t ; } export const $ = { hasClass ( el , className ) { return new RegExp ( '(\\s|^)' + className + '(\\s|$)' ) . test ( el . className ) ; } , addClass ( el , className ) { if ( ! el ) { return ; } if ( el . classList ) { el . classList . add ( className ) ; } else if ( ! this . hasClass ( el , className ) ) { el . className += '' + className ; } } , removeClass ( el , className ) { if ( ! el ) { return ; } if ( el . classList ) { el . classList . remove ( className ) ; } else if ( this . hasClass ( el , className ) ) { el . className = el . className . replace ( new RegExp ( '(\\s|^)' + className + '(\\s|$)' ) , ' ' ) . replace ( /^\s+|\s+$/g , '' ) ; } } } ;