浏览代码

Quit checking if the target index is same with the current one in Controller, and do it in Sync.

Naotoshi Fujita 2 年之前
父节点
当前提交
bb2a245477
共有 3 个文件被更改,包括 9 次插入31 次删除
  1. 3 6
      dist/js/splide.js
  2. 5 24
      src/js/components/Controller/Controller.ts
  3. 1 1
      src/js/components/Sync/Sync.ts

+ 3 - 6
dist/js/splide.js

@@ -1358,17 +1358,14 @@
       if (!isBusy()) {
         const [dest, forwards] = parse(control);
         const index = loop(dest);
-        if (canGo(dest, index)) {
+        const canGo = dest === index || Move.exceededLimit(!forwards) || Move.canShift(forwards);
+        if (index > -1 && canGo) {
           Scroll.cancel();
           setIndex(index);
           Move.move(dest, index, prevIndex, forwards, callback);
         }
       }
     }
-    function canGo(dest, index) {
-      const forward = dest > prevIndex;
-      return index > -1 && (index !== currIndex || !isMoving()) && (dest === index || Move.exceededLimit(!forward) || Move.canShift(forward));
-    }
     function jump(control) {
       const { set } = Components.Breakpoints;
       const { speed } = options;
@@ -2205,7 +2202,7 @@
     function sync(splide, target) {
       const event2 = splide.event.create();
       event2.on(EVENT_MOVE, (index, prev, dest) => {
-        target.go(target.is(LOOP) ? dest : index);
+        target.index !== index && target.go(target.is(LOOP) ? dest : index);
       });
       events.push(event2);
     }

+ 5 - 24
src/js/components/Controller/Controller.ts

@@ -134,7 +134,9 @@ export const Controller: ComponentConstructor<ControllerComponent> = ( Splide, C
   }
 
   /**
-   * Moves the slider by the control pattern.
+   * Moves the carousel by the control pattern.
+   * - `Move.exceededLimit( ! forwards )` checks if the carousel is already shifted
+   * - `Move.canShift( forwards )` checks if there is enough space to shift the carousel.
    *
    * @see `Splide#go()`
    *
@@ -145,8 +147,9 @@ export const Controller: ComponentConstructor<ControllerComponent> = ( Splide, C
     if ( ! isBusy() ) {
       const [ dest, forwards ] = parse( control );
       const index = loop( dest );
+      const canGo = dest === index || Move.exceededLimit( ! forwards ) || Move.canShift( forwards );
 
-      if ( canGo( dest, index ) ) {
+      if ( index > -1 && canGo ) {
         Scroll.cancel();
         setIndex( index );
         Move.move( dest, index, prevIndex, forwards, callback );
@@ -154,28 +157,6 @@ export const Controller: ComponentConstructor<ControllerComponent> = ( Splide, C
     }
   }
 
-  /**
-   * Checks if the carousel can move or not.
-   * - If target and current index are same, allows going only when the carousel is not moving.
-   *   Otherwise, synced carousels will provoke the infinite loop.
-   * - 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.
-   *
-   * @return `true` if the carousel can currently move, or otherwise `false`.
-   */
-  function canGo( dest: number, index: number ): boolean {
-    const forward = dest > prevIndex;
-
-    return index > -1
-      && ( index !== currIndex || ! isMoving() )
-      && ( dest === index || Move.exceededLimit( ! forward ) || Move.canShift( forward ) );
-  }
-
   /**
    * Immediately jumps to the specified index.
    *

+ 1 - 1
src/js/components/Sync/Sync.ts

@@ -95,7 +95,7 @@ export const Sync: ComponentConstructor<SyncComponent> = ( Splide, Components, o
     const event = splide.event.create();
 
     event.on( EVENT_MOVE, ( index, prev, dest ) => {
-      target.go( target.is( LOOP ) ? dest : index );
+      target.index !== index && target.go( target.is( LOOP ) ? dest : index );
     } );
 
     events.push( event );