-
Notifications
You must be signed in to change notification settings - Fork 602
/
core.js
882 lines (761 loc) · 24.8 KB
/
core.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
// Flickity main
/* eslint-disable max-params */
( function( window, factory ) {
// universal module definition
if ( typeof module == 'object' && module.exports ) {
// CommonJS
module.exports = factory(
window,
require('ev-emitter'),
require('get-size'),
require('fizzy-ui-utils'),
require('./cell'),
require('./slide'),
require('./animate'),
);
} else {
// browser global
let _Flickity = window.Flickity;
window.Flickity = factory(
window,
window.EvEmitter,
window.getSize,
window.fizzyUIUtils,
_Flickity.Cell,
_Flickity.Slide,
_Flickity.animatePrototype,
);
}
}( typeof window != 'undefined' ? window : this,
function factory( window, EvEmitter, getSize, utils, Cell, Slide, animatePrototype ) {
/* eslint-enable max-params */
// vars
const { getComputedStyle, console } = window;
let { jQuery } = window;
// -------------------------- Flickity -------------------------- //
// globally unique identifiers
let GUID = 0;
// internal store of all Flickity intances
let instances = {};
function Flickity( element, options ) {
let queryElement = utils.getQueryElement( element );
if ( !queryElement ) {
if ( console ) console.error(`Bad element for Flickity: ${queryElement || element}`);
return;
}
this.element = queryElement;
// do not initialize twice on same element
if ( this.element.flickityGUID ) {
let instance = instances[ this.element.flickityGUID ];
if ( instance ) instance.option( options );
return instance;
}
// add jQuery
if ( jQuery ) {
this.$element = jQuery( this.element );
}
// options
this.options = { ...this.constructor.defaults };
this.option( options );
// kick things off
this._create();
}
Flickity.defaults = {
accessibility: true,
// adaptiveHeight: false,
cellAlign: 'center',
// cellSelector: undefined,
// contain: false,
freeScrollFriction: 0.075, // friction when free-scrolling
friction: 0.28, // friction when selecting
namespaceJQueryEvents: true,
// initialIndex: 0,
percentPosition: true,
resize: true,
selectedAttraction: 0.025,
setGallerySize: true,
// watchCSS: false,
// wrapAround: false
};
// hash of methods triggered on _create()
Flickity.create = {};
let proto = Flickity.prototype;
// inherit EventEmitter
Object.assign( proto, EvEmitter.prototype );
proto._create = function() {
let { resize, watchCSS, rightToLeft } = this.options;
// add id for Flickity.data
let id = this.guid = ++GUID;
this.element.flickityGUID = id; // expando
instances[ id ] = this; // associate via id
// initial properties
this.selectedIndex = 0;
// how many frames slider has been in same position
this.restingFrames = 0;
// initial physics properties
this.x = 0;
this.velocity = 0;
this.beginMargin = rightToLeft ? 'marginRight' : 'marginLeft';
this.endMargin = rightToLeft ? 'marginLeft' : 'marginRight';
// create viewport & slider
this.viewport = document.createElement('div');
this.viewport.className = 'flickity-viewport';
this._createSlider();
// used for keyboard navigation
this.focusableElems = [ this.element ];
if ( resize || watchCSS ) {
window.addEventListener( 'resize', this );
}
// add listeners from on option
for ( let eventName in this.options.on ) {
let listener = this.options.on[ eventName ];
this.on( eventName, listener );
}
for ( let method in Flickity.create ) {
Flickity.create[ method ].call( this );
}
if ( watchCSS ) {
this.watchCSS();
} else {
this.activate();
}
};
/**
* set options
* @param {Object} opts - options to extend
*/
proto.option = function( opts ) {
Object.assign( this.options, opts );
};
proto.activate = function() {
if ( this.isActive ) return;
this.isActive = true;
this.element.classList.add('flickity-enabled');
if ( this.options.rightToLeft ) {
this.element.classList.add('flickity-rtl');
}
this.getSize();
// move initial cell elements so they can be loaded as cells
let cellElems = this._filterFindCellElements( this.element.children );
this.slider.append( ...cellElems );
this.viewport.append( this.slider );
this.element.append( this.viewport );
// get cells from children
this.reloadCells();
if ( this.options.accessibility ) {
// allow element to focusable
this.element.tabIndex = 0;
// listen for key presses
this.element.addEventListener( 'keydown', this );
}
this.emitEvent('activate');
this.selectInitialIndex();
// flag for initial activation, for using initialIndex
this.isInitActivated = true;
// ready event. #493
this.dispatchEvent('ready');
};
// slider positions the cells
proto._createSlider = function() {
// slider element does all the positioning
let slider = document.createElement('div');
slider.className = 'flickity-slider';
this.slider = slider;
};
proto._filterFindCellElements = function( elems ) {
return utils.filterFindElements( elems, this.options.cellSelector );
};
// goes through all children
proto.reloadCells = function() {
// collection of item elements
this.cells = this._makeCells( this.slider.children );
this.positionCells();
this._updateWrapShiftCells();
this.setGallerySize();
};
/**
* turn elements into Flickity.Cells
* @param {[Array, NodeList, HTMLElement]} elems - elements to make into cells
* @returns {Array} items - collection of new Flickity Cells
*/
proto._makeCells = function( elems ) {
let cellElems = this._filterFindCellElements( elems );
// create new Cells for collection
return cellElems.map( ( cellElem ) => new Cell( cellElem ) );
};
proto.getLastCell = function() {
return this.cells[ this.cells.length - 1 ];
};
proto.getLastSlide = function() {
return this.slides[ this.slides.length - 1 ];
};
// positions all cells
proto.positionCells = function() {
// size all cells
this._sizeCells( this.cells );
// position all cells
this._positionCells( 0 );
};
/**
* position certain cells
* @param {Integer} index - which cell to start with
*/
proto._positionCells = function( index ) {
index = index || 0;
// also measure maxCellHeight
// start 0 if positioning all cells
this.maxCellHeight = index ? this.maxCellHeight || 0 : 0;
let cellX = 0;
// get cellX
if ( index > 0 ) {
let startCell = this.cells[ index - 1 ];
cellX = startCell.x + startCell.size.outerWidth;
}
this.cells.slice( index ).forEach( ( cell ) => {
cell.x = cellX;
this._renderCellPosition( cell, cellX );
cellX += cell.size.outerWidth;
this.maxCellHeight = Math.max( cell.size.outerHeight, this.maxCellHeight );
} );
// keep track of cellX for wrap-around
this.slideableWidth = cellX;
// slides
this.updateSlides();
// contain slides target
this._containSlides();
// update slidesWidth
this.slidesWidth = this.cells.length ?
this.getLastSlide().target - this.slides[0].target : 0;
};
proto._renderCellPosition = function( cell, x ) {
// render position of cell with in slider
let sideOffset = this.options.rightToLeft ? -1 : 1;
let renderX = x * sideOffset;
if ( this.options.percentPosition ) renderX *= this.size.innerWidth / cell.size.width;
let positionValue = this.getPositionValue( renderX );
cell.element.style.transform = `translateX( ${positionValue} )`;
};
/**
* cell.getSize() on multiple cells
* @param {Array} cells - cells to size
*/
proto._sizeCells = function( cells ) {
cells.forEach( ( cell ) => cell.getSize() );
};
// -------------------------- -------------------------- //
proto.updateSlides = function() {
this.slides = [];
if ( !this.cells.length ) return;
let { beginMargin, endMargin } = this;
let slide = new Slide( beginMargin, endMargin, this.cellAlign );
this.slides.push( slide );
let canCellFit = this._getCanCellFit();
this.cells.forEach( ( cell, i ) => {
// just add cell if first cell in slide
if ( !slide.cells.length ) {
slide.addCell( cell );
return;
}
let slideWidth = ( slide.outerWidth - slide.firstMargin ) +
( cell.size.outerWidth - cell.size[ endMargin ] );
if ( canCellFit( i, slideWidth ) ) {
slide.addCell( cell );
} else {
// doesn't fit, new slide
slide.updateTarget();
slide = new Slide( beginMargin, endMargin, this.cellAlign );
this.slides.push( slide );
slide.addCell( cell );
}
} );
// last slide
slide.updateTarget();
// update .selectedSlide
this.updateSelectedSlide();
};
proto._getCanCellFit = function() {
let { groupCells } = this.options;
if ( !groupCells ) return () => false;
if ( typeof groupCells == 'number' ) {
// group by number. 3 -> [0,1,2], [3,4,5], ...
let number = parseInt( groupCells, 10 );
return ( i ) => ( i % number ) !== 0;
}
// default, group by width of slide
let percent = 1;
// parse '75%
let percentMatch = typeof groupCells == 'string' && groupCells.match( /^(\d+)%$/ );
if ( percentMatch ) percent = parseInt( percentMatch[1], 10 ) / 100;
let groupWidth = ( this.size.innerWidth + 1 ) * percent;
return ( i, slideWidth ) => slideWidth <= groupWidth;
};
// alias _init for jQuery plugin .flickity()
proto._init =
proto.reposition = function() {
this.positionCells();
this.positionSliderAtSelected();
};
proto.getSize = function() {
this.size = getSize( this.element );
this.setCellAlign();
this.cursorPosition = this.size.innerWidth * this.cellAlign;
};
let cellAlignShorthands = {
left: 0,
center: 0.5,
right: 1,
};
proto.setCellAlign = function() {
let { cellAlign, rightToLeft } = this.options;
let shorthand = cellAlignShorthands[ cellAlign ];
this.cellAlign = shorthand !== undefined ? shorthand : cellAlign;
if ( rightToLeft ) this.cellAlign = 1 - this.cellAlign;
};
proto.setGallerySize = function() {
if ( !this.options.setGallerySize ) return;
let height = this.options.adaptiveHeight && this.selectedSlide ?
this.selectedSlide.height : this.maxCellHeight;
this.viewport.style.height = `${height}px`;
};
proto._updateWrapShiftCells = function() {
// update isWrapping
this.isWrapping = this.getIsWrapping();
// only for wrap-around
if ( !this.isWrapping ) return;
// unshift previous cells
this._unshiftCells( this.beforeShiftCells );
this._unshiftCells( this.afterShiftCells );
// get before cells
// initial gap
let beforeGapX = this.cursorPosition;
let lastIndex = this.cells.length - 1;
this.beforeShiftCells = this._getGapCells( beforeGapX, lastIndex, -1 );
// get after cells
// ending gap between last cell and end of gallery viewport
let afterGapX = this.size.innerWidth - this.cursorPosition;
// start cloning at first cell, working forwards
this.afterShiftCells = this._getGapCells( afterGapX, 0, 1 );
};
proto.getIsWrapping = function() {
let { wrapAround } = this.options;
if ( !wrapAround || this.slides.length < 2 ) return false;
if ( wrapAround !== 'fill' ) return true;
// check that slides can fit
let gapWidth = this.slideableWidth - this.size.innerWidth;
if ( gapWidth > this.size.innerWidth ) return true; // gap * 2x big, all good
// check that content width - shifting cell is bigger than viewport width
for ( let cell of this.cells ) {
if ( cell.size.outerWidth > gapWidth ) return false;
}
return true;
};
proto._getGapCells = function( gapX, cellIndex, increment ) {
// keep adding cells until the cover the initial gap
let cells = [];
while ( gapX > 0 ) {
let cell = this.cells[ cellIndex ];
if ( !cell ) break;
cells.push( cell );
cellIndex += increment;
gapX -= cell.size.outerWidth;
}
return cells;
};
// ----- contain & wrap ----- //
// contain cell targets so no excess sliding
proto._containSlides = function() {
let isContaining = this.options.contain && !this.isWrapping &&
this.cells.length;
if ( !isContaining ) return;
let contentWidth = this.slideableWidth - this.getLastCell().size[ this.endMargin ];
// content is less than gallery size
let isContentSmaller = contentWidth < this.size.innerWidth;
if ( isContentSmaller ) {
// all cells fit inside gallery
this.slides.forEach( ( slide ) => {
slide.target = contentWidth * this.cellAlign;
} );
} else {
// contain to bounds
let beginBound = this.cursorPosition + this.cells[0].size[ this.beginMargin ];
let endBound = contentWidth - this.size.innerWidth * ( 1 - this.cellAlign );
this.slides.forEach( ( slide ) => {
slide.target = Math.max( slide.target, beginBound );
slide.target = Math.min( slide.target, endBound );
} );
}
};
// ----- events ----- //
/**
* emits events via eventEmitter and jQuery events
* @param {String} type - name of event
* @param {Event} event - original event
* @param {Array} args - extra arguments
*/
proto.dispatchEvent = function( type, event, args ) {
let emitArgs = event ? [ event ].concat( args ) : args;
this.emitEvent( type, emitArgs );
if ( jQuery && this.$element ) {
// default trigger with type if no event
type += this.options.namespaceJQueryEvents ? '.flickity' : '';
let $event = type;
if ( event ) {
// create jQuery event
let jQEvent = new jQuery.Event( event );
jQEvent.type = type;
$event = jQEvent;
}
this.$element.trigger( $event, args );
}
};
const unidraggerEvents = [
'dragStart',
'dragMove',
'dragEnd',
'pointerDown',
'pointerMove',
'pointerEnd',
'staticClick',
];
let _emitEvent = proto.emitEvent;
proto.emitEvent = function( eventName, args ) {
if ( eventName === 'staticClick' ) {
// add cellElem and cellIndex args to staticClick
let clickedCell = this.getParentCell( args[0].target );
let cellElem = clickedCell && clickedCell.element;
let cellIndex = clickedCell && this.cells.indexOf( clickedCell );
args = args.concat( cellElem, cellIndex );
}
// do regular thing
_emitEvent.call( this, eventName, args );
// duck-punch in jQuery events for Unidragger events
let isUnidraggerEvent = unidraggerEvents.includes( eventName );
if ( !isUnidraggerEvent || !jQuery || !this.$element ) return;
eventName += this.options.namespaceJQueryEvents ? '.flickity' : '';
let event = args.shift( 0 );
let jQEvent = new jQuery.Event( event );
jQEvent.type = eventName;
this.$element.trigger( jQEvent, args );
};
// -------------------------- select -------------------------- //
/**
* @param {Integer} index - index of the slide
* @param {Boolean} isWrap - will wrap-around to last/first if at the end
* @param {Boolean} isInstant - will immediately set position at selected cell
*/
proto.select = function( index, isWrap, isInstant ) {
if ( !this.isActive ) return;
index = parseInt( index, 10 );
this._wrapSelect( index );
if ( this.isWrapping || isWrap ) {
index = utils.modulo( index, this.slides.length );
}
// bail if invalid index
if ( !this.slides[ index ] ) return;
let prevIndex = this.selectedIndex;
this.selectedIndex = index;
this.updateSelectedSlide();
if ( isInstant ) {
this.positionSliderAtSelected();
} else {
this.startAnimation();
}
if ( this.options.adaptiveHeight ) {
this.setGallerySize();
}
// events
this.dispatchEvent( 'select', null, [ index ] );
// change event if new index
if ( index !== prevIndex ) {
this.dispatchEvent( 'change', null, [ index ] );
}
};
// wraps position for wrapAround, to move to closest slide. #113
proto._wrapSelect = function( index ) {
if ( !this.isWrapping ) return;
const { selectedIndex, slideableWidth, slides: { length } } = this;
// shift index for wrap, do not wrap dragSelect
if ( !this.isDragSelect ) {
let wrapIndex = utils.modulo( index, length );
// go to shortest
let delta = Math.abs( wrapIndex - selectedIndex );
let backWrapDelta = Math.abs( ( wrapIndex + length ) - selectedIndex );
let forewardWrapDelta = Math.abs( ( wrapIndex - length ) - selectedIndex );
if ( backWrapDelta < delta ) {
index += length;
} else if ( forewardWrapDelta < delta ) {
index -= length;
}
}
// wrap position so slider is within normal area
if ( index < 0 ) {
this.x -= slideableWidth;
} else if ( index >= length ) {
this.x += slideableWidth;
}
};
proto.previous = function( isWrap, isInstant ) {
this.select( this.selectedIndex - 1, isWrap, isInstant );
};
proto.next = function( isWrap, isInstant ) {
this.select( this.selectedIndex + 1, isWrap, isInstant );
};
proto.updateSelectedSlide = function() {
let slide = this.slides[ this.selectedIndex ];
// selectedIndex could be outside of slides, if triggered before resize()
if ( !slide ) return;
// unselect previous selected slide
this.unselectSelectedSlide();
// update new selected slide
this.selectedSlide = slide;
slide.select();
this.selectedCells = slide.cells;
this.selectedElements = slide.getCellElements();
// HACK: selectedCell & selectedElement is first cell in slide, backwards compatibility
this.selectedCell = slide.cells[0];
this.selectedElement = this.selectedElements[0];
};
proto.unselectSelectedSlide = function() {
if ( this.selectedSlide ) this.selectedSlide.unselect();
};
proto.selectInitialIndex = function() {
let 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' ) {
let cell = this.queryCell( initialIndex );
if ( cell ) {
this.selectCell( initialIndex, false, true );
return;
}
}
let 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, Number]} value - zero-based index or element to select
* @param {Boolean} isWrap - enables wrapping around for extra index
* @param {Boolean} isInstant - disables slide animation
*/
proto.selectCell = function( value, isWrap, isInstant ) {
// get cell
let cell = this.queryCell( value );
if ( !cell ) return;
let index = this.getCellSlideIndex( cell );
this.select( index, isWrap, isInstant );
};
proto.getCellSlideIndex = function( cell ) {
// get index of slide that has cell
let cellSlide = this.slides.find( ( slide ) => slide.cells.includes( cell ) );
return this.slides.indexOf( cellSlide );
};
// -------------------------- get cells -------------------------- //
/**
* get Flickity.Cell, given an Element
* @param {Element} elem - matching cell element
* @returns {Flickity.Cell} cell - matching cell
*/
proto.getCell = function( elem ) {
// loop through cells to get the one that matches
for ( let cell of this.cells ) {
if ( cell.element === elem ) return cell;
}
};
/**
* get collection of Flickity.Cells, given Elements
* @param {[Element, Array, NodeList]} elems - multiple elements
* @returns {Array} cells - Flickity.Cells
*/
proto.getCells = function( elems ) {
elems = utils.makeArray( elems );
return elems.map( ( elem ) => this.getCell( elem ) ).filter( Boolean );
};
/**
* get cell elements
* @returns {Array} cellElems
*/
proto.getCellElements = function() {
return this.cells.map( ( cell ) => cell.element );
};
/**
* get parent cell from an element
* @param {Element} elem - child element
* @returns {Flickit.Cell} cell - parent cell
*/
proto.getParentCell = function( elem ) {
// first check if elem is cell
let cell = this.getCell( elem );
if ( cell ) return cell;
// try to get parent cell elem
let closest = elem.closest('.flickity-slider > *');
return this.getCell( closest );
};
/**
* get cells adjacent to a slide
* @param {Integer} adjCount - number of adjacent slides
* @param {Integer} index - index of slide to start
* @returns {Array} cells - array of Flickity.Cells
*/
proto.getAdjacentCellElements = function( adjCount, index ) {
if ( !adjCount ) return this.selectedSlide.getCellElements();
index = index === undefined ? this.selectedIndex : index;
let len = this.slides.length;
let cellElems = [];
for ( let i = index - adjCount; i <= index + adjCount; i++ ) {
let slideIndex = this.isWrapping ? utils.modulo( i, len ) : i;
let slide = this.slides[ slideIndex ];
if ( slide ) {
cellElems = cellElems.concat( slide.getCellElements() );
}
}
return cellElems;
};
/**
* select slide from number or cell element
* @param {[Element, String, Number]} selector - element, selector string, or index
* @returns {Flickity.Cell} - matching cell
*/
proto.queryCell = function( selector ) {
if ( typeof selector == 'number' ) {
// use number as index
return this.cells[ selector ];
}
// do not select invalid selectors from hash: #123, #/. #791
let isSelectorString = typeof selector == 'string' && !selector.match( /^[#.]?[\d/]/ );
if ( isSelectorString ) {
// use string as selector, get element
selector = this.element.querySelector( selector );
}
// get cell from element
return this.getCell( selector );
};
// -------------------------- events -------------------------- //
proto.uiChange = function() {
this.emitEvent('uiChange');
};
// ----- resize ----- //
proto.onresize = function() {
this.watchCSS();
this.resize();
};
utils.debounceMethod( Flickity, 'onresize', 150 );
proto.resize = function() {
// #1177 disable resize behavior when animating or dragging for iOS 15
if ( !this.isActive || this.isAnimating || this.isDragging ) return;
this.getSize();
// wrap values
if ( this.isWrapping ) {
this.x = utils.modulo( this.x, this.slideableWidth );
}
this.positionCells();
this._updateWrapShiftCells();
this.setGallerySize();
this.emitEvent('resize');
// update selected index for group slides, instant
// TODO: position can be lost between groups of various numbers
let selectedElement = this.selectedElements && this.selectedElements[0];
this.selectCell( selectedElement, false, true );
};
// watches the :after property, activates/deactivates
proto.watchCSS = function() {
if ( !this.options.watchCSS ) return;
let afterContent = getComputedStyle( this.element, ':after' ).content;
// activate if :after { content: 'flickity' }
if ( afterContent.includes('flickity') ) {
this.activate();
} else {
this.deactivate();
}
};
// ----- keydown ----- //
// go previous/next if left/right keys pressed
proto.onkeydown = function( event ) {
let { activeElement } = document;
let handler = Flickity.keyboardHandlers[ event.key ];
// only work if element is in focus
if ( !this.options.accessibility || !activeElement || !handler ) return;
let isFocused = this.focusableElems.some( ( elem ) => activeElement === elem );
if ( isFocused ) handler.call( this );
};
Flickity.keyboardHandlers = {
ArrowLeft: function() {
this.uiChange();
let leftMethod = this.options.rightToLeft ? 'next' : 'previous';
this[ leftMethod ]();
},
ArrowRight: function() {
this.uiChange();
let rightMethod = this.options.rightToLeft ? 'previous' : 'next';
this[ rightMethod ]();
},
};
// ----- focus ----- //
proto.focus = function() {
this.element.focus({ preventScroll: true });
};
// -------------------------- destroy -------------------------- //
// deactivate all Flickity functionality, but keep stuff available
proto.deactivate = function() {
if ( !this.isActive ) return;
this.element.classList.remove('flickity-enabled');
this.element.classList.remove('flickity-rtl');
this.unselectSelectedSlide();
// destroy cells
this.cells.forEach( ( cell ) => cell.destroy() );
this.viewport.remove();
// move child elements back into element
this.element.append( ...this.slider.children );
if ( this.options.accessibility ) {
this.element.removeAttribute('tabIndex');
this.element.removeEventListener( 'keydown', this );
}
// set flags
this.isActive = false;
this.emitEvent('deactivate');
};
proto.destroy = function() {
this.deactivate();
window.removeEventListener( 'resize', this );
this.allOff();
this.emitEvent('destroy');
if ( jQuery && this.$element ) {
jQuery.removeData( this.element, 'flickity' );
}
delete this.element.flickityGUID;
delete instances[ this.guid ];
};
// -------------------------- prototype -------------------------- //
Object.assign( proto, animatePrototype );
// -------------------------- extras -------------------------- //
/**
* get Flickity instance from element
* @param {[Element, String]} elem - element or selector string
* @returns {Flickity} - Flickity instance
*/
Flickity.data = function( elem ) {
elem = utils.getQueryElement( elem );
if ( elem ) return instances[ elem.flickityGUID ];
};
utils.htmlInit( Flickity, 'flickity' );
let { jQueryBridget } = window;
if ( jQuery && jQueryBridget ) {
jQueryBridget( 'flickity', Flickity, jQuery );
}
// set internal jQuery, for Webpack + jQuery v3, #478
Flickity.setJQuery = function( jq ) {
jQuery = jq;
};
Flickity.Cell = Cell;
Flickity.Slide = Slide;
return Flickity;
} ) );