123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- /**
- * The resolver component for horizontal move.
- *
- * @author Naotoshi Fujita
- * @copyright Naotoshi Fujita. All rights reserved.
- */
- import { applyStyle } from '../../../utils/dom';
- import { between } from '../../../utils/utils';
- /**
- * The resolver component for horizontal move.
- *
- * @param {Splide} Splide - A Splide instance.
- * @param {Object} Components - An object containing components.
- *
- * @return {Object} - The resolver object.
- */
- export default ( Splide, Components ) => {
- /**
- * Hold the Layout object.
- *
- * @type {Object}
- */
- const Layout = Components.Layout;
- return {
- /**
- * Set position with CSS transform.
- *
- * @param {Element} list - A list element.
- * @param {number} position - A new position value.
- */
- translate( list, position ) {
- applyStyle( list, { transform: `translateX(${ position }px)` } );
- },
- /**
- * Calculate position by index.
- *
- * @param {number} index - Slide index.
- *
- * @return {Object} - Calculated position.
- */
- toPosition( index ) {
- return this.sign * ( index * ( Layout.slideWidth + Layout.gap ) + this.offset )
- },
- /**
- * Calculate the closest slide index from the given position.
- *
- * @return {number} - The closest slide position.
- */
- toIndex( position ) {
- return Math.round( ( this.sign * position - this.offset ) / ( Layout.slideWidth + Layout.gap ) );
- },
- /**
- * Trim redundant spaces on the left or right edge if necessary.
- *
- * @param {number} position - Position value to be trimmed.
- *
- * @return {number} - Trimmed position.
- */
- trim( position ) {
- const edge = this.sign * ( Layout.listWidth - ( Layout.width + Layout.gap ) );
- return between( position, edge, 0 );
- },
- /**
- * Return sign according to the direction.
- *
- * @return {number} - -1 for LTR or 1 or RTL.
- */
- get sign() {
- return Components.Controller.isRtl() ? 1 : -1;
- },
- /**
- * Return current offset value, considering direction and a number of clones.
- *
- * @return {number} - Offset amount.
- */
- get offset() {
- const { width, slideWidth, gap } = Layout;
- const { focus } = Splide.options;
- let focusOffset;
- if ( focus === 'center' ) {
- focusOffset = ( width - slideWidth ) / 2;
- } else {
- focusOffset = ( parseInt( focus ) || 0 ) * ( slideWidth + gap );
- }
- return ( slideWidth + gap ) * Components.Clones.length / 2 - focusOffset;
- },
- };
- }
|