Skip to content

Commit aa434dd

Browse files
committed
move getGraphDiv to lib so we can use it outside plot_api
1 parent a6df912 commit aa434dd

File tree

6 files changed

+21
-42
lines changed

6 files changed

+21
-42
lines changed

src/components/dragelement/unhover.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212

1313
var Events = require('../../lib/events');
1414
var throttle = require('../../lib/throttle');
15+
var getGraphDiv = require('../../lib/get_graph_div');
1516

1617
var hoverConstants = require('../fx/constants');
1718

1819
var unhover = module.exports = {};
1920

2021

2122
unhover.wrapped = function(gd, evt, subplot) {
22-
if(typeof gd === 'string') gd = document.getElementById(gd);
23+
gd = getGraphDiv(gd);
2324

2425
// Important, clear any queued hovers
2526
throttle.clear(gd._fullLayout._uid + hoverConstants.HOVERID);

src/components/fx/hover.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ var HOVERTEXTPAD = constants.HOVERTEXTPAD;
6767
// We wrap the hovers in a timer, to limit their frequency.
6868
// The actual rendering is done by private function _hover.
6969
exports.hover = function hover(gd, evt, subplot, noHoverEvent) {
70-
if(typeof gd === 'string') gd = document.getElementById(gd);
70+
gd = Lib.getGraphDiv(gd);
7171

7272
Lib.throttle(
7373
function() { _hover(gd, evt, subplot, noHoverEvent); },

src/lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ var throttleModule = require('./throttle');
101101
lib.throttle = throttleModule.throttle;
102102
lib.clearThrottle = throttleModule.clear;
103103

104+
lib.getGraphDiv = require('./get_graph_div');
105+
104106
lib.notifier = require('./notifier');
105107

106108
lib.filterUnique = require('./filter_unique');

src/plot_api/helpers.js

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,6 @@ var Axes = require('../plots/cartesian/axes');
1919
var Color = require('../components/color');
2020

2121

22-
// Get the container div: we store all variables for this plot as
23-
// properties of this div
24-
// some callers send this in by DOM element, others by id (string)
25-
exports.getGraphDiv = function(gd) {
26-
var gdElement;
27-
28-
if(typeof gd === 'string') {
29-
gdElement = document.getElementById(gd);
30-
31-
if(gdElement === null) {
32-
throw new Error('No DOM element with id \'' + gd + '\' exists on the page.');
33-
}
34-
35-
return gdElement;
36-
}
37-
else if(gd === null || gd === undefined) {
38-
throw new Error('DOM element provided is null or undefined');
39-
}
40-
41-
return gd; // otherwise assume that gd is a DOM element
42-
};
43-
4422
// clear the promise queue if one of them got rejected
4523
exports.clearPromiseQueue = function(gd) {
4624
if(Array.isArray(gd._promises) && gd._promises.length > 0) {

src/plot_api/plot_api.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ var axisIds = require('../plots/cartesian/axis_ids');
6060
Plotly.plot = function(gd, data, layout, config) {
6161
var frames;
6262

63-
gd = helpers.getGraphDiv(gd);
63+
gd = Lib.getGraphDiv(gd);
6464

6565
// Events.init is idempotent and bails early if gd has already been init'd
6666
Events.init(gd);
@@ -581,7 +581,7 @@ function plotPolar(gd, data, layout) {
581581

582582
// convenience function to force a full redraw, mostly for use by plotly.js
583583
Plotly.redraw = function(gd) {
584-
gd = helpers.getGraphDiv(gd);
584+
gd = Lib.getGraphDiv(gd);
585585

586586
if(!Lib.isPlotDiv(gd)) {
587587
throw new Error('This element is not a Plotly plot: ' + gd);
@@ -606,7 +606,7 @@ Plotly.redraw = function(gd) {
606606
* @param {Object} config
607607
*/
608608
Plotly.newPlot = function(gd, data, layout, config) {
609-
gd = helpers.getGraphDiv(gd);
609+
gd = Lib.getGraphDiv(gd);
610610

611611
// remove gl contexts
612612
Plots.cleanPlot([], {}, gd._fullData || {}, gd._fullLayout || {});
@@ -959,7 +959,7 @@ function spliceTraces(gd, update, indices, maxPoints, lengthenArray, spliceArray
959959
*
960960
*/
961961
Plotly.extendTraces = function extendTraces(gd, update, indices, maxPoints) {
962-
gd = helpers.getGraphDiv(gd);
962+
gd = Lib.getGraphDiv(gd);
963963

964964
var undo = spliceTraces(gd, update, indices, maxPoints,
965965

@@ -986,7 +986,7 @@ Plotly.extendTraces = function extendTraces(gd, update, indices, maxPoints) {
986986
};
987987

988988
Plotly.prependTraces = function prependTraces(gd, update, indices, maxPoints) {
989-
gd = helpers.getGraphDiv(gd);
989+
gd = Lib.getGraphDiv(gd);
990990

991991
var undo = spliceTraces(gd, update, indices, maxPoints,
992992

@@ -1022,7 +1022,7 @@ Plotly.prependTraces = function prependTraces(gd, update, indices, maxPoints) {
10221022
*
10231023
*/
10241024
Plotly.addTraces = function addTraces(gd, traces, newIndices) {
1025-
gd = helpers.getGraphDiv(gd);
1025+
gd = Lib.getGraphDiv(gd);
10261026

10271027
var currentIndices = [],
10281028
undoFunc = Plotly.deleteTraces,
@@ -1099,7 +1099,7 @@ Plotly.addTraces = function addTraces(gd, traces, newIndices) {
10991099
* @param {Number|Number[]} indices The indices
11001100
*/
11011101
Plotly.deleteTraces = function deleteTraces(gd, indices) {
1102-
gd = helpers.getGraphDiv(gd);
1102+
gd = Lib.getGraphDiv(gd);
11031103

11041104
var traces = [],
11051105
undoFunc = Plotly.addTraces,
@@ -1165,7 +1165,7 @@ Plotly.deleteTraces = function deleteTraces(gd, indices) {
11651165
* Plotly.moveTraces(gd, [b, d, e, a, c]) // same as 'move to end'
11661166
*/
11671167
Plotly.moveTraces = function moveTraces(gd, currentIndices, newIndices) {
1168-
gd = helpers.getGraphDiv(gd);
1168+
gd = Lib.getGraphDiv(gd);
11691169

11701170
var newData = [],
11711171
movingTraceMap = [],
@@ -1262,7 +1262,7 @@ Plotly.moveTraces = function moveTraces(gd, currentIndices, newIndices) {
12621262
* style files that want to specify cyclical default values).
12631263
*/
12641264
Plotly.restyle = function restyle(gd, astr, val, _traces) {
1265-
gd = helpers.getGraphDiv(gd);
1265+
gd = Lib.getGraphDiv(gd);
12661266
helpers.clearPromiseQueue(gd);
12671267

12681268
var aobj = {};
@@ -1649,7 +1649,7 @@ function _restyle(gd, aobj, traces) {
16491649
* allows setting multiple attributes simultaneously
16501650
*/
16511651
Plotly.relayout = function relayout(gd, astr, val) {
1652-
gd = helpers.getGraphDiv(gd);
1652+
gd = Lib.getGraphDiv(gd);
16531653
helpers.clearPromiseQueue(gd);
16541654

16551655
if(gd.framework && gd.framework.isPolar) {
@@ -2079,7 +2079,7 @@ function _relayout(gd, aobj) {
20792079
*
20802080
*/
20812081
Plotly.update = function update(gd, traceUpdate, layoutUpdate, _traces) {
2082-
gd = helpers.getGraphDiv(gd);
2082+
gd = Lib.getGraphDiv(gd);
20832083
helpers.clearPromiseQueue(gd);
20842084

20852085
if(gd.framework && gd.framework.isPolar) {
@@ -2185,7 +2185,7 @@ Plotly.update = function update(gd, traceUpdate, layoutUpdate, _traces) {
21852185
* configuration for the animation
21862186
*/
21872187
Plotly.animate = function(gd, frameOrGroupNameOrFrameList, animationOpts) {
2188-
gd = helpers.getGraphDiv(gd);
2188+
gd = Lib.getGraphDiv(gd);
21892189

21902190
if(!Lib.isPlotDiv(gd)) {
21912191
throw new Error(
@@ -2549,7 +2549,7 @@ Plotly.animate = function(gd, frameOrGroupNameOrFrameList, animationOpts) {
25492549
* will be overwritten.
25502550
*/
25512551
Plotly.addFrames = function(gd, frameList, indices) {
2552-
gd = helpers.getGraphDiv(gd);
2552+
gd = Lib.getGraphDiv(gd);
25532553

25542554
var numericNameWarningCount = 0;
25552555

@@ -2673,7 +2673,7 @@ Plotly.addFrames = function(gd, frameList, indices) {
26732673
* list of integer indices of frames to be deleted
26742674
*/
26752675
Plotly.deleteFrames = function(gd, frameList) {
2676-
gd = helpers.getGraphDiv(gd);
2676+
gd = Lib.getGraphDiv(gd);
26772677

26782678
if(!Lib.isPlotDiv(gd)) {
26792679
throw new Error('This element is not a Plotly plot: ' + gd);
@@ -2717,7 +2717,7 @@ Plotly.deleteFrames = function(gd, frameList) {
27172717
* the id or DOM element of the graph container div
27182718
*/
27192719
Plotly.purge = function purge(gd) {
2720-
gd = helpers.getGraphDiv(gd);
2720+
gd = Lib.getGraphDiv(gd);
27212721

27222722
var fullLayout = gd._fullLayout || {},
27232723
fullData = gd._fullData || [];

src/plot_api/to_image.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ var helpers = require('../snapshot/helpers');
1515
var toSVG = require('../snapshot/tosvg');
1616
var svgToImg = require('../snapshot/svgtoimg');
1717

18-
var getGraphDiv = require('./helpers').getGraphDiv;
19-
2018
var attrs = {
2119
format: {
2220
valType: 'enumerated',
@@ -95,7 +93,7 @@ function toImage(gd, opts) {
9593
layout = gd.layout || {};
9694
config = gd.config || {};
9795
} else {
98-
gd = getGraphDiv(gd);
96+
gd = Lib.getGraphDiv(gd);
9997
data = Lib.extendDeep([], gd.data);
10098
layout = Lib.extendDeep({}, gd.layout);
10199
config = gd._context;

0 commit comments

Comments
 (0)