From bf78539a354e5ba78fab183960365ad71c0c4738 Mon Sep 17 00:00:00 2001 From: Antoine Roy-Gobeil <antoine@plot.ly> Date: Tue, 18 Jun 2019 13:11:20 -0400 Subject: [PATCH 1/3] responsive handler: do not resize if gd is hidden --- src/plot_api/plot_api.js | 6 +++++- test/jasmine/tests/config_test.js | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/plot_api/plot_api.js b/src/plot_api/plot_api.js index 33a1a000cb7..567973e4e8e 100644 --- a/src/plot_api/plot_api.js +++ b/src/plot_api/plot_api.js @@ -178,11 +178,15 @@ function plot(gd, data, layout, config) { gd.calcdata[i][0].trace = gd._fullData[i]; } + function isHidden(gd) { + var display = window.getComputedStyle(gd).display; + return !display || display === 'none'; + } // make the figure responsive if(gd._context.responsive) { if(!gd._responsiveChartHandler) { // Keep a reference to the resize handler to purge it down the road - gd._responsiveChartHandler = function() { Plots.resize(gd); }; + gd._responsiveChartHandler = function() { if(!isHidden(gd)) Plots.resize(gd).catch(Lib.noop); }; // Listen to window resize window.addEventListener('resize', gd._responsiveChartHandler); diff --git a/test/jasmine/tests/config_test.js b/test/jasmine/tests/config_test.js index 004d77c70b5..25084472885 100644 --- a/test/jasmine/tests/config_test.js +++ b/test/jasmine/tests/config_test.js @@ -771,6 +771,23 @@ describe('config argument', function() { .catch(failTest) .then(done); }); + + it('should not resize if gd is hidden', function(done) { + spyOn(Plotly.Plots, 'resize').and.callThrough(); + + fillParent(1, 1); + Plotly.plot(gd, data, {}, {responsive: true}) + .then(function() { + gd.style.display = 'none'; + viewport.set(width / 2, height / 2); + }) + .then(delay(RESIZE_DELAY)) + .then(function() { + expect(Plotly.Plots.resize.calls.count()).toBe(0); + }) + .catch(failTest) + .then(done); + }); }); }); From aed5e44215a13d06436fb2119a0306fccc88c4b8 Mon Sep 17 00:00:00 2001 From: Antoine Roy-Gobeil <antoine@plot.ly> Date: Tue, 18 Jun 2019 13:21:34 -0400 Subject: [PATCH 2/3] increment by 1 the number of known getComputedStyle calls --- tasks/test_syntax.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks/test_syntax.js b/tasks/test_syntax.js index e3018d5460b..fe9d9b62b57 100644 --- a/tasks/test_syntax.js +++ b/tasks/test_syntax.js @@ -210,7 +210,7 @@ function assertSrcContents() { * - If you use conforms to these rules, you may update * KNOWN_GET_COMPUTED_STYLE_CALLS to count the new use. */ - var KNOWN_GET_COMPUTED_STYLE_CALLS = 5; + var KNOWN_GET_COMPUTED_STYLE_CALLS = 6; if(getComputedStyleCnt !== KNOWN_GET_COMPUTED_STYLE_CALLS) { logs.push('Expected ' + KNOWN_GET_COMPUTED_STYLE_CALLS + ' window.getComputedStyle calls, found ' + getComputedStyleCnt + From 913bb6877df1e7668748ad4b3e535e38bbf25e6b Mon Sep 17 00:00:00 2001 From: Antoine Roy-Gobeil <antoine@plot.ly> Date: Tue, 18 Jun 2019 14:03:31 -0400 Subject: [PATCH 3/3] move isHidden into Lib --- src/lib/index.js | 5 +++++ src/plot_api/plot_api.js | 6 +----- src/plots/plots.js | 9 ++------- tasks/test_syntax.js | 2 +- 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/lib/index.js b/src/lib/index.js index 850b8aa88e6..639360bb294 100644 --- a/src/lib/index.js +++ b/src/lib/index.js @@ -1171,3 +1171,8 @@ lib.formatPercent = function(ratio, n) { } return str; }; + +lib.isHidden = function(gd) { + var display = window.getComputedStyle(gd).display; + return !display || display === 'none'; +}; diff --git a/src/plot_api/plot_api.js b/src/plot_api/plot_api.js index 567973e4e8e..3dd530abf6d 100644 --- a/src/plot_api/plot_api.js +++ b/src/plot_api/plot_api.js @@ -178,15 +178,11 @@ function plot(gd, data, layout, config) { gd.calcdata[i][0].trace = gd._fullData[i]; } - function isHidden(gd) { - var display = window.getComputedStyle(gd).display; - return !display || display === 'none'; - } // make the figure responsive if(gd._context.responsive) { if(!gd._responsiveChartHandler) { // Keep a reference to the resize handler to purge it down the road - gd._responsiveChartHandler = function() { if(!isHidden(gd)) Plots.resize(gd).catch(Lib.noop); }; + gd._responsiveChartHandler = function() { if(!Lib.isHidden(gd)) Plots.resize(gd); }; // Listen to window resize window.addEventListener('resize', gd._responsiveChartHandler); diff --git a/src/plots/plots.js b/src/plots/plots.js index 541c3333826..2bbd3727724 100644 --- a/src/plots/plots.js +++ b/src/plots/plots.js @@ -75,12 +75,7 @@ plots.resize = function(gd) { gd = Lib.getGraphDiv(gd); return new Promise(function(resolve, reject) { - function isHidden(gd) { - var display = window.getComputedStyle(gd).display; - return !display || display === 'none'; - } - - if(!gd || isHidden(gd)) { + if(!gd || Lib.isHidden(gd)) { reject(new Error('Resize must be passed a displayed plot div element.')); } @@ -88,7 +83,7 @@ plots.resize = function(gd) { gd._redrawTimer = setTimeout(function() { // return if there is nothing to resize or is hidden - if(!gd.layout || (gd.layout.width && gd.layout.height) || isHidden(gd)) { + if(!gd.layout || (gd.layout.width && gd.layout.height) || Lib.isHidden(gd)) { resolve(gd); return; } diff --git a/tasks/test_syntax.js b/tasks/test_syntax.js index fe9d9b62b57..e3018d5460b 100644 --- a/tasks/test_syntax.js +++ b/tasks/test_syntax.js @@ -210,7 +210,7 @@ function assertSrcContents() { * - If you use conforms to these rules, you may update * KNOWN_GET_COMPUTED_STYLE_CALLS to count the new use. */ - var KNOWN_GET_COMPUTED_STYLE_CALLS = 6; + var KNOWN_GET_COMPUTED_STYLE_CALLS = 5; if(getComputedStyleCnt !== KNOWN_GET_COMPUTED_STYLE_CALLS) { logs.push('Expected ' + KNOWN_GET_COMPUTED_STYLE_CALLS + ' window.getComputedStyle calls, found ' + getComputedStyleCnt +