import { ElementRef } from '@angular/core' ; import { ContainerRef , IPositionElements , IPositionStats , IResolver } from '../models' ; import { AxisResolver } from './axis-resolver' ; export function createResolver ( { windowElement , axis } ) { return createResolverWithContainer ( { axis , isWindow : isElementWindow ( windowElement ) } , windowElement ) ; } export function createResolverWithContainer ( resolver , windowElement ) { const container = resolver . isWindow || ( windowElement && ! windowElement . nativeElement ) ? windowElement : windowElement . nativeElement ; return { ... resolver , container } ; } export function isElementWindow ( windowElement ) { const isWindow = [ 'Window' , 'global' ] . some ( ( obj ) => Object . prototype . toString . call ( windowElement ) . includes ( obj ) ) ; return isWindow ; } export function getDocumentElement ( isContainerWindow , windowElement ) { return isContainerWindow ? windowElement . document . documentElement : null ; } export function calculatePoints ( element , resolver ) { const height = extractHeightForElement ( resolver ) ; return resolver . isWindow ? calculatePointsForWindow ( height , element , resolver ) : calculatePointsForElement ( height , element , resolver ) ; } export function calculatePointsForWindow ( height , element , resolver ) { const { axis , container , isWindow } = resolver ; const { offsetHeightKey , clientHeightKey } = extractHeightPropKeys ( axis ) ; const scrolled = height + getElementPageYOffset ( getDocumentElement ( isWindow , container ) , axis , isWindow ) ; const nativeElementHeight = getElementHeight ( element . nativeElement , isWindow , offsetHeightKey , clientHeightKey ) ; const totalToScroll = getElementOffsetTop ( element . nativeElement , axis , isWindow ) + nativeElementHeight ; return { height , scrolled , totalToScroll , isWindow } ; } export function calculatePointsForElement ( height , element , resolver ) { const { axis , container } = resolver ; const scrolled = container [ axis . scrollTopKey ( ) ] ; const totalToScroll = container [ axis . scrollHeightKey ( ) ] ; return { height , scrolled , totalToScroll , isWindow : false } ; } export function extractHeightPropKeys ( axis ) { return { offsetHeightKey : axis . offsetHeightKey ( ) , clientHeightKey : axis . clientHeightKey ( ) } ; } export function extractHeightForElement ( { container , isWindow , axis } ) { const { offsetHeightKey , clientHeightKey } = extractHeightPropKeys ( axis ) ; return getElementHeight ( container , isWindow , offsetHeightKey , clientHeightKey ) ; } export function getElementHeight ( elem , isWindow , offsetHeightKey , clientHeightKey ) { if ( isNaN ( elem [ offsetHeightKey ] ) ) { const docElem = getDocumentElement ( isWindow , elem ) ; return docElem ? docElem [ clientHeightKey ] : 0 ; } else { return elem [ offsetHeightKey ] ; } } export function getElementOffsetTop ( elem , axis , isWindow ) { const topKey = axis . topKey ( ) ; if ( ! elem . getBoundingClientRect ) { return ; } return ( elem . getBoundingClientRect ( ) [ topKey ] + getElementPageYOffset ( elem , axis , isWindow ) ) ; } export function getElementPageYOffset ( elem , axis , isWindow ) { const pageYOffset = axis . pageYOffsetKey ( ) ; const scrollTop = axis . scrollTopKey ( ) ; const offsetTop = axis . offsetTopKey ( ) ; if ( isNaN ( window . pageYOffset ) ) { return getDocumentElement ( isWindow , elem ) [ scrollTop ] ; } else if ( elem . ownerDocument ) { return elem . ownerDocument . defaultView [ pageYOffset ] ; } else { return elem [ offsetTop ] ; } }