From 2c16c755ff997fee789adcd0ba8b2ff4f09645c6 Mon Sep 17 00:00:00 2001 From: Aleksandar Odorovic Date: Thu, 30 Jul 2015 14:30:20 -0700 Subject: [PATCH] Expose d3-plugin-sankey from node Although d3 itself exists as a node.js package, the plugins are usable only from the browser. --Make sure sankey plugin can be loaded through node.js --Put in basic package structure and plugin loadin mechanism for node --- package.json | 10 ++++++++++ pluginloader.js | 39 +++++++++++++++++++++++++++++++++++++++ sankey/sankey.js | 13 +++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 package.json create mode 100644 pluginloader.js diff --git a/package.json b/package.json new file mode 100644 index 0000000..c06c644 --- /dev/null +++ b/package.json @@ -0,0 +1,10 @@ +{ + "name": "d3-plugins", + "version": "0.1.0", + + "peerDependencies": { + "d3": "^3.5.0" + }, + + "main": "pluginloader.js" +} \ No newline at end of file diff --git a/pluginloader.js b/pluginloader.js new file mode 100644 index 0000000..fe6d6d2 --- /dev/null +++ b/pluginloader.js @@ -0,0 +1,39 @@ + +;(function() { + /* jshint node:true, asi:true, eqnull:true */ + "use strict"; + var has_require = typeof require !== 'undefined'; + + var d3 = this.d3; + if( typeof d3 === 'undefined' ) { + if( has_require ) { + d3 = require('d3'); + } + else throw new Error('d3 plugins require d3'); + } + + var pluginloader = function(obj) { + if (obj instanceof pluginloader) { + return obj; + } + if (!(this instanceof pluginloader)) { + return new pluginloader(obj); + } + this._wrapped = obj; + }; + + if( typeof exports !== 'undefined' ) { + if( typeof module !== 'undefined' && module.exports ) { + exports = module.exports = pluginloader; + } else { + exports.pluginloader = pluginloader; + } + } + + pluginloader.load = function(pluginname) { + if ( has_require ) { + require('./' + pluginname + '/' + pluginname); + } + } + +}).call(this); diff --git a/sankey/sankey.js b/sankey/sankey.js index abe137b..b10f155 100644 --- a/sankey/sankey.js +++ b/sankey/sankey.js @@ -1,3 +1,15 @@ + +;(function() { +var has_require = typeof require !== 'undefined'; + + var d3 = this.d3; +if( typeof d3 === 'undefined' ) { + if( has_require ) { + d3 = require('d3'); + } + else throw new Error('d3 plugins require d3'); +} + d3.sankey = function() { var sankey = {}, nodeWidth = 24, @@ -292,3 +304,4 @@ d3.sankey = function() { return sankey; }; +}).call(this);