Commit afc8380f by Torkel Ödegaard

Work on getting template variables to work well with functions that take integers, #262

parent 03190518
...@@ -54,6 +54,13 @@ function (angular, _, config, gfunc, Parser) { ...@@ -54,6 +54,13 @@ function (angular, _, config, gfunc, Parser) {
checkOtherSegments($scope.segments.length - 1); checkOtherSegments($scope.segments.length - 1);
} }
function addFunctionParameter(func, value, index, shiftBack) {
if (shiftBack) {
index = Math.max(index - 1, 0);
}
func.params[index] = value;
}
function parseTargeRecursive(astNode, func, index) { function parseTargeRecursive(astNode, func, index) {
if (astNode === null) { if (astNode === null) {
return null; return null;
...@@ -72,29 +79,19 @@ function (angular, _, config, gfunc, Parser) { ...@@ -72,29 +79,19 @@ function (angular, _, config, gfunc, Parser) {
break; break;
case 'series-ref': case 'series-ref':
if ($scope.segments.length === 0) { addFunctionParameter(func, astNode.value, index, $scope.segments.length > 0);
func.params[index] = astNode.value;
}
else {
func.params[index - 1] = astNode.value;
}
break; break;
case 'string': case 'string':
case 'number': case 'number':
if ((index-1) >= func.def.params.length) { if ((index-1) >= func.def.params.length) {
throw { message: 'invalid number of parameters to method ' + func.def.name }; throw { message: 'invalid number of parameters to method ' + func.def.name };
} }
addFunctionParameter(func, astNode.value, index, true);
if (index === 0) {
func.params[index] = astNode.value;
}
else {
func.params[index - 1] = astNode.value;
}
break; break;
case 'metric': case 'metric':
if ($scope.segments.length > 0) { if ($scope.segments.length > 0) {
throw { message: 'Multiple metric params not supported, use text editor.' }; addFunctionParameter(func, astNode.segments[0].value, index, true);
break;
} }
$scope.segments = _.map(astNode.segments, function(segment) { $scope.segments = _.map(astNode.segments, function(segment) {
......
...@@ -555,9 +555,6 @@ function (_) { ...@@ -555,9 +555,6 @@ function (_) {
if (strValue === '' && this.def.params[index].optional) { if (strValue === '' && this.def.params[index].optional) {
this.params.splice(index, 1); this.params.splice(index, 1);
} }
else if (this.def.params[index].type === 'int') {
this.params[index] = parseFloat(strValue, 10);
}
else { else {
this.params[index] = strValue; this.params[index] = strValue;
} }
......
...@@ -47,7 +47,7 @@ function (angular, _) { ...@@ -47,7 +47,7 @@ function (angular, _) {
}; };
this.highlightVariablesAsHtml = function(str) { this.highlightVariablesAsHtml = function(str) {
if (!str) { return str; } if (!str || !_.isString(str)) { return str; }
this.regex.lastIndex = 0; this.regex.lastIndex = 0;
return str.replace(this.regex, function(match, g1, g2) { return str.replace(this.regex, function(match, g1, g2) {
......
...@@ -56,7 +56,7 @@ function (angular, _, kbn) { ...@@ -56,7 +56,7 @@ function (angular, _, kbn) {
return { text: text, value: text }; return { text: text, value: text };
}); });
self.setVariableValue(variable, variable.options[0]); self.setVariableValue(variable, variable.options[0]);
return; return $q.when([]);
} }
var datasource = datasourceSrv.get(variable.datasource); var datasource = datasourceSrv.get(variable.datasource);
......
...@@ -85,7 +85,7 @@ define([ ...@@ -85,7 +85,7 @@ define([
it('should parse numbers as float', function() { it('should parse numbers as float', function() {
var func = gfunc.createFuncInstance('scale'); var func = gfunc.createFuncInstance('scale');
func.updateParam('0.001', 0); func.updateParam('0.001', 0);
expect(func.params[0]).to.be(0.001); expect(func.params[0]).to.be('0.001');
}); });
}); });
...@@ -93,14 +93,14 @@ define([ ...@@ -93,14 +93,14 @@ define([
it('should update value and text', function() { it('should update value and text', function() {
var func = gfunc.createFuncInstance('aliasByNode'); var func = gfunc.createFuncInstance('aliasByNode');
func.updateParam('1', 0); func.updateParam('1', 0);
expect(func.params[0]).to.be(1); expect(func.params[0]).to.be('1');
}); });
it('should slit text and put value in second param', function() { it('should slit text and put value in second param', function() {
var func = gfunc.createFuncInstance('aliasByNode'); var func = gfunc.createFuncInstance('aliasByNode');
func.updateParam('4,-5', 0); func.updateParam('4,-5', 0);
expect(func.params[0]).to.be(4); expect(func.params[0]).to.be('4');
expect(func.params[1]).to.be(-5); expect(func.params[1]).to.be('-5');
expect(func.text).to.be('aliasByNode(4, -5)'); expect(func.text).to.be('aliasByNode(4, -5)');
}); });
...@@ -108,7 +108,7 @@ define([ ...@@ -108,7 +108,7 @@ define([
var func = gfunc.createFuncInstance('aliasByNode'); var func = gfunc.createFuncInstance('aliasByNode');
func.updateParam('4,-5', 0); func.updateParam('4,-5', 0);
func.updateParam('', 1); func.updateParam('', 1);
expect(func.params[0]).to.be(4); expect(func.params[0]).to.be('4');
expect(func.params[1]).to.be(undefined); expect(func.params[1]).to.be(undefined);
expect(func.text).to.be('aliasByNode(4)'); expect(func.text).to.be('aliasByNode(4)');
}); });
......
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