PanelEditor.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 './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 'app/core/components/Tooltip/Tooltip';
  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 'alerts':
  47. return <AlertTab angularPanel={angularPanel} />;
  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. const activeTab = store.getState().location.query.tab || 'queries';
  65. const tabs = [
  66. { id: 'queries', text: 'Queries' },
  67. { id: 'visualization', text: 'Visualization' },
  68. { id: 'advanced', text: 'Advanced' },
  69. ];
  70. if (config.alertingEnabled && plugin.id === 'graph') {
  71. tabs.push({
  72. id: 'alerts',
  73. text: 'Alerts',
  74. });
  75. }
  76. return (
  77. <div className="panel-editor-container__editor">
  78. {
  79. // <div className="panel-editor-resizer">
  80. // <div className="panel-editor-resizer__handle">
  81. // <div className="panel-editor-resizer__handle-dots" />
  82. // </div>
  83. // </div>
  84. }
  85. <div className="panel-editor-tabs">
  86. {tabs.map(tab => {
  87. return <TabItem tab={tab} activeTab={activeTab} onClick={this.onChangeTab} key={tab.id} />;
  88. })}
  89. </div>
  90. <div className="panel-editor__right">{this.renderCurrentTab(activeTab)}</div>
  91. </div>
  92. );
  93. }
  94. }
  95. interface TabItemParams {
  96. tab: PanelEditorTab;
  97. activeTab: string;
  98. onClick: (tab: PanelEditorTab) => void;
  99. }
  100. function TabItem({ tab, activeTab, onClick }: TabItemParams) {
  101. const tabClasses = classNames({
  102. 'panel-editor-tabs__link': true,
  103. active: activeTab === tab.id,
  104. });
  105. return (
  106. <div className="panel-editor-tabs__item" onClick={() => onClick(tab)}>
  107. <a className={tabClasses}>
  108. <Tooltip content={`${tab.text}`} className="popper__manager--block" placement="auto">
  109. <i className={`gicon gicon-${tab.id}${activeTab === tab.id ? '-active' : ''}`} />
  110. </Tooltip>
  111. </a>
  112. </div>
  113. );
  114. }