work on scoped variable values

This commit is contained in:
Torkel Ödegaard
2015-03-17 13:33:58 -04:00
parent 5de499c7f6
commit 9f729900f2
7 changed files with 30 additions and 18 deletions

View File

@@ -33,15 +33,16 @@ function (angular, _) {
return; return;
} }
dashboard.scopedVars = { _.each(variable.options, function(option, index) {
panel: {} if (index > 0) {
}; var copy = dashboard.duplicatePanel(panel, row);
copy.repeat = null;
_.each(variable.options, function(option) { copy.scopedVars = {};
var copy = dashboard.duplicatePanel(panel, row); copy.scopedVars[variable.name] = option;
copy.repeat = null; } else {
dashboard.scopedVars.panel[panel.id] = {}; panel.scopedVars = {};
dashboard.scopedVars.panel[panel.id][variable.name] = option.value; panel.scopedVars[variable.name] = option;
}
console.log('duplicatePanel'); console.log('duplicatePanel');
}); });
}; };

View File

@@ -68,6 +68,7 @@ function (angular, _, kbn, $) {
targets: scope.panel.targets, targets: scope.panel.targets,
format: scope.panel.renderer === 'png' ? 'png' : 'json', format: scope.panel.renderer === 'png' ? 'png' : 'json',
maxDataPoints: scope.resolution, maxDataPoints: scope.resolution,
scopedVars: scope.panel.scopedVars,
cacheTimeout: scope.panel.cacheTimeout cacheTimeout: scope.panel.cacheTimeout
}; };

View File

@@ -11,7 +11,7 @@ function (angular, $, _) {
.directive('panelMenu', function($compile, linkSrv) { .directive('panelMenu', function($compile, linkSrv) {
var linkTemplate = var linkTemplate =
'<span class="panel-title drag-handle pointer">' + '<span class="panel-title drag-handle pointer">' +
'<span class="panel-title-text drag-handle">{{panel.title | interpolateTemplateVars}}</span>' + '<span class="panel-title-text drag-handle">{{panel.title | interpolateTemplateVars:this}}</span>' +
'<span class="panel-links-icon"></span>' + '<span class="panel-links-icon"></span>' +
'<span class="panel-time-info" ng-show="panelMeta.timeInfo"><i class="fa fa-clock-o"></i> {{panelMeta.timeInfo}}</span>' + '<span class="panel-time-info" ng-show="panelMeta.timeInfo"><i class="fa fa-clock-o"></i> {{panelMeta.timeInfo}}</span>' +
'</span>'; '</span>';

View File

@@ -72,7 +72,7 @@ function (angular, _) {
return target.replace(this._regex, function(match, g1, g2) { return target.replace(this._regex, function(match, g1, g2) {
if (scopedVars) { if (scopedVars) {
value = scopedVars[g1 || g2]; value = scopedVars[g1 || g2];
if (value) { return value; } if (value) { return value.value; }
} }
value = self._values[g1 || g2]; value = self._values[g1 || g2];
@@ -82,7 +82,7 @@ function (angular, _) {
}); });
}; };
this.replaceWithText = function(target) { this.replaceWithText = function(target, scopedVars) {
if (!target) { return; } if (!target) { return; }
var value; var value;
@@ -90,6 +90,11 @@ function (angular, _) {
this._regex.lastIndex = 0; this._regex.lastIndex = 0;
return target.replace(this._regex, function(match, g1, g2) { return target.replace(this._regex, function(match, g1, g2) {
if (scopedVars) {
var option = scopedVars[g1 || g2];
if (option) { return option.text; }
}
value = self._values[g1 || g2]; value = self._values[g1 || g2];
text = self._texts[g1 || g2]; text = self._texts[g1 || g2];
if (!value) { return match; } if (!value) { return match; }

View File

@@ -56,8 +56,8 @@ define(['angular', 'jquery', 'lodash', 'moment'], function (angular, $, _, momen
}); });
module.filter('interpolateTemplateVars', function(templateSrv) { module.filter('interpolateTemplateVars', function(templateSrv) {
function interpolateTemplateVars(text) { function interpolateTemplateVars(text, scope) {
return templateSrv.replaceWithText(text); return templateSrv.replaceWithText(text, scope.panel.scopedVars);
} }
interpolateTemplateVars.$stateful = true; interpolateTemplateVars.$stateful = true;

View File

@@ -36,7 +36,7 @@ function (angular, _, $, config, kbn, moment) {
maxDataPoints: options.maxDataPoints, maxDataPoints: options.maxDataPoints,
}; };
var params = this.buildGraphiteParams(graphOptions, options.panelId); var params = this.buildGraphiteParams(graphOptions, options.scopedVars);
if (options.format === 'png') { if (options.format === 'png') {
return $q.when(this.url + '/render' + '?' + params.join('&')); return $q.when(this.url + '/render' + '?' + params.join('&'));
@@ -231,7 +231,7 @@ function (angular, _, $, config, kbn, moment) {
'#Y', '#Z' '#Y', '#Z'
]; ];
GraphiteDatasource.prototype.buildGraphiteParams = function(options, panelId) { GraphiteDatasource.prototype.buildGraphiteParams = function(options, scopedVars) {
var graphite_options = ['from', 'until', 'rawData', 'format', 'maxDataPoints', 'cacheTimeout']; var graphite_options = ['from', 'until', 'rawData', 'format', 'maxDataPoints', 'cacheTimeout'];
var clean_options = [], targets = {}; var clean_options = [], targets = {};
var target, targetValue, i; var target, targetValue, i;
@@ -252,7 +252,7 @@ function (angular, _, $, config, kbn, moment) {
continue; continue;
} }
targetValue = templateSrv.replace(target.target, panelId); targetValue = templateSrv.replace(target.target, scopedVars);
targetValue = targetValue.replace(intervalFormatFixRegex, fixIntervalFormat); targetValue = targetValue.replace(intervalFormatFixRegex, fixIntervalFormat);
targets[this._seriesRefLetters[i]] = targetValue; targets[this._seriesRefLetters[i]] = targetValue;
} }

View File

@@ -35,9 +35,14 @@ define([
}); });
it('should replace $test with scoped value', function() { it('should replace $test with scoped value', function() {
var target = _templateSrv.replace('this.$test.filters', {'test': 'mupp'}); var target = _templateSrv.replace('this.$test.filters', {'test': {value: 'mupp', text: 'asd'}});
expect(target).to.be('this.mupp.filters'); expect(target).to.be('this.mupp.filters');
}); });
it('should replace $test with scoped text', function() {
var target = _templateSrv.replaceWithText('this.$test.filters', {'test': {value: 'mupp', text: 'asd'}});
expect(target).to.be('this.asd.filters');
});
}); });