2022-07-07 01:53:02 -05:00
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
import { LoadingState, PanelData } from '@grafana/data';
|
|
|
|
|
|
|
|
import { SceneDataNode } from '../core/SceneDataNode';
|
|
|
|
import { SceneObjectBase } from '../core/SceneObjectBase';
|
2022-11-16 04:36:30 -06:00
|
|
|
import { sceneGraph } from '../core/sceneGraph';
|
2022-07-19 10:46:49 -05:00
|
|
|
import {
|
|
|
|
SceneComponentProps,
|
|
|
|
SceneObject,
|
|
|
|
SceneObjectStatePlain,
|
|
|
|
SceneLayoutState,
|
|
|
|
SceneLayoutChild,
|
|
|
|
} from '../core/types';
|
|
|
|
|
|
|
|
interface RepeatOptions extends SceneObjectStatePlain {
|
2022-07-07 01:53:02 -05:00
|
|
|
layout: SceneObject<SceneLayoutState>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ScenePanelRepeater extends SceneObjectBase<RepeatOptions> {
|
2022-11-07 08:32:02 -06:00
|
|
|
public activate(): void {
|
2022-07-07 09:49:05 -05:00
|
|
|
super.activate();
|
2022-07-07 01:53:02 -05:00
|
|
|
|
2022-11-07 08:32:02 -06:00
|
|
|
this._subs.add(
|
2022-11-16 04:36:30 -06:00
|
|
|
sceneGraph.getData(this).subscribeToState({
|
2022-07-07 01:53:02 -05:00
|
|
|
next: (data) => {
|
|
|
|
if (data.data?.state === LoadingState.Done) {
|
|
|
|
this.performRepeat(data.data);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-07 08:32:02 -06:00
|
|
|
private performRepeat(data: PanelData) {
|
2022-07-07 01:53:02 -05:00
|
|
|
// assume parent is a layout
|
|
|
|
const firstChild = this.state.layout.state.children[0]!;
|
2022-07-19 10:46:49 -05:00
|
|
|
const newChildren: SceneLayoutChild[] = [];
|
2022-07-07 01:53:02 -05:00
|
|
|
|
|
|
|
for (const series of data.series) {
|
|
|
|
const clone = firstChild.clone({
|
|
|
|
key: `${newChildren.length}`,
|
|
|
|
$data: new SceneDataNode({
|
|
|
|
data: {
|
|
|
|
...data,
|
|
|
|
series: [series],
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
newChildren.push(clone);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.state.layout.setState({ children: newChildren });
|
|
|
|
}
|
|
|
|
|
2022-11-07 08:32:02 -06:00
|
|
|
public static Component = ({ model, isEditing }: SceneComponentProps<ScenePanelRepeater>) => {
|
2022-07-07 01:53:02 -05:00
|
|
|
const { layout } = model.useState();
|
|
|
|
return <layout.Component model={layout} isEditing={isEditing} />;
|
|
|
|
};
|
|
|
|
}
|