mirror of
https://github.com/grafana/grafana.git
synced 2025-02-13 00:55:47 -06:00
* Add and configure eslint-plugin-import * Fix the lint:ts npm command * Autofix + prettier all the files * Manually fix remaining files * Move jquery code in jest-setup to external file to safely reorder imports * Resolve issue caused by circular dependencies within Prometheus * Update .betterer.results * Fix missing // @ts-ignore * ignore iconBundle.ts * Fix missing // @ts-ignore
61 lines
2.2 KiB
TypeScript
61 lines
2.2 KiB
TypeScript
import { set, get as lodashGet } from 'lodash';
|
|
|
|
import { StandardEditorContext, TransformerUIProps, PanelOptionsEditorBuilder } from '@grafana/data';
|
|
import { PanelOptionsSupplier } from '@grafana/data/src/panel/PanelPlugin';
|
|
import { NestedValueAccess } from '@grafana/data/src/utils/OptionsUIBuilders';
|
|
import { OptionsPaneCategoryDescriptor } from 'app/features/dashboard/components/PanelEditor/OptionsPaneCategoryDescriptor';
|
|
import { fillOptionsPaneItems } from 'app/features/dashboard/components/PanelEditor/getVisualizationOptions';
|
|
import { setOptionImmutably } from 'app/features/dashboard/components/PanelEditor/utils';
|
|
|
|
export function getTransformerOptionPane<T = any>(
|
|
props: TransformerUIProps<T>,
|
|
supplier: PanelOptionsSupplier<T>
|
|
): OptionsPaneCategoryDescriptor {
|
|
const context: StandardEditorContext<unknown, unknown> = {
|
|
data: props.input,
|
|
options: props.options,
|
|
};
|
|
|
|
const root = new OptionsPaneCategoryDescriptor({ id: 'root', title: 'root' });
|
|
const getOptionsPaneCategory = (categoryNames?: string[]): OptionsPaneCategoryDescriptor => {
|
|
if (categoryNames?.length) {
|
|
const key = categoryNames[0];
|
|
let sub = root.categories.find((v) => v.props.id === key);
|
|
if (!sub) {
|
|
sub = new OptionsPaneCategoryDescriptor({ id: key, title: key });
|
|
root.categories.push(sub);
|
|
}
|
|
return sub;
|
|
}
|
|
return root;
|
|
};
|
|
|
|
const access: NestedValueAccess = {
|
|
getValue: (path: string) => lodashGet(props.options, path),
|
|
onChange: (path: string, value: any) => {
|
|
props.onChange(setOptionImmutably(props.options as any, path, value));
|
|
},
|
|
};
|
|
|
|
// Use the panel options loader
|
|
fillOptionsPaneItems(supplier, access, getOptionsPaneCategory, context);
|
|
return root;
|
|
}
|
|
|
|
export function getDefaultOptions<T = any>(supplier: PanelOptionsSupplier<T>): T {
|
|
const context: StandardEditorContext<T, unknown> = {
|
|
data: [],
|
|
options: {} as T,
|
|
};
|
|
|
|
const results = {};
|
|
const builder = new PanelOptionsEditorBuilder<T>();
|
|
supplier(builder, context);
|
|
for (const item of builder.getItems()) {
|
|
if (item.defaultValue != null) {
|
|
set(results, item.path, item.defaultValue);
|
|
}
|
|
}
|
|
return results as T;
|
|
}
|