Skip to content

Commit

Permalink
Fix initializing with high initialIndex;
Browse files Browse the repository at this point in the history
+ Fixes #291
+ add instant argument for select, fixes #128
+ remove updating in activating page dots and prev/next
  • Loading branch information
desandro committed Dec 16, 2015
1 parent 33d144e commit 4f5aa20
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
35 changes: 26 additions & 9 deletions js/flickity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 ) {
Expand Down
1 change: 0 additions & 1 deletion js/page-dots.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
4 changes: 3 additions & 1 deletion js/prev-next-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() ) {
Expand All @@ -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 );
Expand Down
3 changes: 2 additions & 1 deletion sandbox/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ <h1>single</h1>
<script>
window.onload = function() {

var flkty = window.flkty = new Flickity('#gallery');
var flkty = window.flkty = new Flickity('#gallery', {
});

};
</script>
Expand Down
9 changes: 9 additions & 0 deletions test/unit/empty.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

});

0 comments on commit 4f5aa20

Please sign in to comment.