/**
 * Created by ananya.chandra on 20/09/18.
 */ import StickyObject , { StickyObjectProps , StickyType } from "./StickyObject" ; import BinarySearch , { ValueAndIndex } from "../../utils/BinarySearch" ; import { WindowCorrection } from "../ViewabilityTracker" ; export interface StickyFooterProps extends StickyObjectProps { alwaysStickyFooter ? : boolean ; } export default class StickyFooter < P extends StickyFooterProps > extends StickyObject < P > { constructor ( props , context ? ) { super ( props , context ) ; } public onScroll ( offsetY ) { const endCorrection = this . getWindowCorrection ( this . props ) . endCorrection ; if ( endCorrection ) { this . containerPosition = { bottom : endCorrection } ; offsetY -= endCorrection ; } super . onScroll ( offsetY ) ; } protected initStickyParams ( ) { this . stickyType = StickyType . FOOTER ; this . stickyTypeMultiplier = - 1 ; this . containerPosition = { bottom : this . getWindowCorrection ( this . props ) . endCorrection } ; this . bounceScrolling = false ; } protected calculateVisibleStickyIndex ( stickyIndices , _smallestVisibleIndex , largestVisibleIndex , offsetY , windowBound ? ) { if ( stickyIndices && largestVisibleIndex ) { this . bounceScrolling = this . hasReachedBoundary ( offsetY , windowBound ) ; if ( largestVisibleIndex > stickyIndices [ stickyIndices . length - 1 ] || this . bounceScrolling ) { this . stickyVisiblity = false ; if ( this . props . alwaysStickyFooter && offsetY === 0 ) { this . stickyVisiblity = true ; } } else { this . stickyVisiblity = true ; const valueAndIndex = BinarySearch . findValueLargerThanTarget ( stickyIndices , largestVisibleIndex ) ; if ( valueAndIndex ) { this . currentIndex = valueAndIndex . index ; this . currentStickyIndex = valueAndIndex . value ; } else { console . log ( "Footer sticky index calculation gone wrong." ) ; } } } } protected getNextYd ( nextY , nextHeight ) { return - 1 * ( nextY + nextHeight ) ; } protected getCurrentYd ( currentY , currentHeight ) { return - 1 * ( currentY + currentHeight ) ; } protected getScrollY ( offsetY , scrollableHeight ) { return scrollableHeight ? - 1 * ( offsetY + scrollableHeight ) : undefined ; } protected hasReachedBoundary ( offsetY , windowBound ? ) { if ( windowBound !== undefined ) { const endReachedMargin = Math . round ( offsetY - ( windowBound ) ) ; return endReachedMargin >= 0 ; } return false ; } }