Skip to content

Commit 0aa87c4

Browse files
committed
add Registry.getComponentMethod + use it in plot_api / plots
1 parent 4f754aa commit 0aa87c4

File tree

3 files changed

+52
-39
lines changed

3 files changed

+52
-39
lines changed

src/plot_api/plot_api.js

+26-27
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@ var Polar = require('../plots/polar');
2626
var Color = require('../components/color');
2727
var Drawing = require('../components/drawing');
2828
var ErrorBars = require('../components/errorbars');
29-
var Annotations = require('../components/annotations');
30-
var Images = require('../components/images');
31-
var Legend = require('../components/legend');
32-
var RangeSlider = require('../components/rangeslider');
33-
var RangeSelector = require('../components/rangeselector');
34-
var UpdateMenus = require('../components/updatemenus');
35-
var Shapes = require('../components/shapes');
3629
var Titles = require('../components/titles');
3730
var ModeBar = require('../components/modebar');
3831
var xmlnsNamespaces = require('../constants/xmlns_namespaces');
@@ -183,9 +176,9 @@ Plotly.plot = function(gd, data, layout, config) {
183176
var calcdata = gd.calcdata;
184177
var i, cd, trace;
185178

186-
Legend.draw(gd);
187-
RangeSelector.draw(gd);
188-
UpdateMenus.draw(gd);
179+
Registry.getComponentMethod('legend', 'draw')(gd);
180+
Registry.getComponentMethod('rangeselector', 'draw')(gd);
181+
Registry.getComponentMethod('updatemenus', 'draw')(gd);
189182

190183
for(i = 0; i < calcdata.length; i++) {
191184
cd = calcdata[i];
@@ -234,8 +227,8 @@ Plotly.plot = function(gd, data, layout, config) {
234227

235228
// TODO: autosize extra for text markers
236229
return Lib.syncOrAsync([
237-
Shapes.calcAutorange,
238-
Annotations.calcAutorange,
230+
Registry.getComponentMethod('shapes', 'calcAutorange'),
231+
Registry.getComponentMethod('annotations', 'calcAutorange'),
239232
doAutoRange
240233
], gd);
241234
}
@@ -287,8 +280,8 @@ Plotly.plot = function(gd, data, layout, config) {
287280
Plots.style(gd);
288281

289282
// show annotations and shapes
290-
Shapes.draw(gd);
291-
Annotations.draw(gd);
283+
Registry.getComponentMethod('shapes', 'draw')(gd);
284+
Registry.getComponentMethod('annoations', 'draw')(gd);
292285

293286
// source links
294287
Plots.addLinks(gd);
@@ -303,13 +296,13 @@ Plotly.plot = function(gd, data, layout, config) {
303296
// correctly sized and the whole plot re-margined. gd._replotting must
304297
// be set to false before these will work properly.
305298
function finalDraw() {
306-
Shapes.draw(gd);
307-
Images.draw(gd);
308-
Annotations.draw(gd);
309-
Legend.draw(gd);
310-
RangeSlider.draw(gd);
311-
RangeSelector.draw(gd);
312-
UpdateMenus.draw(gd);
299+
Registry.getComponentMethod('shapes', 'draw')(gd);
300+
Registry.getComponentMethod('images', 'draw')(gd);
301+
Registry.getComponentMethod('annotations', 'draw')(gd);
302+
Registry.getComponentMethod('legend', 'draw')(gd);
303+
Registry.getComponentMethod('rangeslider', 'draw')(gd);
304+
Registry.getComponentMethod('rangeselector', 'draw')(gd);
305+
Registry.getComponentMethod('updatemenus', 'draw')(gd);
313306
}
314307

315308
function cleanUp() {
@@ -2007,8 +2000,10 @@ Plotly.restyle = function restyle(gd, astr, val, traces) {
20072000
arraysToCalcdata = (((cdi[0] || {}).trace || {})._module || {}).arraysToCalcdata;
20082001
if(arraysToCalcdata) arraysToCalcdata(cdi);
20092002
}
2003+
20102004
Plots.style(gd);
2011-
Legend.draw(gd);
2005+
Registry.getComponentMethod('legend', 'draw')(gd);
2006+
20122007
return Plots.previousPromises(gd);
20132008
});
20142009
}
@@ -2285,8 +2280,8 @@ Plotly.relayout = function relayout(gd, astr, val) {
22852280
var objNum = p.parts[1],
22862281
objType = p.parts[0],
22872282
objList = layout[objType] || [],
2288-
objModule = Plotly[Lib.titleCase(objType)],
22892283
obji = objList[objNum] || {};
2284+
22902285
// if p.parts is just an annotation number, and val is either
22912286
// 'add' or an entire annotation to add, the undo is 'remove'
22922287
// if val is 'remove' then undo is the whole annotation object
@@ -2303,21 +2298,25 @@ Plotly.relayout = function relayout(gd, astr, val) {
23032298
}
23042299
else Lib.log('???', aobj);
23052300
}
2301+
23062302
if((refAutorange(obji, 'x') || refAutorange(obji, 'y')) &&
23072303
!Lib.containsAny(ai, ['color', 'opacity', 'align', 'dash'])) {
23082304
docalc = true;
23092305
}
2306+
23102307
// TODO: combine all edits to a given annotation / shape into one call
23112308
// as it is we get separate calls for x and y (or ax and ay) on move
2312-
objModule.drawOne(gd, objNum, p.parts.slice(2).join('.'), aobj[ai]);
2309+
2310+
var drawOne = Registry.getComponentMethod(objType, 'drawOne');
2311+
drawOne(gd, objNum, p.parts.slice(2).join('.'), aobj[ai]);
23132312
delete aobj[ai];
23142313
}
23152314
else if(p.parts[0] === 'images') {
23162315
var update = Lib.objectFromPath(ai, vi);
23172316
Lib.extendDeepAll(gd.layout, update);
23182317

2319-
Images.supplyLayoutDefaults(gd.layout, gd._fullLayout);
2320-
Images.draw(gd);
2318+
Registry.getComponentMethod('images', 'supplyLayoutDefaults')(gd.layout, gd._fullLayout);
2319+
Registry.getComponentMethod('images', 'draw')(gd);
23212320
}
23222321
else if(p.parts[0] === 'mapbox' && p.parts[1] === 'layers') {
23232322
Lib.extendDeepAll(gd.layout, Lib.objectFromPath(ai, vi));
@@ -2429,7 +2428,7 @@ Plotly.relayout = function relayout(gd, astr, val) {
24292428

24302429
if(dolegend) {
24312430
seq.push(function doLegend() {
2432-
Legend.draw(gd);
2431+
Registry.getComponentMethod('legend', 'draw')(gd);
24332432
return Plots.previousPromises(gd);
24342433
});
24352434
}

src/plots/plots.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ plots.redrawText = function(gd) {
163163

164164
return new Promise(function(resolve) {
165165
setTimeout(function() {
166-
Plotly.Annotations.draw(gd);
167-
Plotly.Legend.draw(gd);
166+
Registry.getComponentMethod('annotations', 'draw')(gd);
167+
Registry.getComponentMethod('legend', 'draw')(gd);
168168

169169
(gd.calcdata || []).forEach(function(d) {
170170
if(d[0] && d[0].t && d[0].t.cb) d[0].t.cb();
@@ -763,17 +763,15 @@ plots.supplyLayoutModuleDefaults = function(layoutIn, layoutOut, fullData) {
763763
}
764764
}
765765

766-
// TODO register these
767-
// Legend must come after traces (e.g. it depends on 'barmode')
768-
var moduleLayoutDefaults = [
769-
'Fx', 'Annotations', 'Shapes', 'Legend', 'Images', 'UpdateMenus'
770-
];
766+
// should FX be a component?
767+
Plotly.Fx.supplyLayoutDefaults(layoutIn, layoutOut, fullData);
771768

772-
for(i = 0; i < moduleLayoutDefaults.length; i++) {
773-
_module = moduleLayoutDefaults[i];
769+
var components = Object.keys(Registry.componentsRegistry);
770+
for(i = 0; i < components.length; i++) {
771+
_module = Registry.componentsRegistry[components[i]];
774772

775-
if(Plotly[_module]) {
776-
Plotly[_module].supplyLayoutDefaults(layoutIn, layoutOut, fullData);
773+
if(_module.supplyLayoutDefaults) {
774+
_module.supplyLayoutDefaults(layoutIn, layoutOut, fullData);
777775
}
778776
}
779777
};

src/registry.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ exports.traceIs = function(traceType, category) {
122122
var _module = exports.modules[traceType];
123123

124124
if(!_module) {
125-
if(traceType !== undefined) {
125+
if(traceType && traceType !== 'area') {
126126
Lib.log('Unrecognized trace type ' + traceType + '.');
127127
}
128128

@@ -132,6 +132,22 @@ exports.traceIs = function(traceType, category) {
132132
return !!_module.categories[category];
133133
};
134134

135+
/**
136+
* Retrieve component module method
137+
*
138+
* @param {string} name
139+
* name of component (as declared in component module)
140+
* @param {string} method
141+
* name of component module method
142+
* @return {function}
143+
*/
144+
exports.getComponentMethod = function(name, method) {
145+
var _module = exports.componentsRegistry[name];
146+
147+
if(!_module) return Lib.noop;
148+
return _module[method];
149+
};
150+
135151
function getTraceType(traceType) {
136152
if(typeof traceType === 'object') traceType = traceType.type;
137153
return traceType;

0 commit comments

Comments
 (0)