PanelEditor.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 = [{ id: 'queries', text: 'Queries' }, { id: 'visualization', text: 'Visualization' }];
  65. if (config.alertingEnabled && plugin.id === 'graph') {
  66. tabs.push({
  67. id: 'alert',
  68. text: 'Alert',
  69. });
  70. }
  71. return (
  72. <div className="panel-editor-container__editor">
  73. {
  74. // <div className="panel-editor-resizer">
  75. // <div className="panel-editor-resizer__handle">
  76. // <div className="panel-editor-resizer__handle-dots" />
  77. // </div>
  78. // </div>
  79. }
  80. <div className="panel-editor-tabs">
  81. {tabs.map(tab => {
  82. return <TabItem tab={tab} activeTab={activeTab} onClick={this.onChangeTab} key={tab.id} />;
  83. })}
  84. </div>
  85. <div className="panel-editor__right">{this.renderCurrentTab(activeTab)}</div>
  86. </div>
  87. );
  88. }
  89. }
  90. interface TabItemParams {
  91. tab: PanelEditorTab;
  92. activeTab: string;
  93. onClick: (tab: PanelEditorTab) => void;
  94. }
  95. function TabItem({ tab, activeTab, onClick }: TabItemParams) {
  96. const tabClasses = classNames({
  97. 'panel-editor-tabs__link': true,
  98. active: activeTab === tab.id,
  99. });
  100. return (
  101. <div className="panel-editor-tabs__item" onClick={() => onClick(tab)}>
  102. <a className={tabClasses}>
  103. <img src={`public/img/panel-tabs/${tab.id}${activeTab === tab.id ? '-selected' : ''}.svg`} />
  104. </a>
  105. </div>
  106. );
  107. }