2021-04-21 08:38:00 +01:00
|
|
|
import { cloneDeep } from 'lodash';
|
2022-04-22 14:33:13 +01:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
|
2022-07-20 09:25:09 +02:00
|
|
|
import { DataSourcePluginMeta, DataSourceSettings } from '@grafana/data';
|
2021-04-15 14:21:06 +02:00
|
|
|
import { AngularComponent, getAngularLoader } from '@grafana/runtime';
|
2018-10-31 13:23:05 +01:00
|
|
|
|
2022-07-20 09:25:09 +02:00
|
|
|
import { GenericDataSourcePlugin } from '../types';
|
2019-05-09 02:48:13 -07:00
|
|
|
|
2018-11-01 13:45:52 +01:00
|
|
|
export interface Props {
|
2019-05-09 02:48:13 -07:00
|
|
|
plugin: GenericDataSourcePlugin;
|
2019-01-17 18:51:07 +01:00
|
|
|
dataSource: DataSourceSettings;
|
2019-04-29 09:14:39 -07:00
|
|
|
dataSourceMeta: DataSourcePluginMeta;
|
2019-01-17 18:51:07 +01:00
|
|
|
onModelChange: (dataSource: DataSourceSettings) => void;
|
2018-10-30 16:40:08 +01:00
|
|
|
}
|
|
|
|
|
2022-07-20 09:25:09 +02:00
|
|
|
export class DataSourcePluginSettings extends PureComponent<Props> {
|
2021-04-15 14:21:06 +02:00
|
|
|
element: HTMLDivElement | null = null;
|
|
|
|
component?: AngularComponent;
|
2018-11-01 11:19:40 +01:00
|
|
|
scopeProps: {
|
2019-05-02 21:26:30 -07:00
|
|
|
ctrl: { datasourceMeta: DataSourcePluginMeta; current: DataSourceSettings };
|
2019-01-17 18:51:07 +01:00
|
|
|
onModelChanged: (dataSource: DataSourceSettings) => void;
|
2018-11-01 11:19:40 +01:00
|
|
|
};
|
2018-10-30 16:40:08 +01:00
|
|
|
|
2019-05-02 21:26:30 -07:00
|
|
|
constructor(props: Props) {
|
2018-11-01 11:19:40 +01:00
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.scopeProps = {
|
2021-04-21 08:38:00 +01:00
|
|
|
ctrl: { datasourceMeta: props.dataSourceMeta, current: cloneDeep(props.dataSource) },
|
2018-11-01 11:19:40 +01:00
|
|
|
onModelChanged: this.onModelChanged,
|
|
|
|
};
|
2019-04-24 23:18:51 +02:00
|
|
|
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() {
|
2019-04-24 23:18:51 +02:00
|
|
|
const { plugin } = this.props;
|
|
|
|
|
2018-10-30 16:40:08 +01:00
|
|
|
if (!this.element) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-24 23:18:51 +02:00
|
|
|
if (!plugin.components.ConfigEditor) {
|
|
|
|
// React editor is not specified, let's render angular editor
|
2021-03-31 16:07:37 -07:00
|
|
|
// How to approach this better? Introduce ReactDataSourcePlugin interface and typeguard it here?
|
2019-04-24 23:18:51 +02:00
|
|
|
const loader = getAngularLoader();
|
|
|
|
const template = '<plugin-component type="datasource-config-ctrl" />';
|
2018-10-30 16:40:08 +01:00
|
|
|
|
2019-04-24 23:18:51 +02:00
|
|
|
this.component = loader.load(this.element, this.scopeProps, template);
|
|
|
|
}
|
2018-11-01 11:19:40 +01:00
|
|
|
}
|
|
|
|
|
2019-05-13 22:55:49 -07:00
|
|
|
componentDidUpdate(prevProps: Props) {
|
2019-04-24 23:18:51 +02:00
|
|
|
const { plugin } = this.props;
|
|
|
|
if (!plugin.components.ConfigEditor && this.props.dataSource !== prevProps.dataSource) {
|
2021-04-21 08:38:00 +01:00
|
|
|
this.scopeProps.ctrl.current = cloneDeep(this.props.dataSource);
|
2018-11-05 13:39:57 +01:00
|
|
|
|
2021-04-15 14:21:06 +02:00
|
|
|
this.component?.digest();
|
2018-11-01 11:19:40 +01:00
|
|
|
}
|
2018-10-30 16:40:08 +01:00
|
|
|
}
|
|
|
|
|
2018-10-31 09:19:08 -07:00
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.component) {
|
|
|
|
this.component.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 18:51:07 +01:00
|
|
|
onModelChanged = (dataSource: DataSourceSettings) => {
|
2018-11-01 11:19:40 +01:00
|
|
|
this.props.onModelChange(dataSource);
|
|
|
|
};
|
2018-10-31 09:19:08 -07:00
|
|
|
|
2018-10-30 16:40:08 +01:00
|
|
|
render() {
|
2019-04-24 23:18:51 +02:00
|
|
|
const { plugin, dataSource } = this.props;
|
|
|
|
|
|
|
|
if (!plugin) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2021-01-20 07:59:48 +01:00
|
|
|
<div ref={(element) => (this.element = element)}>
|
2019-04-24 23:18:51 +02:00
|
|
|
{plugin.components.ConfigEditor &&
|
|
|
|
React.createElement(plugin.components.ConfigEditor, {
|
|
|
|
options: dataSource,
|
|
|
|
onOptionsChange: this.onModelChanged,
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
);
|
2018-10-30 16:40:08 +01:00
|
|
|
}
|
|
|
|
}
|