import { getPrefixSetting , getPrefix } from './prefix' ; import { parseAttributes , parseValues } from './parse' ; import observer from './observer' ; import bind from './bind' ; import { TgElement , Trigger as TriggerType } from './type' ; let activeElements = [ ] ; let ob = null ; getPrefixSetting ( ) ; private function observeElements ( ) { ob = observer ( ( entries ) => { entries . forEach ( ( entry ) => { let { target } = entry ; if ( entry . isIntersecting ) { activeElements . push ( parseAttributes ( target as HTMLElement ) ) ; } else { activeElements = activeElements . filter ( function ( obj ) { return obj . el !== target ; } ) ; } } ) ; } ) ; } private function eventListeners ( ) { window . addEventListener ( 'DOMContentLoaded' , ( ) => { bind ( ob ) ; setTimeout ( ( ) => { let allElements = [ ... document . querySelectorAll ( `[${ getPrefix ( ) }name]` ) , ] . map ( ( element ) => { return parseAttributes ( element as HTMLElement ) ; } ) ; parseValues ( allElements ) ; } ) ; } ) ; window . addEventListener ( 'resize' , ( ) => { bind ( ob , { before : ( ) => { activeElements . forEach ( ( element ) => { ob ?. unobserve ( element . el ) ; } ) ; activeElements = [ ] ; } , } ) ; } ) ; window . addEventListener ( 'scroll' , ( e ) => { parseValues ( activeElements ) ; } ) ; } const Trigger = { start ( ) { if ( ! document . body ) { console . warn ( `Unable to initialise, document.body does not exist.` ) ; return ; } observeElements ( ) ; eventListeners ( ) ; } , } ; export default Trigger ;