Просмотр исходного кода

More optimizations and unit tests for panel repeats #1888

Torkel Ödegaard 10 лет назад
Родитель
Сommit
5768f10769

+ 34 - 17
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);
-      copy.repeat = null;
-      copy.repeatRowId = sourceRowId;
-      copy.repeatIteration = this.iteration;
-      this.dashboard.rows.push(copy);
+      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();
+        }
 
-      // 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;
       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;

+ 10 - 0
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);
       });