PanelEditor.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. export class PanelEditor extends PureComponent<PanelEditorProps> {
  27. constructor(props) {
  28. super(props);
  29. }
  30. onChangeTab = (tab: PanelEditorTab) => {
  31. store.dispatch(
  32. updateLocation({
  33. query: { tab: tab.id },
  34. partial: true,
  35. })
  36. );
  37. this.forceUpdate();
  38. };
  39. renderCurrentTab(activeTab: string) {
  40. const { panel, dashboard, onTypeChanged, plugin, angularPanel } = this.props;
  41. switch (activeTab) {
  42. case 'advanced':
  43. return <GeneralTab panel={panel} />;
  44. case 'queries':
  45. return <QueriesTab panel={panel} dashboard={dashboard} />;
  46. case 'alert':
  47. return <AlertTab angularPanel={angularPanel} dashboard={dashboard} panel={panel} />;
  48. case 'visualization':
  49. return (
  50. <VisualizationTab
  51. panel={panel}
  52. dashboard={dashboard}
  53. plugin={plugin}
  54. onTypeChanged={onTypeChanged}
  55. angularPanel={angularPanel}
  56. />
  57. );
  58. default:
  59. return null;
  60. }
  61. }
  62. render() {
  63. const { plugin } = this.props;
  64. let activeTab = store.getState().location.query.tab || 'queries';
  65. const tabs: PanelEditorTab[] = [
  66. { id: 'queries', text: 'Queries' },
  67. { id: 'visualization', text: 'Visualization' },
  68. { id: 'advanced', text: 'Panel Options' },
  69. ];
  70. // handle panels that do not have queries tab
  71. if (plugin.exports.PanelCtrl) {
  72. if (!plugin.exports.PanelCtrl.prototype.onDataReceived) {
  73. // remove queries tab
  74. tabs.shift();
  75. // switch tab
  76. if (activeTab === 'queries') {
  77. activeTab = 'visualization';
  78. }
  79. }
  80. }
  81. if (config.alertingEnabled && plugin.id === 'graph') {
  82. tabs.push({
  83. id: 'alert',
  84. text: 'Alert',
  85. });
  86. }
  87. return (
  88. <div className="panel-editor-container__editor">
  89. <div className="panel-editor-tabs">
  90. {tabs.map(tab => {
  91. return <TabItem tab={tab} activeTab={activeTab} onClick={this.onChangeTab} key={tab.id} />;
  92. })}
  93. </div>
  94. <div className="panel-editor__right">{this.renderCurrentTab(activeTab)}</div>
  95. </div>
  96. );
  97. }
  98. }
  99. interface TabItemParams {
  100. tab: PanelEditorTab;
  101. activeTab: string;
  102. onClick: (tab: PanelEditorTab) => void;
  103. }
  104. function TabItem({ tab, activeTab, onClick }: TabItemParams) {
  105. const tabClasses = classNames({
  106. 'panel-editor-tabs__link': true,
  107. active: activeTab === tab.id,
  108. });
  109. return (
  110. <div className="panel-editor-tabs__item" onClick={() => onClick(tab)}>
  111. <a className={tabClasses}>
  112. <Tooltip content={`${tab.text}`} placement="auto">
  113. <i className={`gicon gicon-${tab.id}${activeTab === tab.id ? '-active' : ''}`} />
  114. </Tooltip>
  115. </a>
  116. </div>
  117. );
  118. }