From 4f5aa202dd34ddadeb604ddffbd15744d72547da Mon Sep 17 00:00:00 2001 From: David DeSandro Date: Wed, 16 Dec 2015 10:40:38 -0500 Subject: [PATCH] Fix initializing with high initialIndex; + Fixes #291 + add instant argument for select, fixes #128 + remove updating in activating page dots and prev/next --- js/flickity.js | 35 ++++++++++++++++++++++++++--------- js/page-dots.js | 1 - js/prev-next-button.js | 4 +++- sandbox/single.html | 3 ++- test/unit/empty.js | 9 +++++++++ 5 files changed, 40 insertions(+), 12 deletions(-) diff --git a/js/flickity.js b/js/flickity.js index b29e205f..5704a6f3 100644 --- a/js/flickity.js +++ b/js/flickity.js @@ -120,7 +120,7 @@ Flickity.prototype._create = function() { this.element.flickityGUID = id; // expando instances[ id ] = this; // associate via id // initial properties - this.selectedIndex = this.options.initialIndex || 0; + this.selectedIndex = 0; // how many frames slider has been in same position this.restingFrames = 0; // initial physics properties @@ -188,8 +188,19 @@ Flickity.prototype.activate = function() { this.emit('activate'); - this.positionSliderAtSelected(); - this.select( this.selectedIndex ); + var index; + var initialIndex = this.options.initialIndex; + if ( this.isInitActivated ) { + index = this.selectedIndex; + } else if ( initialIndex !== undefined ) { + index = this.cells[ initialIndex ] ? initialIndex : 0; + } else { + index = 0; + } + // select instantly + this.select( index, false, true ); + // flag for initial activation, for using initialIndex + this.isInitActivated = true; }; // slider positions the cells @@ -420,8 +431,9 @@ Flickity.prototype.dispatchEvent = function( type, event, args ) { /** * @param {Integer} index - index of the cell * @param {Boolean} isWrap - will wrap-around to last/first if at the end + * @param {Boolean} isInstant - will immediately set position at selected cell */ -Flickity.prototype.select = function( index, isWrap ) { +Flickity.prototype.select = function( index, isWrap, isInstant ) { if ( !this.isActive ) { return; } @@ -439,13 +451,18 @@ Flickity.prototype.select = function( index, isWrap ) { if ( this.options.wrapAround || isWrap ) { index = utils.modulo( index, len ); } - - if ( this.cells[ index ] ) { - this.selectedIndex = index; - this.setSelectedCell(); + // bail if invalid index + if ( !this.cells[ index ] ) { + return; + } + this.selectedIndex = index; + this.setSelectedCell(); + if ( isInstant ) { + this.positionSliderAtSelected(); + } else { this.startAnimation(); - this.dispatchEvent('cellSelect'); } + this.dispatchEvent('cellSelect'); }; Flickity.prototype.previous = function( isWrap ) { diff --git a/js/page-dots.js b/js/page-dots.js index 45dbb469..d604dfdd 100644 --- a/js/page-dots.js +++ b/js/page-dots.js @@ -68,7 +68,6 @@ PageDots.prototype._create = function() { PageDots.prototype.activate = function() { this.setDots(); - this.updateSelected(); this.bindTap( this.holder ); // add to DOM this.parent.element.appendChild( this.holder ); diff --git a/js/prev-next-button.js b/js/prev-next-button.js index 519815ba..503fe1a2 100644 --- a/js/prev-next-button.js +++ b/js/prev-next-button.js @@ -79,6 +79,9 @@ PrevNextButton.prototype._create = function() { element.className += this.isPrevious ? ' previous' : ' next'; // prevent button from submitting form http://stackoverflow.com/a/10836076/182183 element.setAttribute( 'type', 'button' ); + // init as disabled + this.disable(); + Flickity.setUnselectable( element ); // create arrow if ( supportsInlineSVG() ) { @@ -104,7 +107,6 @@ PrevNextButton.prototype._create = function() { }; PrevNextButton.prototype.activate = function() { - this.update(); this.bindTap( this.element ); // click events from keyboard eventie.bind( this.element, 'click', this ); diff --git a/sandbox/single.html b/sandbox/single.html index 56d0611b..49492d65 100644 --- a/sandbox/single.html +++ b/sandbox/single.html @@ -48,7 +48,8 @@

single

diff --git a/test/unit/empty.js b/test/unit/empty.js index 2fc1e136..c8f865f7 100644 --- a/test/unit/empty.js +++ b/test/unit/empty.js @@ -27,4 +27,13 @@ test('empty', function() { ok( flkty.nextButton.element.disabled, 'next button disabled' ); equal( flkty.pageDots.dots.length, 1, '1 page dots'); + // destroy and re-init with higher initialIndex + flkty.destroy(); + flkty = new Flickity( gallery, { + initialIndex: 2 + }); + + // #291 + ok( true, 'initializing with initialIndex > cells doesnt throw error' ); + });