From 6716fab88366d44ba8d67460463ef0d88d22ab1b Mon Sep 17 00:00:00 2001 From: Dennis Hotson Date: Mon, 8 Dec 2014 10:29:31 +1100 Subject: [PATCH] Universal module pattern (for browserify support) --- springy.js | 50 +++++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/springy.js b/springy.js index 1c5175f..e2a48c0 100644 --- a/springy.js +++ b/springy.js @@ -24,22 +24,24 @@ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. */ +(function (root, factory) { + if (typeof define === 'function' && define.amd) { + // AMD. Register as an anonymous module. + define(['b'], function (b) { + return (root.returnExportsGlobal = factory()); + }); + } else if (typeof exports === 'object') { + // Node. Does not work with strict CommonJS, but + // only CommonJS-like enviroments that support module.exports, + // like Node. + module.exports = factory(); + } else { + // Browser globals + root.Springy = factory(); + } +}(this, function() { -(function() { - // Enable strict mode for EC5 compatible browsers - "use strict"; - - // Establish the root object, `window` in the browser, or `global` on the server. - var root = this; - - // The top-level namespace. All public Springy classes and modules will - // be attached to this. Exported for both CommonJS and the browser. - var Springy; - if (typeof exports !== 'undefined') { - Springy = exports; - } else { - Springy = root.Springy = {}; - } + var Springy = {}; var Graph = Springy.Graph = function() { this.nodeSet = {}; @@ -474,14 +476,14 @@ var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; // stolen from coffeescript, thanks jashkenas! ;-) - Springy.requestAnimationFrame = __bind(root.requestAnimationFrame || - root.webkitRequestAnimationFrame || - root.mozRequestAnimationFrame || - root.oRequestAnimationFrame || - root.msRequestAnimationFrame || + Springy.requestAnimationFrame = __bind(this.requestAnimationFrame || + this.webkitRequestAnimationFrame || + this.mozRequestAnimationFrame || + this.oRequestAnimationFrame || + this.msRequestAnimationFrame || (function(callback, element) { - root.setTimeout(callback, 10); - }), root); + this.setTimeout(callback, 10); + }), this); /** @@ -720,4 +722,6 @@ } return true; }; -}).call(this); + + return Springy; +}));