import Scroller , { ScrollerMethods } from "./Scroller" ; export interface Animation { ( time , start , change , duration ) : number ; } export interface TimeOptions { /**
     * Time increments
     */ increment : number ; /**
     * Duration of the scrolling
     */ duration : number ; } interface Options { checkParent : boolean ; class : string | undefined ; animation : Animation ; time : TimeOptions ; } interface AnchorScrollerMethods { scroll ( to ) : void ; destroy ( ) : void ; } function AnchorScroller ( userOptions ? ) { const defaultAnimation = ( time , start , change , duration ) => { return ( time /= duration / 2 ) < 1 ? change / 2 * time * time * time + start : change / 2 * ( ( time -= 2 ) * time * time + 2 ) + start ; } ; let options = { checkParent : false , class : undefined , animation : defaultAnimation , time : { increment : 25 , duration : 1500 , } , ... userOptions , } ; const scroller = Scroller ( options . animation , options . time ) ; document . addEventListener ( "click" , check ) ; function check ( event ) { if ( ( < any > event ) . target . nodeName === "A" ) { checkElement ( ( < any > event ) . target , event ) ; } else if ( options . checkParent && ( < any > event ) . target . parentNode && ( < any > event ) . target . parentNode . nodeName === "A" ) { checkElement ( ( < any > event ) . target . parentNode , event ) ; } } function checkElement ( element , event ) { if ( options . class && ! element . classList . contains ( options . class ) ) return ; const hash = element . hash ; if ( ! hash ) return ; const anchor = document . getElementById ( hash . slice ( 1 ) ) ; if ( ! anchor ) return ; event . preventDefault ( ) ; if ( window . scrollY !== anchor . offsetTop ) { scrollTo ( anchor . offsetTop ) ; } } function scrollTo ( to ) { scroller . scrollTo ( to ) ; } function destroy ( ) { document . removeEventListener ( "click" , check ) ; } return { scroll : scrollTo , destroy , } ; } export default AnchorScroller ;