1/*! 2 * Flickity sync v2.0.0 3 * enable sync for Flickity 4 */ 5 6/*jshint browser: true, undef: true, unused: true, strict: true*/ 7 8( function( window, factory ) { 9 // universal module definition 10 /*jshint strict: false */ /*globals define, module, require */ 11 if ( typeof define == 'function' && define.amd ) { 12 // AMD 13 define( [ 14 'flickity/js/index', 15 'fizzy-ui-utils/utils' 16 ], factory ); 17 } else if ( typeof module == 'object' && module.exports ) { 18 // CommonJS 19 module.exports = factory( 20 require('flickity'), 21 require('fizzy-ui-utils') 22 ); 23 } else { 24 // browser global 25 window.Flickity = factory( 26 window.Flickity, 27 window.fizzyUIUtils 28 ); 29 } 30 31}( window, function factory( Flickity, utils ) { 32 33'use strict'; 34 35// -------------------------- sync prototype -------------------------- // 36 37// Flickity.defaults.sync = false; 38 39Flickity.createMethods.push('_createSync'); 40 41Flickity.prototype._createSync = function() { 42 this.syncers = {}; 43 var syncOption = this.options.sync; 44 45 this.on( 'destroy', this.unsyncAll ); 46 47 if ( !syncOption ) { 48 return; 49 } 50 // HACK do async, give time for other flickity to be initalized 51 var _this = this; 52 setTimeout( function initSyncCompanion() { 53 _this.sync( syncOption ); 54 }); 55}; 56 57/** 58 * sync 59 * @param {Element} or {String} elem 60 */ 61Flickity.prototype.sync = function( elem ) { 62 elem = utils.getQueryElement( elem ); 63 var companion = Flickity.data( elem ); 64 if ( !companion ) { 65 return; 66 } 67 // two hearts, that beat as one 68 this._syncCompanion( companion ); 69 companion._syncCompanion( this ); 70}; 71 72/** 73 * @param {Flickity} companion 74 */ 75Flickity.prototype._syncCompanion = function( companion ) { 76 var _this = this; 77 function syncListener() { 78 var index = _this.selectedIndex; 79 // do not select if already selected, prevent infinite loop 80 if ( companion.selectedIndex != index ) { 81 companion.select( index ); 82 } 83 } 84 this.on( 'select', syncListener ); 85 // keep track of all synced flickities 86 // hold on to listener to unsync 87 this.syncers[ companion.guid ] = { 88 flickity: companion, 89 listener: syncListener 90 }; 91}; 92 93/** 94 * unsync 95 * @param {Element} or {String} elem 96 */ 97Flickity.prototype.unsync = function( elem ) { 98 elem = utils.getQueryElement( elem ); 99 var companion = Flickity.data( elem ); 100 this._unsync( companion ); 101}; 102 103/** 104 * @param {Flickity} companion 105 */ 106Flickity.prototype._unsync = function( companion ) { 107 if ( !companion ) { 108 return; 109 } 110 // I love you but I've chosen darkness 111 this._unsyncCompanion( companion ); 112 companion._unsyncCompanion( this ); 113}; 114 115/** 116 * @param {Flickity} companion 117 */ 118Flickity.prototype._unsyncCompanion = function( companion ) { 119 var id = companion.guid; 120 var syncer = this.syncers[ id ]; 121 this.off( 'select', syncer.listener ); 122 delete this.syncers[ id ]; 123}; 124 125Flickity.prototype.unsyncAll = function() { 126 for ( var id in this.syncers ) { 127 var syncer = this.syncers[ id ]; 128 this._unsync( syncer.flickity ); 129 } 130}; 131 132// ----- ----- // 133 134return Flickity; 135 136})); 137 138
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.