AddPanelWidget.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import React from 'react';
  2. import _ from 'lodash';
  3. import config from 'app/core/config';
  4. import { PanelModel } from '../../state/PanelModel';
  5. import { DashboardModel } from '../../state/DashboardModel';
  6. import store from 'app/core/store';
  7. import { LS_PANEL_COPY_KEY } from 'app/core/constants';
  8. import { updateLocation } from 'app/core/actions';
  9. import { store as reduxStore } from 'app/store/store';
  10. export interface Props {
  11. panel: PanelModel;
  12. dashboard: DashboardModel;
  13. }
  14. export interface State {
  15. copiedPanelPlugins: any[];
  16. }
  17. export class AddPanelWidget extends React.Component<Props, State> {
  18. constructor(props) {
  19. super(props);
  20. this.handleCloseAddPanel = this.handleCloseAddPanel.bind(this);
  21. this.state = {
  22. copiedPanelPlugins: this.getCopiedPanelPlugins(),
  23. };
  24. }
  25. getCopiedPanelPlugins() {
  26. const panels = _.chain(config.panels)
  27. .filter({ hideFromList: false })
  28. .map(item => item)
  29. .value();
  30. const copiedPanels = [];
  31. const copiedPanelJson = store.get(LS_PANEL_COPY_KEY);
  32. if (copiedPanelJson) {
  33. const copiedPanel = JSON.parse(copiedPanelJson);
  34. const pluginInfo = _.find(panels, { id: copiedPanel.type });
  35. if (pluginInfo) {
  36. const pluginCopy = _.cloneDeep(pluginInfo);
  37. pluginCopy.name = copiedPanel.title;
  38. pluginCopy.sort = -1;
  39. pluginCopy.defaults = copiedPanel;
  40. copiedPanels.push(pluginCopy);
  41. }
  42. }
  43. return _.sortBy(copiedPanels, 'sort');
  44. }
  45. handleCloseAddPanel(evt) {
  46. evt.preventDefault();
  47. this.props.dashboard.removePanel(this.props.dashboard.panels[0]);
  48. }
  49. copyButton(panel) {
  50. return (
  51. <button className="btn-inverse btn" onClick={() => this.onPasteCopiedPanel(panel)} title={panel.name}>
  52. Paste copied Panel
  53. </button>
  54. );
  55. }
  56. moveToEdit(panel, tab) {
  57. reduxStore.dispatch(
  58. updateLocation({
  59. query: {
  60. panelId: panel.id,
  61. edit: true,
  62. fullscreen: true,
  63. tab: tab,
  64. },
  65. partial: true,
  66. })
  67. );
  68. }
  69. onCreateNewPanel = (tab = 'queries') => {
  70. const dashboard = this.props.dashboard;
  71. const { gridPos } = this.props.panel;
  72. const newPanel: any = {
  73. type: 'graph',
  74. title: 'Panel Title',
  75. gridPos: { x: gridPos.x, y: gridPos.y, w: gridPos.w, h: gridPos.h },
  76. };
  77. dashboard.addPanel(newPanel);
  78. dashboard.removePanel(this.props.panel);
  79. this.moveToEdit(newPanel, tab);
  80. };
  81. onPasteCopiedPanel = panelPluginInfo => {
  82. const dashboard = this.props.dashboard;
  83. const { gridPos } = this.props.panel;
  84. const newPanel: any = {
  85. type: panelPluginInfo.id,
  86. title: 'Panel Title',
  87. gridPos: { x: gridPos.x, y: gridPos.y, w: gridPos.w, h: gridPos.h },
  88. };
  89. // apply panel template / defaults
  90. if (panelPluginInfo.defaults) {
  91. _.defaults(newPanel, panelPluginInfo.defaults);
  92. newPanel.title = panelPluginInfo.defaults.title;
  93. store.delete(LS_PANEL_COPY_KEY);
  94. }
  95. dashboard.addPanel(newPanel);
  96. dashboard.removePanel(this.props.panel);
  97. };
  98. onCreateNewRow = () => {
  99. const dashboard = this.props.dashboard;
  100. const newRow: any = {
  101. type: 'row',
  102. title: 'Row title',
  103. gridPos: { x: 0, y: 0 },
  104. };
  105. dashboard.addPanel(newRow);
  106. dashboard.removePanel(this.props.panel);
  107. };
  108. renderOptionLink = (icon, text, onClick) => {
  109. return (
  110. <div>
  111. <a href="#" onClick={onClick} className="add-panel-widget__link btn-inverse">
  112. <div className="add-panel-widget__icon">
  113. <i className={`gicon gicon-${icon}`} />
  114. </div>
  115. <span>{text}</span>
  116. </a>
  117. </div>
  118. );
  119. };
  120. render() {
  121. return (
  122. <div className="panel-container add-panel-widget-container">
  123. <div className="add-panel-widget">
  124. <div className="add-panel-widget__header grid-drag-handle">
  125. <i className="gicon gicon-add-panel" />
  126. <button className="add-panel-widget__close" onClick={this.handleCloseAddPanel}>
  127. <i className="fa fa-close" />
  128. </button>
  129. </div>
  130. <div className="add-panel-widget__btn-container">
  131. {this.renderOptionLink('queries', 'Add query', this.onCreateNewPanel)}
  132. {this.renderOptionLink('visualization', 'Choose Panel type', () => this.onCreateNewPanel('visualization'))}
  133. {this.renderOptionLink('queries', 'Convert to row', this.onCreateNewRow)}
  134. </div>
  135. </div>
  136. </div>
  137. );
  138. }
  139. }