PluginSettings.tsx 1.5 KB

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