Skip to content

Commit a85ed21

Browse files
committed
fix #2930 - don't try to check for _snapshotInProgress on graph id
1 parent a8c6217 commit a85ed21

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

src/snapshot/download.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,26 @@ var toImage = require('../plot_api/to_image');
1313
var Lib = require('../lib'); // for isIE
1414
var fileSaver = require('./filesaver');
1515

16-
/**
17-
* @param {object} gd figure Object
18-
* @param {object} opts option object
19-
* @param opts.format 'jpeg' | 'png' | 'webp' | 'svg'
20-
* @param opts.width width of snapshot in px
21-
* @param opts.height height of snapshot in px
22-
* @param opts.filename name of file excluding extension
16+
/** Plotly.downloadImage
17+
*
18+
* @param {object | string | HTML div} gd
19+
* can either be a data/layout/config object
20+
* or an existing graph <div>
21+
* or an id to an existing graph <div>
22+
* @param {object} opts (see ../plot_api/to_image)
23+
* @return {promise}
2324
*/
2425
function downloadImage(gd, opts) {
26+
var _gd;
27+
if(!Lib.isPlainObject(gd)) _gd = Lib.getGraphDiv(gd);
2528

2629
// check for undefined opts
2730
opts = opts || {};
28-
2931
// default to png
3032
opts.format = opts.format || 'png';
3133

3234
return new Promise(function(resolve, reject) {
33-
if(gd._snapshotInProgress) {
35+
if(_gd && _gd._snapshotInProgress) {
3436
reject(new Error('Snapshotting already in progress.'));
3537
}
3638

@@ -43,19 +45,19 @@ function downloadImage(gd, opts) {
4345
reject(new Error('Sorry IE does not support downloading from canvas. Try {format:\'svg\'} instead.'));
4446
}
4547

46-
gd._snapshotInProgress = true;
48+
_gd._snapshotInProgress = true;
4749
var promise = toImage(gd, opts);
4850

4951
var filename = opts.filename || gd.fn || 'newplot';
5052
filename += '.' + opts.format;
5153

5254
promise.then(function(result) {
53-
gd._snapshotInProgress = false;
55+
_gd._snapshotInProgress = false;
5456
return fileSaver(result, filename);
5557
}).then(function(name) {
5658
resolve(name);
5759
}).catch(function(err) {
58-
gd._snapshotInProgress = false;
60+
_gd._snapshotInProgress = false;
5961
reject(err);
6062
});
6163
});

test/jasmine/tests/download_test.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ describe('Plotly.downloadImage', function() {
6161
downloadTest(gd, 'svg', done);
6262
}, LONG_TIMEOUT_INTERVAL);
6363

64+
it('should work when passing graph div id', function(done) {
65+
downloadTest('graph', 'svg', done);
66+
}, LONG_TIMEOUT_INTERVAL);
67+
6468
it('should produce the right SVG output in IE', function(done) {
6569
// mock up IE behavior
6670
spyOn(Lib, 'isIE').and.callFake(function() { return true; });
@@ -127,7 +131,7 @@ function downloadTest(gd, format, done) {
127131
});
128132
});
129133

130-
Plotly.plot(gd, textchartMock.data, textchartMock.layout).then(function(gd) {
134+
Plotly.plot(gd, textchartMock.data, textchartMock.layout).then(function() {
131135
// start observing dom
132136
// configuration of the observer:
133137
var config = { childList: true };
@@ -136,7 +140,8 @@ function downloadTest(gd, format, done) {
136140
observer.observe(target, config);
137141

138142
return Plotly.downloadImage(gd, {format: format, height: 300, width: 300, filename: 'plotly_download'});
139-
}).then(function(filename) {
143+
})
144+
.then(function(filename) {
140145
// stop observing
141146
observer.disconnect();
142147
// look for an added and removed link
@@ -150,8 +155,9 @@ function downloadTest(gd, format, done) {
150155

151156
// check that link removed
152157
expect(linkadded).toBe(linkdeleted);
153-
done();
154-
});
158+
})
159+
.catch(failTest)
160+
.then(done);
155161
}
156162

157163

0 commit comments

Comments
 (0)