AddPanelWidget.tsx 5.4 KB

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