From 481fcfff8f5e43acb24459f2265b0255c3f8c6fa Mon Sep 17 00:00:00 2001 From: ppisljar Date: Thu, 22 Sep 2016 09:04:23 +0200 Subject: [PATCH] Backport PR #8371 --------- **Commit 1:** fixing charts to not render twice * Original sha: f7f0f12439896fe9f9ee284c5d5999497974644a * Authored by ppisljar on 2016-09-20T08:55:55Z **Commit 2:** fixing tests * Original sha: 40234e8e41db709e99cb861b291ec49bf7abd413 * Authored by ppisljar on 2016-09-21T07:21:01Z **Commit 3:** fixing based on Spencers comments * Original sha: cdad7724bcaa8dcc25665000976d643fcabc839c * Authored by ppisljar on 2016-09-21T19:41:57Z (cherry picked from commit 61d487c) --- src/ui/public/vislib/lib/resize_checker.js | 4 ++++ src/ui/public/vislib/vis.js | 10 +++++++++- .../vislib_vis_type/__tests__/_vislib_renderbot.js | 9 +++++---- src/ui/public/vislib_vis_type/vislib_renderbot.js | 7 ++++++- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/ui/public/vislib/lib/resize_checker.js b/src/ui/public/vislib/lib/resize_checker.js index fcd7e0d78948b..4462630f69a0d 100644 --- a/src/ui/public/vislib/lib/resize_checker.js +++ b/src/ui/public/vislib/lib/resize_checker.js @@ -184,6 +184,10 @@ export default function ResizeCheckerFactory(Private, Notifier, $rootScope) { }, ms)); }; + ResizeChecker.prototype.stopSchedule = function () { + clearTimeout(this._timerId); + }; + /** * Signal that the ResizeChecker should shutdown. * diff --git a/src/ui/public/vislib/vis.js b/src/ui/public/vislib/vis.js index 27569fa04c60e..0c45ac54baaa7 100644 --- a/src/ui/public/vislib/vis.js +++ b/src/ui/public/vislib/vis.js @@ -68,7 +68,7 @@ export default function VisFactory(Private) { } this.handler = handlerTypes[chartType](this) || handlerTypes.column(this); - this._runOnHandler('render'); + this._runWithoutResizeChecker('render'); }; /** @@ -89,6 +89,14 @@ export default function VisFactory(Private) { } }; + Vis.prototype._runWithoutResizeChecker = function (method) { + this.resizeChecker.stopSchedule(); + this._runOnHandler(method); + this.resizeChecker.saveSize(); + this.resizeChecker.saveDirty(false); + this.resizeChecker.continueSchedule(); + }; + Vis.prototype._runOnHandler = function (method) { try { this.handler[method](); diff --git a/src/ui/public/vislib_vis_type/__tests__/_vislib_renderbot.js b/src/ui/public/vislib_vis_type/__tests__/_vislib_renderbot.js index b70e61001a9e4..a07ea0b87a8dd 100644 --- a/src/ui/public/vislib_vis_type/__tests__/_vislib_renderbot.js +++ b/src/ui/public/vislib_vis_type/__tests__/_vislib_renderbot.js @@ -134,10 +134,11 @@ describe('renderbot', function exportWrapper() { let buildStub = sinon.stub(renderbot, 'buildChartData', _.constant(football)); let renderStub = sinon.stub(renderbot.vislibVis, 'render'); - renderbot.render('flat data', persistedState); - expect(renderStub.callCount).to.be(1); - expect(buildStub.callCount).to.be(1); - expect(renderStub.firstCall.args[0]).to.be(football); + return renderbot.render('flat data', persistedState).then(() => { + expect(renderStub.callCount).to.be(1); + expect(buildStub.callCount).to.be(1); + expect(renderStub.firstCall.args[0]).to.be(football); + }); }); }); diff --git a/src/ui/public/vislib_vis_type/vislib_renderbot.js b/src/ui/public/vislib_vis_type/vislib_renderbot.js index 1cbbf1a424c21..8ef87c0ed7f1b 100644 --- a/src/ui/public/vislib_vis_type/vislib_renderbot.js +++ b/src/ui/public/vislib_vis_type/vislib_renderbot.js @@ -46,7 +46,12 @@ module.exports = function VislibRenderbotFactory(Private) { VislibRenderbot.prototype.buildChartData = buildChartData; VislibRenderbot.prototype.render = function (esResponse) { this.chartData = this.buildChartData(esResponse); - this.vislibVis.render(this.chartData, this.uiState); + // to allow legend to render first (wait for angular digest cycle to complete) + return new Promise((resolve, reject) => { + setTimeout(() => { + resolve(this.vislibVis.render(this.chartData, this.uiState)); + }); + }); }; VislibRenderbot.prototype.destroy = function () {