Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Plotly.download using graph id as 1st argument #2931

Merged
merged 4 commits into from
Aug 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/snapshot/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,26 @@ var toImage = require('../plot_api/to_image');
var Lib = require('../lib'); // for isIE
var fileSaver = require('./filesaver');

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

// check for undefined opts
opts = opts || {};

// default to png
opts.format = opts.format || 'png';

return new Promise(function(resolve, reject) {
if(gd._snapshotInProgress) {
if(_gd && _gd._snapshotInProgress) {
reject(new Error('Snapshotting already in progress.'));
}

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

gd._snapshotInProgress = true;
if(_gd) _gd._snapshotInProgress = true;
var promise = toImage(gd, opts);

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

promise.then(function(result) {
gd._snapshotInProgress = false;
if(_gd) _gd._snapshotInProgress = false;
return fileSaver(result, filename);
}).then(function(name) {
resolve(name);
}).catch(function(err) {
gd._snapshotInProgress = false;
if(_gd) _gd._snapshotInProgress = false;
reject(err);
});
});
Expand Down
40 changes: 34 additions & 6 deletions test/jasmine/tests/download_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ describe('Plotly.downloadImage', function() {
downloadTest(gd, 'svg', done);
}, LONG_TIMEOUT_INTERVAL);

it('should work when passing graph div id', function(done) {
downloadTest('graph', 'svg', done);
}, LONG_TIMEOUT_INTERVAL);

it('should work when passing a figure object', function(done) {
var fig = {
data: [{y: [1, 2, 1]}]
};
Plotly.downloadImage(fig)
.then(function() {
expect(document.createElement).toHaveBeenCalledWith('canvas');
expect(gd._snapshotInProgress)
.toBe(undefined, 'should not attach _snapshotInProgress to figure objects');
})
.catch(failTest)
.then(done);
}, LONG_TIMEOUT_INTERVAL);

it('should produce the right SVG output in IE', function(done) {
// mock up IE behavior
spyOn(Lib, 'isIE').and.callFake(function() { return true; });
Expand Down Expand Up @@ -127,16 +145,26 @@ function downloadTest(gd, format, done) {
});
});

Plotly.plot(gd, textchartMock.data, textchartMock.layout).then(function(gd) {
Plotly.plot(gd, textchartMock.data, textchartMock.layout).then(function(_gd) {
// start observing dom
// configuration of the observer:
var config = { childList: true };

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

return Plotly.downloadImage(gd, {format: format, height: 300, width: 300, filename: 'plotly_download'});
}).then(function(filename) {
var promise = Plotly.downloadImage(gd, {
format: format,
height: 300,
width: 300,
filename: 'plotly_download'
});

expect(_gd._snapshotInProgress).toBe(true, 'should attach _snapshotInProgress to graph divs');

return promise;
})
.then(function(filename) {
// stop observing
observer.disconnect();
// look for an added and removed link
Expand All @@ -150,11 +178,11 @@ function downloadTest(gd, format, done) {

// check that link removed
expect(linkadded).toBe(linkdeleted);
done();
});
})
.catch(failTest)
.then(done);
}


// Only chrome supports webp at the time of writing
function checkWebp(cb) {
var img = new Image();
Expand Down