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