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