PanelEditor.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import React, { PureComponent } from 'react';
  2. import classNames from 'classnames';
  3. import { QueriesTab } from './QueriesTab';
  4. import { VizTypePicker } from './VizTypePicker';
  5. import { store } from 'app/store/store';
  6. import { updateLocation } from 'app/core/actions';
  7. import { PanelModel } from '../panel_model';
  8. import { DashboardModel } from '../dashboard_model';
  9. import { PanelPlugin, PluginExports } from 'app/types/plugins';
  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 PureComponent<PanelEditorProps> {
  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, panel } = this.props;
  36. if (pluginExports.PanelOptionsComponent) {
  37. const OptionsComponent = pluginExports.PanelOptionsComponent;
  38. return <OptionsComponent options={panel.getOptions()} onChange={this.onPanelOptionsChanged} />;
  39. } else {
  40. return <p>Visualization has no options</p>;
  41. }
  42. }
  43. onPanelOptionsChanged = (options: any) => {
  44. this.props.panel.updateOptions(options);
  45. this.forceUpdate();
  46. };
  47. renderVizTab() {
  48. return (
  49. <div className="viz-editor">
  50. <div className="viz-editor-col1">
  51. <VizTypePicker currentType={this.props.panel.type} onTypeChanged={this.props.onTypeChanged} />
  52. </div>
  53. <div className="viz-editor-col2">
  54. <h5 className="page-heading">Options</h5>
  55. {this.renderPanelOptions()}
  56. </div>
  57. </div>
  58. );
  59. }
  60. onChangeTab = (tab: PanelEditorTab) => {
  61. store.dispatch(
  62. updateLocation({
  63. query: { tab: tab.id },
  64. partial: true,
  65. })
  66. );
  67. this.forceUpdate();
  68. };
  69. render() {
  70. const { location } = store.getState();
  71. const activeTab = location.query.tab || 'queries';
  72. return (
  73. <div className="tabbed-view tabbed-view--new">
  74. <div className="tabbed-view-header">
  75. <ul className="gf-tabs">
  76. {this.tabs.map(tab => {
  77. return <TabItem tab={tab} activeTab={activeTab} onClick={this.onChangeTab} key={tab.id} />;
  78. })}
  79. </ul>
  80. <button className="tabbed-view-close-btn" ng-click="ctrl.exitFullscreen();">
  81. <i className="fa fa-remove" />
  82. </button>
  83. </div>
  84. <div className="tabbed-view-body">
  85. {activeTab === 'queries' && this.renderQueriesTab()}
  86. {activeTab === 'visualization' && this.renderVizTab()}
  87. </div>
  88. </div>
  89. );
  90. }
  91. }
  92. interface TabItemParams {
  93. tab: PanelEditorTab;
  94. activeTab: string;
  95. onClick: (tab: PanelEditorTab) => void;
  96. }
  97. function TabItem({ tab, activeTab, onClick }: TabItemParams) {
  98. const tabClasses = classNames({
  99. 'gf-tabs-link': true,
  100. active: activeTab === tab.id,
  101. });
  102. return (
  103. <li className="gf-tabs-item" key={tab.id}>
  104. <a className={tabClasses} onClick={() => onClick(tab)}>
  105. <i className={tab.icon} /> {tab.text}
  106. </a>
  107. </li>
  108. );
  109. }