2025-03-13 15:25:55 +08:00
|
|
|
import { SceneComponentProps, SceneObjectBase, SceneObjectState } from '@grafana/scenes';
|
2025-03-13 18:56:20 +08:00
|
|
|
import { ConditionalRenderingGroupKind } from '@grafana/schema/dist/esm/schema/dashboard/v2alpha0';
|
2025-03-13 15:25:55 +08:00
|
|
|
|
|
|
|
import { ConditionalRenderingGroup } from './ConditionalRenderingGroup';
|
|
|
|
|
|
|
|
export interface ConditionalRenderingState extends SceneObjectState {
|
|
|
|
rootGroup: ConditionalRenderingGroup;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ConditionalRendering extends SceneObjectBase<ConditionalRenderingState> {
|
|
|
|
public static Component = ConditionalRenderingRenderer;
|
|
|
|
|
|
|
|
public constructor(state: ConditionalRenderingState) {
|
|
|
|
super(state);
|
|
|
|
|
|
|
|
this.addActivationHandler(() => this._activationHandler());
|
|
|
|
}
|
|
|
|
|
|
|
|
private _activationHandler() {
|
|
|
|
// This ensures that all children are activated when conditional rendering is activated
|
|
|
|
// We need this in order to allow children to subscribe to variable changes etc.
|
|
|
|
this.forEachChild((child) => {
|
|
|
|
if (!child.isActive) {
|
|
|
|
this._subs.add(child.activate());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public evaluate(): boolean {
|
|
|
|
return this.state.rootGroup.evaluate();
|
|
|
|
}
|
|
|
|
|
|
|
|
public notifyChange() {
|
|
|
|
this.parent?.forceRender();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static createEmpty(): ConditionalRendering {
|
|
|
|
return new ConditionalRendering({ rootGroup: ConditionalRenderingGroup.createEmpty() });
|
|
|
|
}
|
2025-03-13 18:56:20 +08:00
|
|
|
|
|
|
|
public serialize(): ConditionalRenderingGroupKind {
|
|
|
|
return this.state.rootGroup.serialize();
|
|
|
|
}
|
2025-03-13 15:25:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function ConditionalRenderingRenderer({ model }: SceneComponentProps<ConditionalRendering>) {
|
|
|
|
const { rootGroup } = model.useState();
|
|
|
|
|
|
|
|
return <rootGroup.Component model={rootGroup} />;
|
|
|
|
}
|