grafana/public/app/features/scenes/variables/sceneTemplateInterpolator.ts
Torkel Ödegaard 6ed35292fe
Variables: SceneVariable update process (#57784)
* First baby steps

* First baby steps

* No progress really

* Updates

* no progress

* refactoring

* Progress on sub menu and value selectors

* Some more tweaks

* Lots of progress

* Progress

* Updates

* Progress

* Tweaks

* Updates

* Updates to variable system

* Cleaner tests

* Update

* Some cleanup

* correct test name

* Renames and moves

* prop rename

* Fixed scene template interpolator

* More tests for SceneObjectBase and fixed issue in EventBus

* Updates

* More tweaks

* More refinements

* Fixed test

* Added test to EventBus

* Clone all scene object arrays

* Simplify

* tried to merge issue

* Update

* added more comments to interface

* temp progress

* Trying to simplify things, but struggling a bit

* Updated

* Tweaks

* Progress on fixing the select componenet and typing, and sharing code in a base class

* Updated

* Multi select

* Simpler loading state

* Update

* removed failOnConsole

* Removed old funcs

* Moved logic from update manage to MultiValueVariable

* Added tests for MultiValueVariable logic

* Made value a more abstract concept to support object values

* renamed func to getValueText

* Refactored and moved logic to VariableSet

* Added test for deactivation and query cancelling

* Tweaks

* Fixed lint issues
2022-11-09 08:02:24 +01:00

49 lines
1.2 KiB
TypeScript

import { isArray } from 'lodash';
import { variableRegex } from 'app/features/variables/utils';
import { SceneObject } from '../core/types';
import { SceneVariable } from './types';
export function sceneTemplateInterpolator(target: string, sceneObject: SceneObject) {
variableRegex.lastIndex = 0;
return target.replace(variableRegex, (match, var1, var2, fmt2, var3, fieldPath, fmt3) => {
const variableName = var1 || var2 || var3;
const variable = lookupSceneVariable(variableName, sceneObject);
if (!variable) {
return match;
}
const value = variable.getValue(fieldPath);
if (isArray(value)) {
return 'not supported yet';
}
return String(value);
});
}
function lookupSceneVariable(name: string, sceneObject: SceneObject): SceneVariable | null | undefined {
const variables = sceneObject.state.$variables;
if (!variables) {
if (sceneObject.parent) {
return lookupSceneVariable(name, sceneObject.parent);
} else {
return null;
}
}
const found = variables.getByName(name);
if (found) {
return found;
} else if (sceneObject.parent) {
return lookupSceneVariable(name, sceneObject.parent);
}
return null;
}