PanelEditor.tsx 3.5 KB

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