PanelEditor.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. interface PanelEditorProps {
  15. panel: PanelModel;
  16. dashboard: DashboardModel;
  17. plugin: PanelPlugin;
  18. angularPanel?: AngularComponent;
  19. onTypeChanged: (newType: PanelPlugin) => void;
  20. }
  21. interface PanelEditorTab {
  22. id: string;
  23. text: string;
  24. }
  25. export class PanelEditor extends PureComponent<PanelEditorProps> {
  26. constructor(props) {
  27. super(props);
  28. }
  29. onChangeTab = (tab: PanelEditorTab) => {
  30. store.dispatch(
  31. updateLocation({
  32. query: { tab: tab.id },
  33. partial: true,
  34. })
  35. );
  36. this.forceUpdate();
  37. };
  38. renderCurrentTab(activeTab: string) {
  39. const { panel, dashboard, onTypeChanged, plugin, angularPanel } = this.props;
  40. switch (activeTab) {
  41. case 'general':
  42. return <GeneralTab panel={panel} />;
  43. case 'queries':
  44. return <QueriesTab panel={panel} dashboard={dashboard} />;
  45. case 'alert':
  46. return <AlertTab angularPanel={angularPanel} />;
  47. case 'visualization':
  48. return (
  49. <VisualizationTab
  50. panel={panel}
  51. dashboard={dashboard}
  52. plugin={plugin}
  53. onTypeChanged={onTypeChanged}
  54. angularPanel={angularPanel}
  55. />
  56. );
  57. default:
  58. return null;
  59. }
  60. }
  61. render() {
  62. const { plugin } = this.props;
  63. const activeTab = store.getState().location.query.tab || 'queries';
  64. const tabs = [
  65. { id: 'queries', text: 'Queries' },
  66. { id: 'visualization', text: 'Visualization' },
  67. ];
  68. if (config.alertingEnabled && plugin.id === 'graph') {
  69. tabs.push({
  70. id: 'alert',
  71. text: 'Alert',
  72. });
  73. }
  74. return (
  75. <div className="panel-editor-container__editor">
  76. <div className="panel-editor-resizer">
  77. <div className="panel-editor-resizer__handle">
  78. <div className="panel-editor-resizer__handle-dots" />
  79. </div>
  80. </div>
  81. <div className="panel-editor-tabs">
  82. {tabs.map(tab => {
  83. return <TabItem tab={tab} activeTab={activeTab} onClick={this.onChangeTab} key={tab.id} />;
  84. })}
  85. </div>
  86. {this.renderCurrentTab(activeTab)}
  87. </div>
  88. );
  89. }
  90. }
  91. interface TabItemParams {
  92. tab: PanelEditorTab;
  93. activeTab: string;
  94. onClick: (tab: PanelEditorTab) => void;
  95. }
  96. function TabItem({ tab, activeTab, onClick }: TabItemParams) {
  97. const tabClasses = classNames({
  98. 'panel-editor-tabs__link': true,
  99. active: activeTab === tab.id,
  100. });
  101. return (
  102. <div className="panel-editor-tabs__item" onClick={() => onClick(tab)}>
  103. <a className={tabClasses}>
  104. <img src={`public/img/panel-tabs/${tab.id}${activeTab === tab.id ? '-selected' : ''}.svg`} />
  105. </a>
  106. </div>
  107. );
  108. }