Commit 4ef79d25 by Torkel Ödegaard

feat(templating): initial work on rethink of value formating

parent 62d703fd
...@@ -136,6 +136,7 @@ ...@@ -136,6 +136,7 @@
<button class="btn btn-inverse btn-large" data-clipboard-text="{{snapshotUrl}}" clipboard-button><i class="fa fa-clipboard"></i> Copy Link</button> <button class="btn btn-inverse btn-large" data-clipboard-text="{{snapshotUrl}}" clipboard-button><i class="fa fa-clipboard"></i> Copy Link</button>
</div> </div>
</div> </div>
</div>
<div ng-if="step === 1" class="gf-form-buttons-row"> <div ng-if="step === 1" class="gf-form-buttons-row">
<button class="btn btn-success btn-large" ng-click="createSnapshot()" ng-disabled="loading"> <button class="btn btn-success btn-large" ng-click="createSnapshot()" ng-disabled="loading">
...@@ -153,5 +154,5 @@ ...@@ -153,5 +154,5 @@
</div> </div>
</div> </div>
</div> </div>
</script> </script>
...@@ -24,22 +24,18 @@ function (angular, _) { ...@@ -24,22 +24,18 @@ function (angular, _) {
this.updateTemplateData = function() { this.updateTemplateData = function() {
this._values = {}; this._values = {};
this._texts = {};
_.each(this.variables, function(variable) { _.each(this.variables, function(variable) {
if (!variable.current || !variable.current.isNone && !variable.current.value) { return; } if (!variable.current || !variable.current.isNone && !variable.current.value) { return; }
this._values[variable.name] = variable.current.value;
this._values[variable.name] = this.renderVariableValue(variable);
this._texts[variable.name] = variable.current.text;
}, this); }, this);
}; };
this.renderVariableValue = function(variable) { this.formatValue = function(value, format) {
var value = variable.current.value;
if (_.isString(value)) { if (_.isString(value)) {
return value; return value;
} else { } else {
switch(variable.multiFormat) { switch(format) {
case "regex values": { case "regex values": {
return '(' + value.join('|') + ')'; return '(' + value.join('|') + ')';
} }
...@@ -89,22 +85,31 @@ function (angular, _) { ...@@ -89,22 +85,31 @@ function (angular, _) {
}); });
}; };
this.replace = function(target, scopedVars) { this.replace = function(target, scopedVars, format) {
if (!target) { return target; } if (!target) { return target; }
var value; var value, systemValue;
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) { if (scopedVars) {
value = scopedVars[g1 || g2]; value = scopedVars[g1 || g2];
if (value) { return value.value; } if (value) {
return self.formatValue(value.value);
}
} }
value = self._values[g1 || g2]; value = self._values[g1 || g2];
if (!value) { return match; } if (!value) {
return match;
}
systemValue = self._grafanaVariables[value];
if (systemValue) {
return self.formatValue(systemValue);
}
return self._grafanaVariables[value] || value; return self.formatValue(value, format);
}); });
}; };
......
...@@ -45,6 +45,22 @@ define([ ...@@ -45,6 +45,22 @@ define([
}); });
}); });
describe.only('replace can pass multi / all format', function() {
beforeEach(function() {
_templateSrv.init([{name: 'test', current: {value: ['value1', 'value2'] }}]);
});
it('should replace $test with globbed value', function() {
var target = _templateSrv.replace('this.$test.filters', {}, 'glob');
expect(target).to.be('this.{value1,value2}.filters');
});
it('should replace $test with piped value', function() {
var target = _templateSrv.replace('this=$test', {}, 'pipe');
expect(target).to.be('this=value1|value2');
});
});
describe('render variable to string values', function() { describe('render variable to string values', function() {
it('single value should return value', function() { it('single value should return value', function() {
var result = _templateSrv.renderVariableValue({current: {value: 'test'}}); var result = _templateSrv.renderVariableValue({current: {value: 'test'}});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment