Skip to content

Commit 4f5aa20

Browse files
committed
Fix initializing with high initialIndex;
+ Fixes metafizzy#291 + add instant argument for select, fixes metafizzy#128 + remove updating in activating page dots and prev/next
1 parent 33d144e commit 4f5aa20

5 files changed

Lines changed: 40 additions & 12 deletions

File tree

js/flickity.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ Flickity.prototype._create = function() {
120120
this.element.flickityGUID = id; // expando
121121
instances[ id ] = this; // associate via id
122122
// initial properties
123-
this.selectedIndex = this.options.initialIndex || 0;
123+
this.selectedIndex = 0;
124124
// how many frames slider has been in same position
125125
this.restingFrames = 0;
126126
// initial physics properties
@@ -188,8 +188,19 @@ Flickity.prototype.activate = function() {
188188

189189
this.emit('activate');
190190

191-
this.positionSliderAtSelected();
192-
this.select( this.selectedIndex );
191+
var index;
192+
var initialIndex = this.options.initialIndex;
193+
if ( this.isInitActivated ) {
194+
index = this.selectedIndex;
195+
} else if ( initialIndex !== undefined ) {
196+
index = this.cells[ initialIndex ] ? initialIndex : 0;
197+
} else {
198+
index = 0;
199+
}
200+
// select instantly
201+
this.select( index, false, true );
202+
// flag for initial activation, for using initialIndex
203+
this.isInitActivated = true;
193204
};
194205

195206
// slider positions the cells
@@ -420,8 +431,9 @@ Flickity.prototype.dispatchEvent = function( type, event, args ) {
420431
/**
421432
* @param {Integer} index - index of the cell
422433
* @param {Boolean} isWrap - will wrap-around to last/first if at the end
434+
* @param {Boolean} isInstant - will immediately set position at selected cell
423435
*/
424-
Flickity.prototype.select = function( index, isWrap ) {
436+
Flickity.prototype.select = function( index, isWrap, isInstant ) {
425437
if ( !this.isActive ) {
426438
return;
427439
}
@@ -439,13 +451,18 @@ Flickity.prototype.select = function( index, isWrap ) {
439451
if ( this.options.wrapAround || isWrap ) {
440452
index = utils.modulo( index, len );
441453
}
442-
443-
if ( this.cells[ index ] ) {
444-
this.selectedIndex = index;
445-
this.setSelectedCell();
454+
// bail if invalid index
455+
if ( !this.cells[ index ] ) {
456+
return;
457+
}
458+
this.selectedIndex = index;
459+
this.setSelectedCell();
460+
if ( isInstant ) {
461+
this.positionSliderAtSelected();
462+
} else {
446463
this.startAnimation();
447-
this.dispatchEvent('cellSelect');
448464
}
465+
this.dispatchEvent('cellSelect');
449466
};
450467

451468
Flickity.prototype.previous = function( isWrap ) {

js/page-dots.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ PageDots.prototype._create = function() {
6868

6969
PageDots.prototype.activate = function() {
7070
this.setDots();
71-
this.updateSelected();
7271
this.bindTap( this.holder );
7372
// add to DOM
7473
this.parent.element.appendChild( this.holder );

js/prev-next-button.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ PrevNextButton.prototype._create = function() {
7979
element.className += this.isPrevious ? ' previous' : ' next';
8080
// prevent button from submitting form http://stackoverflow.com/a/10836076/182183
8181
element.setAttribute( 'type', 'button' );
82+
// init as disabled
83+
this.disable();
84+
8285
Flickity.setUnselectable( element );
8386
// create arrow
8487
if ( supportsInlineSVG() ) {
@@ -104,7 +107,6 @@ PrevNextButton.prototype._create = function() {
104107
};
105108

106109
PrevNextButton.prototype.activate = function() {
107-
this.update();
108110
this.bindTap( this.element );
109111
// click events from keyboard
110112
eventie.bind( this.element, 'click', this );

sandbox/single.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ <h1>single</h1>
4848
<script>
4949
window.onload = function() {
5050

51-
var flkty = window.flkty = new Flickity('#gallery');
51+
var flkty = window.flkty = new Flickity('#gallery', {
52+
});
5253

5354
};
5455
</script>

test/unit/empty.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,13 @@ test('empty', function() {
2727
ok( flkty.nextButton.element.disabled, 'next button disabled' );
2828
equal( flkty.pageDots.dots.length, 1, '1 page dots');
2929

30+
// destroy and re-init with higher initialIndex
31+
flkty.destroy();
32+
flkty = new Flickity( gallery, {
33+
initialIndex: 2
34+
});
35+
36+
// #291
37+
ok( true, 'initializing with initialIndex > cells doesnt throw error' );
38+
3039
});

0 commit comments

Comments
 (0)