Skip to content

Override toImage modebar button opts via config #2607

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

Merged
merged 3 commits into from
May 3, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 11 additions & 4 deletions src/components/modebar/buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,26 @@ var modeBarButtons = module.exports = {};

modeBarButtons.toImage = {
name: 'toImage',
title: function(gd) { return _(gd, 'Download plot as a png'); },
title: function(gd) { return _(gd, 'Download plot'); },
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't make dynamic strings here, so _(gd, 'Download plot as a ' + format) won't work, but perhaps we can keep 'Download plot as a png' as the default string and only degrade to 'Download plot' if it's not a png? Two reasons:

  • I don't want to unnecessarily break all our existing translations, for folks who are not using this new option.
  • I do find "as a png" helpful, to clarify that it's not the data or json for example, though I guess the icon mitigates that substantially.

icon: Icons.camera,
click: function(gd) {
var format = 'png';
var toImageButtonDefaults = gd._context.toImageButtonDefaults;
var opts = {format: toImageButtonDefaults.format || 'png'};

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

if(Lib.isIE()) {
Lib.notifier(_(gd, 'IE only supports svg. Changing format to svg.'), 'long');
format = 'svg';
opts.format = 'svg';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also we should change the conditional to if(Lib.isIE() && opts.format !== 'svg')

}

Registry.call('downloadImage', gd, {'format': format})
['filename', 'width', 'height', 'scale'].forEach(function(key) {
if(toImageButtonDefaults[key]) {
opts[key] = toImageButtonDefaults[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
toImageButtonDefaults: {},

// 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: {
toImageButtonDefaults: {
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