Skip to content

Commit e5280d5

Browse files
committed
Make template variables work in row titles, grafana#1956
1 parent 3675b3f commit e5280d5

File tree

5 files changed

+25
-30
lines changed

5 files changed

+25
-30
lines changed

public/app/features/dashboard/dynamicDashboardSrv.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ function (angular, _) {
105105

106106
_.each(selected, function(option, index) {
107107
copy = self.getRowClone(row, index);
108+
copy.scopedVars = {};
109+
copy.scopedVars[variable.name] = option;
108110

109111
for (i = 0; i < copy.panels.length; i++) {
110112
panel = copy.panels[i];

public/app/features/dashboard/rowCtrl.js

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ function (angular, app, _, config) {
2222

2323
$scope.init = function() {
2424
$scope.editor = {index: 0};
25-
$scope.reset_panel();
2625
};
2726

2827
$scope.togglePanelMenu = function(posX) {
@@ -64,8 +63,18 @@ function (angular, app, _, config) {
6463
};
6564

6665
$scope.add_panel_default = function(type) {
67-
$scope.reset_panel(type);
68-
$scope.add_panel($scope.panel);
66+
var defaultSpan = 12;
67+
var _as = 12 - $scope.dashboard.rowSpan($scope.row);
68+
69+
var panel = {
70+
title: config.new_panel_title,
71+
error: false,
72+
span: _as < defaultSpan && _as > 0 ? _as : defaultSpan,
73+
editable: true,
74+
type: type
75+
};
76+
77+
$scope.add_panel(panel);
6978

7079
$timeout(function() {
7180
$scope.$broadcast('render');
@@ -105,31 +114,6 @@ function (angular, app, _, config) {
105114
});
106115
};
107116

108-
$scope.reset_panel = function(type) {
109-
var defaultSpan = 12;
110-
var _as = 12 - $scope.dashboard.rowSpan($scope.row);
111-
112-
$scope.panel = {
113-
title: config.new_panel_title,
114-
error: false,
115-
span: _as < defaultSpan && _as > 0 ? _as : defaultSpan,
116-
editable: true,
117-
type: type
118-
};
119-
120-
function fixRowHeight(height) {
121-
if (!height) {
122-
return '200px';
123-
}
124-
if (!_.isString(height)) {
125-
return height + 'px';
126-
}
127-
return height;
128-
}
129-
130-
$scope.row.height = fixRowHeight($scope.row.height);
131-
};
132-
133117
$scope.init();
134118

135119
});

public/app/filters/all.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ define(['angular', 'jquery', 'lodash', 'moment'], function (angular, $, _, momen
5757

5858
module.filter('interpolateTemplateVars', function(templateSrv) {
5959
function interpolateTemplateVars(text, scope) {
60-
return templateSrv.replaceWithText(text, scope.panel.scopedVars);
60+
if (scope.panel) {
61+
return templateSrv.replaceWithText(text, scope.panel.scopedVars);
62+
} else {
63+
return templateSrv.replaceWithText(text, scope.row.scopedVars);
64+
}
6165
}
6266

6367
interpolateTemplateVars.$stateful = true;

public/app/partials/dashboard.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
</div>
7676

7777
<div class="panels-wrapper" ng-if="!row.collapse">
78-
<div class="row-text pointer" ng-click="toggle_row(row)" ng-if="row.showTitle" ng-bind="row.title">
78+
<div class="row-text pointer" ng-click="toggle_row(row)" ng-if="row.showTitle" ng-bind="row.title | interpolateTemplateVars:this">
7979
</div>
8080

8181
<!-- Panels, draggable needs to be disabled in fullscreen because Firefox bug -->

public/test/specs/dynamicDashboardSrv-specs.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ define([
135135
expect(ctx.rows[1].repeat).to.be(null);
136136
});
137137

138+
it('should add scopedVars to rows', function() {
139+
expect(ctx.rows[0].scopedVars.servers.value).to.be('se1');
140+
expect(ctx.rows[1].scopedVars.servers.value).to.be('se2');
141+
});
142+
138143
it('should generate a repeartRowId based on repeat row index', function() {
139144
expect(ctx.rows[1].repeatRowId).to.be(1);
140145
});

0 commit comments

Comments
 (0)