Skip to content

Commit

Permalink
πŸ”” enable initialIndex selector string #881, #818
Browse files Browse the repository at this point in the history
add selectInitialIndex

βœ… initialIndex tests
  • Loading branch information
desandro committed Jan 28, 2019
1 parent aa22639 commit 578f710
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
38 changes: 26 additions & 12 deletions js/flickity.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,7 @@ proto.activate = function() {
}

this.emitEvent('activate');

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 );
this.selectInitialIndex();
// flag for initial activation, for using initialIndex
this.isInitActivated = true;
// ready event. #493
Expand Down Expand Up @@ -603,6 +592,31 @@ proto.unselectSelectedSlide = function() {
}
};

proto.selectInitialIndex = function() {
var initialIndex = this.options.initialIndex;
// already activated, select previous selectedIndex
if ( this.isInitActivated ) {
this.select( this.selectedIndex, false, true );
return;
}
// select with selector string
if ( initialIndex && typeof initialIndex == 'string' ) {
var cell = this.queryCell( initialIndex );
if ( cell ) {
this.selectCell( initialIndex, false, true );
return;
}
}

var index = 0;
// select with number
if ( initialIndex && this.slides[ initialIndex ] ) {
index = initialIndex;
}
// select instantly
this.select( index, false, true );
};

/**
* select slide from number or cell element
* @param {Element or Number} elem
Expand Down
15 changes: 15 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<script src="unit/adaptive-height.js"></script>
<script src="unit/select-cell.js"></script>
<script src="unit/change.js"></script>
<script src="unit/initial-index.js"></script>

</head>
<body>
Expand Down Expand Up @@ -297,5 +298,19 @@ <h2>change</h2>
<div class="cell">4</div>
</div>

<h2>initialIndex</h2>
<div id="initial-index" class="gallery variable-width">
<div class="cell">0</div>
<div class="cell">1</div>
<div class="cell">2</div>
<div class="cell">3</div>
<div class="cell cell--initial">4</div>
<div class="cell">5</div>
<div class="cell">6</div>
<div class="cell">7</div>
<div class="cell">8</div>
<div class="cell">9</div>
</div>

</body>
</html>
26 changes: 26 additions & 0 deletions test/unit/initial-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
QUnit.test( 'initialIndex', function( assert ) {
'use strict';
// initialIndex number
var flkty = new Flickity( '#initial-index', {
initialIndex: 3,
});
assert.equal( flkty.selectedIndex, 3, 'initialIndex number' );
// selectedIndex remains same after reactivation
flkty.deactivate();
flkty.activate();
assert.equal( flkty.selectedIndex, 3, 'reactivated selectedIndex stays the same' );
flkty.destroy();
// initialIndex selector string
flkty = new Flickity( '#initial-index', {
initialIndex: '.cell--initial',
});
assert.equal( flkty.selectedIndex, 4, 'initialIndex selector string' );
flkty.destroy();
// initialIndex selector string with groupCells #881
flkty = new Flickity( '#initial-index', {
groupCells: 3,
initialIndex: '.cell--initial',
});
assert.equal( flkty.selectedIndex, 1, 'initialIndex selector string with groupCells' );

});

0 comments on commit 578f710

Please sign in to comment.