mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* DashbaordScene: Add macro for interpoladint __dashboard variable * Review * Test fix * Test fix
41 lines
966 B
TypeScript
41 lines
966 B
TypeScript
import { FormatVariable, SceneObject, sceneUtils } from '@grafana/scenes';
|
|
|
|
import { getDashboardSceneFor } from '../utils/utils';
|
|
|
|
/**
|
|
* Handles expressions like ${__dashboard.uid}
|
|
*/
|
|
class DashboardMacro implements FormatVariable {
|
|
public state: { name: string; type: string };
|
|
|
|
public constructor(
|
|
name: string,
|
|
private _sceneObject: SceneObject
|
|
) {
|
|
this.state = { name: name, type: 'dashboard_macro' };
|
|
}
|
|
|
|
public getValue(fieldPath?: string): string {
|
|
const dashboard = getDashboardSceneFor(this._sceneObject);
|
|
switch (fieldPath) {
|
|
case 'uid':
|
|
return dashboard.state.uid || '';
|
|
case 'title':
|
|
case 'name':
|
|
case 'id':
|
|
default:
|
|
return dashboard.state.title;
|
|
}
|
|
}
|
|
|
|
public getValueText?(): string {
|
|
return '';
|
|
}
|
|
}
|
|
|
|
export function registerDashboardMacro() {
|
|
const unregister = sceneUtils.registerVariableMacro('__dashboard', DashboardMacro);
|
|
|
|
return () => unregister();
|
|
}
|