PluginSettings.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import React, { PureComponent } from 'react';
  2. import _ from 'lodash';
  3. import { Plugin } from 'app/types';
  4. import { DataSourceSettings } from '@grafana/ui/src/types';
  5. import { getAngularLoader, AngularComponent } from 'app/core/services/AngularLoader';
  6. export interface Props {
  7. dataSource: DataSourceSettings;
  8. dataSourceMeta: Plugin;
  9. onModelChange: (dataSource: DataSourceSettings) => void;
  10. }
  11. export class PluginSettings extends PureComponent<Props> {
  12. element: any;
  13. component: AngularComponent;
  14. scopeProps: {
  15. ctrl: { datasourceMeta: Plugin; current: DataSourceSettings };
  16. onModelChanged: (dataSource: DataSourceSettings) => void;
  17. };
  18. constructor(props) {
  19. super(props);
  20. this.scopeProps = {
  21. ctrl: { datasourceMeta: props.dataSourceMeta, current: _.cloneDeep(props.dataSource) },
  22. onModelChanged: this.onModelChanged,
  23. };
  24. }
  25. componentDidMount() {
  26. if (!this.element) {
  27. return;
  28. }
  29. const loader = getAngularLoader();
  30. const template = '<plugin-component type="datasource-config-ctrl" />';
  31. this.component = loader.load(this.element, this.scopeProps, template);
  32. }
  33. componentDidUpdate(prevProps) {
  34. if (this.props.dataSource !== prevProps.dataSource) {
  35. this.scopeProps.ctrl.current = _.cloneDeep(this.props.dataSource);
  36. this.component.digest();
  37. }
  38. }
  39. componentWillUnmount() {
  40. if (this.component) {
  41. this.component.destroy();
  42. }
  43. }
  44. onModelChanged = (dataSource: DataSourceSettings) => {
  45. this.props.onModelChange(dataSource);
  46. };
  47. render() {
  48. return <div ref={element => (this.element = element)} />;
  49. }
  50. }
  51. export default PluginSettings;