Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 910e17c

Browse files
committed
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
1 parent 00536f4 commit 910e17c

File tree

3 files changed

+154
-119
lines changed

3 files changed

+154
-119
lines changed

src/plot_api/register.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
'use strict';
1010

11-
var Plots = require('../plots/plots');
11+
var Registry = require('../registry');
1212
var Lib = require('../lib');
1313

1414

@@ -43,10 +43,10 @@ module.exports = function register(_modules) {
4343
};
4444

4545
function registerTraceModule(newModule) {
46-
Plots.register(newModule, newModule.name, newModule.categories, newModule.meta);
46+
Registry.register(newModule, newModule.name, newModule.categories, newModule.meta);
4747

48-
if(!Plots.subplotsRegistry[newModule.basePlotModule.name]) {
49-
Plots.registerSubplot(newModule.basePlotModule);
48+
if(!Registry.subplotsRegistry[newModule.basePlotModule.name]) {
49+
Registry.registerSubplot(newModule.basePlotModule);
5050
}
5151
}
5252

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

70-
Plots.transformsRegistry[newModule.name] = newModule;
70+
Registry.transformsRegistry[newModule.name] = newModule;
7171
}

src/plots/plots.js

Lines changed: 12 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -13,134 +13,32 @@ var d3 = require('d3');
1313
var isNumeric = require('fast-isnumeric');
1414

1515
var Plotly = require('../plotly');
16+
var Registry = require('../registry');
1617
var Lib = require('../lib');
1718
var Color = require('../components/color');
1819

1920
var plots = module.exports = {};
2021

21-
var modules = plots.modules = {},
22-
allTypes = plots.allTypes = [],
23-
allCategories = plots.allCategories = {},
24-
subplotsRegistry = plots.subplotsRegistry = {},
25-
transformsRegistry = plots.transformsRegistry = {};
22+
// Expose registry methods that used to be on Plots for backward-compatibility
23+
plots.modules = Registry.modules;
24+
plots.allTypes = Registry.allTypes;
25+
plots.allCategories = Registry.allCategories;
26+
plots.subplotsRegistry = Registry.subplotsRegistry;
27+
plots.transformsRegistry = Registry.transformsRegistry;
28+
plots.traceIs = Registry.traceIs;
29+
plots.getModule = Registry.getModule;
2630

2731
plots.attributes = require('./attributes');
28-
plots.attributes.type.values = allTypes;
32+
plots.attributes.type.values = plots.allTypes;
2933
plots.fontAttrs = require('./font_attributes');
3034
plots.layoutAttributes = require('./layout_attributes');
3135

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

35-
/**
36-
* plots.register: register a module as the handler for a trace type
37-
*
38-
* @param {object} _module the module that will handle plotting this trace type
39-
* @param {string} thisType
40-
* @param {array of strings} categoriesIn all the categories this type is in,
41-
* tested by calls: Plotly.Plots.traceIs(trace, oneCategory)
42-
* @param {object} meta meta information about the trace type
43-
*/
44-
plots.register = function(_module, thisType, categoriesIn, meta) {
45-
if(modules[thisType]) {
46-
Lib.log('Type ' + thisType + ' already registered');
47-
return;
48-
}
49-
50-
var categoryObj = {};
51-
for(var i = 0; i < categoriesIn.length; i++) {
52-
categoryObj[categoriesIn[i]] = true;
53-
allCategories[categoriesIn[i]] = true;
54-
}
55-
56-
modules[thisType] = {
57-
_module: _module,
58-
categories: categoryObj
59-
};
60-
61-
if(meta && Object.keys(meta).length) {
62-
modules[thisType].meta = meta;
63-
}
64-
65-
allTypes.push(thisType);
66-
};
67-
68-
function getTraceType(traceType) {
69-
if(typeof traceType === 'object') traceType = traceType.type;
70-
return traceType;
71-
}
72-
73-
plots.getModule = function(trace) {
74-
if(trace.r !== undefined) {
75-
Lib.warn('Tried to put a polar trace ' +
76-
'on an incompatible graph of cartesian ' +
77-
'data. Ignoring this dataset.', trace
78-
);
79-
return false;
80-
}
81-
82-
var _module = modules[getTraceType(trace)];
83-
if(!_module) return false;
84-
return _module._module;
85-
};
86-
87-
88-
/**
89-
* plots.traceIs: is this trace type in this category?
90-
*
91-
* traceType: a trace (object) or trace type (string)
92-
* category: a category (string)
93-
*/
94-
plots.traceIs = function traceIs(traceType, category) {
95-
traceType = getTraceType(traceType);
96-
97-
if(traceType === 'various') return false; // FIXME
39+
var subplotsRegistry = plots.subplotsRegistry;
40+
var transformsRegistry = plots.transformsRegistry;
9841

99-
var _module = modules[traceType];
100-
101-
if(!_module) {
102-
if(traceType !== undefined) {
103-
Lib.log('Unrecognized trace type ' + traceType + '.');
104-
}
105-
_module = modules[plots.attributes.type.dflt];
106-
}
107-
108-
return !!_module.categories[category];
109-
};
110-
111-
112-
/**
113-
* plots.registerSubplot: register a subplot type
114-
*
115-
* @param {object} _module subplot module:
116-
*
117-
* @param {string or array of strings} attr
118-
* attribute name in traces and layout
119-
* @param {string or array of strings} idRoot
120-
* root of id (setting the possible value for attrName)
121-
* @param {object} attributes
122-
* attribute(s) for traces of this subplot type
123-
*
124-
* In trace objects `attr` is the object key taking a valid `id` as value
125-
* (the set of all valid ids is generated below and stored in idRegex).
126-
*
127-
* In the layout object, a or several valid `attr` name(s) can be keys linked
128-
* to a nested attribute objects
129-
* (the set of all valid attr names is generated below and stored in attrRegex).
130-
*
131-
* TODO use these in Lib.coerce
132-
*/
133-
plots.registerSubplot = function(_module) {
134-
var plotType = _module.name;
135-
136-
if(subplotsRegistry[plotType]) {
137-
Lib.log('Plot type ' + plotType + ' already registered.');
138-
return;
139-
}
140-
141-
// not sure what's best for the 'cartesian' type at this point
142-
subplotsRegistry[plotType] = _module;
143-
};
14442

