8000 Add registry of component modules by etpinard · Pull Request #845 · plotly/plotly.js · GitHub
[go: up one dir, main page]

Skip to content

Add registry of component modules #845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Aug 19, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
move Plotly.register in plot_api/register.js
  • Loading branch information
etpinard committed Aug 10, 2016
commit bf28ad9db8b7f8a299e0da1c9375503c1a354cfe
5 changes: 4 additions & 1 deletion src/core.js
10000
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ exports.deleteTraces = Plotly.deleteTraces;
exports.moveTraces = Plotly.moveTraces;
exports.purge = Plotly.purge;
exports.setPlotConfig = require('./plot_api/set_plot_config');
exports.register = Plotly.register;
exports.register = require('./plot_api/register');
exports.toImage = require('./plot_api/to_image');
exports.downloadImage = require('./snapshot/download');
exports.validate = require('./plot_api/validate');

// scatter is the only trace included by default
exports.register(require('./traces/scatter'));

// plot icons
exports.Icons = require('../build/ploticon');

Expand Down
65 changes: 65 additions & 0 deletions src/plot_api/register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

var Plots = require('../plots/plots');
var Lib = require('../lib');


module.exports = function register(_modules) {
if(!_modules) {
throw new Error('No argument passed to Plotly.register.');
}
else if(_modules && !Array.isArray(_modules)) {
_modules = [_modules];
}

for(var i = 0; i < _modules.length; i++) {
var newModule = _modules[i];

if(!newModule) {
throw new Error('Invalid module was attempted to be registered!');
}

switch(newModule.moduleType) {
case 'trace':
Plots.register(newModule, newModule.name, newModule.categories, newModule.meta);

if(!Plots.subplotsRegistry[newModule.basePlotModule.name]) {
Plots.registerSubplot(newModule.basePlotModule);
}

break;

case 'transform':
if(typeof newModule.name !== 'string') {
throw new Error('Transform module *name* must be a string.');
}

var prefix = 'Transform module ' + newModule.name;

if(typeof newModule.transform !== 'function') {
throw new Error(prefix + ' is missing a *transform* function.');
}
if(!Lib.isPlainObject(newModule.attributes)) {
Lib.log(prefix + ' registered without an *attributes* object.');
}
if(typeof newModule.supplyDefaults !== 'function') {
Lib.log(prefix + ' registered without a *supplyDefaults* function.');
}

Plots.transformsRegistry[newModule.name] = newModule;

break;

default:
throw new Error('Invalid module was attempted to be registered!');
}
}
};
60 changes: 2 additions & 58 deletions src/plotly.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
require('es6-promise').polyfill();

// lib functions
var Lib = exports.Lib = require('./lib');
exports.Lib = require('./lib');
exports.util = require('./lib/svg_text_utils');
exports.Queue = require('./lib/queue');

Expand All @@ -34,8 +34,7 @@ exports.MathJaxConfig = require('./fonts/mathjax_config');
exports.defaultConfig = require('./plot_api/plot_config');

// plots
var Plots = exports.Plots = require('./plots/plots');

exports.Plots = require('./plots/plots');
exports.Axes = require('./plots/cartesian/axes');
exports.Fx = require('./plots/cartesian/graph_interact');

Expand All @@ -52,61 +51,6 @@ exports.Images = require('./components/images');
exports.UpdateMenus = require('./components/updatemenus');
exports.ModeBar = require('./components/modebar');

exports.register = function register(_modules) {
if(!_modules) {
throw new Error('No argument passed to Plotly.register.');
}
else if(_modules && !Array.isArray(_modules)) {
_modules = [_modules];
}

for(var i = 0; i < _modules.length; i++) {
var newModule = _modules[i];

if(!newModule) {
throw new Error('Invalid module was attempted to be registered!');
}

switch(newModule.moduleType) {
case 'trace':
Plots.register(newModule, newModule.name, newModule.categories, newModule.meta);

if(!Plots.subplotsRegistry[newModule.basePlotModule.name]) {
Plots.registerSubplot(newModule.basePlotModule);
}

break;

case 'transform':
if(typeof newModule.name !== 'string') {
throw new Error('Transform module *name* must be a string.');
}

var prefix = 'Transform module ' + newModule.name;

if(typeof newModule.transform !== 'function') {
throw new Error(prefix + ' is missing a *transform* function.');
}
if(!Lib.isPlainObject(newModule.attributes)) {
Lib.log(prefix + ' registered without an *attributes* object.');
}
if(typeof newModule.supplyDefaults !== 'function') {
Lib.log(prefix + ' registered without a *supplyDefaults* function.');
}

Plots.transformsRegistry[newModule.name] = newModule;

break;

default:
throw new Error('Invalid module was attempted to be registered!');
}
}
};

// Scatter is the only trace included by default
exports.register(require('./traces/scatter'));

// plot api
require('./plot_api/plot_api');
exports.PlotSchema = require('./plot_api/plot_schema');
Expand Down
0