QueryEditorRow.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. import classNames from 'classnames';
  4. import _ from 'lodash';
  5. // Utils & Services
  6. import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
  7. import { AngularComponent, getAngularLoader } from 'app/core/services/AngularLoader';
  8. import { Emitter } from 'app/core/utils/emitter';
  9. // Types
  10. import { PanelModel } from '../panel_model';
  11. import { DataQuery, DataSourceApi } from 'app/types/series';
  12. interface Props {
  13. panel: PanelModel;
  14. query: DataQuery;
  15. onAddQuery: (query?: DataQuery) => void;
  16. onRemoveQuery: (query: DataQuery) => void;
  17. onMoveQuery: (query: DataQuery, direction: number) => void;
  18. datasourceName: string | null;
  19. inMixedMode: boolean;
  20. }
  21. interface State {
  22. datasource: DataSourceApi | null;
  23. isCollapsed: boolean;
  24. angularScope: AngularQueryComponentScope | null;
  25. }
  26. export class QueryEditorRow extends PureComponent<Props, State> {
  27. element: HTMLElement | null = null;
  28. angularQueryEditor: AngularComponent | null = null;
  29. state: State = {
  30. datasource: null,
  31. isCollapsed: false,
  32. angularScope: null,
  33. };
  34. componentDidMount() {
  35. this.loadDatasource();
  36. }
  37. getAngularQueryComponentScope(): AngularQueryComponentScope {
  38. const { panel, onAddQuery, onMoveQuery, onRemoveQuery, query } = this.props;
  39. const { datasource } = this.state;
  40. return {
  41. datasource: datasource,
  42. target: query,
  43. panel: panel,
  44. refresh: () => panel.refresh(),
  45. render: () => panel.render,
  46. addQuery: onAddQuery,
  47. moveQuery: onMoveQuery,
  48. removeQuery: onRemoveQuery,
  49. events: panel.events,
  50. };
  51. }
  52. async loadDatasource() {
  53. const { query, panel } = this.props;
  54. const dataSourceSrv = getDatasourceSrv();
  55. const datasource = await dataSourceSrv.get(query.datasource || panel.datasource);
  56. this.setState({ datasource });
  57. }
  58. componentDidUpdate() {
  59. const { datasource } = this.state;
  60. // check if we need to load another datasource
  61. if (datasource && datasource.name !== this.props.datasourceName) {
  62. if (this.angularQueryEditor) {
  63. this.angularQueryEditor.destroy();
  64. this.angularQueryEditor = null;
  65. }
  66. this.loadDatasource();
  67. return;
  68. }
  69. if (!this.element || this.angularQueryEditor) {
  70. return;
  71. }
  72. const loader = getAngularLoader();
  73. const template = '<plugin-component type="query-ctrl" />';
  74. const scopeProps = { ctrl: this.getAngularQueryComponentScope() };
  75. this.angularQueryEditor = loader.load(this.element, scopeProps, template);
  76. // give angular time to compile
  77. setTimeout(() => {
  78. this.setState({ angularScope: scopeProps.ctrl });
  79. }, 10);
  80. }
  81. componentWillUnmount() {
  82. if (this.angularQueryEditor) {
  83. this.angularQueryEditor.destroy();
  84. }
  85. }
  86. onToggleCollapse = () => {
  87. this.setState({ isCollapsed: !this.state.isCollapsed });
  88. };
  89. renderPluginEditor() {
  90. const { datasource } = this.state;
  91. if (datasource.pluginExports.QueryCtrl) {
  92. }
  93. return <div ref={element => (this.element = element)} />;
  94. if (datasource.pluginExports.QueryEditor) {
  95. const QueryEditor = datasource.pluginExports.QueryEditor;
  96. return <QueryEditor />;
  97. }
  98. return <div>Data source plugin does not export any Query Editor component</div>;
  99. }
  100. onToggleEditMode = () => {
  101. const { angularScope } = this.state;
  102. if (angularScope && angularScope.toggleEditorMode) {
  103. angularScope.toggleEditorMode();
  104. this.angularQueryEditor.digest();
  105. }
  106. }
  107. get hasTextEditMode() {
  108. const { angularScope } = this.state;
  109. return angularScope && angularScope.toggleEditorMode;
  110. }
  111. onRemoveQuery = () => {
  112. this.props.onRemoveQuery(this.props.query);
  113. };
  114. onCopyQuery = () => {
  115. const copy = _.cloneDeep(this.props.query);
  116. this.props.onAddQuery(copy);
  117. };
  118. onDisableQuery = () => {
  119. this.props.query.hide = !this.props.query.hide;
  120. this.forceUpdate();
  121. };
  122. render() {
  123. const { query, datasourceName, inMixedMode } = this.props;
  124. const { datasource, isCollapsed } = this.state;
  125. const isDisabled = query.hide;
  126. const bodyClasses = classNames('query-editor-row__body gf-form-query', {
  127. 'query-editor-row__body--collapsed': isCollapsed,
  128. });
  129. const rowClasses = classNames('query-editor-row', {
  130. 'query-editor-row--disabled': isDisabled,
  131. 'gf-form-disabled': isDisabled,
  132. });
  133. if (!datasource) {
  134. return null;
  135. }
  136. return (
  137. <div className={rowClasses}>
  138. <div className="query-editor-row__header">
  139. <div className="query-editor-row__ref-id" onClick={this.onToggleCollapse}>
  140. {isCollapsed && <i className="fa fa-caret-right" />}
  141. {!isCollapsed && <i className="fa fa-caret-down" />}
  142. <span>{query.refId}</span>
  143. {inMixedMode && <em className="query-editor-row__context-info"> ({datasourceName})</em>}
  144. {isDisabled && <em className="query-editor-row__context-info"> Disabled</em>}
  145. </div>
  146. <div className="query-editor-row__actions">
  147. {this.hasTextEditMode && (
  148. <button className="query-editor-row__action" onClick={this.onToggleEditMode} title="Toggle text edit mode">
  149. <i className="fa fa-fw fa-pencil" />
  150. </button>
  151. )}
  152. <button className="query-editor-row__action" onClick={() => this.props.onMoveQuery(query, 1)}>
  153. <i className="fa fa-fw fa-arrow-down" />
  154. </button>
  155. <button className="query-editor-row__action" onClick={() => this.props.onMoveQuery(query, -1)}>
  156. <i className="fa fa-fw fa-arrow-up" />
  157. </button>
  158. <button className="query-editor-row__action" onClick={this.onCopyQuery} title="Duplicate query">
  159. <i className="fa fa-fw fa-copy" />
  160. </button>
  161. <button className="query-editor-row__action" onClick={this.onDisableQuery} title="Disable/enable query">
  162. {isDisabled && <i className="fa fa-fw fa-eye-slash" />}
  163. {!isDisabled && <i className="fa fa-fw fa-eye" />}
  164. </button>
  165. <button className="query-editor-row__action" onClick={this.onRemoveQuery} title="Remove query">
  166. <i className="fa fa-fw fa-trash" />
  167. </button>
  168. </div>
  169. </div>
  170. <div className={bodyClasses}>{this.renderPluginEditor()}</div>
  171. </div>
  172. );
  173. }
  174. }
  175. export interface AngularQueryComponentScope {
  176. target: DataQuery;
  177. panel: PanelModel;
  178. events: Emitter;
  179. refresh: () => void;
  180. render: () => void;
  181. removeQuery: (query: DataQuery) => void;
  182. addQuery: (query?: DataQuery) => void;
  183. moveQuery: (query: DataQuery, direction: number) => void;
  184. datasource: DataSourceApi;
  185. toggleEditorMode?: () => void;
  186. }