Skip to content

Commit 7acca0a

Browse files
Merge pull request #2607 from plotly/download_config
Override toImage modebar button opts via config
2 parents 398eeb3 + e7ef7ae commit 7acca0a

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

src/components/modebar/buttons.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,32 @@ var modeBarButtons = module.exports = {};
4747

4848
modeBarButtons.toImage = {
4949
name: 'toImage',
50-
title: function(gd) { return _(gd, 'Download plot as a png'); },
50+
title: function(gd) {
51+
var opts = gd._context.toImageButtonOptions || {};
52+
var format = opts.format || 'png';
53+
return format === 'png' ?
54+
_(gd, 'Download plot as a png') : // legacy text
55+
_(gd, 'Download plot'); // generic non-PNG text
56+
},
5157
icon: Icons.camera,
5258
click: function(gd) {
53-
var format = 'png';
59+
var toImageButtonOptions = gd._context.toImageButtonOptions;
60+
var opts = {format: toImageButtonOptions.format || 'png'};
5461

5562
Lib.notifier(_(gd, 'Taking snapshot - this may take a few seconds'), 'long');
5663

57-
if(Lib.isIE()) {
64+
if(opts.format !== 'svg' && Lib.isIE()) {
5865
Lib.notifier(_(gd, 'IE only supports svg. Changing format to svg.'), 'long');
59-
format = 'svg';
66+
opts.format = 'svg';
6067
}
6168

62-
Registry.call('downloadImage', gd, {'format': format})
69+
['filename', 'width', 'height', 'scale'].forEach(function(key) {
70+
if(toImageButtonOptions[key]) {
71+
opts[key] = toImageButtonOptions[key];
72+
}
73+
});
74+
75+
Registry.call('downloadImage', gd, opts)
6376
.then(function(filename) {
6477
Lib.notifier(_(gd, 'Snapshot succeeded') + ' - ' + filename, 'long');
6578
})

src/plot_api/plot_config.js

+5
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ module.exports = {
115115
*/
116116
modeBarButtons: false,
117117

118+
// statically override options for toImage modebar button
119+
// allowed keys are format, filename, width, height, scale
120+
// see ./components/modebar/buttons.js
121+
toImageButtonOptions: {},
122+
118123
// add the plotly logo on the end of the mode bar
119124
displaylogo: true,
120125

test/jasmine/tests/modebar_test.js

+37
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var manageModeBar = require('@src/components/modebar/manage');
55

66
var Plotly = require('@lib/index');
77
var Plots = require('@src/plots/plots');
8+
var Registry = require('@src/registry');
89
var createGraphDiv = require('../assets/create_graph_div');
910
var destroyGraphDiv = require('../assets/destroy_graph_div');
1011
var selectButton = require('../assets/modebar_button');
@@ -767,6 +768,42 @@ describe('ModeBar', function() {
767768
}
768769
}
769770

771+
describe('toImage handlers', function() {
772+
beforeEach(function() {
773+
spyOn(Registry, 'call').and.callFake(function() {
774+
return Promise.resolve();
775+
});
776+
gd = createGraphDiv();
777+
});
778+
779+
it('should use defaults', function(done) {
780+
Plotly.plot(gd, {data: [], layout: {}})
781+
.then(function() {
782+
selectButton(gd._fullLayout._modeBar, 'toImage').click();
783+
expect(Registry.call).toHaveBeenCalledWith('downloadImage', gd,
784+
{format: 'png'});
785+
done();
786+
});
787+
});
788+
789+
it('should accept overriding defaults', function(done) {
790+
Plotly.plot(gd, {data: [], layout: {}, config: {
791+
toImageButtonOptions: {
792+
format: 'svg',
793+
filename: 'x',
794+
unsupported: 'should not pass'
795+
}
796+
} })
797+
.then(function() {
798+
selectButton(gd._fullLayout._modeBar, 'toImage').click();
799+
expect(Registry.call).toHaveBeenCalledWith('downloadImage', gd,
800+
{format: 'svg', filename: 'x'});
801+
done();
802+
});
803+
});
804+
805+
});
806+
770807
describe('cartesian handlers', function() {
771808

772809
beforeEach(function(done) {

0 commit comments

Comments
 (0)