mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(templating): initial work on rethink of value formating
This commit is contained in:
parent
62d703fdf7
commit
4ef79d250d
@ -136,22 +136,23 @@
|
||||
<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 ng-if="step === 1" class="gf-form-buttons-row">
|
||||
<button class="btn btn-success btn-large" ng-click="createSnapshot()" ng-disabled="loading">
|
||||
<i class="fa fa-save"></i>
|
||||
Local Snapshot
|
||||
</button>
|
||||
<button class="btn btn-primary btn-large" ng-if="externalEnabled" ng-click="createSnapshot(true)" ng-disabled="loading">
|
||||
<i class="fa fa-cloud-upload"></i>
|
||||
{{sharingButtonText}}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="pull-right" ng-if="step === 2" style="padding: 5px">
|
||||
Did you make a mistake? <a class="pointer" ng-click="deleteSnapshot()" target="_blank">delete snapshot.</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div ng-if="step === 1" class="gf-form-buttons-row">
|
||||
<button class="btn btn-success btn-large" ng-click="createSnapshot()" ng-disabled="loading">
|
||||
<i class="fa fa-save"></i>
|
||||
Local Snapshot
|
||||
</button>
|
||||
<button class="btn btn-primary btn-large" ng-if="externalEnabled" ng-click="createSnapshot(true)" ng-disabled="loading">
|
||||
<i class="fa fa-cloud-upload"></i>
|
||||
{{sharingButtonText}}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="pull-right" ng-if="step === 2" style="padding: 5px">
|
||||
Did you make a mistake? <a class="pointer" ng-click="deleteSnapshot()" target="_blank">delete snapshot.</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</script>
|
||||
|
@ -24,22 +24,18 @@ function (angular, _) {
|
||||
|
||||
this.updateTemplateData = function() {
|
||||
this._values = {};
|
||||
this._texts = {};
|
||||
|
||||
_.each(this.variables, function(variable) {
|
||||
if (!variable.current || !variable.current.isNone && !variable.current.value) { return; }
|
||||
|
||||
this._values[variable.name] = this.renderVariableValue(variable);
|
||||
this._texts[variable.name] = variable.current.text;
|
||||
}, this);
|
||||
if (!variable.current || !variable.current.isNone && !variable.current.value) { return; }
|
||||
this._values[variable.name] = variable.current.value;
|
||||
}, this);
|
||||
};
|
||||
|
||||
this.renderVariableValue = function(variable) {
|
||||
var value = variable.current.value;
|
||||
this.formatValue = function(value, format) {
|
||||
if (_.isString(value)) {
|
||||
return value;
|
||||
} else {
|
||||
switch(variable.multiFormat) {
|
||||
switch(format) {
|
||||
case "regex values": {
|
||||
return '(' + value.join('|') + ')';
|
||||
}
|
||||
@ -89,22 +85,31 @@ function (angular, _) {
|
||||
});
|
||||
};
|
||||
|
||||
this.replace = function(target, scopedVars) {
|
||||
this.replace = function(target, scopedVars, format) {
|
||||
if (!target) { return target; }
|
||||
|
||||
var value;
|
||||
var value, systemValue;
|
||||
this._regex.lastIndex = 0;
|
||||
|
||||
return target.replace(this._regex, function(match, g1, g2) {
|
||||
if (scopedVars) {
|
||||
value = scopedVars[g1 || g2];
|
||||
if (value) { return value.value; }
|
||||
if (value) {
|
||||
return self.formatValue(value.value);
|
||||
}
|
||||
}
|
||||
|
||||
value = self._values[g1 || g2];
|
||||
if (!value) { return match; }
|
||||
if (!value) {
|
||||
return match;
|
||||
}
|
||||
|
||||
return self._grafanaVariables[value] || value;
|
||||
systemValue = self._grafanaVariables[value];
|
||||
if (systemValue) {
|
||||
return self.formatValue(systemValue);
|
||||
}
|
||||
|
||||
return self.formatValue(value, format);
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -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() {
|
||||
it('single value should return value', function() {
|
||||
var result = _templateSrv.renderVariableValue({current: {value: 'test'}});
|
||||
|
Loading…
Reference in New Issue
Block a user