QueryEditorRow.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 '@grafana/ui';
  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. dataSourceValue: string | null;
  19. inMixedMode: boolean;
  20. }
  21. interface State {
  22. loadedDataSourceValue: string | null | undefined;
  23. datasource: DataSourceApi | null;
  24. isCollapsed: boolean;
  25. angularScope: AngularQueryComponentScope | null;
  26. }
  27. export class QueryEditorRow extends PureComponent<Props, State> {
  28. element: HTMLElement | null = null;
  29. angularQueryEditor: AngularComponent | null = null;
  30. state: State = {
  31. datasource: null,
  32. isCollapsed: false,
  33. angularScope: null,
  34. loadedDataSourceValue: undefined,
  35. };
  36. componentDidMount() {
  37. this.loadDatasource();
  38. }
  39. getAngularQueryComponentScope(): AngularQueryComponentScope {
  40. const { panel, query } = this.props;
  41. const { datasource } = this.state;
  42. return {
  43. datasource: datasource,
  44. target: query,
  45. panel: panel,
  46. refresh: () => panel.refresh(),
  47. render: () => panel.render(),
  48. events: panel.events,
  49. };
  50. }
  51. async loadDatasource() {
  52. const { query, panel } = this.props;
  53. const dataSourceSrv = getDatasourceSrv();
  54. const datasource = await dataSourceSrv.get(query.datasource || panel.datasource);
  55. this.setState({ datasource, loadedDataSourceValue: this.props.dataSourceValue });
  56. }
  57. componentDidUpdate() {
  58. const { loadedDataSourceValue } = this.state;
  59. // check if we need to load another datasource
  60. if (loadedDataSourceValue !== this.props.dataSourceValue) {
  61. if (this.angularQueryEditor) {
  62. this.angularQueryEditor.destroy();
  63. this.angularQueryEditor = null;
  64. }
  65. this.loadDatasource();
  66. return;
  67. }
  68. if (!this.element || this.angularQueryEditor) {
  69. return;
  70. }
  71. const loader = getAngularLoader();
  72. const template = '<plugin-component type="query-ctrl" />';
  73. const scopeProps = { ctrl: this.getAngularQueryComponentScope() };
  74. this.angularQueryEditor = loader.load(this.element, scopeProps, template);
  75. // give angular time to compile
  76. setTimeout(() => {
  77. this.setState({ angularScope: scopeProps.ctrl });
  78. }, 10);
  79. }
  80. componentWillUnmount() {
  81. if (this.angularQueryEditor) {
  82. this.angularQueryEditor.destroy();
  83. }
  84. }
  85. onToggleCollapse = () => {
  86. this.setState({ isCollapsed: !this.state.isCollapsed });
  87. };
  88. onQueryChange = (query: DataQuery) => {
  89. Object.assign(this.props.query, query);
  90. this.onExecuteQuery();
  91. };
  92. onExecuteQuery = () => {
  93. this.props.panel.refresh();
  94. };
  95. renderPluginEditor() {
  96. const { query } = this.props;
  97. const { datasource } = this.state;
  98. if (datasource.pluginExports.QueryCtrl) {
  99. return <div ref={element => (this.element = element)} />;
  100. }
  101. if (datasource.pluginExports.QueryEditor) {
  102. const QueryEditor = datasource.pluginExports.QueryEditor;
  103. return (
  104. <QueryEditor
  105. query={query}
  106. datasource={datasource}
  107. onQueryChange={this.onQueryChange}
  108. onExecuteQuery={this.onExecuteQuery}
  109. />
  110. );
  111. }
  112. return <div>Data source plugin does not export any Query Editor component</div>;
  113. }
  114. onToggleEditMode = () => {
  115. const { angularScope } = this.state;
  116. if (angularScope && angularScope.toggleEditorMode) {
  117. angularScope.toggleEditorMode();
  118. this.angularQueryEditor.digest();
  119. }
  120. if (this.state.isCollapsed) {
  121. this.setState({ isCollapsed: false });
  122. }
  123. };
  124. get hasTextEditMode() {
  125. const { angularScope } = this.state;
  126. return angularScope && angularScope.toggleEditorMode;
  127. }
  128. onRemoveQuery = () => {
  129. this.props.onRemoveQuery(this.props.query);
  130. };
  131. onCopyQuery = () => {
  132. const copy = _.cloneDeep(this.props.query);
  133. this.props.onAddQuery(copy);
  134. };
  135. onDisableQuery = () => {
  136. this.props.query.hide = !this.props.query.hide;
  137. this.forceUpdate();
  138. };
  139. renderCollapsedText(): string | null {
  140. const { angularScope } = this.state;
  141. if (angularScope && angularScope.getCollapsedText) {
  142. return angularScope.getCollapsedText();
  143. }
  144. return null;
  145. }
  146. render() {
  147. const { query, inMixedMode } = this.props;
  148. const { datasource, isCollapsed } = this.state;
  149. const isDisabled = query.hide;
  150. const bodyClasses = classNames('query-editor-row__body gf-form-query', {
  151. 'query-editor-row__body--collapsed': isCollapsed,
  152. });
  153. const rowClasses = classNames('query-editor-row', {
  154. 'query-editor-row--disabled': isDisabled,
  155. 'gf-form-disabled': isDisabled,
  156. });
  157. if (!datasource) {
  158. return null;
  159. }
  160. return (
  161. <div className={rowClasses}>
  162. <div className="query-editor-row__header">
  163. <div className="query-editor-row__ref-id" onClick={this.onToggleCollapse}>
  164. {isCollapsed && <i className="fa fa-caret-right" />}
  165. {!isCollapsed && <i className="fa fa-caret-down" />}
  166. <span>{query.refId}</span>
  167. {inMixedMode && <em className="query-editor-row__context-info"> ({datasource.name})</em>}
  168. {isDisabled && <em className="query-editor-row__context-info"> Disabled</em>}
  169. </div>
  170. <div className="query-editor-row__collapsed-text" onClick={this.onToggleEditMode}>
  171. {isCollapsed && <div>{this.renderCollapsedText()}</div>}
  172. </div>
  173. <div className="query-editor-row__actions">
  174. {this.hasTextEditMode && (
  175. <button
  176. className="query-editor-row__action"
  177. onClick={this.onToggleEditMode}
  178. title="Toggle text edit mode"
  179. >
  180. <i className="fa fa-fw fa-pencil" />
  181. </button>
  182. )}
  183. <button className="query-editor-row__action" onClick={() => this.props.onMoveQuery(query, 1)}>
  184. <i className="fa fa-fw fa-arrow-down" />
  185. </button>
  186. <button className="query-editor-row__action" onClick={() => this.props.onMoveQuery(query, -1)}>
  187. <i className="fa fa-fw fa-arrow-up" />
  188. </button>
  189. <button className="query-editor-row__action" onClick={this.onCopyQuery} title="Duplicate query">
  190. <i className="fa fa-fw fa-copy" />
  191. </button>
  192. <button className="query-editor-row__action" onClick={this.onDisableQuery} title="Disable/enable query">
  193. {isDisabled && <i className="fa fa-fw fa-eye-slash" />}
  194. {!isDisabled && <i className="fa fa-fw fa-eye" />}
  195. </button>
  196. <button className="query-editor-row__action" onClick={this.onRemoveQuery} title="Remove query">
  197. <i className="fa fa-fw fa-trash" />
  198. </button>
  199. </div>
  200. </div>
  201. <div className={bodyClasses}>{this.renderPluginEditor()}</div>
  202. </div>
  203. );
  204. }
  205. }
  206. export interface AngularQueryComponentScope {
  207. target: DataQuery;
  208. panel: PanelModel;
  209. events: Emitter;
  210. refresh: () => void;
  211. render: () => void;
  212. datasource: DataSourceApi;
  213. toggleEditorMode?: () => void;
  214. getCollapsedText?: () => string;
  215. }