-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial commit, pulling sync from Flickity source
- Loading branch information
0 parents
commit 69b8495
Showing
5 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
bower_components/ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |