-
Notifications
You must be signed in to change notification settings - Fork 13
/
as-nav-for.js
127 lines (101 loc) · 3.41 KB
/
as-nav-for.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*!
* Flickity asNavFor v3.0.0
* enable asNavFor for Flickity
*/
( function( window, factory ) {
// universal module definition
if ( typeof module == 'object' && module.exports ) {
// CommonJS
module.exports = factory(
require('flickity'),
require('fizzy-ui-utils'),
);
} else {
// browser global
window.Flickity = factory(
window.Flickity,
window.fizzyUIUtils,
);
}
}( window, function factory( Flickity, utils ) {
// -------------------------- asNavFor prototype -------------------------- //
// Flickity.defaults.asNavFor = null;
Flickity.create.asNavFor = function() {
this.on( 'activate', this.activateAsNavFor );
this.on( 'deactivate', this.deactivateAsNavFor );
this.on( 'destroy', this.destroyAsNavFor );
let asNavForOption = this.options.asNavFor;
if ( !asNavForOption ) return;
// HACK do async, give time for other flickity to be initalized
setTimeout( () => {
this.setNavCompanion( asNavForOption );
} );
};
let proto = Flickity.prototype;
proto.setNavCompanion = function( elem ) {
elem = utils.getQueryElement( elem );
let companion = Flickity.data( elem );
// stop if no companion or companion is self
if ( !companion || companion === this ) return;
this.navCompanion = companion;
// companion select
this.onNavCompanionSelect = () => {
this.navCompanionSelect();
};
companion.on( 'select', this.onNavCompanionSelect );
// click
this.on( 'staticClick', this.onNavStaticClick );
this.navCompanionSelect( true );
};
proto.navCompanionSelect = function( isInstant ) {
// wait for companion & selectedCells first. #8
let companionCells = this.navCompanion && this.navCompanion.selectedCells;
if ( !companionCells ) return;
// select slide that matches first cell of slide
let selectedCell = companionCells[0];
let firstIndex = this.navCompanion.cells.indexOf( selectedCell );
let lastIndex = firstIndex + companionCells.length - 1;
let selectIndex = Math.floor( lerp( firstIndex, lastIndex,
this.navCompanion.cellAlign ) );
this.selectCell( selectIndex, false, isInstant );
// set nav selected class
this.removeNavSelectedElements();
// stop if companion has more cells than this one
if ( selectIndex >= this.cells.length ) return;
let selectedCells = this.cells.slice( firstIndex, lastIndex + 1 );
this.navSelectedElements = selectedCells.map( ( cell ) => cell.element );
this.changeNavSelectedClass('add');
};
function lerp( a, b, t ) {
return ( b - a ) * t + a;
}
proto.changeNavSelectedClass = function( method ) {
this.navSelectedElements.forEach( function( navElem ) {
navElem.classList[ method ]('is-nav-selected');
} );
};
proto.activateAsNavFor = function() {
this.navCompanionSelect( true );
};
proto.removeNavSelectedElements = function() {
if ( !this.navSelectedElements ) return;
this.changeNavSelectedClass('remove');
delete this.navSelectedElements;
};
proto.onNavStaticClick = function( event, pointer, cellElement, cellIndex ) {
if ( typeof cellIndex == 'number' ) {
this.navCompanion.selectCell( cellIndex );
}
};
proto.deactivateAsNavFor = function() {
this.removeNavSelectedElements();
};
proto.destroyAsNavFor = function() {
if ( !this.navCompanion ) return;
this.navCompanion.off( 'select', this.onNavCompanionSelect );
this.off( 'staticClick', this.onNavStaticClick );
delete this.navCompanion;
};
// ----- ----- //
return Flickity;
} ) );