PanelEditor.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/stores/store';
  6. import { observer } from 'mobx-react';
  7. import { QueriesTab } from './QueriesTab';
  8. import { PanelPlugin, PluginExports } from 'app/types/plugins';
  9. import { VizTypePicker } from './VizTypePicker';
  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. @observer
  23. export class PanelEditor extends React.Component<PanelEditorProps, any> {
  24. tabs: PanelEditorTab[];
  25. constructor(props) {
  26. super(props);
  27. this.tabs = [
  28. { id: 'queries', text: 'Queries', icon: 'fa fa-database' },
  29. { id: 'visualization', text: 'Visualization', icon: 'fa fa-line-chart' },
  30. ];
  31. }
  32. renderQueriesTab() {
  33. return <QueriesTab panel={this.props.panel} dashboard={this.props.dashboard} />;
  34. }
  35. renderPanelOptions() {
  36. const { pluginExports } = this.props;
  37. if (pluginExports.PanelOptions) {
  38. const PanelOptions = pluginExports.PanelOptions;
  39. return <PanelOptions />;
  40. } else {
  41. return <p>Visualization has no options</p>;
  42. }
  43. }
  44. renderVizTab() {
  45. return (
  46. <div className="viz-editor">
  47. <div className="viz-editor-col1">
  48. <VizTypePicker currentType={this.props.panel.type} onTypeChanged={this.props.onTypeChanged} />
  49. </div>
  50. <div className="viz-editor-col2">
  51. <h5 className="page-heading">Options</h5>
  52. {this.renderPanelOptions()}
  53. </div>
  54. </div>
  55. );
  56. }
  57. onChangeTab = (tab: PanelEditorTab) => {
  58. store.view.updateQuery({ tab: tab.id }, false);
  59. };
  60. render() {
  61. const activeTab: string = store.view.query.get('tab') || 'queries';
  62. return (
  63. <div className="tabbed-view tabbed-view--new">
  64. <div className="tabbed-view-header">
  65. <ul className="gf-tabs">
  66. {this.tabs.map(tab => {
  67. return <TabItem tab={tab} activeTab={activeTab} onClick={this.onChangeTab} key={tab.id} />;
  68. })}
  69. </ul>
  70. <button className="tabbed-view-close-btn" ng-click="ctrl.exitFullscreen();">
  71. <i className="fa fa-remove" />
  72. </button>
  73. </div>
  74. <div className="tabbed-view-body">
  75. {activeTab === 'queries' && this.renderQueriesTab()}
  76. {activeTab === 'visualization' && this.renderVizTab()}
  77. </div>
  78. </div>
  79. );
  80. }
  81. }
  82. interface TabItemParams {
  83. tab: PanelEditorTab;
  84. activeTab: string;
  85. onClick: (tab: PanelEditorTab) => void;
  86. }
  87. function TabItem({ tab, activeTab, onClick }: TabItemParams) {
  88. const tabClasses = classNames({
  89. 'gf-tabs-link': true,
  90. active: activeTab === tab.id,
  91. });
  92. return (
  93. <li className="gf-tabs-item" key={tab.id}>
  94. <a className={tabClasses} onClick={() => onClick(tab)}>
  95. <i className={tab.icon} /> {tab.text}
  96. </a>
  97. </li>
  98. );
  99. }