Dashboard: Support TemplateSrv.containsTemplate in scenes context (#104072)

This commit is contained in:
Haris Rozajac
2025-04-17 10:55:03 -06:00
committed by GitHub
parent 8021dee6f1
commit 82184686dc
2 changed files with 32 additions and 0 deletions
@@ -904,6 +904,29 @@ describe('templateSrv', () => {
expect(podVar.current.text).toEqual(['podA', 'podB']);
});
it('Can use containsTemplate to check if a variable exists', () => {
window.__grafanaSceneContext = new EmbeddedScene({
$variables: new SceneVariableSet({
variables: [
new QueryVariable({ name: 'server', value: 'serverA', text: 'Server A', query: { refId: 'A' } }),
new QueryVariable({ name: 'pods', value: ['pA', 'pB'], text: ['podA', 'podB'], query: { refId: 'A' } }),
new DataSourceVariable({ name: 'ds', value: 'dsA', text: 'dsA', pluginId: 'prometheus' }),
new CustomVariable({ name: 'custom', value: 'A', text: 'A', query: 'A, B, C' }),
new IntervalVariable({ name: 'interval', value: '1m', intervals: ['1m', '2m'] }),
],
}),
body: new SceneCanvasText({ text: 'hello' }),
});
window.__grafanaSceneContext.activate();
expect(_templateSrv.containsTemplate('${server}')).toBe(true);
expect(_templateSrv.containsTemplate('${pods}')).toBe(true);
expect(_templateSrv.containsTemplate('${ds}')).toBe(true);
expect(_templateSrv.containsTemplate('${custom}')).toBe(true);
expect(_templateSrv.containsTemplate('${interval}')).toBe(true);
});
it('Should return timeRange from scenes context', () => {
window.__grafanaSceneContext = new EmbeddedScene({
body: new SceneCanvasText({ text: 'hello' }),
@@ -200,6 +200,15 @@ export class TemplateSrv implements BaseTemplateSrv {
if (!target) {
return false;
}
// Scenes compatability
if (window.__grafanaSceneContext && window.__grafanaSceneContext.isActive) {
// We are just checking that this is a valid variable reference, and we are not looking up the variable
this.regex.lastIndex = 0;
const match = this.regex.exec(target);
return !!match;
}
const name = this.getVariableName(target);
const variable = name && this.getVariableAtIndex(name);
return variable !== null && variable !== undefined;