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

Override toImage modebar button opts via config #2607

Merged
merged 3 commits into from
May 3, 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
23 changes: 18 additions & 5 deletions src/components/modebar/buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,32 @@ var modeBarButtons = module.exports = {};

modeBarButtons.toImage = {
name: 'toImage',
title: function(gd) { return _(gd, 'Download plot as a png'); },
title: function(gd) {
var opts = gd._context.toImageButtonOptions || {};
var format = opts.format || 'png';
return format === 'png' ?
_(gd, 'Download plot as a png') : // legacy text
_(gd, 'Download plot'); // generic non-PNG text
},
icon: Icons.camera,
click: function(gd) {
var format = 'png';
var toImageButtonOptions = gd._context.toImageButtonOptions;
var opts = {format: toImageButtonOptions.format || 'png'};

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

if(Lib.isIE()) {
if(opts.format !== 'svg' && Lib.isIE()) {
Lib.notifier(_(gd, 'IE only supports svg. Changing format to svg.'), 'long');
format = 'svg';
opts.format = 'svg';
}

Registry.call('downloadImage', gd, {'format': format})
['filename', 'width', 'height', 'scale'].forEach(function(key) {
if(toImageButtonOptions[key]) {
opts[key] = toImageButtonOptions[key];
}
});

Registry.call('downloadImage', gd, opts)
.then(function(filename) {
Lib.notifier(_(gd, 'Snapshot succeeded') + ' - ' + filename, 'long');
})
Expand Down
5 changes: 5 additions & 0 deletions src/plot_api/plot_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ module.exports = {
*/
modeBarButtons: false,

// statically override options for toImage modebar button
// allowed keys are format, filename, width, height, scale
// see ./components/modebar/buttons.js
toImageButtonOptions: {},

// add the plotly logo on the end of the mode bar
displaylogo: true,

Expand Down
37 changes: 37 additions & 0 deletions test/jasmine/tests/modebar_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var manageModeBar = require('@src/components/modebar/manage');

var Plotly = require('@lib/index');
var Plots = require('@src/plots/plots');
var Registry = require('@src/registry');
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var selectButton = require('../assets/modebar_button');
Expand Down Expand Up @@ -767,6 +768,42 @@ describe('ModeBar', function() {
}
}

describe('toImage handlers', function() {
beforeEach(function() {
spyOn(Registry, 'call').and.callFake(function() {
return Promise.resolve();
});
gd = createGraphDiv();
});

it('should use defaults', function(done) {
Plotly.plot(gd, {data: [], layout: {}})
.then(function() {
selectButton(gd._fullLayout._modeBar, 'toImage').click();
expect(Registry.call).toHaveBeenCalledWith('downloadImage', gd,
{format: 'png'});
done();
});
});

it('should accept overriding defaults', function(done) {
Plotly.plot(gd, {data: [], layout: {}, config: {
toImageButtonOptions: {
format: 'svg',
filename: 'x',
unsupported: 'should not pass'
}
} })
.then(function() {
selectButton(gd._fullLayout._modeBar, 'toImage').click();
expect(Registry.call).toHaveBeenCalledWith('downloadImage', gd,
{format: 'svg', filename: 'x'});
done();
});
});

});

describe('cartesian handlers', function() {

beforeEach(function(done) {
Expand Down