From 69b84957faa8fdd00f5a9e15c051a5daaa7d6070 Mon Sep 17 00:00:00 2001 From: David DeSandro Date: Mon, 2 Feb 2015 08:01:49 -0500 Subject: [PATCH] initial commit, pulling sync from Flickity source --- .gitignore | 2 + README.md | 11 ++++ bower.json | 33 +++++++++++ flickity-sync.js | 140 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 28 ++++++++++ 5 files changed, 214 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 bower.json create mode 100644 flickity-sync.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c346b13 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bower_components/ +node_modules/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..fcb1731 --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +# Flickity sync + +## Install + +Bower: `bower install flickity-sync --save` + +npm: `npm install flickity-sync` + +--- + +By [Metafizzy](http://metafizzy.co) diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..0ec09a9 --- /dev/null +++ b/bower.json @@ -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" + ] +} diff --git a/flickity-sync.js b/flickity-sync.js new file mode 100644 index 0000000..3fffc64 --- /dev/null +++ b/flickity-sync.js @@ -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; + +})); diff --git a/package.json b/package.json new file mode 100644 index 0000000..dd334bc --- /dev/null +++ b/package.json @@ -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" +}