react-image-gallery
Advanced tools
Comparing version 0.6.5 to 0.6.6
@@ -42,2 +42,3 @@ 'use strict'; | ||
}; | ||
return function () { | ||
@@ -259,3 +260,3 @@ var now = new Date().getTime(); | ||
if (this._canSlideLeft() && !this._intervalId) { | ||
this._slideLeft(); | ||
this._slideLeft();; | ||
} | ||
@@ -321,6 +322,18 @@ break; | ||
}, { | ||
key: '_shouldSlideOnSwipe', | ||
value: function _shouldSlideOnSwipe() { | ||
var shouldSlide = Math.abs(this.state.offsetPercentage) > 30 || this.state.isFlick; | ||
if (shouldSlide) { | ||
// reset isFlick state after so data is not persisted | ||
this.setState({ isFlick: false }); | ||
} | ||
return shouldSlide; | ||
} | ||
}, { | ||
key: '_handleOnSwipedTo', | ||
value: function _handleOnSwipedTo(index) { | ||
var slideTo = this.state.currentIndex; | ||
if (Math.abs(this.state.offsetPercentage) > 30 || this.state.isFlick) { | ||
if (this._shouldSlideOnSwipe()) { | ||
slideTo += index; | ||
@@ -355,7 +368,3 @@ } | ||
value: function _canSlideLeft() { | ||
if (this.props.infinite) { | ||
return true; | ||
} else { | ||
return this.state.currentIndex > 0; | ||
} | ||
return this.props.infinite || this.state.currentIndex > 0; | ||
} | ||
@@ -365,7 +374,3 @@ }, { | ||
value: function _canSlideRight() { | ||
if (this.props.infinite) { | ||
return true; | ||
} else { | ||
return this.state.currentIndex < this.props.items.length - 1; | ||
} | ||
return this.props.infinite || this.state.currentIndex < this.props.items.length - 1; | ||
} | ||
@@ -417,32 +422,52 @@ }, { | ||
value: function _getAlignmentClassName(index) { | ||
var currentIndex = this.state.currentIndex; | ||
var alignment = ''; | ||
var LEFT = 'left'; | ||
var CENTER = 'center'; | ||
var RIGHT = 'right'; | ||
if (this.state.currentIndex === index) { | ||
alignment = ' center'; | ||
} | ||
switch (index) { | ||
case currentIndex - 1: | ||
alignment = ' ' + LEFT; | ||
break; | ||
case currentIndex: | ||
alignment = ' ' + CENTER; | ||
break; | ||
case currentIndex + 1: | ||
alignment = ' ' + RIGHT; | ||
break; | ||
return alignment; | ||
} | ||
}, { | ||
key: '_getTranslateXForTwoSlide', | ||
value: function _getTranslateXForTwoSlide(index) { | ||
// For taking care of infinite swipe when there are only two slides | ||
var _state = this.state; | ||
var currentIndex = _state.currentIndex; | ||
var offsetPercentage = _state.offsetPercentage; | ||
var previousIndex = _state.previousIndex; | ||
var baseTranslateX = -100 * currentIndex; | ||
var translateX = baseTranslateX + index * 100 + offsetPercentage; | ||
// keep track of user swiping direction | ||
if (offsetPercentage > 0) { | ||
this.direction = 'left'; | ||
} else if (offsetPercentage < 0) { | ||
this.direction = 'right'; | ||
} | ||
if (this.props.items.length >= 3 && this.props.infinite) { | ||
if (index === 0 && currentIndex === this.props.items.length - 1) { | ||
// set first slide as right slide if were sliding right from last slide | ||
alignment = ' ' + RIGHT; | ||
} else if (index === this.props.items.length - 1 && currentIndex === 0) { | ||
// set last slide as left slide if were sliding left from first slide | ||
alignment = ' ' + LEFT; | ||
// when swiping make sure the slides are on the correct side | ||
if (currentIndex === 0 && index === 1 && offsetPercentage > 0) { | ||
translateX = -100 + offsetPercentage; | ||
} else if (currentIndex === 1 && index === 0 && offsetPercentage < 0) { | ||
translateX = 100 + offsetPercentage; | ||
} | ||
if (currentIndex !== previousIndex) { | ||
// when swiped move the slide to the correct side | ||
if (previousIndex === 0 && index === 0 && offsetPercentage === 0 && this.direction === 'left') { | ||
translateX = 100; | ||
} else if (previousIndex === 1 && index === 1 && offsetPercentage === 0 && this.direction === 'right') { | ||
translateX = -100; | ||
} | ||
} else { | ||
// keep the slide on the correct slide even when not a swipe | ||
if (currentIndex === 0 && index === 1 && offsetPercentage === 0 && this.direction === 'left') { | ||
translateX = -100; | ||
} else if (currentIndex === 1 && index === 0 && offsetPercentage === 0 && this.direction === 'right') { | ||
translateX = 100; | ||
} | ||
} | ||
return alignment; | ||
return translateX; | ||
} | ||
@@ -452,13 +477,26 @@ }, { | ||
value: function _getSlideStyle(index) { | ||
var _state = this.state; | ||
var currentIndex = _state.currentIndex; | ||
var offsetPercentage = _state.offsetPercentage; | ||
var _state2 = this.state; | ||
var currentIndex = _state2.currentIndex; | ||
var offsetPercentage = _state2.offsetPercentage; | ||
var _props = this.props; | ||
var infinite = _props.infinite; | ||
var items = _props.items; | ||
var basetranslateX = -100 * currentIndex; | ||
var totalSlides = this.props.items.length - 1; | ||
var baseTranslateX = -100 * currentIndex; | ||
var totalSlides = items.length - 1; | ||
var translateX = basetranslateX + index * 100 + offsetPercentage; | ||
// calculates where the other slides belong based on currentIndex | ||
var translateX = baseTranslateX + index * 100 + offsetPercentage; | ||
// adjust zIndex so that only the current slide and the slide were going | ||
// to is at the top layer, this prevents transitions from flying in the | ||
// background when swiping before the first slide or beyond the last slide | ||
var zIndex = 1; | ||
if (index === currentIndex) { | ||
zIndex = 3; | ||
} else if (index === this.state.previousIndex) { | ||
zIndex = 2; | ||
} | ||
if (this.props.infinite && this.props.items.length > 2) { | ||
if (infinite && items.length > 2) { | ||
if (currentIndex === 0 && index === totalSlides) { | ||
@@ -473,7 +511,5 @@ // make the last slide the slide before the first | ||
// current index has more zIndex so slides wont fly by toggling infinite | ||
if (index === currentIndex) { | ||
zIndex = 3; | ||
} else if (index === this.state.previousIndex) { | ||
zIndex = 2; | ||
// Special case when there are only 2 items with infinite on | ||
if (infinite && items.length === 2) { | ||
translateX = this._getTranslateXForTwoSlide(index); | ||
} | ||
@@ -480,0 +516,0 @@ |
{ | ||
"name": "react-image-gallery", | ||
"version": "0.6.5", | ||
"version": "0.6.6", | ||
"description": "React Carousel, React Image gallery, React Slide Show component", | ||
@@ -5,0 +5,0 @@ "main": "./build/image-gallery", |
@@ -1,35 +0,37 @@ | ||
import React from 'react' | ||
import Swipeable from 'react-swipeable' | ||
import React from 'react'; | ||
import Swipeable from 'react-swipeable'; | ||
const MIN_INTERVAL = 500 | ||
const MIN_INTERVAL = 500; | ||
function throttle(func, wait) { | ||
let context, args, result | ||
let timeout = null | ||
let previous = 0 | ||
let context, args, result; | ||
let timeout = null; | ||
let previous = 0; | ||
let later = function() { | ||
previous = new Date().getTime() | ||
timeout = null | ||
result = func.apply(context, args) | ||
if (!timeout) context = args = null | ||
} | ||
previous = new Date().getTime(); | ||
timeout = null; | ||
result = func.apply(context, args); | ||
if (!timeout) context = args = null; | ||
}; | ||
return function() { | ||
let now = new Date().getTime() | ||
let remaining = wait - (now - previous) | ||
context = this | ||
args = arguments | ||
let now = new Date().getTime(); | ||
let remaining = wait - (now - previous); | ||
context = this; | ||
args = arguments; | ||
if (remaining <= 0 || remaining > wait) { | ||
if (timeout) { | ||
clearTimeout(timeout) | ||
timeout = null | ||
clearTimeout(timeout); | ||
timeout = null; | ||
} | ||
previous = now | ||
result = func.apply(context, args) | ||
if (!timeout) context = args = null | ||
previous = now; | ||
result = func.apply(context, args); | ||
if (!timeout) context = args = null; | ||
} else if (!timeout) { | ||
timeout = setTimeout(later, remaining) | ||
timeout = setTimeout(later, remaining); | ||
} | ||
return result | ||
} | ||
return result; | ||
}; | ||
} | ||
@@ -40,11 +42,11 @@ | ||
function debounceEventHandler(...args) { | ||
const throttled = throttle(...args) | ||
const throttled = throttle(...args); | ||
return function(event) { | ||
if (event) { | ||
event.persist() | ||
return throttled(event) | ||
event.persist(); | ||
return throttled(event); | ||
} | ||
return throttled() | ||
} | ||
return throttled(); | ||
}; | ||
} | ||
@@ -56,3 +58,3 @@ | ||
constructor(props) { | ||
super(props) | ||
super(props); | ||
this.state = { | ||
@@ -63,3 +65,3 @@ currentIndex: props.startIndex, | ||
galleryWidth: 0 | ||
} | ||
}; | ||
} | ||
@@ -74,3 +76,3 @@ | ||
-this._getThumbsTranslateX( | ||
this.state.currentIndex > 0 ? 1 : 0) * this.state.currentIndex) | ||
this.state.currentIndex > 0 ? 1 : 0) * this.state.currentIndex); | ||
@@ -81,6 +83,6 @@ } | ||
if (this.props.onSlide) { | ||
this.props.onSlide(this.state.currentIndex) | ||
this.props.onSlide(this.state.currentIndex); | ||
} | ||
this._updateThumbnailTranslateX(prevState) | ||
this._updateThumbnailTranslateX(prevState); | ||
} | ||
@@ -91,10 +93,10 @@ } | ||
this._slideLeft = debounceEventHandler( | ||
this._slideLeft.bind(this), MIN_INTERVAL, true) | ||
this._slideLeft.bind(this), MIN_INTERVAL, true); | ||
this._slideRight = debounceEventHandler( | ||
this._slideRight.bind(this), MIN_INTERVAL, true) | ||
this._slideRight.bind(this), MIN_INTERVAL, true); | ||
this._handleResize = this._handleResize.bind(this) | ||
this._handleKeyDown = this._handleKeyDown.bind(this) | ||
this._thumbnailDelay = 300 | ||
this._handleResize = this._handleResize.bind(this); | ||
this._handleKeyDown = this._handleKeyDown.bind(this); | ||
this._thumbnailDelay = 300; | ||
} | ||
@@ -104,11 +106,11 @@ | ||
// delay initial resize to get the accurate this._imageGallery.offsetWidth | ||
window.setTimeout(() => this._handleResize(), 500) | ||
window.setTimeout(() => this._handleResize(), 500); | ||
if (this.props.autoPlay) { | ||
this.play() | ||
this.play(); | ||
} | ||
if (!this.props.disableArrowKeys) { | ||
window.addEventListener('keydown', this._handleKeyDown) | ||
window.addEventListener('keydown', this._handleKeyDown); | ||
} | ||
window.addEventListener('resize', this._handleResize) | ||
window.addEventListener('resize', this._handleResize); | ||
} | ||
@@ -118,8 +120,8 @@ | ||
if (!this.props.disableArrowKeys) { | ||
window.removeEventListener('keydown', this._handleKeyDown) | ||
window.removeEventListener('keydown', this._handleKeyDown); | ||
} | ||
window.removeEventListener('resize', this._handleResize) | ||
window.removeEventListener('resize', this._handleResize); | ||
if (this._intervalId) { | ||
window.clearInterval(this._intervalId) | ||
this._intervalId = null | ||
window.clearInterval(this._intervalId); | ||
this._intervalId = null; | ||
} | ||
@@ -130,17 +132,17 @@ } | ||
if (this._intervalId) { | ||
return | ||
return; | ||
} | ||
const {slideInterval} = this.props | ||
const {slideInterval} = this.props; | ||
this._intervalId = window.setInterval(() => { | ||
if (!this.state.hovering) { | ||
if (!this.props.infinite && !this._canSlideRight()) { | ||
this.pause() | ||
this.pause(); | ||
} else { | ||
this.slideToIndex(this.state.currentIndex + 1) | ||
this.slideToIndex(this.state.currentIndex + 1); | ||
} | ||
} | ||
}, slideInterval > MIN_INTERVAL ? slideInterval : MIN_INTERVAL) | ||
}, slideInterval > MIN_INTERVAL ? slideInterval : MIN_INTERVAL); | ||
if (this.props.onPlay && callback) { | ||
this.props.onPlay(this.state.currentIndex) | ||
this.props.onPlay(this.state.currentIndex); | ||
} | ||
@@ -152,8 +154,8 @@ | ||
if (this._intervalId) { | ||
window.clearInterval(this._intervalId) | ||
this._intervalId = null | ||
window.clearInterval(this._intervalId); | ||
this._intervalId = null; | ||
} | ||
if (this.props.onPause && callback) { | ||
this.props.onPause(this.state.currentIndex) | ||
this.props.onPause(this.state.currentIndex); | ||
} | ||
@@ -163,12 +165,12 @@ } | ||
fullScreen() { | ||
const gallery = this._imageGallery | ||
const gallery = this._imageGallery; | ||
if (gallery.requestFullscreen) { | ||
gallery.requestFullscreen() | ||
gallery.requestFullscreen(); | ||
} else if (gallery.msRequestFullscreen) { | ||
gallery.msRequestFullscreen() | ||
gallery.msRequestFullscreen(); | ||
} else if (gallery.mozRequestFullScreen) { | ||
gallery.mozRequestFullScreen() | ||
gallery.mozRequestFullScreen(); | ||
} else if (gallery.webkitRequestFullscreen) { | ||
gallery.webkitRequestFullscreen() | ||
gallery.webkitRequestFullscreen(); | ||
} | ||
@@ -179,17 +181,17 @@ } | ||
if (event) { | ||
event.preventDefault() | ||
event.preventDefault(); | ||
if (this._intervalId) { | ||
// user triggered event while ImageGallery is playing, reset interval | ||
this.pause(false) | ||
this.play(false) | ||
this.pause(false); | ||
this.play(false); | ||
} | ||
} | ||
let slideCount = this.props.items.length - 1 | ||
let currentIndex = index | ||
let slideCount = this.props.items.length - 1; | ||
let currentIndex = index; | ||
if (index < 0) { | ||
currentIndex = slideCount | ||
currentIndex = slideCount; | ||
} else if (index > slideCount) { | ||
currentIndex = 0 | ||
currentIndex = 0; | ||
} | ||
@@ -204,7 +206,7 @@ | ||
} | ||
}) | ||
}); | ||
} | ||
getCurrentIndex() { | ||
return this.state.currentIndex | ||
return this.state.currentIndex; | ||
} | ||
@@ -214,3 +216,3 @@ | ||
if (this._imageGallery) { | ||
this.setState({galleryWidth: this._imageGallery.offsetWidth}) | ||
this.setState({galleryWidth: this._imageGallery.offsetWidth}); | ||
} | ||
@@ -220,5 +222,5 @@ } | ||
_handleKeyDown(event) { | ||
const LEFT_ARROW = 37 | ||
const RIGHT_ARROW = 39 | ||
const key = parseInt(event.keyCode || event.which || 0) | ||
const LEFT_ARROW = 37; | ||
const RIGHT_ARROW = 39; | ||
const key = parseInt(event.keyCode || event.which || 0); | ||
@@ -228,10 +230,10 @@ switch(key) { | ||
if (this._canSlideLeft() && !this._intervalId) { | ||
this._slideLeft() | ||
this._slideLeft();; | ||
} | ||
break | ||
break; | ||
case RIGHT_ARROW: | ||
if (this._canSlideRight() && !this._intervalId) { | ||
this._slideRight() | ||
this._slideRight(); | ||
} | ||
break | ||
break; | ||
} | ||
@@ -242,10 +244,10 @@ } | ||
if (this.props.slideOnThumbnailHover) { | ||
this.setState({hovering: true}) | ||
this.setState({hovering: true}); | ||
if (this._thumbnailTimer) { | ||
window.clearTimeout(this._thumbnailTimer) | ||
this._thumbnailTimer = null | ||
window.clearTimeout(this._thumbnailTimer); | ||
this._thumbnailTimer = null; | ||
} | ||
this._thumbnailTimer = window.setTimeout(() => { | ||
this.slideToIndex(index) | ||
}, this._thumbnailDelay) | ||
this.slideToIndex(index); | ||
}, this._thumbnailDelay); | ||
} | ||
@@ -256,17 +258,17 @@ } | ||
if (this._thumbnailTimer) { | ||
window.clearTimeout(this._thumbnailTimer) | ||
this._thumbnailTimer = null | ||
window.clearTimeout(this._thumbnailTimer); | ||
this._thumbnailTimer = null; | ||
if (this.props.autoPlay === true) { | ||
this.play(false) | ||
this.play(false); | ||
} | ||
} | ||
this.setState({hovering: false}) | ||
this.setState({hovering: false}); | ||
} | ||
_handleMouseOver() { | ||
this.setState({hovering: true}) | ||
this.setState({hovering: true}); | ||
} | ||
_handleMouseLeave() { | ||
this.setState({hovering: false}) | ||
this.setState({hovering: false}); | ||
} | ||
@@ -277,3 +279,3 @@ | ||
event.target.src.indexOf(this.props.defaultImage) === -1) { | ||
event.target.src = this.props.defaultImage | ||
event.target.src = this.props.defaultImage; | ||
} | ||
@@ -283,9 +285,21 @@ } | ||
_handleOnSwiped(ev, x, y, isFlick) { | ||
this.setState({isFlick: isFlick}) | ||
this.setState({isFlick: isFlick}); | ||
} | ||
_shouldSlideOnSwipe() { | ||
const shouldSlide = Math.abs(this.state.offsetPercentage) > 30 || | ||
this.state.isFlick; | ||
if (shouldSlide) { | ||
// reset isFlick state after so data is not persisted | ||
this.setState({isFlick: false}); | ||
} | ||
return shouldSlide; | ||
} | ||
_handleOnSwipedTo(index) { | ||
let slideTo = this.state.currentIndex | ||
if (Math.abs(this.state.offsetPercentage) > 30 || this.state.isFlick) { | ||
slideTo += index | ||
let slideTo = this.state.currentIndex; | ||
if (this._shouldSlideOnSwipe()) { | ||
slideTo += index; | ||
} | ||
@@ -295,36 +309,29 @@ | ||
if (!this._canSlideLeft()) { | ||
slideTo = this.state.currentIndex | ||
slideTo = this.state.currentIndex; | ||
} | ||
} else { | ||
if (!this._canSlideRight()) { | ||
slideTo = this.state.currentIndex | ||
slideTo = this.state.currentIndex; | ||
} | ||
} | ||
this.slideToIndex(slideTo) | ||
this.slideToIndex(slideTo); | ||
} | ||
_handleSwiping(index, _, delta) { | ||
const offsetPercentage = index * (delta / this.state.galleryWidth * 100) | ||
this.setState({offsetPercentage: offsetPercentage, style: {}}) | ||
const offsetPercentage = index * (delta / this.state.galleryWidth * 100); | ||
this.setState({offsetPercentage: offsetPercentage, style: {}}); | ||
} | ||
_canNavigate() { | ||
return this.props.items.length >= 2 | ||
return this.props.items.length >= 2; | ||
} | ||
_canSlideLeft() { | ||
if (this.props.infinite) { | ||
return true | ||
} else { | ||
return this.state.currentIndex > 0 | ||
} | ||
return this.props.infinite || this.state.currentIndex > 0; | ||
} | ||
_canSlideRight() { | ||
if (this.props.infinite) { | ||
return true | ||
} else { | ||
return this.state.currentIndex < this.props.items.length - 1 | ||
} | ||
return this.props.infinite || | ||
this.state.currentIndex < this.props.items.length - 1; | ||
} | ||
@@ -334,14 +341,14 @@ | ||
if (this.state.currentIndex === 0) { | ||
this._setThumbsTranslateX(0) | ||
this._setThumbsTranslateX(0); | ||
} else { | ||
let indexDifference = Math.abs( | ||
prevState.currentIndex - this.state.currentIndex) | ||
let scrollX = this._getThumbsTranslateX(indexDifference) | ||
prevState.currentIndex - this.state.currentIndex); | ||
let scrollX = this._getThumbsTranslateX(indexDifference); | ||
if (scrollX > 0) { | ||
if (prevState.currentIndex < this.state.currentIndex) { | ||
this._setThumbsTranslateX( | ||
this.state.thumbsTranslateX - scrollX) | ||
this.state.thumbsTranslateX - scrollX); | ||
} else if (prevState.currentIndex > this.state.currentIndex) { | ||
this._setThumbsTranslateX( | ||
this.state.thumbsTranslateX + scrollX) | ||
this.state.thumbsTranslateX + scrollX); | ||
} | ||
@@ -353,3 +360,3 @@ } | ||
_setThumbsTranslateX(thumbsTranslateX) { | ||
this.setState({thumbsTranslateX}) | ||
this.setState({thumbsTranslateX}); | ||
} | ||
@@ -359,3 +366,3 @@ | ||
if (this.props.disableThumbnailScroll) { | ||
return 0 | ||
return 0; | ||
} | ||
@@ -365,11 +372,11 @@ | ||
if (this._thumbnails.scrollWidth <= this.state.galleryWidth) { | ||
return 0 | ||
return 0; | ||
} | ||
let totalThumbnails = this._thumbnails.children.length | ||
let totalThumbnails = this._thumbnails.children.length; | ||
// total scroll-x required to see the last thumbnail | ||
let totalScrollX = this._thumbnails.scrollWidth - this.state.galleryWidth | ||
let totalScrollX = this._thumbnails.scrollWidth - this.state.galleryWidth; | ||
// scroll-x required per index change | ||
let perIndexScrollX = totalScrollX / (totalThumbnails - 1) | ||
let perIndexScrollX = totalScrollX / (totalThumbnails - 1); | ||
return indexDifference * perIndexScrollX | ||
return indexDifference * perIndexScrollX; | ||
} | ||
@@ -379,59 +386,88 @@ } | ||
_getAlignmentClassName(index) { | ||
let {currentIndex} = this.state | ||
let alignment = '' | ||
const LEFT = 'left' | ||
const CENTER = 'center' | ||
const RIGHT = 'right' | ||
let alignment = ''; | ||
if (this.state.currentIndex === index) { | ||
alignment = ' center'; | ||
} | ||
switch (index) { | ||
case (currentIndex - 1): | ||
alignment = ` ${LEFT}` | ||
break | ||
case (currentIndex): | ||
alignment = ` ${CENTER}` | ||
break | ||
case (currentIndex + 1): | ||
alignment = ` ${RIGHT}` | ||
break | ||
return alignment; | ||
} | ||
_getTranslateXForTwoSlide(index) { | ||
// For taking care of infinite swipe when there are only two slides | ||
const {currentIndex, offsetPercentage, previousIndex} = this.state; | ||
const baseTranslateX = -100 * currentIndex; | ||
let translateX = baseTranslateX + (index * 100) + offsetPercentage; | ||
// keep track of user swiping direction | ||
if (offsetPercentage > 0) { | ||
this.direction = 'left'; | ||
} else if (offsetPercentage < 0) { | ||
this.direction = 'right'; | ||
} | ||
if (this.props.items.length >= 3 && this.props.infinite) { | ||
if (index === 0 && currentIndex === this.props.items.length - 1) { | ||
// set first slide as right slide if were sliding right from last slide | ||
alignment = ` ${RIGHT}` | ||
} else if (index === this.props.items.length - 1 && currentIndex === 0) { | ||
// set last slide as left slide if were sliding left from first slide | ||
alignment = ` ${LEFT}` | ||
// when swiping make sure the slides are on the correct side | ||
if (currentIndex === 0 && index === 1 && offsetPercentage > 0) { | ||
translateX = -100 + offsetPercentage; | ||
} else if (currentIndex === 1 && index === 0 && offsetPercentage < 0) { | ||
translateX = 100 + offsetPercentage; | ||
} | ||
if (currentIndex !== previousIndex) { | ||
// when swiped move the slide to the correct side | ||
if (previousIndex === 0 && index === 0 && | ||
offsetPercentage === 0 && this.direction === 'left') { | ||
translateX = 100; | ||
} else if (previousIndex === 1 && index === 1 && | ||
offsetPercentage === 0 && this.direction === 'right') { | ||
translateX = -100; | ||
} | ||
} else { | ||
// keep the slide on the correct slide even when not a swipe | ||
if (currentIndex === 0 && index === 1 && | ||
offsetPercentage === 0 && this.direction === 'left') { | ||
translateX = -100; | ||
} else if (currentIndex === 1 && index === 0 && | ||
offsetPercentage === 0 && this.direction === 'right') { | ||
translateX = 100; | ||
} | ||
} | ||
return alignment | ||
return translateX; | ||
} | ||
_getSlideStyle(index) { | ||
const {currentIndex, offsetPercentage} = this.state | ||
const basetranslateX = -100 * currentIndex | ||
const totalSlides = this.props.items.length - 1 | ||
const {currentIndex, offsetPercentage} = this.state; | ||
const {infinite, items} = this.props; | ||
const baseTranslateX = -100 * currentIndex; | ||
const totalSlides = items.length - 1; | ||
let translateX = basetranslateX + (index * 100) + offsetPercentage | ||
let zIndex = 1 | ||
// calculates where the other slides belong based on currentIndex | ||
let translateX = baseTranslateX + (index * 100) + offsetPercentage; | ||
if (this.props.infinite && this.props.items.length > 2) { | ||
// adjust zIndex so that only the current slide and the slide were going | ||
// to is at the top layer, this prevents transitions from flying in the | ||
// background when swiping before the first slide or beyond the last slide | ||
let zIndex = 1; | ||
if (index === currentIndex) { | ||
zIndex = 3; | ||
} else if (index === this.state.previousIndex) { | ||
zIndex = 2; | ||
} | ||
if (infinite && items.length > 2) { | ||
if (currentIndex === 0 && index === totalSlides) { | ||
// make the last slide the slide before the first | ||
translateX = -100 + offsetPercentage | ||
translateX = -100 + offsetPercentage; | ||
} else if (currentIndex === totalSlides && index === 0) { | ||
// make the first slide the slide after the last | ||
translateX = 100 + offsetPercentage | ||
translateX = 100 + offsetPercentage; | ||
} | ||
} | ||
// current index has more zIndex so slides wont fly by toggling infinite | ||
if (index === currentIndex) { | ||
zIndex = 3 | ||
} else if (index === this.state.previousIndex) { | ||
zIndex = 2 | ||
// Special case when there are only 2 items with infinite on | ||
if (infinite && items.length === 2) { | ||
translateX = this._getTranslateXForTwoSlide(index); | ||
} | ||
const translate3d = `translate3d(${translateX}%, 0, 0)` | ||
const translate3d = `translate3d(${translateX}%, 0, 0)`; | ||
@@ -445,7 +481,7 @@ return { | ||
zIndex: zIndex | ||
} | ||
}; | ||
} | ||
_getThumbnailStyle() { | ||
const translate3d = `translate3d(${this.state.thumbsTranslateX}px, 0, 0)` | ||
const translate3d = `translate3d(${this.state.thumbsTranslateX}px, 0, 0)`; | ||
return { | ||
@@ -457,15 +493,15 @@ WebkitTransform: translate3d, | ||
transform: translate3d | ||
} | ||
}; | ||
} | ||
_slideLeft(event) { | ||
this.slideToIndex(this.state.currentIndex - 1, event) | ||
this.slideToIndex(this.state.currentIndex - 1, event); | ||
} | ||
_slideRight(event) { | ||
this.slideToIndex(this.state.currentIndex + 1, event) | ||
this.slideToIndex(this.state.currentIndex + 1, event); | ||
} | ||
_renderItem(item) { | ||
const onImageError = this.props.onImageError || this._handleImageError | ||
const onImageError = this.props.onImageError || this._handleImageError; | ||
@@ -489,25 +525,25 @@ return ( | ||
</div> | ||
) | ||
); | ||
} | ||
render() { | ||
const {currentIndex} = this.state | ||
const thumbnailStyle = this._getThumbnailStyle() | ||
const {currentIndex} = this.state; | ||
const thumbnailStyle = this._getThumbnailStyle(); | ||
const slideLeft = this._slideLeft.bind(this) | ||
const slideRight = this._slideRight.bind(this) | ||
const slideLeft = this._slideLeft.bind(this); | ||
const slideRight = this._slideRight.bind(this); | ||
let slides = [] | ||
let thumbnails = [] | ||
let bullets = [] | ||
let slides = []; | ||
let thumbnails = []; | ||
let bullets = []; | ||
this.props.items.map((item, index) => { | ||
const alignment = this._getAlignmentClassName(index) | ||
const alignment = this._getAlignmentClassName(index); | ||
const originalClass = item.originalClass ? | ||
` ${item.originalClass}` : '' | ||
` ${item.originalClass}` : ''; | ||
const thumbnailClass = item.thumbnailClass ? | ||
` ${item.thumbnailClass}` : '' | ||
` ${item.thumbnailClass}` : ''; | ||
const renderItem = item.renderItem || | ||
this.props.renderItem || this._renderItem.bind(this) | ||
this.props.renderItem || this._renderItem.bind(this); | ||
@@ -523,15 +559,15 @@ const slide = ( | ||
</div> | ||
) | ||
); | ||
if (this.props.lazyLoad) { | ||
if (alignment) { | ||
slides.push(slide) | ||
slides.push(slide); | ||
} | ||
} else { | ||
slides.push(slide) | ||
slides.push(slide); | ||
} | ||
let onThumbnailError = this._handleImageError | ||
let onThumbnailError = this._handleImageError; | ||
if (this.props.onThumbnailError) { | ||
onThumbnailError = this.props.onThumbnailError | ||
onThumbnailError = this.props.onThumbnailError; | ||
} | ||
@@ -559,3 +595,3 @@ | ||
</a> | ||
) | ||
); | ||
} | ||
@@ -574,5 +610,5 @@ | ||
</li> | ||
) | ||
); | ||
} | ||
}) | ||
}); | ||
@@ -663,3 +699,3 @@ return ( | ||
</section> | ||
) | ||
); | ||
} | ||
@@ -693,3 +729,3 @@ | ||
renderItem: React.PropTypes.func, | ||
} | ||
}; | ||
@@ -711,2 +747,2 @@ ImageGallery.defaultProps = { | ||
slideInterval: 3000 | ||
} | ||
}; |
Sorry, the diff of this file is too big to display
263221
2268