PanelEditor.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import React, { PureComponent } from 'react';
  2. import classNames from 'classnames';
  3. import { QueriesTab } from './QueriesTab';
  4. import { VisualizationTab } from './VisualizationTab';
  5. import { store } from 'app/store/configureStore';
  6. import { updateLocation } from 'app/core/actions';
  7. import { PanelModel } from '../panel_model';
  8. import { DashboardModel } from '../dashboard_model';
  9. import { PanelPlugin } from 'app/types/plugins';
  10. interface PanelEditorProps {
  11. panel: PanelModel;
  12. dashboard: DashboardModel;
  13. plugin: PanelPlugin;
  14. onTypeChanged: (newType: PanelPlugin) => void;
  15. }
  16. interface PanelEditorTab {
  17. id: string;
  18. text: string;
  19. icon: string;
  20. }
  21. export class PanelEditor extends PureComponent<PanelEditorProps> {
  22. tabs: PanelEditorTab[];
  23. constructor(props) {
  24. super(props);
  25. this.tabs = [
  26. { id: 'queries', text: 'Queries', icon: 'fa fa-database' },
  27. { id: 'visualization', text: 'Visualization', icon: 'fa fa-line-chart' },
  28. { id: 'alert', text: 'Alert', icon: 'gicon gicon-alert' },
  29. ];
  30. }
  31. onChangeTab = (tab: PanelEditorTab) => {
  32. store.dispatch(
  33. updateLocation({
  34. query: { tab: tab.id },
  35. partial: true,
  36. })
  37. );
  38. this.forceUpdate();
  39. };
  40. onClose = () => {
  41. store.dispatch(
  42. updateLocation({
  43. query: { tab: null, fullscreen: null, edit: null },
  44. partial: true,
  45. })
  46. );
  47. };
  48. render() {
  49. const { panel, dashboard, onTypeChanged, plugin } = this.props;
  50. const { location } = store.getState();
  51. const activeTab = location.query.tab || 'queries';
  52. return (
  53. <div className="panel-editor-container__editor">
  54. <div className="panel-editor-resizer">
  55. <div className="panel-editor-resizer__handle">
  56. <div className="panel-editor-resizer__handle-dots" />
  57. </div>
  58. </div>
  59. <div className="panel-editor-tabs">
  60. <ul className="gf-tabs">
  61. {this.tabs.map(tab => {
  62. return <TabItem tab={tab} activeTab={activeTab} onClick={this.onChangeTab} key={tab.id} />;
  63. })}
  64. </ul>
  65. <button className="panel-editor-tabs__close" onClick={this.onClose}>
  66. <i className="fa fa-reply" />
  67. </button>
  68. </div>
  69. {activeTab === 'queries' && <QueriesTab panel={panel} dashboard={dashboard} />}
  70. {activeTab === 'visualization' && (
  71. <VisualizationTab panel={panel} dashboard={dashboard} plugin={plugin} onTypeChanged={onTypeChanged} />
  72. )}
  73. </div>
  74. );
  75. }
  76. }
  77. interface TabItemParams {
  78. tab: PanelEditorTab;
  79. activeTab: string;
  80. onClick: (tab: PanelEditorTab) => void;
  81. }
  82. function TabItem({ tab, activeTab, onClick }: TabItemParams) {
  83. const tabClasses = classNames({
  84. 'gf-tabs-link': true,
  85. active: activeTab === tab.id,
  86. });
  87. return (
  88. <li className="gf-tabs-item" onClick={() => onClick(tab)}>
  89. <a className={tabClasses}>
  90. <i className={tab.icon} /> {tab.text}
  91. </a>
  92. </li>
  93. );
  94. }