Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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
Prev Previous commit
Next Next commit
move registry objects and helper method to src/registry.js
- this will help free Plots of some circular dependencies
- make sure the ref back Registry contents in Plots
  for backward compatibility
  • Loading branch information
etpinard committed Aug 11, 2016
commit 910e17c756062d95d50f3698008520802009663f
10 changes: 5 additions & 5 deletions 10 src/plot_api/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

'use strict';

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


Expand Down Expand Up @@ -43,10 +43,10 @@ module.exports = function register(_modules) {
};

function registerTraceModule(newModule) {
Plots.register(newModule, newModule.name, newModule.categories, newModule.meta);
Registry.register(newModule, newModule.name, newModule.categories, newModule.meta);

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

Expand All @@ -67,5 +67,5 @@ function registerTransformModule(newModule) {
Lib.log(prefix + ' registered without a *supplyDefaults* function.');
}

Plots.transformsRegistry[newModule.name] = newModule;
Registry.transformsRegistry[newModule.name] = newModule;
}
126 changes: 12 additions & 114 deletions 126 src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,134 +13,32 @@ var d3 = require('d3');
var isNumeric = require('fast-isnumeric');

var Plotly = require('../plotly');
var Registry = require('../registry');
var Lib = require('../lib');
var Color = require('../components/color');

var plots = module.exports = {};

var modules = plots.modules = {},
allTypes = plots.allTypes = [],
allCategories = plots.allCategories = {},
subplotsRegistry = plots.subplotsRegistry = {},
transformsRegistry = plots.transformsRegistry = {};
// Expose registry methods that used to be on Plots for backward-compatibility
plots.modules = Registry.modules;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important. Ensure backward compatibility in the Plotly.Plots props and methods ⏬

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use one of the Lib.extend variants here?

plots.allTypes = Registry.allTypes;
plots.allCategories = Registry.allCategories;
plots.subplotsRegistry = Registry.subplotsRegistry;
plots.transformsRegistry = Registry.transformsRegistry;
plots.traceIs = Registry.traceIs;
plots.getModule = Registry.getModule;

plots.attributes = require('./attributes');
plots.attributes.type.values = allTypes;
plots.attributes.type.values = plots.allTypes;
plots.fontAttrs = require('./font_attributes');
plots.layoutAttributes = require('./layout_attributes');

// TODO make this a plot attribute?
plots.fontWeight = 'normal';

/**
* plots.register: register a module as the handler for a trace type
*
* @param {object} _module the module that will handle plotting this trace type
* @param {string} thisType
* @param {array of strings} categoriesIn all the categories this type is in,
* tested by calls: Plotly.Plots.traceIs(trace, oneCategory)
* @param {object} meta meta information about the trace type
*/
plots.register = function(_module, thisType, categoriesIn, meta) {
if(modules[thisType]) {
Lib.log('Type ' + thisType + ' already registered');
return;
}

var categoryObj = {};
for(var i = 0; i < categoriesIn.length; i++) {
categoryObj[categoriesIn[i]] = true;
allCategories[categoriesIn[i]] = true;
}

modules[thisType] = {
_module: _module,
categories: categoryObj
};

if(meta && Object.keys(meta).length) {
modules[thisType].meta = meta;
}

allTypes.push(thisType);
};

function getTraceType(traceType) {
if(typeof traceType === 'object') traceType = traceType.type;
return traceType;
}

plots.getModule = function(trace) {
if(trace.r !== undefined) {
Lib.warn('Tried to put a polar trace ' +
'on an incompatible graph of cartesian ' +
'data. Ignoring this dataset.', trace
);
return false;
}

var _module = modules[getTraceType(trace)];
if(!_module) return false;
return _module._module;
};


/**
* plots.traceIs: is this trace type in this category?
*
* traceType: a trace (object) or trace type (string)
* category: a category (string)
*/
plots.traceIs = function traceIs(traceType, category) {
traceType = getTraceType(traceType);

if(traceType === 'various') return false; // FIXME
var subplotsRegistry = plots.subplotsRegistry;
var transformsRegistry = plots.transformsRegistry;

var _module = modules[traceType];

if(!_module) {
if(traceType !== undefined) {
Lib.log('Unrecognized trace type ' + traceType + '.');
}
_module = modules[plots.attributes.type.dflt];
}

return !!_module.categories[category];
};


