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

87 lines
2.3 KiB
TypeScript
Raw Normal View History

2018-10-30 10:40:08 -05:00
import React, { PureComponent } from 'react';
2018-11-01 05:19:40 -05:00
import _ from 'lodash';
import { DataSourceSettings, DataSourcePlugin, DataSourcePluginMeta } from '@grafana/ui';
2018-10-30 10:40:08 -05:00
import { getAngularLoader, AngularComponent } from 'app/core/services/AngularLoader';
2018-10-31 07:23:05 -05:00
2018-11-01 07:45:52 -05:00
export interface Props {
plugin: DataSourcePlugin;
dataSource: DataSourceSettings;
dataSourceMeta: DataSourcePluginMeta;
onModelChange: (dataSource: DataSourceSettings) => void;
2018-10-30 10:40:08 -05:00
}
2018-10-31 07:23:05 -05:00
export class PluginSettings extends PureComponent<Props> {
2018-10-30 10:40:08 -05:00
element: any;
component: AngularComponent;
2018-11-01 05:19:40 -05:00
scopeProps: {
ctrl: { datasourceMeta: Plugin; current: DataSourceSettings };
onModelChanged: (dataSource: DataSourceSettings) => void;
2018-11-01 05:19:40 -05:00
};
2018-10-30 10:40:08 -05:00
2018-11-01 05:19:40 -05:00
constructor(props) {
super(props);
this.scopeProps = {
ctrl: { datasourceMeta: props.dataSourceMeta, current: _.cloneDeep(props.dataSource) },
onModelChanged: this.onModelChanged,
};
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() {
const { plugin } = this.props;
2018-10-30 10:40:08 -05:00
if (!this.element) {
return;
}
if (!plugin.components.ConfigEditor) {
// React editor is not specified, let's render angular editor
// How to apprach this better? Introduce ReactDataSourcePlugin interface and typeguard it here?
const loader = getAngularLoader();
const template = '<plugin-component type="datasource-config-ctrl" />';
2018-10-30 10:40:08 -05:00
this.component = loader.load(this.element, this.scopeProps, template);
}
2018-11-01 05:19:40 -05:00
}
componentDidUpdate(prevProps) {
const { plugin } = this.props;
if (!plugin.components.ConfigEditor && this.props.dataSource !== prevProps.dataSource) {
2018-11-01 05:19:40 -05:00
this.scopeProps.ctrl.current = _.cloneDeep(this.props.dataSource);
2018-11-05 06:39:57 -06:00
this.component.digest();
2018-11-01 05:19:40 -05:00
}
2018-10-30 10:40:08 -05:00
}
componentWillUnmount() {
if (this.component) {
this.component.destroy();
}
}
onModelChanged = (dataSource: DataSourceSettings) => {
2018-11-01 05:19:40 -05:00
this.props.onModelChange(dataSource);
};
2018-10-30 10:40:08 -05: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 10:40:08 -05:00
}
}
2018-10-31 07:23:05 -05:00
export default PluginSettings;