14543
/**
14644
* Find subplot ids in data.

src/registry.js

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
var Lib = require('./lib');
13+
var basePlotAttributes = require('./plots/attributes');
14+
15+
exports.modules = {};
16+
exports.allTypes = [];
17+
exports.allCategories = {};
18+
exports.subplotsRegistry = {};
19+
exports.transformsRegistry = {};
20+
21+
/**
22+
* register a module as the handler for a trace type
23+
*
24+
* @param {object} _module the module that will handle plotting this trace type
25+
* @param {string} thisType
26+
* @param {array of strings} categoriesIn all the categories this type is in,
27+
* tested by calls: traceIs(trace, oneCategory)
28+
* @param {object} meta meta information about the trace type
29+
*/
30+
exports.register = function(_module, thisType, categoriesIn, meta) {
31+
if(exports.modules[thisType]) {
32+
Lib.log('Type ' + thisType + ' already registered');
33+
return;
34+
}
35+
36+
var categoryObj = {};
37+
for(var i = 0; i < categoriesIn.length; i++) {
38+
categoryObj[categoriesIn[i]] = true;
39+
exports.allCategories[categoriesIn[i]] = true;
40+
}
41+
42+
exports.modules[thisType] = {
43+
_module: _module,
44+
categories: categoryObj
45+
};
46+
47+
if(meta && Object.keys(meta).length) {
48+
exports.modules[thisType].meta = meta;
49+
}
50+
51+
exports.allTypes.push(thisType);
52+
};
53+
54+
/**
55+
* register a subplot type
56+
*
57+
* @param {object} _module subplot module:
58+
*
59+
* @param {string or array of strings} attr
60+
* attribute name in traces and layout
61+
* @param {string or array of strings} idRoot
62+
* root of id (setting the possible value for attrName)
63+
* @param {object} attributes
64+
* attribute(s) for traces of this subplot type
65+
*
66+
* In trace objects `attr` is the object key taking a valid `id` as value
67+
* (the set of all valid ids is generated below and stored in idRegex).
68+
*
69+
* In the layout object, a or several valid `attr` name(s) can be keys linked
70+
* to a nested attribute objects
71+
* (the set of all valid attr names is generated below and stored in attrRegex).
72+
*/
73+
exports.registerSubplot = function(_module) {
74+
var plotType = _module.name;
75+
76+
if(exports.subplotsRegistry[plotType]) {
77+
Lib.log('Plot type ' + plotType + ' already registered.');
78+
return;
79+
}
80+
81+
// not sure what's best for the 'cartesian' type at this point
82+
exports.subplotsRegistry[plotType] = _module;
83+
};
84+
85+
/**
86+
* Get registered module using trace object or trace type
87+
*
88+
* @param {object||string} trace
89+
* trace object with prop 'type' or trace type as a string
90+
* @return {object}
91+
* module object corresponding to trace type
92+
*/
93+
exports.getModule = function(trace) {
94+
if(trace.r !== undefined) {
95+
Lib.warn('Tried to put a polar trace ' +
96+
'on an incompatible graph of cartesian ' +
97+
'data. Ignoring this dataset.', trace
98+
);
99+
return false;
100+
}
101+
102+
var _module = exports.modules[getTraceType(trace)];
103+
if(!_module) return false;
104+
return _module._module;
105+
};
106+
107+
/**
108+
* Determine if this trace type is in a given category
109+
*
110+
* @param {object||string} traceType
111+
* a trace (object) or trace type (string)
112+
* @param {string} category
113+
* category in question
114+
* @return {boolean}
115+
*/
116+
exports.traceIs = function(traceType, category) {
117+
traceType = getTraceType(traceType);
118+
119+
if(traceType === 'various') return false; // FIXME
120+
121+
var _module = exports.modules[traceType];
122+
123+
if(!_module) {
124+
if(traceType !== undefined) {
125+
Lib.log('Unrecognized trace type ' + traceType + '.');
126+
}
127+
128+
_module = exports.modules[basePlotAttributes.type.dflt];
129+
}
130+
131+
return !!_module.categories[category];
132+
};
133+
134+
function getTraceType(traceType) {
135+
if(typeof traceType === 'object') traceType = traceType.type;
136+
return traceType;
137+
}

0 commit comments

Comments
 (0)