Skip to content

Commit

Permalink
Carousel v2: add logic for spacers for looping. (#20293)
Browse files Browse the repository at this point in the history
This implements the logic needed to ensure the correct amount of space is reserved for either side of the carousel when looping. The logic to snap slides as well as to actually move them still needs to be added.
  • Loading branch information
Sepand Parhami authored Jan 16, 2019
1 parent 2fc85a2 commit d135c75
Show file tree
Hide file tree
Showing 3 changed files with 465 additions and 6 deletions.
56 changes: 56 additions & 0 deletions extensions/amp-carousel/0.2/array-util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Copyright 2019 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {mod} from '../../../src/utils/math';

/**
* Calculates the distance between two indicies in an Array, treating the first
* and last indicies as adjacent. In the array ['a', 'b', 'c'], the
* wrappingDistance between 'a' and 'c' is 1. Likewise, the wrappingDistance
* betweeb 'a' and 'b' is also 1.
* @param {number} a A start index.
* @param {number} b An end index.
* @param {!IArrayLike} arr An array like Object.
*/
export function wrappingDistance(a, b, arr) {
return a === b ? 0 : Math.min(
forwardWrappingDistance(a, b, arr),
backwardWrappingDistance(a, b, arr));
}

/**
* Calculates the forwards distance between two indicies in an Array, treating
* the first and last indicies as adjacent. In the array ['a', 'b', 'c'], the
* forwardWrappingDistance between 'b' and 'a' is 2.
* @param {number} a A start index.
* @param {number} b An end index.
* @param {!IArrayLike} arr An array like Object.
*/
export function forwardWrappingDistance(a, b, {length}) {
return a === b ? length : mod(b - a, length);
}

/**
* Calculates the backwards distance between two indicies in an Array, treating
* the first and last indicies as adjacent. In the array ['a', 'b', 'c'], the
* backwardWrappingDistance between 'a' and 'b' is 2.
* @param {number} a A start index.
* @param {number} b An end index.
* @param {!IArrayLike} arr An array like Object.
*/
export function backwardWrappingDistance(a, b, {length}) {
return a === b ? length : mod(a - b, length);
}
Loading

0 comments on commit d135c75

Please sign in to comment.