123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- /**
- * The component for handing slide layouts and their sizes.
- *
- * @author Naotoshi Fujita
- * @copyright Naotoshi Fujita. All rights reserved.
- */
- import Horizontal from './resolvers/horizontal';
- import Vertical from './resolvers/vertical';
- import { unit } from '../../utils/utils';
- import { throttle } from '../../utils/time';
- import { subscribe, applyStyle } from '../../utils/dom';
- /**
- * Interval time for throttle.
- *
- * @type {number}
- */
- const THROTTLE = 30;
- /**
- * The component for handing slide layouts and their sizes.
- *
- * @param {Splide} Splide - A Splide instance.
- * @param {Object} Components - An object containing components.
- *
- * @return {Object} - The component object.
- */
- export default ( Splide, Components ) => {
- /**
- * Store the root element.
- *
- * @type {Element}
- */
- const root = Splide.root;
- /**
- * Store the list element.
- *
- * @type {Element}
- */
- let list;
- /**
- * Store all Slide objects.
- *
- * @type {Object}
- */
- let Slides;
- /**
- * Hold a resolver object.
- *
- * @type {Object}
- */
- let Resolver;
- /**
- * Whether the slider is vertical or not.
- * @type {boolean}
- */
- const isVertical = Splide.options.direction === 'ttb';
- /**
- * Layout component object.
- *
- * @type {Object}
- */
- const Layout = {
- /**
- * Called when the component is mounted.
- */
- mount() {
- list = Components.Elements.list;
- Slides = Components.Slides.getSlides( true, true );
- bind();
- init();
- },
- /**
- * Return slider width without padding.
- *
- * @return {number} - Current slide width.
- */
- get width() {
- return Resolver.width;
- },
- /**
- * Return list width.
- *
- * @return {number} - Current list width.
- */
- get listWidth() {
- return Resolver.listWidth;
- },
- /**
- * Return list height.
- *
- * @return {number} - Current list height.
- */
- get listHeight() {
- return Resolver.listHeight;
- },
- /**
- * Return slide width including gap size.
- * Note that slideWidth * perPage is NOT equal to slider width.
- *
- * @return {number} - Current slide width including gap size.
- */
- get slideWidth() {
- return Resolver.getSlideWidth( true );
- },
- /**
- * Return slide height.
- *
- * @return {number} - Computed slide height.
- */
- get slideHeight() {
- return Resolver.getSlideHeight( true );
- },
- /**
- * Return gap in px.
- *
- * @return {Object} - Gap amount in px.
- */
- get gap() {
- return Resolver.gap;
- },
- /**
- * Return padding object.
- *
- * @return {Object} - An object containing padding left and right in horizontal mode
- * or top and bottom in vertical one.
- */
- get padding() {
- return Resolver.padding;
- },
- };
- /**
- * Init slider styles according to options.
- */
- function init() {
- const options = Splide.options;
- if ( isVertical ) {
- Resolver = Vertical( Splide, Components, options );
- } else {
- Resolver = Horizontal( Splide, Components, options );
- }
- Resolver.init();
- applyStyle( root, { maxWidth: unit( options.width ) } );
- for ( const i in Slides ) {
- applyStyle( Slides[ i ].slide, { [ Resolver.marginProp ]: unit( options.gap ) } )
- }
- resize();
- }
- /**
- * Listen the resize native event with throttle.
- * Initialize when the component is mounted or options are updated.
- */
- function bind() {
- const throttledResize = throttle( () => { Splide.emit( 'resize' ) }, THROTTLE );
- subscribe( window, 'resize', throttledResize );
- Splide.on( 'mounted resize', resize ).on( 'updated', init );
- if ( ! isVertical ) {
- Splide.on( 'resize', updatePerPage );
- }
- }
- /**
- * Resize the list and slides including clones.
- */
- function resize() {
- applyStyle( list, { width: unit( Layout.listWidth ), height: unit( Layout.listHeight ) } );
- const slideWidth = unit( Resolver.getSlideWidth( false ) );
- const slideHeight = unit( Resolver.getSlideHeight( false ) );
- for ( let i in Slides ) {
- const { slide, container } = Slides[ i ];
- applyStyle( container, { height: slideHeight } );
- applyStyle( slide, { width: slideWidth, height: ! container ? slideHeight : '' } );
- }
- }
- /**
- * Update the perPage number automatically according to the fixedWidth.
- */
- function updatePerPage() {
- const options = Splide.options;
- if ( options.fixedWidth ) {
- const perPage = Math.floor( ( Layout.width + Resolver.gap ) / Layout.slideWidth ) || 1;
- if ( options.perPage !== perPage ) {
- Splide.options = { perPage };
- }
- }
- }
- return Layout;
- }
|