QueryEditorRow.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. // Utils & Services
  4. import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
  5. import { AngularComponent, getAngularLoader } from 'app/core/services/AngularLoader';
  6. import { Emitter } from 'app/core/utils/emitter';
  7. // Types
  8. import { PanelModel } from '../panel_model';
  9. import { DataQuery, DataSourceApi } from 'app/types/series';
  10. interface Props {
  11. panel: PanelModel;
  12. query: DataQuery;
  13. onAddQuery: (query?: DataQuery) => void;
  14. onRemoveQuery: (query: DataQuery) => void;
  15. onMoveQuery: (query: DataQuery, direction: number) => void;
  16. datasourceName: string | null;
  17. }
  18. interface State {
  19. datasource: DataSourceApi | null;
  20. }
  21. export class QueryEditorRow extends PureComponent<Props, State> {
  22. element: HTMLElement | null = null;
  23. angularQueryEditor: AngularComponent | null = null;
  24. state: State = {
  25. datasource: null,
  26. };
  27. componentDidMount() {
  28. this.loadDatasource();
  29. }
  30. getAngularQueryComponentScope(): AngularQueryComponentScope {
  31. const { panel, onAddQuery, onMoveQuery, onRemoveQuery, query } = this.props;
  32. const { datasource } = this.state;
  33. return {
  34. datasource: datasource,
  35. target: query,
  36. panel: panel,
  37. refresh: () => panel.refresh(),
  38. render: () => panel.render,
  39. addQuery: onAddQuery,
  40. moveQuery: onMoveQuery,
  41. removeQuery: onRemoveQuery,
  42. events: panel.events,
  43. };
  44. }
  45. async loadDatasource() {
  46. const { query, panel } = this.props;
  47. const dataSourceSrv = getDatasourceSrv();
  48. const datasource = await dataSourceSrv.get(query.datasource || panel.datasource);
  49. this.setState({ datasource });
  50. }
  51. componentDidUpdate() {
  52. const { datasource } = this.state;
  53. // check if we need to load another datasource
  54. if (datasource && datasource.name !== this.props.datasourceName) {
  55. if (this.angularQueryEditor) {
  56. this.angularQueryEditor.destroy();
  57. this.angularQueryEditor = null;
  58. }
  59. this.loadDatasource();
  60. return;
  61. }
  62. if (!this.element || this.angularQueryEditor) {
  63. return;
  64. }
  65. const loader = getAngularLoader();
  66. const template = '<plugin-component type="query-ctrl" />';
  67. const scopeProps = { ctrl: this.getAngularQueryComponentScope() };
  68. this.angularQueryEditor = loader.load(this.element, scopeProps, template);
  69. }
  70. componentWillUnmount() {
  71. if (this.angularQueryEditor) {
  72. this.angularQueryEditor.destroy();
  73. }
  74. }
  75. render() {
  76. const { datasource } = this.state;
  77. if (!datasource) {
  78. return null;
  79. }
  80. if (datasource.pluginExports.QueryCtrl) {
  81. return <div ref={element => (this.element = element)} />;
  82. } else if (datasource.pluginExports.QueryEditor) {
  83. const QueryEditor = datasource.pluginExports.QueryEditor;
  84. return <QueryEditor />;
  85. }
  86. return <div>Data source plugin does not export any Query Editor component</div>;
  87. }
  88. }
  89. export interface AngularQueryComponentScope {
  90. target: DataQuery;
  91. panel: PanelModel;
  92. events: Emitter;
  93. refresh: () => void;
  94. render: () => void;
  95. removeQuery: (query: DataQuery) => void;
  96. addQuery: (query?: DataQuery) => void;
  97. moveQuery: (query: DataQuery, direction: number) => void;
  98. datasource: DataSourceApi;
  99. }