index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /**
  2. * The component for supporting mouse drag and swipe.
  3. *
  4. * @author Naotoshi Fujita
  5. * @copyright Naotoshi Fujita. All rights reserved.
  6. */
  7. import { FADE, SLIDE } from '../../constants/types';
  8. import { TTB } from '../../constants/directions';
  9. import { IDLE } from '../../constants/states';
  10. import { between } from '../../utils/utils';
  11. import { each } from "../../utils/object";
  12. const { abs } = Math;
  13. /**
  14. * Adjust how much the track can be pulled on the first or last page.
  15. * The larger number this is, the farther the track moves.
  16. * This should be around 5 - 9.
  17. *
  18. * @type {number}
  19. */
  20. const FRICTION_REDUCER = 7;
  21. /**
  22. * The component supporting mouse drag and swipe.
  23. *
  24. * @param {Splide} Splide - A Splide instance.
  25. * @param {Object} Components - An object containing components.
  26. *
  27. * @return {Object} - The component object.
  28. */
  29. export default ( Splide, Components ) => {
  30. /**
  31. * Store the Move component.
  32. *
  33. * @type {Object}
  34. */
  35. const Track = Components.Track;
  36. /**
  37. * Store the Controller component.
  38. *
  39. * @type {Object}
  40. */
  41. const Controller = Components.Controller;
  42. /**
  43. * Coordinate of the track on starting drag.
  44. *
  45. * @type {Object}
  46. */
  47. let startCoord;
  48. /**
  49. * Analyzed info on starting drag.
  50. *
  51. * @type {Object|null}
  52. */
  53. let startInfo;
  54. /**
  55. * Analyzed info being updated while dragging/swiping.
  56. *
  57. * @type {Object}
  58. */
  59. let currentInfo;
  60. /**
  61. * Determine whether slides are being dragged or not.
  62. *
  63. * @type {boolean}
  64. */
  65. let isDragging;
  66. /**
  67. * Whether the slider direction is vertical or not.
  68. *
  69. * @type {boolean}
  70. */
  71. const isVertical = Splide.options.direction === TTB;
  72. /**
  73. * Axis for the direction.
  74. *
  75. * @type {string}
  76. */
  77. const axis = isVertical ? 'y' : 'x';
  78. /**
  79. * Drag component object.
  80. *
  81. * @type {Object}
  82. */
  83. const Drag = {
  84. /**
  85. * Whether dragging is disabled or not.
  86. *
  87. * @type {boolean}
  88. */
  89. disabled: false,
  90. /**
  91. * Called when the component is mounted.
  92. */
  93. mount() {
  94. const list = Components.Elements.list;
  95. Splide
  96. .on( 'touchstart mousedown', start, list )
  97. .on( 'touchmove mousemove', move, list, { passive: false } )
  98. .on( 'touchend touchcancel mouseleave mouseup dragend', end, list )
  99. .on( 'mounted refresh', () => {
  100. // Prevent dragging an image or anchor itself.
  101. each( list.querySelectorAll( 'img, a' ), elm => {
  102. Splide
  103. .off( 'dragstart', elm )
  104. .on( 'dragstart', e => { e.preventDefault() }, elm, { passive: false } );
  105. } );
  106. } )
  107. .on( 'mounted updated', () => {
  108. this.disabled = ! Splide.options.drag;
  109. } );
  110. },
  111. };
  112. /**
  113. * Called when the track starts to be dragged.
  114. *
  115. * @param {TouchEvent|MouseEvent} e - TouchEvent or MouseEvent object.
  116. */
  117. function start( e ) {
  118. if ( ! Drag.disabled && ! isDragging && Splide.State.is( IDLE ) ) {
  119. startCoord = Track.toCoord( Track.position );
  120. startInfo = analyze( e, {} );
  121. currentInfo = startInfo;
  122. }
  123. }
  124. /**
  125. * Called while the track being dragged.
  126. *
  127. * @param {TouchEvent|MouseEvent} e - TouchEvent or MouseEvent object.
  128. */
  129. function move( e ) {
  130. if ( startInfo ) {
  131. currentInfo = analyze( e, startInfo );
  132. if ( isDragging ) {
  133. if ( e.cancelable ) {
  134. e.preventDefault();
  135. }
  136. if ( ! Splide.is( FADE ) ) {
  137. const position = startCoord[ axis ] + currentInfo.offset[ axis ];
  138. Track.translate( resist( position ) );
  139. }
  140. } else {
  141. if ( shouldMove( currentInfo ) ) {
  142. Splide.emit( 'drag', startInfo );
  143. isDragging = true;
  144. }
  145. }
  146. }
  147. }
  148. /**
  149. * Determine whether to start moving the track or not by drag angle.
  150. *
  151. * @param {Object} info - An information object.
  152. *
  153. * @return {boolean} - True if the track should be moved or false if not.
  154. */
  155. function shouldMove( { offset } ) {
  156. if ( Splide.State.is( IDLE ) ) {
  157. let angle = Math.atan( abs( offset.y ) / abs( offset.x ) ) * 180 / Math.PI;
  158. if ( isVertical ) {
  159. angle = 90 - angle;
  160. }
  161. return angle < Splide.options.dragAngleThreshold;
  162. }
  163. return false;
  164. }
  165. /**
  166. * Resist dragging the track on the first/last page because there is no more.
  167. *
  168. * @param {number} position - A position being applied to the track.
  169. *
  170. * @return {Object} - Adjusted position.
  171. */
  172. function resist( position ) {
  173. if ( Splide.is( SLIDE ) ) {
  174. const sign = Track.sign;
  175. const start = sign * Track.trim( Track.toPosition( 0 ) );
  176. const end = sign * Track.trim( Track.toPosition( Controller.edgeIndex ) );
  177. position *= sign;
  178. if ( position < start ) {
  179. position = start - FRICTION_REDUCER * Math.log( start - position );
  180. } else if ( position > end ) {
  181. position = end + FRICTION_REDUCER * Math.log( position - end );
  182. }
  183. position *= sign;
  184. }
  185. return position;
  186. }
  187. /**
  188. * Called when dragging ends.
  189. */
  190. function end() {
  191. startInfo = null;
  192. if ( isDragging ) {
  193. Splide.emit( 'dragged', currentInfo );
  194. go( currentInfo );
  195. isDragging = false;
  196. }
  197. }
  198. /**
  199. * Go to the slide determined by the analyzed data.
  200. *
  201. * @param {Object} info - An info object.
  202. */
  203. function go( info ) {
  204. const velocity = info.velocity[ axis ];
  205. const absV = abs( velocity );
  206. if ( absV > 0 ) {
  207. const Layout = Components.Layout;
  208. const options = Splide.options;
  209. const index = Splide.index;
  210. const sign = velocity < 0 ? -1 : 1;
  211. let destIndex = index;
  212. if ( ! Splide.is( FADE ) ) {
  213. let destination = Track.position;
  214. if ( absV > options.flickVelocityThreshold && abs( info.offset[ axis ] ) < options.swipeDistanceThreshold ) {
  215. destination += sign * Math.min( absV * options.flickPower, Layout.width * ( options.flickMaxPages || 1 ) );
  216. }
  217. destIndex = Track.toIndex( destination );
  218. }
  219. /*
  220. * Do not allow the track to go to a previous position.
  221. * Always use the adjacent index for the fade mode.
  222. */
  223. if ( destIndex === index ) {
  224. destIndex = index + sign * Track.sign;
  225. }
  226. if ( Splide.is( SLIDE ) ) {
  227. destIndex = between( destIndex, 0, Controller.edgeIndex );
  228. }
  229. Controller.go( destIndex, options.isNavigation );
  230. }
  231. }
  232. /**
  233. * Analyze the given event object and return important information for handling swipe behavior.
  234. *
  235. * @param {Event} e - Touch or Mouse event object.
  236. * @param {Object} startInfo - Information analyzed on start for calculating difference from the current one.
  237. *
  238. * @return {Object} - An object containing analyzed information, such as offset, velocity, etc.
  239. */
  240. function analyze( e, startInfo ) {
  241. const { timeStamp, touches } = e;
  242. const { clientX, clientY } = touches ? touches[0] : e;
  243. const { x: fromX = clientX, y: fromY = clientY } = startInfo.to || {};
  244. const startTime = startInfo.time || 0;
  245. const offset = { x: clientX - fromX, y: clientY - fromY };
  246. const duration = timeStamp - startTime;
  247. const velocity = { x: offset.x / duration, y: offset.y / duration };
  248. return {
  249. to: { x: clientX, y: clientY },
  250. offset,
  251. time: timeStamp,
  252. velocity,
  253. };
  254. }
  255. return Drag;
  256. }