Procházet zdrojové kódy

Triggers the `onMoved` callback when the transition is cancelled.

Naotoshi Fujita před 2 roky
rodič
revize
e975408720

+ 14 - 13
dist/js/splide.js

@@ -1166,13 +1166,14 @@
 
   const Move = (Splide, Components, options, event) => {
     const { on, emit } = event;
-    const { set } = Splide.state;
+    const { set, is } = Splide.state;
     const { Slides } = Components;
     const { slideSize, getPadding, listSize, sliderSize, totalSize, trackSize } = Components.Layout;
     const { resolve, orient } = Components.Direction;
     const { list, track } = Components.Elements;
     let Transition;
     let indices;
+    let callback;
     function mount() {
       Transition = Components.Transition;
       on([EVENT_MOUNTED, EVENT_RESIZED, EVENT_UPDATED, EVENT_REFRESH], reposition);
@@ -1184,27 +1185,27 @@
         Slides.update();
       }
     }
-    function move(dest, index, prev, forwards, callback) {
+    function move(dest, index, prev, forwards, onMoved) {
       cancel();
       const shiftBackwards = dest !== index ? dest > index : forwards;
-      if ((dest !== index || exceededLimit(forwards)) && canShift(shiftBackwards)) {
-        translate(shift(getPosition(), shiftBackwards), true);
-      }
+      const shouldShift = (dest !== index || exceededLimit(forwards)) && canShift(shiftBackwards);
+      shouldShift && translate(shift(getPosition(), shiftBackwards), true);
       indices = [index, prev, dest];
+      callback = onMoved;
       set(MOVING);
       emit(EVENT_MOVE, index, prev, dest);
-      Transition.start(index, () => {
-        set(IDLE);
-        emit(EVENT_MOVED, index, prev, dest);
-        callback && callback();
-      });
+      Transition.start(index, onTransitionEnd);
+    }
+    function onTransitionEnd() {
+      set(IDLE);
+      emit(EVENT_MOVED, ...indices);
+      callback && callback();
     }
     function cancel() {
-      if (Splide.state.is(MOVING) && indices) {
+      if (is(MOVING) && indices) {
         translate(getPosition(), true);
         Transition.cancel();
-        set(IDLE);
-        emit(EVENT_MOVED, ...indices);
+        onTransitionEnd();
       }
     }
     function jump(index) {

Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 0 - 0
dist/js/splide.min.js


binární
dist/js/splide.min.js.gz


Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 0 - 0
dist/js/splide.min.js.map


+ 21 - 17
src/js/components/Move/Move.ts

@@ -50,7 +50,7 @@ export interface MoveComponent extends BaseComponent {
  */
 export const Move: ComponentConstructor<MoveComponent> = ( Splide, Components, options, event ) => {
   const { on, emit } = event;
-  const { set } = Splide.state;
+  const { set, is } = Splide.state;
   const { Slides } = Components;
   const { slideSize, getPadding, listSize, sliderSize, totalSize, trackSize } = Components.Layout;
   const { resolve, orient } = Components.Direction;
@@ -66,6 +66,8 @@ export const Move: ComponentConstructor<MoveComponent> = ( Splide, Components, o
    */
   let indices: [ number, number, number ];
 
+  let callback: AnyFunction;
+
   /**
    * Called when the component is mounted.
    */
@@ -93,43 +95,45 @@ export const Move: ComponentConstructor<MoveComponent> = ( Splide, Components, o
    * - Crossing bounds (dest !== index)
    * - The destination is further than the opposite destination.
    *
-   * @todo trigger the callback when the transition is cancelled
-   *
    * @param dest     - A destination index to go to, including clones'.
    * @param index    - A slide index.
    * @param prev     - A previous index.
    * @param forwards - Specifies the move direction.
-   * @param callback - Optional. A callback function invoked after transition ends.
+   * @param onMoved  - Optional. A callback function invoked after transition ends.
    */
-  function move( dest: number, index: number, prev: number, forwards: boolean, callback?: AnyFunction ): void {
+  function move( dest: number, index: number, prev: number, forwards: boolean, onMoved?: AnyFunction ): void {
     cancel();
 
     const shiftBackwards = dest !== index ? dest > index : forwards;
+    const shouldShift    = ( dest !== index || exceededLimit( forwards ) ) && canShift( shiftBackwards );
 
-    if ( ( dest !== index || exceededLimit( forwards ) ) && canShift( shiftBackwards ) ) {
-      translate( shift( getPosition(), shiftBackwards ), true );
-    }
+    shouldShift && translate( shift( getPosition(), shiftBackwards ), true );
+
+    indices  = [ index, prev, dest ];
+    callback = onMoved;
 
-    indices = [ index, prev, dest ];
     set( MOVING );
     emit( EVENT_MOVE, index, prev, dest );
+    Transition.start( index, onTransitionEnd );
+  }
 
-    Transition.start( index, () => {
-      set( IDLE );
-      emit( EVENT_MOVED, index, prev, dest );
-      callback && callback();
-    } );
+  /**
+   * Called when transition ends or is cancelled.
+   */
+  function onTransitionEnd(): void {
+    set( IDLE );
+    emit( EVENT_MOVED, ...indices );
+    callback && callback();
   }
 
   /**
    * Cancels transition.
    */
   function cancel(): void {
-    if ( Splide.state.is( MOVING ) && indices ) {
+    if ( is( MOVING ) && indices ) {
       translate( getPosition(), true );
       Transition.cancel();
-      set( IDLE );
-      emit( EVENT_MOVED, ...indices );
+      onTransitionEnd();
     }
   }
 

Některé soubory nejsou zobrazeny, neboť je v těchto rozdílových datech změněno mnoho souborů