export class PositionCache { els : HTMLElement [ ] ; originClientRect : ClientRect ; lefts : any ; rights : any ; tops : any ; bottoms : any ; constructor ( originEl , els , isHorizontal , isVertical ) { this . els = els ; let originClientRect = this . originClientRect = originEl . getBoundingClientRect ( ) ; if ( isHorizontal ) { this . buildElHorizontals ( originClientRect . left ) ; } if ( isVertical ) { this . buildElVerticals ( originClientRect . top ) ; } } buildElHorizontals ( originClientLeft ) { let lefts = [ ] ; let rights = [ ] ; for ( let el of this . els ) { let rect = el . getBoundingClientRect ( ) ; lefts . push ( rect . left - originClientLeft ) ; rights . push ( rect . right - originClientLeft ) ; } this . lefts = lefts ; this . rights = rights ; } buildElVerticals ( originClientTop ) { let tops = [ ] ; let bottoms = [ ] ; for ( let el of this . els ) { let rect = el . getBoundingClientRect ( ) ; tops . push ( rect . top - originClientTop ) ; bottoms . push ( rect . bottom - originClientTop ) ; } this . tops = tops ; this . bottoms = bottoms ; } leftToIndex ( leftPosition ) { let { lefts , rights } = this ; let len = lefts . length ; let i ; for ( i = 0 ; i < len ; i += 1 ) { if ( leftPosition >= lefts [ i ] && leftPosition < rights [ i ] ) { return i ; } } return undefined ; } topToIndex ( topPosition ) { let { tops , bottoms } = this ; let len = tops . length ; let i ; for ( i = 0 ; i < len ; i += 1 ) { if ( topPosition >= tops [ i ] && topPosition < bottoms [ i ] ) { return i ; } } return undefined ; } getWidth ( leftIndex ) { return this . rights [ leftIndex ] - this . lefts [ leftIndex ] ; } getHeight ( topIndex ) { return this . bottoms [ topIndex ] - this . tops [ topIndex ] ; } }