QueryEditorRow.tsx 7.4 KB

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