Skip to content

Commit 3042ccb

Browse files
committed
snapshot: rm circular dep patterns
- by adding a `snapshot/helpers.js` module
1 parent 210ff6d commit 3042ccb

File tree

5 files changed

+50
-42
lines changed

5 files changed

+50
-42
lines changed

src/plot_api/to_image.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var isNumeric = require('fast-isnumeric');
1212

1313
var Plotly = require('../plotly');
1414
var Lib = require('../lib');
15+
var Snapshot = require('../snapshot');
1516

1617

1718
/**
@@ -22,7 +23,6 @@ var Lib = require('../lib');
2223
* @param opts.height height of snapshot in px
2324
*/
2425
function toImage(gd, opts) {
25-
var Snapshot = require('../snapshot');
2626

2727
var promise = new Promise(function(resolve, reject) {
2828
// check for undefined opts
@@ -91,12 +91,6 @@ function toImage(gd, opts) {
9191
var redrawFunc = Snapshot.getRedrawFunc(clonedGd);
9292

9393
Plotly.plot(clonedGd, clone.data, clone.layout, clone.config)
94-
// TODO: the following is Plotly.Plots.redrawText but without the waiting.
95-
// we shouldn't need to do this, but in *occasional* cases we do. Figure
96-
// out why and take it out.
97-
98-
// not sure the above TODO makes sense anymore since
99-
// we have converted to promises
10094
.then(redrawFunc)
10195
.then(wait)
10296
.then(function(url) { resolve(url); })

src/snapshot/cloneplot.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99

1010
'use strict';
1111

12-
var Plotly = require('../plotly');
12+
var Lib = require('../lib');
13+
var Plots = require('../plots/plots');
1314

14-
var extendFlat = Plotly.Lib.extendFlat;
15-
var extendDeep = Plotly.Lib.extendDeep;
15+
var extendFlat = Lib.extendFlat;
16+
var extendDeep = Lib.extendDeep;
1617

1718
// Put default plotTile layouts here
1819
function cloneLayoutOverride(tileClass) {
@@ -99,7 +100,7 @@ module.exports = function clonePlot(graphObj, options) {
99100
}
100101
}
101102

102-
var sceneIds = Plotly.Plots.getSubplotIds(newLayout, 'gl3d');
103+
var sceneIds = Plots.getSubplotIds(newLayout, 'gl3d');
103104

104105
if(sceneIds.length) {
105106
var axesImageOverride = {};

src/snapshot/helpers.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
exports.getDelay = function(fullLayout) {
13+
14+
// polar clears fullLayout._has for some reason
15+
if(!fullLayout._has) return 0;
16+
17+
// maybe we should add a 'gl' (and 'svg') layoutCategory ??
18+
return (fullLayout._has('gl3d') || fullLayout._has('gl2d')) ? 500 : 0;
19+
};
20+
21+
exports.getRedrawFunc = function(gd) {
22+
23+
// do not work if polar is present
24+
if((gd.data && gd.data[0] && gd.data[0].r)) return;
25+
26+
return function() {
27+
(gd.calcdata || []).forEach(function(d) {
28+
if(d[0] && d[0].t && d[0].t.cb) d[0].t.cb();
29+
});
30+
};
31+
};

src/snapshot/index.js

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,11 @@
99

1010
'use strict';
1111

12-
function getDelay(fullLayout) {
13-
14-
// polar clears fullLayout._has for some reason
15-
if(!fullLayout._has) return 0;
16-
17-
// maybe we should add a 'gl' (and 'svg') layoutCategory ??
18-
return (fullLayout._has('gl3d') || fullLayout._has('gl2d')) ? 500 : 0;
19-
}
20-
21-
function getRedrawFunc(gd) {
22-
23-
// do not work if polar is present
24-
if((gd.data && gd.data[0] && gd.data[0].r)) return;
25-
26-
return function() {
27-
(gd.calcdata || []).forEach(function(d) {
28-
if(d[0] && d[0].t && d[0].t.cb) d[0].t.cb();
29-
});
30-
};
31-
}
12+
var helpers = require('./helpers');
3213

3314
var Snapshot = {
34-
getDelay: getDelay,
35-
getRedrawFunc: getRedrawFunc,
15+
getDelay: helpers.getDelay,
16+
getRedrawFunc: helpers.getRedrawFunc,
3617
clone: require('./cloneplot'),
3718
toSVG: require('./tosvg'),
3819
svgToImg: require('./svgtoimg'),

src/snapshot/toimage.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ var EventEmitter = require('events').EventEmitter;
1313
var Plotly = require('../plotly');
1414
var Lib = require('../lib');
1515

16+
var helpers = require('./helpers');
17+
var clonePlot = require('./cloneplot');
18+
var toSVG = require('./tosvg');
19+
var svgToImg = require('./svgtoimg');
20+
1621

1722
/**
1823
* @param {object} gd figure Object
@@ -22,10 +27,9 @@ var Lib = require('../lib');
2227
function toImage(gd, opts) {
2328

2429
// first clone the GD so we can operate in a clean environment
25-
var Snapshot = Plotly.Snapshot;
2630
var ev = new EventEmitter();
2731

28-
var clone = Snapshot.clone(gd, {format: 'png'});
32+
var clone = clonePlot(gd, {format: 'png'});
2933
var clonedGd = clone.td;
3034

3135
// put the cloned div somewhere off screen before attaching to DOM
@@ -34,15 +38,15 @@ function toImage(gd, opts) {
3438
document.body.appendChild(clonedGd);
3539

3640
function wait() {
37-
var delay = Snapshot.getDelay(clonedGd._fullLayout);
41+
var delay = helpers.getDelay(clonedGd._fullLayout);
3842

3943
setTimeout(function() {
40-
var svg = Plotly.Snapshot.toSVG(clonedGd);
44+
var svg = toSVG(clonedGd);
4145

4246
var canvas = document.createElement('canvas');
4347
canvas.id = Lib.randstr();
4448

45-
ev = Plotly.Snapshot.svgToImg({
49+
ev = svgToImg({
4650
format: opts.format,
4751
width: clonedGd._fullLayout.width,
4852
height: clonedGd._fullLayout.height,
@@ -58,12 +62,9 @@ function toImage(gd, opts) {
5862
}, delay);
5963
}
6064

61-
var redrawFunc = Snapshot.getRedrawFunc(clonedGd);
65+
var redrawFunc = helpers.getRedrawFunc(clonedGd);
6266

6367
Plotly.plot(clonedGd, clone.data, clone.layout, clone.config)
64-
// TODO: the following is Plotly.Plots.redrawText but without the waiting.
65-
// we shouldn't need to do this, but in *occasional* cases we do. Figure
66-
// out why and take it out.
6768
.then(redrawFunc)
6869
.then(wait)
6970
.catch(function(err) {

0 commit comments

Comments
 (0)