2025-06-12 17:03:52 +08:00
|
|
|
import { t } from '@grafana/i18n';
|
2025-04-03 01:50:36 +08:00
|
|
|
import { SceneComponentProps, sceneGraph, SceneObject, SceneObjectBase, SceneObjectState } from '@grafana/scenes';
|
2025-04-02 22:22:24 +08:00
|
|
|
import { ConditionalRenderingGroupKind } from '@grafana/schema/dist/esm/schema/dashboard/v2alpha1/types.spec.gen';
|
2025-03-13 15:25:55 +08:00
|
|
|
|
2025-04-03 01:50:36 +08:00
|
|
|
import { ConditionalRenderingChangedEvent } from '../edit-pane/shared';
|
|
|
|
|
|
|
|
import { ConditionalRenderingBase } from './ConditionalRenderingBase';
|
2025-03-13 15:25:55 +08:00
|
|
|
import { ConditionalRenderingGroup } from './ConditionalRenderingGroup';
|
2025-04-03 01:50:36 +08:00
|
|
|
import { ItemsWithConditionalRendering } from './types';
|
|
|
|
import { getItemType } from './utils';
|
2025-03-13 15:25:55 +08:00
|
|
|
|
|
|
|
export interface ConditionalRenderingState extends SceneObjectState {
|
|
|
|
rootGroup: ConditionalRenderingGroup;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ConditionalRendering extends SceneObjectBase<ConditionalRenderingState> {
|
|
|
|
public static Component = ConditionalRenderingRenderer;
|
|
|
|
|
2025-04-03 01:50:36 +08:00
|
|
|
public get info(): string {
|
|
|
|
return t(
|
|
|
|
'dashboard.conditional-rendering.root.info',
|
|
|
|
'Set rules to control {{type}} visibility by matching any or all rules.',
|
|
|
|
{ type: this.getItemType() }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-03-13 15:25:55 +08:00
|
|
|
public constructor(state: ConditionalRenderingState) {
|
|
|
|
super(state);
|
|
|
|
|
|
|
|
this.addActivationHandler(() => this._activationHandler());
|
|
|
|
}
|
|
|
|
|
|
|
|
private _activationHandler() {
|
|
|
|
// This ensures that all children are activated when conditional rendering is activated
|
2025-04-03 01:50:36 +08:00
|
|
|
// We need this to allow children to subscribe to variable changes etc.
|
2025-03-13 15:25:55 +08:00
|
|
|
this.forEachChild((child) => {
|
|
|
|
if (!child.isActive) {
|
|
|
|
this._subs.add(child.activate());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public evaluate(): boolean {
|
|
|
|
return this.state.rootGroup.evaluate();
|
|
|
|
}
|
|
|
|
|
|
|
|
public notifyChange() {
|
|
|
|
this.parent?.forceRender();
|
2025-04-03 01:50:36 +08:00
|
|
|
this.parent?.publishEvent(new ConditionalRenderingChangedEvent(this), true);
|
2025-03-13 15:25:55 +08:00
|
|
|
}
|
|
|
|
|
2025-04-03 01:50:36 +08:00
|
|
|
public deleteItem<T extends ConditionalRenderingBase>(item: T) {
|
|
|
|
sceneGraph.getAncestor(item, ConditionalRenderingGroup).removeItem(item.state.key!);
|
2025-03-13 15:25:55 +08:00
|
|
|
}
|
2025-03-13 18:56:20 +08:00
|
|
|
|
|
|
|
public serialize(): ConditionalRenderingGroupKind {
|
|
|
|
return this.state.rootGroup.serialize();
|
|
|
|
}
|
2025-04-03 01:50:36 +08:00
|
|
|
|
|
|
|
public getItem(): SceneObject {
|
|
|
|
return this.parent!;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getItemType(): ItemsWithConditionalRendering {
|
|
|
|
return getItemType(this.getItem());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static createEmpty(): ConditionalRendering {
|
|
|
|
return new ConditionalRendering({ rootGroup: ConditionalRenderingGroup.createEmpty() });
|
|
|
|
}
|
2025-03-13 15:25:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function ConditionalRenderingRenderer({ model }: SceneComponentProps<ConditionalRendering>) {
|
|
|
|
const { rootGroup } = model.useState();
|
|
|
|
|
2025-04-03 01:50:36 +08:00
|
|
|
return rootGroup.render(false);
|
2025-03-13 15:25:55 +08:00
|
|
|
}
|