PanelEditor.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 '../panel_model';
  12. import { DashboardModel } from '../dashboard_model';
  13. import { PanelPlugin } from 'app/types/plugins';
  14. import { Tooltip } from '@grafana/ui';
  15. import { Themes } from '@grafana/ui/src/components/Tooltip/Popper';
  16. interface PanelEditorProps {
  17. panel: PanelModel;
  18. dashboard: DashboardModel;
  19. plugin: PanelPlugin;
  20. angularPanel?: AngularComponent;
  21. onTypeChanged: (newType: PanelPlugin) => void;
  22. }
  23. interface PanelEditorTab {
  24. id: string;
  25. text: string;
  26. }
  27. export class PanelEditor extends PureComponent<PanelEditorProps> {
  28. constructor(props) {
  29. super(props);
  30. }
  31. onChangeTab = (tab: PanelEditorTab) => {
  32. store.dispatch(
  33. updateLocation({
  34. query: { tab: tab.id },
  35. partial: true,
  36. })
  37. );
  38. this.forceUpdate();
  39. };
  40. renderCurrentTab(activeTab: string) {
  41. const { panel, dashboard, onTypeChanged, plugin, angularPanel } = this.props;
  42. switch (activeTab) {
  43. case 'advanced':
  44. return <GeneralTab panel={panel} />;
  45. case 'queries':
  46. return <QueriesTab panel={panel} dashboard={dashboard} />;
  47. case 'alert':
  48. return <AlertTab angularPanel={angularPanel} dashboard={dashboard} panel={panel} />;
  49. case 'visualization':
  50. return (
  51. <VisualizationTab
  52. panel={panel}
  53. dashboard={dashboard}
  54. plugin={plugin}
  55. onTypeChanged={onTypeChanged}
  56. angularPanel={angularPanel}
  57. />
  58. );
  59. default:
  60. return null;
  61. }
  62. }
  63. render() {
  64. const { plugin } = this.props;
  65. let activeTab = store.getState().location.query.tab || 'queries';
  66. const tabs: PanelEditorTab[] = [
  67. { id: 'queries', text: 'Queries' },
  68. { id: 'visualization', text: 'Visualization' },
  69. { id: 'advanced', text: 'Panel Options' },
  70. ];
  71. // handle panels that do not have queries tab
  72. if (plugin.exports.PanelCtrl) {
  73. if (!plugin.exports.PanelCtrl.prototype.onDataReceived) {
  74. // remove queries tab
  75. tabs.shift();
  76. // switch tab
  77. if (activeTab === 'queries') {
  78. activeTab = 'visualization';
  79. }
  80. }
  81. }
  82. if (config.alertingEnabled && plugin.id === 'graph') {
  83. tabs.push({
  84. id: 'alert',
  85. text: 'Alert',
  86. });
  87. }
  88. return (
  89. <div className="panel-editor-container__editor">
  90. {
  91. // <div className="panel-editor__close">
  92. // <i className="fa fa-arrow-left" />
  93. // </div>
  94. // <div className="panel-editor-resizer">
  95. // <div className="panel-editor-resizer__handle">
  96. // <div className="panel-editor-resizer__handle-dots" />
  97. // </div>
  98. // </div>
  99. }
  100. <div className="panel-editor-tabs">
  101. {tabs.map(tab => {
  102. return <TabItem tab={tab} activeTab={activeTab} onClick={this.onChangeTab} key={tab.id} />;
  103. })}
  104. </div>
  105. <div className="panel-editor__right">{this.renderCurrentTab(activeTab)}</div>
  106. </div>
  107. );
  108. }
  109. }
  110. interface TabItemParams {
  111. tab: PanelEditorTab;
  112. activeTab: string;
  113. onClick: (tab: PanelEditorTab) => void;
  114. }
  115. function TabItem({ tab, activeTab, onClick }: TabItemParams) {
  116. const tabClasses = classNames({
  117. 'panel-editor-tabs__link': true,
  118. active: activeTab === tab.id,
  119. });
  120. return (
  121. <div className="panel-editor-tabs__item" onClick={() => onClick(tab)}>
  122. <a className={tabClasses}>
  123. <Tooltip content={`${tab.text}`} placement="auto" theme={Themes.Brand}>
  124. <i className={`gicon gicon-${tab.id}${activeTab === tab.id ? '-active' : ''}`} />
  125. </Tooltip>
  126. </a>
  127. </div>
  128. );
  129. }