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

allow use of element ID in Plots.resize #2212

Merged
merged 2 commits into from
Dec 18, 2017
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
3 changes: 3 additions & 0 deletions src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ plots.getSubplotCalcData = function(calcData, type, subplotId) {
// the text is at first, so it needs to draw it,
// then wait a little, then draw it again
plots.redrawText = function(gd) {
gd = Lib.getGraphDiv(gd);

// do not work if polar is present
if((gd.data && gd.data[0] && gd.data[0].r)) return;
Expand All @@ -211,6 +212,8 @@ plots.redrawText = function(gd) {

// resize plot about the container size
plots.resize = function(gd) {
gd = Lib.getGraphDiv(gd);

return new Promise(function(resolve, reject) {

function isHidden(gd) {
Expand Down
22 changes: 20 additions & 2 deletions test/jasmine/tests/plot_promise_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,8 @@ describe('Plotly.___ methods', function() {
Plotly.plot(initialDiv, data, {}).then(done);
});

afterEach(destroyGraphDiv);

it('should return a resolved promise of the gd', function(done) {
Plotly.Plots.resize(initialDiv).then(function(gd) {
expect(gd).toBeDefined();
Expand All @@ -469,14 +471,30 @@ describe('Plotly.___ methods', function() {
}).then(done);
});

it('should return a rejected promise with no argument', function(done) {
Plotly.Plots.resize().then(function() {
it('should return a rejected promise if gd is hidden', function(done) {
initialDiv.style.display = 'none';
Plotly.Plots.resize(initialDiv).then(function() {
expect(1).toBe(0, 'We were supposed to get an error.');
}, function(err) {
expect(err).toBeDefined();
expect(err.message).toBe('Resize must be passed a displayed plot div element.');
}).then(done);
});

it('should return a rejected promise if gd is detached from the DOM', function(done) {
destroyGraphDiv();
Plotly.Plots.resize(initialDiv).then(function() {
expect(1).toBe(0, 'We were supposed to get an error.');
}, function(err) {
expect(err).toBeDefined();
expect(err.message).toBe('Resize must be passed a displayed plot div element.');
}).then(done);
});

it('errors before even generating a promise if gd is not defined', function() {
expect(function() { Plotly.Plots.resize(); })
.toThrow(new Error('DOM element provided is null or undefined'));
});
});

});
19 changes: 18 additions & 1 deletion test/jasmine/tests/plots_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var d3 = require('d3');
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var supplyAllDefaults = require('../assets/supply_defaults');
var failTest = require('../assets/fail_test');


describe('Test Plots', function() {
Expand Down Expand Up @@ -369,7 +370,7 @@ describe('Test Plots', function() {
.then(done);
});

afterEach(destroyGraphDiv);
afterAll(destroyGraphDiv);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oops, if we're doing beforeAll we'd better match it with an afterAll! The second test (should resize...) didn't consider that it might be running against a missing plot (mainSvgs.length was 0) so was in fact a noop. The third test (should update...) I guess was OK, gd was still an object with properties even if it wasn't attached to the DOM...

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice find 🔬


it('should resize the plot clip', function() {
var uid = gd._fullLayout._uid;
Expand All @@ -385,6 +386,7 @@ describe('Test Plots', function() {

it('should resize the main svgs', function() {
var mainSvgs = document.getElementsByClassName('main-svg');
expect(mainSvgs.length).toBe(2);

for(var i = 0; i < mainSvgs.length; i++) {
var svg = mainSvgs[i],
Expand All @@ -397,6 +399,9 @@ describe('Test Plots', function() {
});

it('should update the axis scales', function() {
var mainSvgs = document.getElementsByClassName('main-svg');
expect(mainSvgs.length).toBe(2);

var fullLayout = gd._fullLayout,
plotinfo = fullLayout._plots.xy;

Expand All @@ -406,6 +411,18 @@ describe('Test Plots', function() {
expect(plotinfo.xaxis._length).toEqual(240);
expect(plotinfo.yaxis._length).toEqual(220);
});

it('should allow resizing by plot ID', function(done) {
var mainSvgs = document.getElementsByClassName('main-svg');
expect(mainSvgs.length).toBe(2);

expect(typeof gd.id).toBe('string');
expect(gd.id).toBeTruthy();

Plotly.Plots.resize(gd.id)
.catch(failTest)
.then(done);
});
});

describe('Plots.purge', function() {
Expand Down