Files
grafana/public/app/features/datasources/settings/PluginSettings.tsx

96 lines
2.6 KiB
TypeScript
Raw Normal View History

2018-10-30 16:40:08 +01:00
import React, { PureComponent } from 'react';
import { cloneDeep } from 'lodash';
import {
DataQuery,
DataSourceApi,
DataSourceJsonData,
DataSourcePlugin,
DataSourcePluginMeta,
DataSourceSettings,
} from '@grafana/data';
import { AngularComponent, getAngularLoader } from '@grafana/runtime';
2018-10-31 13:23:05 +01:00
export type GenericDataSourcePlugin = DataSourcePlugin<DataSourceApi<DataQuery, DataSourceJsonData>>;
2018-11-01 13:45:52 +01:00
export interface Props {
plugin: GenericDataSourcePlugin;
dataSource: DataSourceSettings;
dataSourceMeta: DataSourcePluginMeta;
onModelChange: (dataSource: DataSourceSettings) => void;
2018-10-30 16:40:08 +01:00
}
2018-10-31 13:23:05 +01:00
export class PluginSettings extends PureComponent<Props> {
element: HTMLDivElement | null = null;
component?: AngularComponent;
2018-11-01 11:19:40 +01:00
scopeProps: {
ctrl: { datasourceMeta: DataSourcePluginMeta; current: DataSourceSettings };
onModelChanged: (dataSource: DataSourceSettings) => void;
2018-11-01 11:19:40 +01:00
};
2018-10-30 16:40:08 +01:00
constructor(props: Props) {
2018-11-01 11:19:40 +01:00
super(props);
this.scopeProps = {
ctrl: { datasourceMeta: props.dataSourceMeta, current: cloneDeep(props.dataSource) },
2018-11-01 11:19:40 +01:00
onModelChanged: this.onModelChanged,
};
this.onModelChanged = this.onModelChanged.bind(this);
2018-11-01 11:19:40 +01:00
}
2018-10-31 13:23:05 +01:00
2018-11-01 11:19:40 +01:00
componentDidMount() {
const { plugin } = this.props;
2018-10-30 16:40:08 +01:00
if (!this.element) {
return;
}
if (!plugin.components.ConfigEditor) {
// React editor is not specified, let's render angular editor
// How to approach this better? Introduce ReactDataSourcePlugin interface and typeguard it here?
const loader = getAngularLoader();
const template = '<plugin-component type="datasource-config-ctrl" />';
2018-10-30 16:40:08 +01:00
this.component = loader.load(this.element, this.scopeProps, template);
}
2018-11-01 11:19:40 +01:00
}
componentDidUpdate(prevProps: Props) {
const { plugin } = this.props;
if (!plugin.components.ConfigEditor && this.props.dataSource !== prevProps.dataSource) {
this.scopeProps.ctrl.current = cloneDeep(this.props.dataSource);
2018-11-05 13:39:57 +01:00
this.component?.digest();
2018-11-01 11:19:40 +01:00
}
2018-10-30 16:40:08 +01:00
}
componentWillUnmount() {
if (this.component) {
this.component.destroy();
}
}
onModelChanged = (dataSource: DataSourceSettings) => {
2018-11-01 11:19:40 +01:00
this.props.onModelChange(dataSource);
};
2018-10-30 16:40:08 +01:00
render() {
const { plugin, dataSource } = this.props;
if (!plugin) {
return null;
}
return (
<div ref={(element) => (this.element = element)}>
{plugin.components.ConfigEditor &&
React.createElement(plugin.components.ConfigEditor, {
options: dataSource,
onOptionsChange: this.onModelChanged,
})}
</div>
);
2018-10-30 16:40:08 +01:00
}
}
2018-10-31 13:23:05 +01:00
export default PluginSettings;