PanelEditor.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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__close">
  80. // <i className="fa fa-arrow-left" />
  81. // </div>
  82. // <div className="panel-editor-resizer">
  83. // <div className="panel-editor-resizer__handle">
  84. // <div className="panel-editor-resizer__handle-dots" />
  85. // </div>
  86. // </div>
  87. }
  88. <div className="panel-editor-tabs">
  89. {tabs.map(tab => {
  90. return <TabItem tab={tab} activeTab={activeTab} onClick={this.onChangeTab} key={tab.id} />;
  91. })}
  92. </div>
  93. <div className="panel-editor__right">{this.renderCurrentTab(activeTab)}</div>
  94. </div>
  95. );
  96. }
  97. }
  98. interface TabItemParams {
  99. tab: PanelEditorTab;
  100. activeTab: string;
  101. onClick: (tab: PanelEditorTab) => void;
  102. }
  103. function TabItem({ tab, activeTab, onClick }: TabItemParams) {
  104. const tabClasses = classNames({
  105. 'panel-editor-tabs__link': true,
  106. active: activeTab === tab.id,
  107. });
  108. return (
  109. <div className="panel-editor-tabs__item" onClick={() => onClick(tab)}>
  110. <a className={tabClasses}>
  111. <Tooltip content={`${tab.text}`} className="popper__manager--block" placement="auto">
  112. <i className={`gicon gicon-${tab.id}${activeTab === tab.id ? '-active' : ''}`} />
  113. </Tooltip>
  114. </a>
  115. </div>
  116. );
  117. }