AddPanelWidget.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. type Location = {
  18. query: {
  19. panelId: number;
  20. edit: boolean;
  21. fullscreen: boolean;
  22. tab?: string;
  23. isVizPickerOpen?: boolean;
  24. };
  25. partial: boolean;
  26. };
  27. export class AddPanelWidget extends React.Component<Props, State> {
  28. constructor(props) {
  29. super(props);
  30. this.handleCloseAddPanel = this.handleCloseAddPanel.bind(this);
  31. this.state = {
  32. copiedPanelPlugins: this.getCopiedPanelPlugins(),
  33. };
  34. }
  35. getCopiedPanelPlugins() {
  36. const panels = _.chain(config.panels)
  37. .filter({ hideFromList: false })
  38. .map(item => item)
  39. .value();
  40. const copiedPanels = [];
  41. const copiedPanelJson = store.get(LS_PANEL_COPY_KEY);
  42. if (copiedPanelJson) {
  43. const copiedPanel = JSON.parse(copiedPanelJson);
  44. const pluginInfo = _.find(panels, { id: copiedPanel.type });
  45. if (pluginInfo) {
  46. const pluginCopy = _.cloneDeep(pluginInfo);
  47. pluginCopy.name = copiedPanel.title;
  48. pluginCopy.sort = -1;
  49. pluginCopy.defaults = copiedPanel;
  50. copiedPanels.push(pluginCopy);
  51. }
  52. }
  53. return _.sortBy(copiedPanels, 'sort');
  54. }
  55. handleCloseAddPanel(evt) {
  56. evt.preventDefault();
  57. this.props.dashboard.removePanel(this.props.dashboard.panels[0]);
  58. }
  59. copyButton(panel) {
  60. return (
  61. <button className="btn-inverse btn" onClick={() => this.onPasteCopiedPanel(panel)} title={panel.name}>
  62. Paste copied Panel
  63. </button>
  64. );
  65. }
  66. moveToEdit(location) {
  67. reduxStore.dispatch(updateLocation(location));
  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. let location: Location = {
  80. query: {
  81. panelId: newPanel.id,
  82. edit: true,
  83. fullscreen: true,
  84. },
  85. partial: true,
  86. };
  87. if (tab === 'visualization') {
  88. location = {
  89. ...location,
  90. query: {
  91. ...location.query,
  92. tab: 'visualization',
  93. isVizPickerOpen: true,
  94. },
  95. };
  96. this.moveToEdit(location);
  97. } else {
  98. this.moveToEdit(location);
  99. }
  100. };
  101. onPasteCopiedPanel = panelPluginInfo => {
  102. const dashboard = this.props.dashboard;
  103. const { gridPos } = this.props.panel;
  104. const newPanel: any = {
  105. type: panelPluginInfo.id,
  106. title: 'Panel Title',
  107. gridPos: { x: gridPos.x, y: gridPos.y, w: gridPos.w, h: gridPos.h },
  108. };
  109. // apply panel template / defaults
  110. if (panelPluginInfo.defaults) {
  111. _.defaults(newPanel, panelPluginInfo.defaults);
  112. newPanel.title = panelPluginInfo.defaults.title;
  113. store.delete(LS_PANEL_COPY_KEY);
  114. }
  115. dashboard.addPanel(newPanel);
  116. dashboard.removePanel(this.props.panel);
  117. };
  118. onCreateNewRow = () => {
  119. const dashboard = this.props.dashboard;
  120. const newRow: any = {
  121. type: 'row',
  122. title: 'Row title',
  123. gridPos: { x: 0, y: 0 },
  124. };
  125. dashboard.addPanel(newRow);
  126. dashboard.removePanel(this.props.panel);
  127. };
  128. renderOptionLink = (icon, text, onClick) => {
  129. return (
  130. <div>
  131. <a href="#" onClick={onClick} className="add-panel-widget__link btn-inverse">
  132. <div className="add-panel-widget__icon">
  133. <i className={`gicon gicon-${icon}`} />
  134. </div>
  135. <span>{text}</span>
  136. </a>
  137. </div>
  138. );
  139. };
  140. render() {
  141. return (
  142. <div className="panel-container add-panel-widget-container">
  143. <div className="add-panel-widget">
  144. <div className="add-panel-widget__header grid-drag-handle">
  145. <i className="gicon gicon-add-panel" />
  146. <button className="add-panel-widget__close" onClick={this.handleCloseAddPanel}>
  147. <i className="fa fa-close" />
  148. </button>
  149. </div>
  150. <div className="add-panel-widget__btn-container">
  151. {this.renderOptionLink('queries', 'Add query', this.onCreateNewPanel)}
  152. {this.renderOptionLink('visualization', 'Choose Panel type', () => this.onCreateNewPanel('visualization'))}
  153. {this.renderOptionLink('queries', 'Convert to row', this.onCreateNewRow)}
  154. </div>
  155. </div>
  156. </div>
  157. );
  158. }
  159. }