mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
DashboardScene: Add macro for interpolating __dashboard variable (#78172)
* DashbaordScene: Add macro for interpoladint __dashboard variable * Review * Test fix * Test fix
This commit is contained in:
parent
05febc4665
commit
384db8e0ca
40
public/app/features/dashboard-scene/scene/DashboardMacro.ts
Normal file
40
public/app/features/dashboard-scene/scene/DashboardMacro.ts
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
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();
|
||||||
|
}
|
@ -113,9 +113,9 @@ describe('transformSaveModelToScene', () => {
|
|||||||
|
|
||||||
const scene = createDashboardSceneFromDashboardModel(oldModel);
|
const scene = createDashboardSceneFromDashboardModel(oldModel);
|
||||||
|
|
||||||
expect(scene.state.$behaviors).toHaveLength(1);
|
expect(scene.state.$behaviors).toHaveLength(2);
|
||||||
expect(scene.state.$behaviors![0]).toBeInstanceOf(behaviors.CursorSync);
|
expect(scene.state.$behaviors![1]).toBeInstanceOf(behaviors.CursorSync);
|
||||||
expect((scene.state.$behaviors![0] as behaviors.CursorSync).state.sync).toEqual(DashboardCursorSync.Crosshair);
|
expect((scene.state.$behaviors![1] as behaviors.CursorSync).state.sync).toEqual(DashboardCursorSync.Crosshair);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -32,6 +32,7 @@ import { DashboardDTO } from 'app/types';
|
|||||||
|
|
||||||
import { AlertStatesDataLayer } from '../scene/AlertStatesDataLayer';
|
import { AlertStatesDataLayer } from '../scene/AlertStatesDataLayer';
|
||||||
import { DashboardAnnotationsDataLayer } from '../scene/DashboardAnnotationsDataLayer';
|
import { DashboardAnnotationsDataLayer } from '../scene/DashboardAnnotationsDataLayer';
|
||||||
|
import { registerDashboardMacro } from '../scene/DashboardMacro';
|
||||||
import { DashboardScene } from '../scene/DashboardScene';
|
import { DashboardScene } from '../scene/DashboardScene';
|
||||||
import { LibraryVizPanel } from '../scene/LibraryVizPanel';
|
import { LibraryVizPanel } from '../scene/LibraryVizPanel';
|
||||||
import { VizPanelLinks, VizPanelLinksMenu } from '../scene/PanelLinks';
|
import { VizPanelLinks, VizPanelLinksMenu } from '../scene/PanelLinks';
|
||||||
@ -249,6 +250,7 @@ export function createDashboardSceneFromDashboardModel(oldModel: DashboardModel)
|
|||||||
}),
|
}),
|
||||||
$variables: variables,
|
$variables: variables,
|
||||||
$behaviors: [
|
$behaviors: [
|
||||||
|
registerDashboardMacro,
|
||||||
new behaviors.CursorSync({
|
new behaviors.CursorSync({
|
||||||
sync: oldModel.graphTooltip,
|
sync: oldModel.graphTooltip,
|
||||||
}),
|
}),
|
||||||
|
@ -146,6 +146,15 @@ jest.mock('@grafana/runtime', () => ({
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
jest.mock('@grafana/scenes', () => ({
|
||||||
|
...jest.requireActual('@grafana/scenes'),
|
||||||
|
sceneUtils: {
|
||||||
|
...jest.requireActual('@grafana/scenes').sceneUtils,
|
||||||
|
registerVariableMacro: jest.fn(),
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
describe('transformSceneToSaveModel', () => {
|
describe('transformSceneToSaveModel', () => {
|
||||||
describe('Given a simple scene with variables', () => {
|
describe('Given a simple scene with variables', () => {
|
||||||
it('Should transform back to persisted model', () => {
|
it('Should transform back to persisted model', () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user