mirror of
https://github.com/grafana/grafana.git
synced 2025-02-12 00:25:46 -06:00
* Update scenes to latest * Replace FormatRegistryID with VariableFormatID * Remove scene demos that were moved to scenes repo, fix the remaining demos * Fix grafana Monitoring app * DashboardsLoader migration * Fix test
52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
import { formatRegistry } from '@grafana/scenes';
|
|
import { VariableFormatID } from '@grafana/schema';
|
|
|
|
import { isAdHoc } from '../variables/guard';
|
|
|
|
import { getVariableWrapper } from './LegacyVariableWrapper';
|
|
|
|
export function formatVariableValue(value: any, format?: any, variable?: any, text?: string): string {
|
|
// for some scopedVars there is no variable
|
|
variable = variable || {};
|
|
|
|
if (value === null || value === undefined) {
|
|
return '';
|
|
}
|
|
|
|
if (isAdHoc(variable) && format !== VariableFormatID.QueryParam) {
|
|
return '';
|
|
}
|
|
|
|
// if it's an object transform value to string
|
|
if (!Array.isArray(value) && typeof value === 'object') {
|
|
value = `${value}`;
|
|
}
|
|
|
|
if (typeof format === 'function') {
|
|
return format(value, variable, formatVariableValue);
|
|
}
|
|
|
|
if (!format) {
|
|
format = VariableFormatID.Glob;
|
|
}
|
|
|
|
// some formats have arguments that come after ':' character
|
|
let args = format.split(':');
|
|
if (args.length > 1) {
|
|
format = args[0];
|
|
args = args.slice(1);
|
|
} else {
|
|
args = [];
|
|
}
|
|
|
|
let formatItem = formatRegistry.getIfExists(format);
|
|
|
|
if (!formatItem) {
|
|
console.error(`Variable format ${format} not found. Using glob format as fallback.`);
|
|
formatItem = formatRegistry.get(VariableFormatID.Glob);
|
|
}
|
|
|
|
const formatVariable = getVariableWrapper(variable, value, text ?? value);
|
|
return formatItem.formatter(value, args, formatVariable);
|
|
}
|