Skip to content

Commit 52b83b3

Browse files
committed
add apiMethodRegistry in src/registry
- export api methods in plot_api/index.js - expose and register them in src/core.js
1 parent 87275af commit 52b83b3

File tree

4 files changed

+89
-63
lines changed

4 files changed

+89
-63
lines changed

src/core.js

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@
88

99
'use strict';
1010

11-
/*
12-
* Export the plotly.js API methods.
13-
*/
14-
15-
var Plotly = require('./plotly');
16-
1711
// package version injected by `npm run preprocess`
1812
exports.version = '1.34.0';
1913

@@ -30,33 +24,25 @@ require('./fonts/mathjax_config');
3024
var Registry = require('./registry');
3125
var register = exports.register = Registry.register;
3226

33-
// plot api
34-
exports.plot = Plotly.plot;
35-
exports.newPlot = Plotly.newPlot;
36-
exports.restyle = Plotly.restyle;
37-
exports.relayout = Plotly.relayout;
38-
exports.redraw = Plotly.redraw;
39-
exports.update = Plotly.update;
40-
exports.react = Plotly.react;
41-
exports.extendTraces = Plotly.extendTraces;
42-
exports.prependTraces = Plotly.prependTraces;
43-
exports.addTraces = Plotly.addTraces;
44-
exports.deleteTraces = Plotly.deleteTraces;
45-
exports.moveTraces = Plotly.moveTraces;
46-
exports.purge = Plotly.purge;
4727
exports.setPlotConfig = require('./plot_api/set_plot_config');
48-
exports.toImage = require('./plot_api/to_image');
49-
exports.downloadImage = require('./snapshot/download');
50-
exports.validate = require('./plot_api/validate');
51-
exports.addFrames = Plotly.addFrames;
52-
exports.deleteFrames = Plotly.deleteFrames;
53-
exports.animate = Plotly.animate;
28+
// expose plot api methods
29+
var plotApi = require('./plot_api');
30+
var methodNames = Object.keys(plotApi);
31+
for(var i = 0; i < methodNames.length; i++) {
32+
var name = methodNames[i];
33+
exports[name] = plotApi[name];
34+
register({
35+
moduleType: 'apiMethod',
36+
name: name,
37+
fn: plotApi[name]
38+
});
39+
}
5440

5541
// scatter is the only trace included by default
56-
exports.register(require('./traces/scatter'));
42+
register(require('./traces/scatter'));
5743

