PanelEditor.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import React, { PureComponent } from 'react';
  2. import classNames from 'classnames';
  3. import { QueriesTab } from './QueriesTab';
  4. import VisualizationTab from './VisualizationTab';
  5. import { GeneralTab } from './GeneralTab';
  6. import { AlertTab } from '../../alerting/AlertTab';
  7. import config from 'app/core/config';
  8. import { store } from 'app/store/store';
  9. import { updateLocation } from 'app/core/actions';
  10. import { AngularComponent } from 'app/core/services/AngularLoader';
  11. import { PanelModel } from '../state/PanelModel';
  12. import { DashboardModel } from '../state/DashboardModel';
  13. import { PanelPlugin } from 'app/types/plugins';
  14. import { Tooltip } from '@grafana/ui';
  15. interface PanelEditorProps {
  16. panel: PanelModel;
  17. dashboard: DashboardModel;
  18. plugin: PanelPlugin;
  19. angularPanel?: AngularComponent;
  20. onTypeChanged: (newType: PanelPlugin) => void;
  21. }
  22. interface PanelEditorTab {
  23. id: string;
  24. text: string;
  25. }
  26. enum PanelEditorTabIds {
  27. Queries = 'queries',
  28. Visualization = 'visualization',
  29. Advanced = 'advanced',
  30. Alert = 'alert',
  31. }
  32. interface PanelEditorTab {
  33. id: string;
  34. text: string;
  35. }
  36. const panelEditorTabTexts = {
  37. [PanelEditorTabIds.Queries]: 'Queries',
  38. [PanelEditorTabIds.Visualization]: 'Visualization',
  39. [PanelEditorTabIds.Advanced]: 'General',
  40. [PanelEditorTabIds.Alert]: 'Alert',
  41. };
  42. const getPanelEditorTab = (tabId: PanelEditorTabIds): PanelEditorTab => {
  43. return {
  44. id: tabId,
  45. text: panelEditorTabTexts[tabId],
  46. };
  47. };
  48. export class PanelEditor extends PureComponent<PanelEditorProps> {
  49. constructor(props) {
  50. super(props);
  51. }
  52. onChangeTab = (tab: PanelEditorTab) => {
  53. store.dispatch(
  54. updateLocation({
  55. query: { tab: tab.id, openVizPicker: null },
  56. partial: true,
  57. })
  58. );
  59. this.forceUpdate();
  60. };
  61. renderCurrentTab(activeTab: string) {
  62. const { panel, dashboard, onTypeChanged, plugin, angularPanel } = this.props;
  63. switch (activeTab) {
  64. case 'advanced':
  65. return <GeneralTab panel={panel} />;
  66. case 'queries':
  67. return <QueriesTab panel={panel} dashboard={dashboard} />;
  68. case 'alert':
  69. return <AlertTab angularPanel={angularPanel} dashboard={dashboard} panel={panel} />;
  70. case 'visualization':
  71. return (
  72. <VisualizationTab
  73. panel={panel}
  74. dashboard={dashboard}
  75. plugin={plugin}
  76. onTypeChanged={onTypeChanged}
  77. angularPanel={angularPanel}
  78. />
  79. );
  80. default:
  81. return null;
  82. }
  83. }
  84. render() {
  85. const { plugin } = this.props;
  86. let activeTab: PanelEditorTabIds = store.getState().location.query.tab || PanelEditorTabIds.Queries;
  87. const tabs: PanelEditorTab[] = [
  88. getPanelEditorTab(PanelEditorTabIds.Queries),
  89. getPanelEditorTab(PanelEditorTabIds.Visualization),
  90. getPanelEditorTab(PanelEditorTabIds.Advanced),
  91. ];
  92. // handle panels that do not have queries tab
  93. if (plugin.dataFormats.length === 0) {
  94. // remove queries tab
  95. tabs.shift();
  96. // switch tab
  97. if (activeTab === PanelEditorTabIds.Queries) {
  98. activeTab = PanelEditorTabIds.Visualization;
  99. }
  100. }
  101. if (config.alertingEnabled && plugin.id === 'graph') {
  102. tabs.push(getPanelEditorTab(PanelEditorTabIds.Alert));
  103. }
  104. return (
  105. <div className="panel-editor-container__editor">
  106. <div className="panel-editor-tabs">
  107. {tabs.map(tab => {
  108. return <TabItem tab={tab} activeTab={activeTab} onClick={this.onChangeTab} key={tab.id} />;
  109. })}
  110. </div>
  111. <div className="panel-editor__right">{this.renderCurrentTab(activeTab)}</div>
  112. </div>
  113. );
  114. }
  115. }
  116. interface TabItemParams {
  117. tab: PanelEditorTab;
  118. activeTab: string;
  119. onClick: (tab: PanelEditorTab) => void;
  120. }
  121. function TabItem({ tab, activeTab, onClick }: TabItemParams) {
  122. const tabClasses = classNames({
  123. 'panel-editor-tabs__link': true,
  124. active: activeTab === tab.id,
  125. });
  126. return (
  127. <div className="panel-editor-tabs__item" onClick={() => onClick(tab)}>
  128. <a className={tabClasses}>
  129. <Tooltip content={`${tab.text}`} placement="auto">
  130. <i className={`gicon gicon-${tab.id}${activeTab === tab.id ? '-active' : ''}`} />
  131. </Tooltip>
  132. </a>
  133. </div>
  134. );
  135. }