Skip to content

Commit

Permalink
🔔 v3.0.0 for Flickity v3
Browse files Browse the repository at this point in the history
- remove AMD / RequireJS
- switch to Flickity.create.sync
- lint with ESLint
- use node_modules for local development
  • Loading branch information
desandro committed Mar 6, 2022
1 parent da5b75e commit 99d0c08
Show file tree
Hide file tree
Showing 9 changed files with 1,950 additions and 135 deletions.
19 changes: 19 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
plugins: [ 'metafizzy' ],
extends: 'plugin:metafizzy/browser',
env: {
browser: true,
commonjs: true,
},
parserOptions: {
ecmaVersion: 2018,
},
globals: {
Flickity: 'readonly',
QUnit: 'readonly',
},
rules: {
'prefer-object-spread': 'error',
},
ignorePatterns: [ 'bower_components' ],
};
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16
24 changes: 7 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,27 @@ sync: document.querySelector('.carousel-b')
// set as an element
```

[See demo on CodePen](https://codepen.io/desandro/pen/OPZJmE).
[See demo on CodePen](https://codepen.io/desandro/pen/gOXEKPK).

## Install

Add `flickity-sync.js` to your scripts.

### Download

+ [flickity-sync.js](https://unpkg.com/flickity-sync@2/flickity-sync.js)
+ [flickity-sync.js](https://unpkg.com/flickity-sync@3/flickity-sync.js)

### CDN

``` html
<script src="https://unpkg.com/flickity-sync@2/flickity-sync.js"></script>
<script src="https://unpkg.com/flickity-sync@3/flickity-sync.js"></script>
```

### Package managers

npm: `npm install flickity-sync`

Bower: `bower install flickity-sync`
Yarn: `yarn add flickity-sync`

## Usage

Expand Down Expand Up @@ -76,28 +76,18 @@ var flktyB = new Flickity('.carousel-b');
</div>
```

### Webpack & Browserify
### Webpack

``` js
var Flickity = require('flickity-sync');
const Flickity = require('flickity');
require('flickity-sync');

var flktyA = new Flickity( '.carousel-a', {
sync: '.carousel-b'
});
var flktyB = new Flickity('.carousel-b');
```

### RequireJS

``` js
requirejs( [ 'path/to/flickity-sync' ], function( Flickity ) {
var flktyA = new Flickity( '.carousel-a', {
sync: '.carousel-b'
});
var flktyB = new Flickity('.carousel-b')
});
```

---

By [Metafizzy 🌈🐻](https://metafizzy.co)
90 changes: 36 additions & 54 deletions flickity-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,51 @@
* enable sync for Flickity
*/

/*jshint browser: true, undef: true, unused: true, strict: true*/

( function( window, factory ) {
// universal module definition
/*jshint strict: false */ /*globals define, module, require */
if ( typeof define == 'function' && define.amd ) {
// AMD
define( [
'flickity/js/index',
'fizzy-ui-utils/utils'
], factory );
} else if ( typeof module == 'object' && module.exports ) {
if ( typeof module == 'object' && module.exports ) {
// CommonJS
module.exports = factory(
require('flickity'),
require('fizzy-ui-utils')
require('flickity'),
require('fizzy-ui-utils'),
);
} else {
// browser global
window.Flickity = factory(
window.Flickity,
window.fizzyUIUtils
factory(
window.Flickity,
window.fizzyUIUtils,
);
}

}( window, function factory( Flickity, utils ) {

'use strict';
}( typeof window != 'undefined' ? window : this, function factory( Flickity, utils ) {

// -------------------------- sync prototype -------------------------- //

// Flickity.defaults.sync = false;

Flickity.createMethods.push('_createSync');

Flickity.prototype._createSync = function() {
Flickity.create.sync = function() {
this.syncers = {};
var syncOption = this.options.sync;
let syncOption = this.options.sync;

this.on( 'destroy', this.unsyncAll );

if ( !syncOption ) {
return;
}
if ( !syncOption ) return;
// HACK do async, give time for other flickity to be initalized
var _this = this;
setTimeout( function initSyncCompanion() {
_this.sync( syncOption );
});
setTimeout( () => {
this.sync( syncOption );
} );
};

let proto = Flickity.prototype;

/**
* sync
* @param {Element} or {String} elem
* @param {[Element, String]} elem
*/
Flickity.prototype.sync = function( elem ) {
proto.sync = function( elem ) {
elem = utils.getQueryElement( elem );
var companion = Flickity.data( elem );
if ( !companion ) {
return;
}
let companion = Flickity.data( elem );
if ( !companion ) return;
// two hearts, that beat as one
this._syncCompanion( companion );
companion._syncCompanion( this );
Expand All @@ -72,12 +56,12 @@ Flickity.prototype.sync = function( elem ) {
/**
* @param {Flickity} companion
*/
Flickity.prototype._syncCompanion = function( companion ) {
var _this = this;
proto._syncCompanion = function( companion ) {
let _this = this;
function syncListener() {
var index = _this.selectedIndex;
let index = _this.selectedIndex;
// do not select if already selected, prevent infinite loop
if ( companion.selectedIndex != index ) {
if ( companion.selectedIndex !== index ) {
companion.select( index );
}
}
Expand All @@ -86,27 +70,25 @@ Flickity.prototype._syncCompanion = function( companion ) {
// hold on to listener to unsync
this.syncers[ companion.guid ] = {
flickity: companion,
listener: syncListener
listener: syncListener,
};
};

/**
* unsync
* @param {Element} or {String} elem
* @param {[Element, String]} elem
*/
Flickity.prototype.unsync = function( elem ) {
proto.unsync = function( elem ) {
elem = utils.getQueryElement( elem );
var companion = Flickity.data( elem );
let companion = Flickity.data( elem );
this._unsync( companion );
};

/**
* @param {Flickity} companion
*/
Flickity.prototype._unsync = function( companion ) {
if ( !companion ) {
return;
}
proto._unsync = function( companion ) {
if ( !companion ) return;
// I love you but I've chosen darkness
this._unsyncCompanion( companion );
companion._unsyncCompanion( this );
Expand All @@ -115,16 +97,16 @@ Flickity.prototype._unsync = function( companion ) {
/**
* @param {Flickity} companion
*/
Flickity.prototype._unsyncCompanion = function( companion ) {
var id = companion.guid;
var syncer = this.syncers[ id ];
proto._unsyncCompanion = function( companion ) {
let id = companion.guid;
let syncer = this.syncers[ id ];
this.off( 'select', syncer.listener );
delete this.syncers[ id ];
};

Flickity.prototype.unsyncAll = function() {
for ( var id in this.syncers ) {
var syncer = this.syncers[ id ];
proto.unsyncAll = function() {
for ( let id in this.syncers ) {
let syncer = this.syncers[ id ];
this._unsync( syncer.flickity );
}
};
Expand All @@ -133,4 +115,4 @@ Flickity.prototype.unsyncAll = function() {

return Flickity;

}));
} ) );
Loading

0 comments on commit 99d0c08

Please sign in to comment.