/**
* plots.registerSubplot: register a subplot type
*
* @param {object} _module subplot module:
*
* @param {string or array of strings} attr
* attribute name in traces and layout
* @param {string or array of strings} idRoot
* root of id (setting the possible value for attrName)
* @param {object} attributes
* attribute(s) for traces of this subplot type
*
* In trace objects `attr` is the object key taking a valid `id` as value
* (the set of all valid ids is generated below and stored in idRegex).
*
* In the layout object, a or several valid `attr` name(s) can be keys linked
* to a nested attribute objects
* (the set of all valid attr names is generated below and stored in attrRegex).
*
* TODO use these in Lib.coerce
*/
plots.registerSubplot = function(_module) {
var plotType = _module.name;

if(subplotsRegistry[plotType]) {
Lib.log('Plot type ' + plotType + ' already registered.');
return;
}

// not sure what's best for the 'cartesian' type at this point
subplotsRegistry[plotType] = _module;
};

/**
* Find subplot ids in data.
Expand Down
137 changes: 137 additions & 0 deletions 137 src/registry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/**
* 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';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This module doesn't really fit anywhere else then in the src/ root I think.

Maybe we should move plot_api/plot_config.js to the src/ root too. That way both modules with objects that can be modified at runtime would be in a very up-front location.


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

exports.modules = {};
exports.allTypes = [];
exports.allCategories = {};
exports.subplotsRegistry = {};
exports.transformsRegistry = {};

/**
* register a module as the handler for a trace type
*
* @param {object} _module the module that will handle plotting this trace type
* @param {string} thisType
* @param {array of strings} categoriesIn all the categories this type is in,
* tested by calls: traceIs(trace, oneCategory)
* @param {object} meta meta information about the trace type
*/
exports.register = function(_module, thisType, categoriesIn, meta) {
if(exports.modules[thisType]) {
Lib.log('Type ' + thisType + ' already registered');
return;
}

var categoryObj = {};
for(var i = 0; i < categoriesIn.length; i++) {
categoryObj[categoriesIn[i]] = true;
exports.allCategories[categoriesIn[i]] = true;
}

exports.modules[thisType] = {
_module: _module,
categories: categoryObj
};

if(meta && Object.keys(meta).length) {
exports.modules[thisType].meta = meta;
}

exports.allTypes.push(thisType);
};

/**
* register a subplot type
*
* @param {object} _module subplot module:
*
* @param {string or array of strings} attr
* attribute name in traces and layout
* @param {string or array of strings} idRoot
* root of id (setting the possible value for attrName)
* @param {object} attributes
* attribute(s) for traces of this subplot type
*
* In trace objects `attr` is the object key taking a valid `id` as value
* (the set of all valid ids is generated below and stored in idRegex).
*
* In the layout object, a or several valid `attr` name(s) can be keys linked
* to a nested attribute objects
* (the set of all valid attr names is generated below and stored in attrRegex).
*/
exports.registerSubplot = function(_module) {
var plotType = _module.name;

if(exports.subplotsRegistry[plotType]) {
Lib.log('Plot type ' + plotType + ' already registered.');
return;
}

// not sure what's best for the 'cartesian' type at this point
exports.subplotsRegistry[plotType] = _module;
};

/**
* Get registered module using trace object or trace type
*
* @param {object||string} trace
* trace object with prop 'type' or trace type as a string
* @return {object}
* module object corresponding to trace type
*/
exports.getModule = function(trace) {
if(trace.r !== undefined) {
Lib.warn('Tried to put a polar trace ' +
'on an incompatible graph of cartesian ' +
'data. Ignoring this dataset.', trace
);
return false;
}

var _module = exports.modules[getTraceType(trace)];
if(!_module) return false;
return _module._module;
};

/**
* Determine if this trace type is in a given category
*
* @param {object||string} traceType
* a trace (object) or trace type (string)
* @param {string} category
* category in question
* @return {boolean}
*/
exports.traceIs = function(traceType, category) {
traceType = getTraceType(traceType);

if(traceType === 'various') return false; // FIXME
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😕 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

old plot.ly workspace hack to we need to keep 😞

Copy link
Contributor Author

@etpinard etpinard Aug 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll clean up the comment though. Good 👀


var _module = exports.modules[traceType];

if(!_module) {
if(traceType !== undefined) {
Lib.log('Unrecognized trace type ' + traceType + '.');
}

_module = exports.modules[basePlotAttributes.type.dflt];
}

return !!_module.categories[category];
};

function getTraceType(traceType) {
if(typeof traceType === 'object') traceType = traceType.type;
return traceType;
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.