PanelEditor.tsx 3.2 KB

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