|
@@ -1,3 +1,4 @@
|
|
|
+import { MEDIA_PREFERS_REDUCED_MOTION } from '../../constants/media';
|
|
|
import { DESTROYED } from '../../constants/states';
|
|
|
import { EventBinder } from '../../constructors';
|
|
|
import { Splide } from '../../core/Splide/Splide';
|
|
@@ -29,15 +30,10 @@ export function Media( Splide: Splide, Components: Components, options: Options
|
|
|
const binder = EventBinder();
|
|
|
const breakpoints = options.breakpoints || {};
|
|
|
|
|
|
- /**
|
|
|
- * Keeps the initial options to apply when no matched query exists.
|
|
|
- */
|
|
|
- const initialOptions = Splide._io;
|
|
|
-
|
|
|
/**
|
|
|
* Stores options and MediaQueryList object.
|
|
|
*/
|
|
|
- const queries: Array<[ string, Options, MediaQueryList ]> = [];
|
|
|
+ const queries: Array<[ Options, MediaQueryList ]> = [];
|
|
|
|
|
|
/**
|
|
|
* Called when the component is constructed.
|
|
@@ -48,10 +44,10 @@ export function Media( Splide: Splide, Components: Components, options: Options
|
|
|
ownKeys( breakpoints )
|
|
|
.sort( ( n, m ) => isMin ? +n - +m : +m - +n )
|
|
|
.forEach( key => {
|
|
|
- register( key, breakpoints[ key ], `(${ isMin ? 'min' : 'max' }-width:${ key }px)` );
|
|
|
+ register( breakpoints[ key ], `(${ isMin ? 'min' : 'max' }-width:${ key }px)` );
|
|
|
} );
|
|
|
|
|
|
- register( 'motion', options.reducedMotion || {}, '(prefers-reduced-motion: reduce)' );
|
|
|
+ register( options.reducedMotion || {}, MEDIA_PREFERS_REDUCED_MOTION );
|
|
|
update();
|
|
|
}
|
|
|
|
|
@@ -69,60 +65,47 @@ export function Media( Splide: Splide, Components: Components, options: Options
|
|
|
/**
|
|
|
* Registers entries as [ Options, media query string ].
|
|
|
*
|
|
|
- * @param key - An unique key.
|
|
|
- * @param options - Options merged to current options when the query matches the media.
|
|
|
+ * @param options - Options merged to current options when the document matches the query.
|
|
|
* @param query - A query string.
|
|
|
*/
|
|
|
- function register( key: string, options: Options, query: string ): void {
|
|
|
+ function register( options: Options, query: string ): void {
|
|
|
const queryList = matchMedia( query );
|
|
|
binder.bind( queryList, 'change', update );
|
|
|
- queries.push( [ key, options, queryList ] );
|
|
|
+ queries.push( [ options, queryList ] );
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Checks all media queries in entries and updates options.
|
|
|
*/
|
|
|
function update(): void {
|
|
|
- const merged = accumulate();
|
|
|
const direction = options.direction;
|
|
|
- const { destroy: destruction } = merged;
|
|
|
+ const merged = queries.reduce<Options>( ( merged, entry ) => {
|
|
|
+ return merge( merged, entry[ 1 ].matches ? entry[ 0 ] : {} );
|
|
|
+ }, {} );
|
|
|
|
|
|
- forOwn( options, ( value, key ) => {
|
|
|
- ! ( key in initialOptions ) && delete options[ key ];
|
|
|
- } );
|
|
|
+ forOwn( options, ( value, key ) => { delete options[ key ] } );
|
|
|
+ merge( options, merged );
|
|
|
|
|
|
- if ( destruction ) {
|
|
|
- Splide.destroy( destruction === 'completely' );
|
|
|
+ if ( options.destroy ) {
|
|
|
+ Splide.destroy( options.destroy === 'completely' );
|
|
|
} else if ( Splide.state.is( DESTROYED ) ) {
|
|
|
destroy( true );
|
|
|
Splide.mount();
|
|
|
} else {
|
|
|
Splide.options = merged;
|
|
|
- direction !== merged.direction && Splide.refresh();
|
|
|
+ direction !== options.direction && Splide.refresh();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Accumulates all options assigned to predefined media queries,
|
|
|
- * and merges them into user options.
|
|
|
- *
|
|
|
- * @return Merged options.
|
|
|
- */
|
|
|
- function accumulate(): Options {
|
|
|
- return queries.reduce( ( merged, entry ) => {
|
|
|
- return merge( merged, entry[ 2 ].matches ? entry[ 1 ] : {} );
|
|
|
- }, merge( {}, initialOptions ) );
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Checks if the query registered by the specified key matches the current media or not.
|
|
|
+ * Checks if the document matches the registered media query or not.
|
|
|
*
|
|
|
- * @param key - A key.
|
|
|
+ * @param media - A registered media query.
|
|
|
*
|
|
|
- * @return `true` if the query matches the media, or otherwise `false`.
|
|
|
+ * @return `true` if the document matches the query, or otherwise `false`.
|
|
|
*/
|
|
|
- function matches( key: string ): boolean {
|
|
|
- return queries.some( entry => entry[ 0 ] === key && entry[ 2 ].matches );
|
|
|
+ function matches( media: string ): boolean {
|
|
|
+ return queries.some( entry => entry[ 1 ].media === media && entry[ 1 ].matches );
|
|
|
}
|
|
|
|
|
|
return {
|