5844
// register all registrable components modules
59-
exports.register([
45+
register([
6046
require('./components/fx'),
6147
require('./components/legend'),
6248
require('./components/annotations'),
@@ -72,7 +58,7 @@ exports.register([
7258
]);
7359

7460
// locales en and en-US are required for default behavior
75-
exports.register([
61+
register([
7662
require('./locale-en'),
7763
require('./locale-en-us')
7864
]);

src/plot_api/index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright 2012-2018, 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+
'use strict';
10+
11+
var main = require('./plot_api');
12+
13+
exports.plot = main.plot;
14+
exports.newPlot = main.newPlot;
15+
exports.restyle = main.restyle;
16+
exports.relayout = main.relayout;
17+
exports.redraw = main.redraw;
18+
exports.update = main.update;
19+
exports.react = main.react;
20+
exports.extendTraces = main.extendTraces;
21+
exports.prependTraces = main.prependTraces;
22+
exports.addTraces = main.addTraces;
23+
exports.deleteTraces = main.deleteTraces;
24+
exports.moveTraces = main.moveTraces;
25+
exports.purge = main.purge;
26+
exports.addFrames = main.addFrames;
27+
exports.deleteFrames = main.deleteFrames;
28+
exports.animate = main.animate;
29+
30+
exports.toImage = require('./to_image');
31+
exports.validate = require('./validate');
32+
exports.downloadImage = require('../snapshot/download');

src/plot_api/plot_api.js

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ var numericNameWarningCountLimit = 5;
6565
* object containing `data`, `layout`, `config`, and `frames` members
6666
*
6767
*/
68-
Plotly.plot = function(gd, data, layout, config) {
68+
exports.plot = function(gd, data, layout, config) {
6969
var frames;
7070

7171
gd = Lib.getGraphDiv(gd);
@@ -93,7 +93,7 @@ Plotly.plot = function(gd, data, layout, config) {
9393

9494
function addFrames() {
9595
if(frames) {
96-
return Plotly.addFrames(gd, frames);
96+
return exports.addFrames(gd, frames);
9797
}
9898
}
9999

@@ -627,7 +627,7 @@ function plotPolar(gd, data, layout) {
627627
}
628628

629629
// convenience function to force a full redraw, mostly for use by plotly.js
630-
Plotly.redraw = function(gd) {
630+
exports.redraw = function(gd) {
631631
gd = Lib.getGraphDiv(gd);
632632

633633
if(!Lib.isPlotDiv(gd)) {
@@ -638,7 +638,7 @@ Plotly.redraw = function(gd) {
638638
helpers.cleanLayout(gd.layout);
639639

640640
gd.calcdata = undefined;
641-
return Plotly.plot(gd).then(function() {
641+
return exports.plot(gd).then(function() {
642642
gd.emit('plotly_redraw');
643643
return gd;
644644
});
@@ -652,14 +652,14 @@ Plotly.redraw = function(gd) {
652652
* @param {Object} layout
653653
* @param {Object} config
654654
*/
655-
Plotly.newPlot = function(gd, data, layout, config) {
655+
exports.newPlot = function(gd, data, layout, config) {
656656
gd = Lib.getGraphDiv(gd);
657657

658658
// remove gl contexts
659659
Plots.cleanPlot([], {}, gd._fullData || {}, gd._fullLayout || {});
660660

661661
Plots.purge(gd);
662-
return Plotly.plot(gd, data, layout, config);
662+
return exports.plot(gd, data, layout, config);
663663
};
664664

665665
/**
@@ -987,7 +987,7 @@ function concatTypedArray(arr0, arr1) {
987987
* @param {Number|Object} [maxPoints] Number of points for trace window after lengthening.
988988
*
989989
*/
990-
Plotly.extendTraces = function extendTraces(gd, update, indices, maxPoints) {
990+
exports.extendTraces = function extendTraces(gd, update, indices, maxPoints) {
991991
gd = Lib.getGraphDiv(gd);
992992

993993
function updateArray(target, insert, maxp) {
@@ -1038,14 +1038,14 @@ Plotly.extendTraces = function extendTraces(gd, update, indices, maxPoints) {
10381038
}
10391039

10401040
var undo = spliceTraces(gd, update, indices, maxPoints, updateArray);
1041-
var promise = Plotly.redraw(gd);
1041+
var promise = exports.redraw(gd);
10421042
var undoArgs = [gd, undo.update, indices, undo.maxPoints];
1043-
Queue.add(gd, Plotly.prependTraces, undoArgs, extendTraces, arguments);
1043+
Queue.add(gd, exports.prependTraces, undoArgs, extendTraces, arguments);
10441044

10451045
return promise;
10461046
};
10471047

1048-
Plotly.prependTraces = function prependTraces(gd, update, indices, maxPoints) {
1048+
exports.prependTraces = function prependTraces(gd, update, indices, maxPoints) {
10491049
gd = Lib.getGraphDiv(gd);
10501050

10511051
function updateArray(target, insert, maxp) {
@@ -1095,9 +1095,9 @@ Plotly.prependTraces = function prependTraces(gd, update, indices, maxPoints) {
10951095
}
10961096

10971097
var undo = spliceTraces(gd, update, indices, maxPoints, updateArray);
1098-
var promise = Plotly.redraw(gd);
1098+
var promise = exports.redraw(gd);
10991099
var undoArgs = [gd, undo.update, indices, undo.maxPoints];
1100-
Queue.add(gd, Plotly.extendTraces, undoArgs, prependTraces, arguments);
1100+
Queue.add(gd, exports.extendTraces, undoArgs, prependTraces, arguments);
11011101

11021102
return promise;
11031103
};
@@ -1111,11 +1111,11 @@ Plotly.prependTraces = function prependTraces(gd, update, indices, maxPoints) {
11111111
* @param {Number[]|Number} [newIndices=[gd.data.length]] Locations to add traces
11121112
*
11131113
*/
1114-
Plotly.addTraces = function addTraces(gd, traces, newIndices) {
1114+
exports.addTraces = function addTraces(gd, traces, newIndices) {
11151115
gd = Lib.getGraphDiv(gd);
11161116

11171117
var currentIndices = [],
1118-
undoFunc = Plotly.deleteTraces,
1118+
undoFunc = exports.deleteTraces,
11191119
redoFunc = addTraces,
11201120
undoArgs = [gd, currentIndices],
11211121
redoArgs = [gd, traces], // no newIndices here
@@ -1150,7 +1150,7 @@ Plotly.addTraces = function addTraces(gd, traces, newIndices) {
11501150
// if the user didn't define newIndices, they just want the traces appended
11511151
// i.e., we can simply redraw and be done
11521152
if(typeof newIndices === 'undefined') {
1153-
promise = Plotly.redraw(gd);
1153+
promise = exports.redraw(gd);
11541154
Queue.add(gd, undoFunc, undoArgs, redoFunc, redoArgs);
11551155
return promise;
11561156
}
@@ -1176,7 +1176,7 @@ Plotly.addTraces = function addTraces(gd, traces, newIndices) {
11761176
// this requires some extra work that moveTraces will do
11771177
Queue.startSequence(gd);
11781178
Queue.add(gd, undoFunc, undoArgs, redoFunc, redoArgs);
1179-
promise = Plotly.moveTraces(gd, currentIndices, newIndices);
1179+
promise = exports.moveTraces(gd, currentIndices, newIndices);
11801180
Queue.stopSequence(gd);
11811181
return promise;
11821182
};
@@ -1188,11 +1188,11 @@ Plotly.addTraces = function addTraces(gd, traces, newIndices) {
11881188
* @param {Object[]} gd.data The array of traces we're removing from
11891189
* @param {Number|Number[]} indices The indices
11901190
*/
1191-
Plotly.deleteTraces = function deleteTraces(gd, indices) {
1191+
exports.deleteTraces = function deleteTraces(gd, indices) {
11921192
gd = Lib.getGraphDiv(gd);
11931193

11941194
var traces = [],
1195-
undoFunc = Plotly.addTraces,
1195+
undoFunc = exports.addTraces,
11961196
redoFunc = deleteTraces,
11971197
undoArgs = [gd, traces, indices],
11981198
redoArgs = [gd, indices],
@@ -1217,7 +1217,7 @@ Plotly.deleteTraces = function deleteTraces(gd, indices) {
12171217
traces.push(deletedTrace);
12181218
}
12191219

1220-
var promise = Plotly.redraw(gd);
1220+
var promise = exports.redraw(gd);
12211221
Queue.add(gd, undoFunc, undoArgs, redoFunc, redoArgs);
12221222

12231223
return promise;
@@ -1254,7 +1254,7 @@ Plotly.deleteTraces = function deleteTraces(gd, indices) {
12541254
* // reorder all traces (assume there are 5--a, b, c, d, e)
12551255
* Plotly.moveTraces(gd, [b, d, e, a, c]) // same as 'move to end'
12561256
*/
1257-
Plotly.moveTraces = function moveTraces(gd, currentIndices, newIndices) {
1257+
exports.moveTraces = function moveTraces(gd, currentIndices, newIndices) {
12581258
gd = Lib.getGraphDiv(gd);
12591259

12601260
var newData = [],
@@ -1315,7 +1315,7 @@ Plotly.moveTraces = function moveTraces(gd, currentIndices, newIndices) {
13151315

13161316
gd.data = newData;
13171317

1318-
var promise = Plotly.redraw(gd);
1318+
var promise = exports.redraw(gd);
13191319
Queue.add(gd, undoFunc, undoArgs, redoFunc, redoArgs);
13201320

13211321
return promise;
@@ -1351,7 +1351,7 @@ Plotly.moveTraces = function moveTraces(gd, currentIndices, newIndices) {
13511351
* If the array is too short, it will wrap around (useful for
13521352
* style files that want to specify cyclical default values).
13531353
*/
1354-
Plotly.restyle = function restyle(gd, astr, val, _traces) {
1354+
exports.restyle = function restyle(gd, astr, val, _traces) {
13551355
gd = Lib.getGraphDiv(gd);
13561356
helpers.clearPromiseQueue(gd);
13571357

@@ -1382,7 +1382,7 @@ Plotly.restyle = function restyle(gd, astr, val, _traces) {
13821382
var seq = [];
13831383

13841384
if(flags.fullReplot) {
1385-
seq.push(Plotly.plot);
1385+
seq.push(exports.plot);
13861386
} else {
13871387
seq.push(Plots.previousPromises);
13881388

@@ -2156,7 +2156,7 @@ function refAutorange(gd, obj, axLetter) {
21562156
* integer or array of integers for the traces to alter (all if omitted)
21572157
*
21582158
*/
2159-
Plotly.update = function update(gd, traceUpdate, layoutUpdate, _traces) {
2159+
exports.update = function update(gd, traceUpdate, layoutUpdate, _traces) {
21602160
gd = Lib.getGraphDiv(gd);
21612161
helpers.clearPromiseQueue(gd);
21622162

@@ -2194,10 +2194,10 @@ Plotly.update = function update(gd, traceUpdate, layoutUpdate, _traces) {
21942194
gd.data = undefined;
21952195
gd.layout = undefined;
21962196

2197-
seq.push(function() { return Plotly.plot(gd, data, layout); });
2197+
seq.push(function() { return exports.plot(gd, data, layout); });
21982198
}
21992199
else if(restyleFlags.fullReplot) {
2200-
seq.push(Plotly.plot);
2200+
seq.push(exports.plot);
22012201
}
22022202
else if(relayoutFlags.layoutReplot) {
22032203
seq.push(subroutines.layoutReplot);
@@ -2258,10 +2258,10 @@ Plotly.update = function update(gd, traceUpdate, layoutUpdate, _traces) {
22582258
* object containing `data`, `layout`, `config`, and `frames` members
22592259
*
22602260
*/
2261-
Plotly.react = function(gd, data, layout, config) {
2261+
exports.react = function(gd, data, layout, config) {
22622262
var frames, plotDone;
22632263

2264-
function addFrames() { return Plotly.addFrames(gd, frames); }
2264+
function addFrames() { return exports.addFrames(gd, frames); }
22652265

22662266
gd = Lib.getGraphDiv(gd);
22672267

@@ -2270,7 +2270,7 @@ Plotly.react = function(gd, data, layout, config) {
22702270

22712271
// you can use this as the initial draw as well as to update
22722272
if(!Lib.isPlotDiv(gd) || !oldFullData || !oldFullLayout) {
2273-
plotDone = Plotly.newPlot(gd, data, layout, config);
2273+
plotDone = exports.newPlot(gd, data, layout, config);
22742274
}
22752275
else {
22762276

@@ -2323,7 +2323,7 @@ Plotly.react = function(gd, data, layout, config) {
23232323

23242324
if(restyleFlags.fullReplot || relayoutFlags.layoutReplot || configChanged) {
23252325
gd._fullLayout._skipDefaults = true;
2326-
seq.push(Plotly.plot);
2326+
seq.push(exports.plot);
23272327
}
23282328
else {
23292329
for(var componentType in relayoutFlags.arrays) {
@@ -2660,7 +2660,7 @@ function diffConfig(oldConfig, newConfig) {
26602660
* @param {object} animationOpts
26612661
* configuration for the animation
26622662
*/
2663-
Plotly.animate = function(gd, frameOrGroupNameOrFrameList, animationOpts) {
2663+
exports.animate = function(gd, frameOrGroupNameOrFrameList, animationOpts) {
26642664
gd = Lib.getGraphDiv(gd);
26652665

26662666
if(!Lib.isPlotDiv(gd)) {
@@ -3024,7 +3024,7 @@ Plotly.animate = function(gd, frameOrGroupNameOrFrameList, animationOpts) {
30243024
* provided, an index will be provided in serial order. If already used, the frame
30253025
* will be overwritten.
30263026
*/
3027-
Plotly.addFrames = function(gd, frameList, indices) {
3027+
exports.addFrames = function(gd, frameList, indices) {
30283028
gd = Lib.getGraphDiv(gd);
30293029

30303030
if(frameList === null || frameList === undefined) {
@@ -3153,7 +3153,7 @@ Plotly.addFrames = function(gd, frameList, indices) {
31533153
* @param {array of integers} frameList
31543154
* list of integer indices of frames to be deleted
31553155
*/
3156-
Plotly.deleteFrames = function(gd, frameList) {
3156+
exports.deleteFrames = function(gd, frameList) {
31573157
gd = Lib.getGraphDiv(gd);
31583158

31593159
if(!Lib.isPlotDiv(gd)) {
@@ -3197,7 +3197,7 @@ Plotly.deleteFrames = function(gd, frameList) {
31973197
* @param {string id or DOM element} gd
31983198
* the id or DOM element of the graph container div
31993199
*/
3200-
Plotly.purge = function purge(gd) {
3200+
exports.purge = function purge(gd) {
32013201
gd = Lib.getGraphDiv(gd);
32023202

32033203
var fullLayout = gd._fullLayout || {},

0 commit comments

Comments
 (0)