import { Slides } from '../slides' ; import { Platform } from '../../../platform/platform' ; import { fixLoop , onTransitionEnd , onTransitionStart } from './swiper' ; import { updateActiveIndex } from './swiper-index' ; import { isHorizontal , maxTranslate , minTranslate } from './swiper-utils' ; import { updateProgress } from './swiper-progress' ; export const SWIPER_CONTROLLER = { LinearSpline : function ( _s , _platform , x , y ) { this . x = x ; this . y = y ; this . lastIndex = x . length - 1 ; var i1 , i3 ; this . interpolate = function ( x2 ) { if ( ! x2 ) return 0 ; i3 = binarySearch ( this . x , x2 ) ; i1 = i3 - 1 ; return ( ( x2 - this . x [ i1 ] ) * ( this . y [ i3 ] - this . y [ i1 ] ) ) / ( this . x [ i3 ] - this . x [ i1 ] ) + this . y [ i1 ] ; } ; var binarySearch = ( function ( ) { var maxIndex , minIndex , guess ; return function ( array , val ) { minIndex = - 1 ; maxIndex = array . length ; while ( maxIndex - minIndex > 1 ) if ( array [ guess = maxIndex + minIndex >> 1 ] <= val ) { minIndex = guess ; } else { maxIndex = guess ; } return maxIndex ; } ; } ) ( ) ; } , getInterpolateFunction : function ( s , plt , c ) { if ( ! s . _spline ) s . _spline = s . loop ? new ( < any > SWIPER_CONTROLLER ) . LinearSpline ( s , plt , s . _slidesGrid , c . _slidesGrid ) : new ( < any > SWIPER_CONTROLLER ) . LinearSpline ( s , plt , s . _snapGrid , c . _snapGrid ) ; } , setTranslate : function ( s , plt , translate , byController , setWrapperTranslate ) { var controlled = s . control ; var multiplier , controlledTranslate ; function setControlledTranslate ( c ) { translate = c . _rtl && isHorizontal ( c ) ? - s . _translate : s . _translate ; if ( s . controlBy === 'slide' ) { SWIPER_CONTROLLER . getInterpolateFunction ( s , plt , c ) ; controlledTranslate = - s . _spline . interpolate ( - translate ) ; } if ( ! controlledTranslate || s . controlBy === 'container' ) { multiplier = ( maxTranslate ( c ) - minTranslate ( c ) ) / ( maxTranslate ( s ) - minTranslate ( s ) ) ; controlledTranslate = ( translate - minTranslate ( s ) ) * multiplier + minTranslate ( c ) ; } if ( s . controlInverse ) { controlledTranslate = maxTranslate ( c ) - controlledTranslate ; } updateProgress ( c , controlledTranslate ) ; setWrapperTranslate ( c , plt , controlledTranslate , false , s ) ; updateActiveIndex ( c ) ; } if ( Array . isArray ( controlled ) ) { for ( var i = 0 ; i < controlled . length ; i ++ ) { if ( controlled [ i ] !== byController ) { setControlledTranslate ( controlled [ i ] ) ; } } } else if ( byController !== controlled ) { setControlledTranslate ( controlled ) ; } } , setTransition : function ( s , plt , duration , byController , setWrapperTransition ) { var controlled = s . control ; var i ; function setControlledTransition ( c ) { setWrapperTransition ( c , plt , duration , s ) ; if ( duration !== 0 ) { onTransitionStart ( c ) ; plt . transitionEnd ( c . _wrapper , ( ) => { if ( ! controlled ) return ; if ( c . loop && s . controlBy === 'slide' ) { fixLoop ( c , plt ) ; } onTransitionEnd ( c , plt ) ; } ) ; } } if ( Array . isArray ( controlled ) ) { for ( i = 0 ; i < controlled . length ; i ++ ) { if ( controlled [ i ] !== byController ) { setControlledTransition ( controlled [ i ] ) ; } } } else if ( byController !== controlled ) { setControlledTransition ( controlled ) ; } } } ;