Skip to content

Commit b5f8b23

Browse files
authored
Merge pull request #2931 from plotly/fix-download-from-graph-id
Fix Plotly.download using graph id as 1st argument
2 parents 3c1adeb + a5af83c commit b5f8b23

File tree

2 files changed

+48
-18
lines changed

2 files changed

+48
-18
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+
if(_gd) _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+
if(_gd) _gd._snapshotInProgress = false;
5456
return fileSaver(result, filename);
5557
}).then(function(name) {
5658
resolve(name);
5759
}).catch(function(err) {
58-
gd._snapshotInProgress = false;
60+
if(_gd) _gd._snapshotInProgress = false;
5961
reject(err);
6062
});
6163
});

test/jasmine/tests/download_test.js

+34-6
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,24 @@ 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+
68+
it('should work when passing a figure object', function(done) {
69+
var fig = {
70+
data: [{y: [1, 2, 1]}]
71+
};
72+
Plotly.downloadImage(fig)
73+
.then(function() {
74+
expect(document.createElement).toHaveBeenCalledWith('canvas');
75+
expect(gd._snapshotInProgress)
76+
.toBe(undefined, 'should not attach _snapshotInProgress to figure objects');
77+
})
78+
.catch(failTest)
79+
.then(done);
80+
}, LONG_TIMEOUT_INTERVAL);
81+
6482
it('should produce the right SVG output in IE', function(done) {
6583
// mock up IE behavior
6684
spyOn(Lib, 'isIE').and.callFake(function() { return true; });
@@ -127,16 +145,26 @@ function downloadTest(gd, format, done) {
127145
});
128146
});
129147

130-
Plotly.plot(gd, textchartMock.data, textchartMock.layout).then(function(gd) {
148+
Plotly.plot(gd, textchartMock.data, textchartMock.layout).then(function(_gd) {
131149
// start observing dom
132150
// configuration of the observer:
133151
var config = { childList: true };
134152

135153
// pass in the target node and observer options
136154
observer.observe(target, config);
137155

138-
return Plotly.downloadImage(gd, {format: format, height: 300, width: 300, filename: 'plotly_download'});
139-
}).then(function(filename) {
156+
var promise = Plotly.downloadImage(gd, {
157+
format: format,
158+
height: 300,
159+
width: 300,
160+
filename: 'plotly_download'
161+
});
162+
163+
expect(_gd._snapshotInProgress).toBe(true, 'should attach _snapshotInProgress to graph divs');
164+
165+
return promise;
166+
})
167+
.then(function(filename) {
140168
// stop observing
141169
observer.disconnect();
142170
// look for an added and removed link
@@ -150,11 +178,11 @@ function downloadTest(gd, format, done) {
150178

151179
// check that link removed
152180
expect(linkadded).toBe(linkdeleted);
153-
done();
154-
});
181+
})
182+
.catch(failTest)
183+
.then(done);
155184
}
156185

157-
158186
// Only chrome supports webp at the time of writing
159187
function checkWebp(cb) {
160188
var img = new Image();

0 commit comments

Comments
 (0)