From 5768f107695509db96da52e8b4468ff892ddcf18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Tue, 28 Apr 2015 10:23:35 +0200 Subject: [PATCH] More optimizations and unit tests for panel repeats #1888 --- .../features/dashboard/dynamicDashboardSrv.js | 51 ++++++++++++------- .../test/specs/dynamicDashboardSrv-specs.js | 10 ++++ 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/public/app/features/dashboard/dynamicDashboardSrv.js b/public/app/features/dashboard/dynamicDashboardSrv.js index 391a8d1bf7b..bea79a81d8c 100644 --- a/public/app/features/dashboard/dynamicDashboardSrv.js +++ b/public/app/features/dashboard/dynamicDashboardSrv.js @@ -64,30 +64,35 @@ function (angular, _) { return sourceRow; } - var i, panel, row; + var i, panel, row, copy; var sourceRowId = _.indexOf(this.dashboard.rows, sourceRow) + 1; // look for row to reuse for (i = 0; i < this.dashboard.rows.length; i++) { row = this.dashboard.rows[i]; if (row.repeatRowId === sourceRowId && row.repeatIteration !== this.iteration) { - row.repeatIteration = this.iteration; - return row; + copy = row; + break; } } - var copy = angular.copy(sourceRow); + if (!copy) { + copy = angular.copy(sourceRow); + this.dashboard.rows.push(copy); + + // set new panel ids + for (i = 0; i < copy.panels.length; i++) { + panel = copy.panels[i]; + panel.id = this.dashboard.getNextPanelId(); + } + + } else { + // update reused instance + } + copy.repeat = null; copy.repeatRowId = sourceRowId; copy.repeatIteration = this.iteration; - this.dashboard.rows.push(copy); - - // set new panel ids - for (i = 0; i < copy.panels.length; i++) { - panel = copy.panels[i]; - panel.id = this.dashboard.getNextPanelId(); - } - return copy; }; @@ -122,16 +127,28 @@ function (angular, _) { return sourcePanel; } + var i, tmpId, panel, clone; + // first try finding an existing clone to use - for (var i = 0; i < row.panels.length; i++) { - var panel = row.panels[i]; + for (i = 0; i < row.panels.length; i++) { + panel = row.panels[i]; if (panel.repeatIteration !== this.iteration && panel.repeatPanelId === sourcePanel.id) { - panel.repeatIteration = this.iteration; - return panel; + clone = panel; + break; } } - var clone = this.dashboard.duplicatePanel(sourcePanel, row); + if (!clone) { + clone = { id: this.dashboard.getNextPanelId() }; + row.panels.push(clone); + } + + // save id + tmpId = clone.id; + // copy properties from source + angular.extend(clone, sourcePanel); + // restore id + clone.id = tmpId; clone.repeatIteration = this.iteration; clone.repeatPanelId = sourcePanel.id; clone.repeat = null; diff --git a/public/test/specs/dynamicDashboardSrv-specs.js b/public/test/specs/dynamicDashboardSrv-specs.js index 62c722cf98e..968a181e288 100644 --- a/public/test/specs/dynamicDashboardSrv-specs.js +++ b/public/test/specs/dynamicDashboardSrv-specs.js @@ -70,6 +70,7 @@ define([ beforeEach(function() { repeatedPanelAfterIteration1 = ctx.rows[0].panels[1]; + ctx.rows[0].panels[0].fill = 10; ctx.dynamicDashboardSrv.update(ctx.dash); }); @@ -77,6 +78,10 @@ define([ expect(ctx.rows[0].panels[1]).to.be(repeatedPanelAfterIteration1); }); + it('reused panel should copy properties from source', function() { + expect(ctx.rows[0].panels[1].fill).to.be(10); + }); + it('should have same panel count', function() { expect(ctx.rows[0].panels.length).to.be(2); }); @@ -144,6 +149,7 @@ define([ beforeEach(function() { repeatedRowAfterFirstIteration = ctx.rows[1]; + ctx.rows[0].height = 500; ctx.dynamicDashboardSrv.update(ctx.dash); }); @@ -151,6 +157,10 @@ define([ expect(ctx.rows.length).to.be(2); }); + it.skip('should have updated props from source', function() { + expect(ctx.rows[1].height).to.be(500); + }); + it('should reuse row instance', function() { expect(ctx.rows[1]).to.be(repeatedRowAfterFirstIteration); });