From 2e21613be73267e861cb1bd402233f63963e93b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Tue, 11 Oct 2016 15:17:26 +0200 Subject: [PATCH] fix(templating): fixed issues with dynamic dashboard srv (panel/row) repeats, fixes #6237 --- .../app/features/dashboard/dashboard_ctrl.ts | 6 ++-- .../dashboard/dynamic_dashboard_srv.ts | 30 ++++++++----------- .../app/features/dashboard/export/exporter.ts | 3 +- .../specs/dynamic_dashboard_srv_specs.ts | 18 ++++++----- 4 files changed, 30 insertions(+), 27 deletions(-) diff --git a/public/app/features/dashboard/dashboard_ctrl.ts b/public/app/features/dashboard/dashboard_ctrl.ts index 4daf8ef6a68..0e80598e758 100644 --- a/public/app/features/dashboard/dashboard_ctrl.ts +++ b/public/app/features/dashboard/dashboard_ctrl.ts @@ -51,7 +51,9 @@ export class DashboardCtrl { .catch($scope.onInitFailed.bind(this, 'Templating init failed', false)) // continue .finally(function() { - dynamicDashboardSrv.init(dashboard); + dynamicDashboardSrv.init(dashboard, variableSrv); + dynamicDashboardSrv.process(); + unsavedChangesSrv.init(dashboard, $scope); $scope.dashboard = dashboard; @@ -87,7 +89,7 @@ export class DashboardCtrl { }; $scope.templateVariableUpdated = function() { - dynamicDashboardSrv.update($scope.dashboard); + dynamicDashboardSrv.process(); }; $scope.updateSubmenuVisibility = function() { diff --git a/public/app/features/dashboard/dynamic_dashboard_srv.ts b/public/app/features/dashboard/dynamic_dashboard_srv.ts index c818aa80c7a..a7189627820 100644 --- a/public/app/features/dashboard/dynamic_dashboard_srv.ts +++ b/public/app/features/dashboard/dynamic_dashboard_srv.ts @@ -9,23 +9,21 @@ import coreModule from 'app/core/core_module'; export class DynamicDashboardSrv { iteration: number; dashboard: any; + variables: any; - init(dashboard) { - if (dashboard.snapshot) { return; } - this.process(dashboard, {}); - } - - update(dashboard) { - if (dashboard.snapshot) { return; } - this.process(dashboard, {}); - } - - process(dashboard, options) { - if (dashboard.templating.list.length === 0) { return; } - + init(dashboard, variableSrv) { this.dashboard = dashboard; + this.variables = variableSrv.variables; + } + + process(options) { + if (this.dashboard.snapshot || this.variables.length === 0) { + return; + } + this.iteration = (this.iteration || new Date().getTime()) + 1; + options = options || {}; var cleanUpOnly = options.cleanUpOnly; var i, j, row, panel; @@ -105,8 +103,7 @@ export class DynamicDashboardSrv { // returns a new row clone or reuses a clone from previous iteration repeatRow(row, rowIndex) { - var variables = this.dashboard.templating.list; - var variable = _.find(variables, {name: row.repeat}); + var variable = _.find(this.variables, {name: row.repeat}); if (!variable) { return; } @@ -166,8 +163,7 @@ export class DynamicDashboardSrv { } repeatPanel(panel, row) { - var variables = this.dashboard.templating.list; - var variable = _.find(variables, {name: panel.repeat}); + var variable = _.find(this.variables, {name: panel.repeat}); if (!variable) { return; } var selected; diff --git a/public/app/features/dashboard/export/exporter.ts b/public/app/features/dashboard/export/exporter.ts index b57f26ebb8b..9f9e326e848 100644 --- a/public/app/features/dashboard/export/exporter.ts +++ b/public/app/features/dashboard/export/exporter.ts @@ -13,7 +13,8 @@ export class DashboardExporter { makeExportable(dash) { var dynSrv = new DynamicDashboardSrv(); - dynSrv.process(dash, {cleanUpOnly: true}); + dynSrv.init(dash, {variables: dash.templating.list}); + dynSrv.process({cleanUpOnly: true}); dash.id = null; diff --git a/public/app/features/dashboard/specs/dynamic_dashboard_srv_specs.ts b/public/app/features/dashboard/specs/dynamic_dashboard_srv_specs.ts index 0173c6569a5..95de65b2459 100644 --- a/public/app/features/dashboard/specs/dynamic_dashboard_srv_specs.ts +++ b/public/app/features/dashboard/specs/dynamic_dashboard_srv_specs.ts @@ -20,6 +20,8 @@ function dynamicDashScenario(desc, func) { beforeEach(angularMocks.inject(function(dashboardSrv) { ctx.dashboardSrv = dashboardSrv; + ctx.variableSrv = {}; + var model = { rows: [], templating: { list: [] } @@ -27,8 +29,10 @@ function dynamicDashScenario(desc, func) { setupFunc(model); ctx.dash = ctx.dashboardSrv.create(model); + ctx.variableSrv.variables = ctx.dash.templating.list; ctx.dynamicDashboardSrv = new DynamicDashboardSrv(); - ctx.dynamicDashboardSrv.init(ctx.dash); + ctx.dynamicDashboardSrv.init(ctx.dash, ctx.variableSrv); + ctx.dynamicDashboardSrv.process(); ctx.rows = ctx.dash.rows; })); }; @@ -78,7 +82,7 @@ dynamicDashScenario('given dashboard with panel repeat', function(ctx) { beforeEach(function() { repeatedPanelAfterIteration1 = ctx.rows[0].panels[1]; ctx.rows[0].panels[0].fill = 10; - ctx.dynamicDashboardSrv.update(ctx.dash); + ctx.dynamicDashboardSrv.process(); }); it('should have reused same panel instances', function() { @@ -102,7 +106,7 @@ dynamicDashScenario('given dashboard with panel repeat', function(ctx) { options: [{text: 'se1', value: 'se1', selected: true}] }); ctx.rows[0].panels[0].repeat = "server"; - ctx.dynamicDashboardSrv.update(ctx.dash); + ctx.dynamicDashboardSrv.process(); }); it('should remove scopedVars value for last variable', function() { @@ -117,7 +121,7 @@ dynamicDashScenario('given dashboard with panel repeat', function(ctx) { describe('After a second iteration and selected values reduced', function() { beforeEach(function() { ctx.dash.templating.list[0].options[1].selected = false; - ctx.dynamicDashboardSrv.update(ctx.dash); + ctx.dynamicDashboardSrv.process(); }); it('should clean up repeated panel', function() { @@ -128,7 +132,7 @@ dynamicDashScenario('given dashboard with panel repeat', function(ctx) { describe('After a second iteration and panel repeat is turned off', function() { beforeEach(function() { ctx.rows[0].panels[0].repeat = null; - ctx.dynamicDashboardSrv.update(ctx.dash); + ctx.dynamicDashboardSrv.process(); }); it('should clean up repeated panel', function() { @@ -199,7 +203,7 @@ dynamicDashScenario('given dashboard with row repeat', function(ctx) { beforeEach(function() { repeatedRowAfterFirstIteration = ctx.rows[1]; ctx.rows[0].height = 500; - ctx.dynamicDashboardSrv.update(ctx.dash); + ctx.dynamicDashboardSrv.process(); }); it('should still only have 2 rows', function() { @@ -218,7 +222,7 @@ dynamicDashScenario('given dashboard with row repeat', function(ctx) { describe('After a second iteration and selected values reduced', function() { beforeEach(function() { ctx.dash.templating.list[0].options[1].selected = false; - ctx.dynamicDashboardSrv.update(ctx.dash); + ctx.dynamicDashboardSrv.process(); }); it('should remove repeated second row', function() {