123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- /**
- * The resolver component for horizontal layout.
- *
- * @author Naotoshi Fujita
- * @copyright Naotoshi Fujita. All rights reserved.
- */
- import { applyStyle } from "../../../utils/dom";
- import { unit, toPixel } from "../../../utils/utils";
- import { RTL } from '../../../constants/directions';
- /**
- * Max width of a slide.
- *
- * @type {number}
- */
- const SLIDE_MAX_WIDTH = 5000;
- /**
- * The resolver component for horizontal layout.
- *
- * @param {Splide} Splide - A Splide instance.
- * @param {Object} Components - An object containing components.
- *
- * @return {Object} - The resolver object.
- */
- export default ( Splide, Components ) => {
- /**
- * Keep the Elements component.
- *
- * @type {string}
- */
- const Elements = Components.Elements;
- /**
- * Keep the root element.
- *
- * @type {Element}
- */
- const root = Splide.root;
- /**
- * Keep the track element.
- *
- * @type {Element}
- */
- let track;
- /**
- * Keep the latest options.
- *
- * @type {Element}
- */
- let options = Splide.options;
- return {
- /**
- * Margin property name.
- *
- * @type {string}
- */
- margin: 'margin' + ( options.direction === RTL ? 'Left' : 'Right' ),
- /**
- * Always 0 because the height will be determined by inner contents.
- *
- * @type {number}
- */
- height: 0,
- /**
- * Always 0 because the height will be determined by inner contents.
- *
- * @type {number}
- */
- listHeight: 0,
- /**
- * Initialization.
- */
- init() {
- options = Splide.options;
- track = Elements.track;
- this.gap = toPixel( root, options.gap );
- const padding = options.padding;
- const { left = padding, right = padding } = padding;
- this.padding = {
- left : toPixel( root, left ),
- right: toPixel( root, right ),
- };
- applyStyle( track, {
- paddingLeft : unit( left ),
- paddingRight: unit( right ),
- } );
- },
- /**
- * Accumulate slide width including the gap to the designated index.
- *
- * @param {number|undefined} index - If undefined, width of all slides will be accumulated.
- *
- * @return {number} - Accumulated width.
- */
- totalWidth( index ) {
- return Elements.getSlides( true )
- .filter( Slide => Slide.index <= index )
- .reduce( ( accumulator, Slide ) => {
- return accumulator + this.slideWidth( Slide.index ) + this.gap;
- }, 0 );
- },
- /**
- * Return the slide width in px.
- *
- * @param {number} index - Slide index.
- *
- * @return {number} - The slide width.
- */
- slideWidth( index ) {
- if ( options.autoWidth ) {
- const Slide = Elements.getSlide( index );
- return Slide ? Slide.slide.offsetWidth : 0;
- }
- const width = options.fixedWidth || ( ( this.width + this.gap ) / options.perPage ) - this.gap;
- return toPixel( root, width );
- },
- /**
- * Return the slide height in px.
- *
- * @return {number} - The slide height.
- */
- slideHeight() {
- const height = options.height || options.fixedHeight || this.width * options.heightRatio;
- return toPixel( root, height );
- },
- /**
- * Return slider width without padding.
- *
- * @return {number} - Current slider width.
- */
- get width() {
- return track.clientWidth - this.padding.left - this.padding.right;
- },
- /**
- * Return list width.
- *
- * @return {number} - Current list width.
- */
- get listWidth() {
- const total = Elements.total;
- return options.autoWidth ? total * SLIDE_MAX_WIDTH : this.totalWidth( total );
- },
- }
- }
|