-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Changes from 1 commit
bf28ad9
00536f4
910e17c
11f475e
4a3f254
161af7e
2382c04
710f312
c6aa0fb
68268e3
4f754aa
0aa87c4
65ffb52
d11c8f9
a1ea365
2802d22
39c32d0
489513b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- this will help free Plots of some circular dependencies - make sure the ref back Registry contents in Plots for backward compatibility
- Loading branch information
There are no files selected for viewing
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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This module doesn't really fit anywhere else then in the Maybe we should move |
||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😕 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. old plot.ly workspace hack to we need to keep 😞 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
There was a problem hiding this comment.
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 ⏬There was a problem hiding this comment.
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?