From ab40b55c8f4579673ae888c24300e150499729a6 Mon Sep 17 00:00:00 2001 From: Hyunje Alex Jun Date: Thu, 12 Feb 2015 18:24:05 +0000 Subject: [PATCH] Add CommonJS support for the jQuery adaptor. --- README.md | 11 +++++++++ index.js | 6 +++++ jquery.js | 6 +++++ package.json | 2 +- src/js/adaptor/global.js | 2 +- src/js/adaptor/jquery.js | 51 +++++++++++++++++++++++----------------- src/js/main.js | 10 +++++++- src/js/plugin/ps.js | 14 ----------- 8 files changed, 64 insertions(+), 38 deletions(-) create mode 100644 index.js create mode 100644 jquery.js delete mode 100644 src/js/plugin/ps.js diff --git a/README.md b/README.md index 9094f7e..3b07132 100644 --- a/README.md +++ b/README.md @@ -177,10 +177,21 @@ As you may already know, perfect-scrollbar was a jQuery plugin. And it *is* as well. There's a jQuery adaptor and the plugin can be used in the same way it used to be used before. +I also recommend using NPM and CommonJS here, but it's not mandatory. + +```javascript +var $ = require('jquery'); +require('perfect-scrollbar/jquery')($); +``` + +For sure, you can just import a built script. + ```html ``` +After importing it, you can use the plugin in the usual way. + ```javascript $('#container').perfectScrollbar(); // Initialize $('#container').perfectScrollbar({ ... }); // with options diff --git a/index.js b/index.js new file mode 100644 index 0000000..b5d7d33 --- /dev/null +++ b/index.js @@ -0,0 +1,6 @@ +/* Copyright (c) 2015 Hyunje Alex Jun and other contributors + * Licensed under the MIT License + */ +'use strict'; + +module.exports = require('./src/js/main'); diff --git a/jquery.js b/jquery.js new file mode 100644 index 0000000..f823e96 --- /dev/null +++ b/jquery.js @@ -0,0 +1,6 @@ +/* Copyright (c) 2015 Hyunje Alex Jun and other contributors + * Licensed under the MIT License + */ +'use strict'; + +module.exports = require('./src/js/adaptor/jquery'); diff --git a/package.json b/package.json index 435f581..27ee20d 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "email": "me@noraesae.net" } ], - "main": "src/js/main.js", + "main": "./index.js", "repository": { "type": "git", "url": "https://github.com/noraesae/perfect-scrollbar" diff --git a/src/js/adaptor/global.js b/src/js/adaptor/global.js index b87dd21..dce0e3a 100644 --- a/src/js/adaptor/global.js +++ b/src/js/adaptor/global.js @@ -3,7 +3,7 @@ */ 'use strict'; -var ps = require('../plugin/ps'); +var ps = require('../main'); window.PerfectScrollbar = ps; if (typeof window.Ps === 'undefined') { diff --git a/src/js/adaptor/jquery.js b/src/js/adaptor/jquery.js index df07e33..cbc1058 100644 --- a/src/js/adaptor/jquery.js +++ b/src/js/adaptor/jquery.js @@ -3,30 +3,39 @@ */ 'use strict'; -var ps = require('../plugin/ps') +var ps = require('../main') , psInstances = require('../plugin/instances'); -$.fn.perfectScrollbar = function (settingOrCommand) { - return this.each(function () { - if (typeof settingOrCommand === 'object' || - typeof settingOrCommand === 'undefined') { - // If it's an object or none, initialize. - var settings = settingOrCommand; +function mountJQuery(jQuery) { + jQuery.fn.perfectScrollbar = function (settingOrCommand) { + return this.each(function () { + if (typeof settingOrCommand === 'object' || + typeof settingOrCommand === 'undefined') { + // If it's an object or none, initialize. + var settings = settingOrCommand; - if (!psInstances.get(this)) { - ps.initialize(this, settings); + if (!psInstances.get(this)) { + ps.initialize(this, settings); + } + } else { + // Unless, it may be a command. + var command = settingOrCommand; + + if (command === 'update') { + ps.update(this); + } else if (command === 'destroy') { + ps.destroy(this); + } } - } else { - // Unless, it may be a command. - var command = settingOrCommand; - if (command === 'update') { - ps.update(this); - } else if (command === 'destroy') { - ps.destroy(this); - } - } + return $(this); + }); + }; +} - return $(this); - }); -}; +var jq = window.jQuery ? window.jQuery : window.$; +if (typeof jq !== 'undefined') { + mountJQuery(jq); +} + +module.exports = mountJQuery; diff --git a/src/js/main.js b/src/js/main.js index faccca5..cb0f6ff 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -3,4 +3,12 @@ */ 'use strict'; -module.exports = require('./plugin/ps'); +var destroy = require('./plugin/destroy') + , initialize = require('./plugin/initialize') + , update = require('./plugin/update'); + +module.exports = { + initialize: initialize, + update: update, + destroy: destroy +}; diff --git a/src/js/plugin/ps.js b/src/js/plugin/ps.js deleted file mode 100644 index 3a218d5..0000000 --- a/src/js/plugin/ps.js +++ /dev/null @@ -1,14 +0,0 @@ -/* Copyright (c) 2015 Hyunje Alex Jun and other contributors - * Licensed under the MIT License - */ -'use strict'; - -var destroy = require('./destroy') - , initialize = require('./initialize') - , update = require('./update'); - -module.exports = { - initialize: initialize, - update: update, - destroy: destroy -};