Ver Fonte

Change condition of shifting a carousel.

Naotoshi Fujita há 2 anos atrás
pai
commit
9518f8e03f

+ 13 - 10
dist/js/splide.cjs.js

@@ -1189,10 +1189,12 @@ const Move = (Splide, Components, options, event) => {
     }
   }
   function move(dest, index, prev, callback) {
-    const forward = dest > prev;
+    const forwards = dest > prev;
+    const closest = toIndex(getPosition());
+    const detouring = exceededLimit(forwards) && abs(dest - closest) > abs(dest - prev);
     cancel();
-    if ((dest !== index || exceededLimit(forward)) && canShift(forward)) {
-      translate(shift(getPosition(), forward), true);
+    if ((dest !== index || detouring) && canShift(forwards)) {
+      translate(shift(getPosition(), forwards), true);
     }
     indices = [index, prev, dest];
     set(MOVING);
@@ -1203,6 +1205,14 @@ const Move = (Splide, Components, options, event) => {
       callback && callback();
     });
   }
+  function cancel() {
+    if (Splide.state.is(MOVING) && indices) {
+      translate(getPosition(), true);
+      Transition.cancel();
+      set(IDLE);
+      emit(EVENT_MOVED, ...indices);
+    }
+  }
   function jump(index) {
     translate(toPosition(index));
   }
@@ -1228,13 +1238,6 @@ const Move = (Splide, Components, options, event) => {
     position -= orient(size * (ceil(abs(excess) / size) || 1)) * (backwards ? 1 : -1);
     return position;
   }
-  function cancel() {
-    if (Splide.state.is(MOVING) && indices) {
-      translate(getPosition(), true);
-      Transition.cancel();
-      emit(EVENT_MOVED, ...indices);
-    }
-  }
   function toIndex(position) {
     const slides = Slides.get();
     let index = 0;

+ 13 - 10
dist/js/splide.esm.js

@@ -1185,10 +1185,12 @@ const Move = (Splide, Components, options, event) => {
     }
   }
   function move(dest, index, prev, callback) {
-    const forward = dest > prev;
+    const forwards = dest > prev;
+    const closest = toIndex(getPosition());
+    const detouring = exceededLimit(forwards) && abs(dest - closest) > abs(dest - prev);
     cancel();
-    if ((dest !== index || exceededLimit(forward)) && canShift(forward)) {
-      translate(shift(getPosition(), forward), true);
+    if ((dest !== index || detouring) && canShift(forwards)) {
+      translate(shift(getPosition(), forwards), true);
     }
     indices = [index, prev, dest];
     set(MOVING);
@@ -1199,6 +1201,14 @@ const Move = (Splide, Components, options, event) => {
       callback && callback();
     });
   }
+  function cancel() {
+    if (Splide.state.is(MOVING) && indices) {
+      translate(getPosition(), true);
+      Transition.cancel();
+      set(IDLE);
+      emit(EVENT_MOVED, ...indices);
+    }
+  }
   function jump(index) {
     translate(toPosition(index));
   }
@@ -1224,13 +1234,6 @@ const Move = (Splide, Components, options, event) => {
     position -= orient(size * (ceil(abs(excess) / size) || 1)) * (backwards ? 1 : -1);
     return position;
   }
-  function cancel() {
-    if (Splide.state.is(MOVING) && indices) {
-      translate(getPosition(), true);
-      Transition.cancel();
-      emit(EVENT_MOVED, ...indices);
-    }
-  }
   function toIndex(position) {
     const slides = Slides.get();
     let index = 0;

+ 13 - 10
dist/js/splide.js

@@ -1185,10 +1185,12 @@
       }
     }
     function move(dest, index, prev, callback) {
-      const forward = dest > prev;
+      const forwards = dest > prev;
+      const closest = toIndex(getPosition());
+      const detouring = exceededLimit(forwards) && abs(dest - closest) > abs(dest - prev);
       cancel();
-      if ((dest !== index || exceededLimit(forward)) && canShift(forward)) {
-        translate(shift(getPosition(), forward), true);
+      if ((dest !== index || detouring) && canShift(forwards)) {
+        translate(shift(getPosition(), forwards), true);
       }
       indices = [index, prev, dest];
       set(MOVING);
@@ -1199,6 +1201,14 @@
         callback && callback();
       });
     }
