Direction.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { RTL, TTB } from '../../constants/directions';
  2. import { Splide } from '../../core/Splide/Splide';
  3. import { BaseComponent, Components, Options } from '../../types';
  4. /**
  5. * The interface for the Direction component.
  6. *
  7. * @since 3.0.0
  8. */
  9. export interface DirectionComponent extends BaseComponent {
  10. resolve( prop: string, axisOnly?: boolean ): string;
  11. orient( value: number ): number;
  12. }
  13. /**
  14. * The translation map for directions.
  15. *
  16. * @since 3.0.0
  17. */
  18. export const ORIENTATION_MAP = {
  19. marginRight : [ 'marginBottom', 'marginLeft' ],
  20. autoWidth : [ 'autoHeight' ],
  21. fixedWidth : [ 'fixedHeight' ],
  22. paddingLeft : [ 'paddingTop', 'paddingRight' ],
  23. paddingRight: [ 'paddingBottom', 'paddingLeft' ],
  24. width : [ 'height' ],
  25. Width : [ 'Height' ],
  26. left : [ 'top', 'right' ],
  27. right : [ 'bottom', 'left' ],
  28. x : [ 'y' ],
  29. X : [ 'Y' ],
  30. Y : [ 'X' ],
  31. ArrowLeft : [ 'ArrowUp', 'ArrowRight' ],
  32. ArrowRight : [ 'ArrowDown', 'ArrowLeft' ],
  33. };
  34. /**
  35. * The component that absorbs the difference among directions.
  36. *
  37. * @since 3.0.0
  38. *
  39. * @param Splide - A Splide instance.
  40. * @param Components - A collection of components.
  41. * @param options - Options.
  42. *
  43. * @return A Direction component object.
  44. */
  45. export function Direction( Splide: Splide, Components: Components, options: Options ): DirectionComponent {
  46. /**
  47. * Resolves the provided property name.
  48. *
  49. * @param prop - A property name to translate.
  50. * @param axisOnly - Optional. If `ture`, returns the same property for LTR and RTL.
  51. */
  52. function resolve( prop: string, axisOnly?: boolean ): string {
  53. const { direction } = options;
  54. const index = direction === RTL && ! axisOnly ? 1 : direction === TTB ? 0 : -1;
  55. return ORIENTATION_MAP[ prop ][ index ] || prop;
  56. }
  57. /**
  58. * Orients the value towards the current direction.
  59. *
  60. * @param value - A value to orient.
  61. *
  62. * @return The oriented value.
  63. */
  64. function orient( value: number ): number {
  65. return value * ( options.direction === RTL ? 1 : -1 );
  66. }
  67. return {
  68. resolve,
  69. orient,
  70. };
  71. }