mirror of https://github.com/grafana/grafana
Templating: global/system variables should be properly replaced in templated values. (#27394)
* Fixed so we try to use the variables in the redux store to replace values in template variables. * First draft of working version. * Including fieldPath when adding :text format. * cleaned up code by introducing helper function. * some minor refactoring. * Added tests and support for multi variables. * added test and code to handle the All scenario of a multivariable. * fixed according to feedback. * added docs. * added text format to gdev dashboard. * updated e2e tests. * make sure we use the same function for formatting och variable lable. * increased the number to 22. * changed label for tests to be All. * existing format should be respected.pull/21835/head^2
parent
b3b72b8ae6
commit
0c8390cea2
@ -0,0 +1,51 @@ |
||||
import { VariableModel } from '@grafana/data'; |
||||
import { VariableWithOptions } from '../types'; |
||||
|
||||
export const formatVariableLabel = (variable: VariableModel) => { |
||||
if (!isVariableWithOptions(variable)) { |
||||
return variable.name; |
||||
} |
||||
|
||||
const { current, options = [] } = variable; |
||||
|
||||
if (!current.tags || current.tags.length === 0) { |
||||
if (Array.isArray(current.text)) { |
||||
return current.text.join(' + '); |
||||
} |
||||
return current.text; |
||||
} |
||||
|
||||
// filer out values that are in selected tags
|
||||
const selectedAndNotInTag = options.filter(option => { |
||||
if (!option.selected) { |
||||
return false; |
||||
} |
||||
|
||||
if (!current || !current.tags || !current.tags.length) { |
||||
return false; |
||||
} |
||||
|
||||
for (let i = 0; i < current.tags.length; i++) { |
||||
const tag = current.tags[i]; |
||||
const foundIndex = tag?.values?.findIndex(v => v === option.value); |
||||
if (foundIndex && foundIndex !== -1) { |
||||
return false; |
||||
} |
||||
} |
||||
return true; |
||||
}); |
||||
|
||||
// convert values to text
|
||||
const currentTexts = selectedAndNotInTag.map(s => s.text); |
||||
|
||||
// join texts
|
||||
const newLinkText = currentTexts.join(' + '); |
||||
return newLinkText.length > 0 ? `${newLinkText} + ` : newLinkText; |
||||
}; |
||||
|
||||
const isVariableWithOptions = (variable: VariableModel): variable is VariableWithOptions => { |
||||
return ( |
||||
Array.isArray((variable as VariableWithOptions)?.options) || |
||||
typeof (variable as VariableWithOptions)?.current === 'object' |
||||
); |
||||
}; |
||||
Loading…
Reference in new issue