QueryEditorRow.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. import classNames from 'classnames';
  4. // Utils & Services
  5. import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
  6. import { AngularComponent, getAngularLoader } from 'app/core/services/AngularLoader';
  7. import { Emitter } from 'app/core/utils/emitter';
  8. // Types
  9. import { PanelModel } from '../panel_model';
  10. import { DataQuery, DataSourceApi } from 'app/types/series';
  11. interface Props {
  12. panel: PanelModel;
  13. query: DataQuery;
  14. onAddQuery: (query?: DataQuery) => void;
  15. onRemoveQuery: (query: DataQuery) => void;
  16. onMoveQuery: (query: DataQuery, direction: number) => void;
  17. datasourceName: string | null;
  18. }
  19. interface State {
  20. datasource: DataSourceApi | null;
  21. isCollapsed: boolean;
  22. }
  23. export class QueryEditorRow extends PureComponent<Props, State> {
  24. element: HTMLElement | null = null;
  25. angularQueryEditor: AngularComponent | null = null;
  26. state: State = {
  27. datasource: null,
  28. isCollapsed: false,
  29. };
  30. componentDidMount() {
  31. this.loadDatasource();
  32. }
  33. getAngularQueryComponentScope(): AngularQueryComponentScope {
  34. const { panel, onAddQuery, onMoveQuery, onRemoveQuery, query } = this.props;
  35. const { datasource } = this.state;
  36. return {
  37. datasource: datasource,
  38. target: query,
  39. panel: panel,
  40. refresh: () => panel.refresh(),
  41. render: () => panel.render,
  42. addQuery: onAddQuery,
  43. moveQuery: onMoveQuery,
  44. removeQuery: onRemoveQuery,
  45. events: panel.events,
  46. };
  47. }
  48. async loadDatasource() {
  49. const { query, panel } = this.props;
  50. const dataSourceSrv = getDatasourceSrv();
  51. const datasource = await dataSourceSrv.get(query.datasource || panel.datasource);
  52. this.setState({ datasource });
  53. }
  54. componentDidUpdate() {
  55. const { datasource } = this.state;
  56. // check if we need to load another datasource
  57. if (datasource && datasource.name !== this.props.datasourceName) {
  58. if (this.angularQueryEditor) {
  59. this.angularQueryEditor.destroy();
  60. this.angularQueryEditor = null;
  61. }
  62. this.loadDatasource();
  63. return;
  64. }
  65. if (!this.element || this.angularQueryEditor) {
  66. return;
  67. }
  68. const loader = getAngularLoader();
  69. const template = '<plugin-component type="query-ctrl" />';
  70. const scopeProps = { ctrl: this.getAngularQueryComponentScope() };
  71. this.angularQueryEditor = loader.load(this.element, scopeProps, template);
  72. }
  73. componentWillUnmount() {
  74. if (this.angularQueryEditor) {
  75. this.angularQueryEditor.destroy();
  76. }
  77. }
  78. onToggleCollapse = () => {
  79. this.setState({ isCollapsed: !this.state.isCollapsed });
  80. };
  81. render() {
  82. const { query } = this.props;
  83. const { datasource, isCollapsed } = this.state;
  84. const bodyClasses = classNames('query-editor-box__body gf-form-query', {hide: isCollapsed});
  85. if (!datasource) {
  86. return null;
  87. }
  88. if (datasource.pluginExports.QueryCtrl) {
  89. return (
  90. <div className="query-editor-box">
  91. <div className="query-editor-box__header">
  92. <div className="query-editor-box__ref-id" onClick={this.onToggleCollapse}>
  93. {isCollapsed && <i className="fa fa-caret-right" />}
  94. {!isCollapsed && <i className="fa fa-caret-down" />}
  95. <span>{query.refId}</span>
  96. </div>
  97. <div className="query-editor-box__actions">
  98. <button className="query-editor-box__action">
  99. <i className="fa fa-fw fa-arrow-down" />
  100. </button>
  101. <button className="query-editor-box__action">
  102. <i className="fa fa-fw fa-arrow-up" />
  103. </button>
  104. <button className="query-editor-box__action">
  105. <i className="fa fa-fw fa-copy" />
  106. </button>
  107. <button className="query-editor-box__action">
  108. <i className="fa fa-fw fa-eye" />
  109. </button>
  110. <button className="query-editor-box__action">
  111. <i className="fa fa-fw fa-trash" />
  112. </button>
  113. </div>
  114. </div>
  115. <div className={bodyClasses}>
  116. <div ref={element => (this.element = element)} />
  117. </div>
  118. </div>
  119. );
  120. } else if (datasource.pluginExports.QueryEditor) {
  121. const QueryEditor = datasource.pluginExports.QueryEditor;
  122. return <QueryEditor />;
  123. }
  124. return <div>Data source plugin does not export any Query Editor component</div>;
  125. }
  126. }
  127. export interface AngularQueryComponentScope {
  128. target: DataQuery;
  129. panel: PanelModel;
  130. events: Emitter;
  131. refresh: () => void;
  132. render: () => void;
  133. removeQuery: (query: DataQuery) => void;
  134. addQuery: (query?: DataQuery) => void;
  135. moveQuery: (query: DataQuery, direction: number) => void;
  136. datasource: DataSourceApi;
  137. }