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

64 lines
1.5 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';
2018-10-31 07:23:05 -05:00
import { DataSource, Plugin } from 'app/types/';
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 {
2018-10-30 10:40:08 -05:00
dataSource: DataSource;
dataSourceMeta: Plugin;
2018-11-01 05:19:40 -05:00
onModelChange: (dataSource: DataSource) => 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: DataSource };
onModelChanged: (dataSource: DataSource) => void;
};
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,
};
}
2018-10-31 07:23:05 -05:00
2018-11-01 05:19:40 -05:00
componentDidMount() {
2018-10-30 10:40:08 -05:00
if (!this.element) {
return;
}
const loader = getAngularLoader();
const template = '<plugin-component type="datasource-config-ctrl" />';
2018-11-01 05:19:40 -05:00
this.component = loader.load(this.element, this.scopeProps, template);
}
componentDidUpdate(prevProps) {
if (this.props.dataSource !== prevProps.dataSource) {
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();
}
}
2018-11-01 05:19:40 -05:00
onModelChanged = (dataSource: DataSource) => {
this.props.onModelChange(dataSource);
};
2018-10-30 10:40:08 -05:00
render() {
return <div ref={element => (this.element = element)} />;
}
}
2018-10-31 07:23:05 -05:00
export default PluginSettings;