QueryEditorRow.tsx 8.7 KB

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