Skip to content

Commit

Permalink
initial commit, pulling sync from Flickity source
Browse files Browse the repository at this point in the history
  • Loading branch information
desandro committed Feb 2, 2015
0 parents commit 69b8495
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bower_components/
node_modules/
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Flickity sync

## Install

Bower: `bower install flickity-sync --save`

npm: `npm install flickity-sync`

---

By [Metafizzy](http://metafizzy.co)
33 changes: 33 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "flickity-sync",
"version": "0.1.0",
"description": "Enable sync for Flickity",
"main": "flickity-sync.js",
"dependencies": {
"flickity": "~0.1.0",
"fizzy-ui-utils": "~0.1.0"
},
"devDependencies": {
"qunit": "~1.17.1"
},
"authors": [
"David DeSandro"
],
"moduleType": [
"amd",
"globals",
"node"
],
"keywords": [
"flickity"
],
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests",
"package.json"
]
}
140 changes: 140 additions & 0 deletions flickity-sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*!
* Flickity sync v0.1.0
* enable sync for Flickity
*/

( function( window, factory ) {
'use strict';
// universal module definition

if ( typeof define == 'function' && define.amd ) {
// AMD
define( [
'./flickity',
'fizzy-ui-utils/utils'
], function( Flickity, utils ) {
return factory( window, Flickity, utils );
});
} else if ( typeof exports == 'object' ) {
// CommonJS
module.exports = factory(
window,
require('/.flickity'),
require('fizzy-ui-utils')
);
} else {
// browser global
window.Flickity = window.Flickity || {};
window.Flickity = factory(
window,
window.Flickity,
window.fizzyUIUtils
);
}

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

'use strict';

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

// Flickity.defaults.sync = false;

Flickity.createMethods.push('_createSync');

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

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

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

/**
* sync
* @param {Element} or {String} elem
*/
Flickity.prototype.sync = function( elem ) {
elem = utils.getQueryElement( elem );
var companion = Flickity.data( elem );
if ( !companion ) {
return;
}
// two hearts, that beat as one
this._syncCompanion( companion );
companion._syncCompanion( this );
};

/**
* @param {Flickity} companion
*/
Flickity.prototype._syncCompanion = function( companion ) {
var _this = this;
function syncListener() {
var index = _this.selectedIndex;
// do not select if already selected, prevent infinite loop
if ( companion.selectedIndex != index ) {
companion.select( index );
}
}
this.on( 'select', syncListener );
// keep track of all synced flickities
// hold on to listener to unsync
this.syncers[ companion.guid ] = {
flickity: companion,
listener: syncListener
};
};

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

/**
* @param {Flickity} companion
*/
Flickity.prototype._unsync = function( companion ) {
if ( !companion ) {
return;
}
// I love you but I've chosen darkness
this._unsyncCompanion( companion );
companion._unsyncCompanion( this );
};

/**
* @param {Flickity} companion
*/
Flickity.prototype._unsyncCompanion = function( companion ) {
var id = companion.guid;
var 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 ];
this._unsync( syncer.flickity );
}
};

// ----- ----- //

return Flickity;

}));
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "flickity-sync",
"version": "0.1.0",
"description": "Enable sync for Flickity",
"main": "flickity-sync.js",
"dependencies": {
"flickity": "^0.1.0",
"fizzy-ui-utils": "^0.1.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git://github.com/metafizzy/flickity-sync.git"
},
"keywords": [
"flickity",
"browser",
"DOM"
],
"author": "David DeSandro",
"license": "GPL",
"bugs": {
"url": "https://github.com/metafizzy/flickity-sync/issues"
},
"homepage": "https://github.com/metafizzy/flickity-sync"
}

0 comments on commit 69b8495

Please sign in to comment.