+    function cancel() {
+      if (Splide.state.is(MOVING) && indices) {
+        translate(getPosition(), true);
+        Transition.cancel();
+        set(IDLE);
+        emit(EVENT_MOVED, ...indices);
+      }
+    }
     function jump(index) {
       translate(toPosition(index));
     }
@@ -1224,13 +1234,6 @@
       position -= orient(size * (ceil(abs(excess) / size) || 1)) * (backwards ? 1 : -1);
       return position;
     }
-    function cancel() {
-      if (Splide.state.is(MOVING) && indices) {
-        translate(getPosition(), true);
-        Transition.cancel();
-        emit(EVENT_MOVED, ...indices);
-      }
-    }
     function toIndex(position) {
       const slides = Slides.get();
       let index = 0;

Diff do ficheiro suprimidas por serem muito extensas
+ 0 - 0
dist/js/splide.min.js


BIN
dist/js/splide.min.js.gz


Diff do ficheiro suprimidas por serem muito extensas
+ 0 - 0
dist/js/splide.min.js.map


+ 3 - 1
src/js/components/Controller/Controller.ts

@@ -151,6 +151,8 @@ export const Controller: ComponentConstructor<ControllerComponent> = ( Splide, C
    * - If the carousel is looping (`dest !== index`),
    *   the carousel can be shifted or has been already shifted.
    *
+   *   @todo dest
+   *
    * @param dest  - A dest index.
    * @param index - An actual index.
    *
@@ -160,7 +162,7 @@ export const Controller: ComponentConstructor<ControllerComponent> = ( Splide, C
     const forward = dest > prevIndex;
 
     return index > -1
-      &&( index !== currIndex || ! isMoving() )
+      && ( index !== currIndex || ! isMoving() )
       && ( dest === index || Move.exceededLimit( ! forward ) || Move.canShift( forward ) );
   }
 

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

@@ -90,9 +90,10 @@ export const Move: ComponentConstructor<MoveComponent> = ( Splide, Components, o
   /**
    * Moves the slider to the dest index with the Transition component.
    * Needs to shift the carousel when:
-   * - Crossing bounds (`dest !== index && ! exceededLimit( ! forward )`).
-   *   But the second condition is not necessary because of `canShift()`.
-   * - Going further although the carousel already outside bounds (`exceededLimit( forward )`)
+   * - 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.
@@ -100,12 +101,14 @@ export const Move: ComponentConstructor<MoveComponent> = ( Splide, Components, o
    * @param callback - Optional. A callback function invoked after transition ends.
    */
   function move( dest: number, index: number, prev: number, callback?: AnyFunction ): void {
-    const forward = dest > prev;
+    const forwards  = dest > prev;
+    const closest   = toIndex( getPosition() );
+    const detouring = exceededLimit( forwards ) && ( abs( dest - closest ) > abs( dest - prev ) );
 
     cancel();
 
-    if ( ( dest !== index || exceededLimit( forward ) ) && canShift( forward ) ) {
-      translate( shift( getPosition(), forward ), true );
+    if ( ( dest !== index || detouring ) && canShift( forwards ) ) {
+      translate( shift( getPosition(), forwards  ), true );
     }
 
     indices = [ index, prev, dest ];
@@ -119,6 +122,18 @@ export const Move: ComponentConstructor<MoveComponent> = ( Splide, Components, o
     } );
   }
 
+  /**
+   * Cancels transition.
+   */
+  function cancel(): void {
+    if ( Splide.state.is( MOVING ) && indices ) {
+      translate( getPosition(), true );
+      Transition.cancel();
+      set( IDLE );
+      emit( EVENT_MOVED, ...indices );
+    }
+  }
+
   /**
    * Jumps to the slide at the specified index (silently).
    *
@@ -176,17 +191,6 @@ export const Move: ComponentConstructor<MoveComponent> = ( Splide, Components, o
     return position;
   }
 
-  /**
-   * Cancels transition.
-   */
-  function cancel(): void {
-    if ( Splide.state.is( MOVING ) && indices ) {
-      translate( getPosition(), true );
-      Transition.cancel();
-      emit( EVENT_MOVED, ...indices );
-    }
-  }
-
   /**
    * Returns the closest index to the position.
    *

Alguns ficheiros não foram mostrados porque muitos ficheiros mudaram neste diff