PanelEditor.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import React from 'react';
  2. import classNames from 'classnames';
  3. import { PanelModel } from '../panel_model';
  4. import { DashboardModel } from '../dashboard_model';
  5. import { store } from 'app/store/configureStore';
  6. import { QueriesTab } from './QueriesTab';
  7. import { PanelPlugin, PluginExports } from 'app/types/plugins';
  8. import { VizTypePicker } from './VizTypePicker';
  9. import { updateLocation } from 'app/core/actions';
  10. interface PanelEditorProps {
  11. panel: PanelModel;
  12. dashboard: DashboardModel;
  13. panelType: string;
  14. pluginExports: PluginExports;
  15. onTypeChanged: (newType: PanelPlugin) => void;
  16. }
  17. interface PanelEditorTab {
  18. id: string;
  19. text: string;
  20. icon: string;
  21. }
  22. export class PanelEditor extends React.Component<PanelEditorProps, any> {
  23. tabs: PanelEditorTab[];
  24. constructor(props) {
  25. super(props);
  26. this.tabs = [
  27. { id: 'queries', text: 'Queries', icon: 'fa fa-database' },
  28. { id: 'visualization', text: 'Visualization', icon: 'fa fa-line-chart' },
  29. ];
  30. }
  31. renderQueriesTab() {
  32. return <QueriesTab panel={this.props.panel} dashboard={this.props.dashboard} />;
  33. }
  34. renderPanelOptions() {
  35. const { pluginExports } = this.props;
  36. if (pluginExports.PanelOptions) {
  37. const PanelOptions = pluginExports.PanelOptions;
  38. return <PanelOptions />;
  39. } else {
  40. return <p>Visualization has no options</p>;
  41. }
  42. }
  43. renderVizTab() {
  44. return (
  45. <div className="viz-editor">
  46. <div className="viz-editor-col1">
  47. <VizTypePicker currentType={this.props.panel.type} onTypeChanged={this.props.onTypeChanged} />
  48. </div>
  49. <div className="viz-editor-col2">
  50. <h5 className="page-heading">Options</h5>
  51. {this.renderPanelOptions()}
  52. </div>
  53. </div>
  54. );
  55. }
  56. onChangeTab = (tab: PanelEditorTab) => {
  57. store.dispatch(
  58. updateLocation({
  59. query: { tab: tab.id },
  60. partial: true,
  61. })
  62. );
  63. };
  64. render() {
  65. const { location } = store.getState();
  66. const activeTab = location.query.tab || 'queries';
  67. return (
  68. <div className="tabbed-view tabbed-view--new">
  69. <div className="tabbed-view-header">
  70. <ul className="gf-tabs">
  71. {this.tabs.map(tab => {
  72. return <TabItem tab={tab} activeTab={activeTab} onClick={this.onChangeTab} key={tab.id} />;
  73. })}
  74. </ul>
  75. <button className="tabbed-view-close-btn" ng-click="ctrl.exitFullscreen();">
  76. <i className="fa fa-remove" />
  77. </button>
  78. </div>
  79. <div className="tabbed-view-body">
  80. {activeTab === 'queries' && this.renderQueriesTab()}
  81. {activeTab === 'visualization' && this.renderVizTab()}
  82. </div>
  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" key={tab.id}>
  99. <a className={tabClasses} onClick={() => onClick(tab)}>
  100. <i className={tab.icon} /> {tab.text}
  101. </a>
  102. </li>
  103. );
  104. }