Files
grafana/public/app/features/dashboard-scene/serialization/layoutSerializers/TabsLayoutSerializer.ts
T
Marcus Andersson c3cce293e8 Schema: Re-introduce exports for the schema package (#116683)
* wip

* added fallback exports.#

* renamed files to new pattern to follow exports.

* added fallback exports.

* added more exports.

* wip

* wip

* wip.

* Updating rollup to output proper paths.

* wip

* wip

* reverted new export APIs, will be done in follow up PRs.

* fixed rollup config.

* replaced imports with new proper exports.

* fixed tests.

* renamed missing file.

* fixed error.

* Needed to make the file names shorter due to publint limitations.

* Fixed issue with generated index fiels.

* fixed preferences export as well.

* reverted import.

* fixed conflicts.

* fixed issue with type imports.

* fixed so we are backwards compatible with the node moduleresolution.
2026-02-11 22:27:26 +01:00

75 lines
2.2 KiB
TypeScript

import { Spec as DashboardV2Spec, TabsLayoutTabKind } from '@grafana/schema/apis/dashboard.grafana.app/v2';
import { TabItem } from '../../scene/layout-tabs/TabItem';
import { TabsLayoutManager } from '../../scene/layout-tabs/TabsLayoutManager';
import { layoutDeserializerRegistry } from './layoutSerializerRegistry';
import { getConditionalRendering } from './utils';
export function serializeTabsLayout(layoutManager: TabsLayoutManager): DashboardV2Spec['layout'] {
return {
kind: 'TabsLayout',
spec: {
tabs: layoutManager.state.tabs.filter((tab) => !tab.state.repeatSourceKey).map(serializeTab),
},
};
}
export function serializeTab(tab: TabItem): TabsLayoutTabKind {
const layout = tab.state.layout.serialize();
const tabKind: TabsLayoutTabKind = {
kind: 'TabsLayoutTab',
spec: {
title: tab.state.title,
layout: layout,
...(tab.state.repeatByVariable && {
repeat: {
mode: 'variable',
value: tab.state.repeatByVariable,
},
}),
},
};
const conditionalRenderingRootGroup = tab.state.conditionalRendering?.serialize();
// Only serialize the conditional rendering if it has items
if (conditionalRenderingRootGroup?.spec.items.length) {
tabKind.spec.conditionalRendering = conditionalRenderingRootGroup;
}
return tabKind;
}
export function deserializeTabsLayout(
layout: DashboardV2Spec['layout'],
elements: DashboardV2Spec['elements'],
preload: boolean,
panelIdGenerator?: () => number
): TabsLayoutManager {
if (layout.kind !== 'TabsLayout') {
throw new Error('Invalid layout kind');
}
const tabs = layout.spec.tabs.map((tab) => {
return deserializeTab(tab, elements, preload, panelIdGenerator);
});
return new TabsLayoutManager({ tabs });
}
export function deserializeTab(
tab: TabsLayoutTabKind,
elements: DashboardV2Spec['elements'],
preload: boolean,
panelIdGenerator?: () => number
): TabItem {
const layout = tab.spec.layout;
return new TabItem({
title: tab.spec.title,
layout: layoutDeserializerRegistry.get(layout.kind).deserialize(layout, elements, preload, panelIdGenerator),
repeatByVariable: tab.spec.repeat?.value,
conditionalRendering: getConditionalRendering(tab),
});
}