AddPanelPanel.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import React from 'react';
  2. import _ from 'lodash';
  3. import config from 'app/core/config';
  4. import { PanelModel } from '../panel_model';
  5. import { PanelContainer } from './PanelContainer';
  6. import ScrollBar from 'app/core/components/ScrollBar/ScrollBar';
  7. import store from 'app/core/store';
  8. import { LS_PANEL_COPY_KEY } from 'app/core/constants';
  9. export interface AddPanelPanelProps {
  10. panel: PanelModel;
  11. getPanelContainer: () => PanelContainer;
  12. }
  13. export interface AddPanelPanelState {
  14. filter: string;
  15. panelPlugins: any[];
  16. }
  17. export class AddPanelPanel extends React.Component<AddPanelPanelProps, AddPanelPanelState> {
  18. constructor(props) {
  19. super(props);
  20. this.state = {
  21. panelPlugins: this.getPanelPlugins(),
  22. filter: '',
  23. };
  24. }
  25. getPanelPlugins() {
  26. let panels = _.chain(config.panels)
  27. .filter({ hideFromList: false })
  28. .map(item => item)
  29. .value();
  30. // add special row type
  31. panels.push({ id: 'row', name: 'Row', sort: 8, info: { logos: { small: 'public/img/icn-row.svg' } } });
  32. let copiedPanelJson = store.get(LS_PANEL_COPY_KEY);
  33. if (copiedPanelJson) {
  34. let copiedPanel = JSON.parse(copiedPanelJson);
  35. let pluginInfo = _.find(panels, { id: copiedPanel.type });
  36. if (pluginInfo) {
  37. let pluginCopy = _.cloneDeep(pluginInfo);
  38. pluginCopy.name = copiedPanel.title;
  39. pluginCopy.sort = -1;
  40. pluginCopy.defaults = copiedPanel;
  41. panels.push(pluginCopy);
  42. }
  43. }
  44. // add sort by sort property
  45. return _.sortBy(panels, 'sort');
  46. }
  47. onAddPanel = panelPluginInfo => {
  48. const panelContainer = this.props.getPanelContainer();
  49. const dashboard = panelContainer.getDashboard();
  50. const { gridPos } = this.props.panel;
  51. var newPanel: any = {
  52. type: panelPluginInfo.id,
  53. title: 'Panel Title',
  54. gridPos: { x: gridPos.x, y: gridPos.y, w: gridPos.w, h: gridPos.h },
  55. };
  56. if (panelPluginInfo.id === 'row') {
  57. newPanel.title = 'Row title';
  58. newPanel.gridPos = { x: 0, y: 0 };
  59. }
  60. // apply panel template / defaults
  61. if (panelPluginInfo.defaults) {
  62. _.defaults(newPanel, panelPluginInfo.defaults);
  63. newPanel.gridPos.w = panelPluginInfo.defaults.gridPos.w;
  64. newPanel.gridPos.h = panelPluginInfo.defaults.gridPos.h;
  65. newPanel.title = panelPluginInfo.defaults.title;
  66. store.delete(LS_PANEL_COPY_KEY);
  67. }
  68. dashboard.addPanel(newPanel);
  69. dashboard.removePanel(this.props.panel);
  70. };
  71. renderPanelItem(panel, index) {
  72. console.log('render panel', index);
  73. return (
  74. <div key={index} className="add-panel__item" onClick={() => this.onAddPanel(panel)} title={panel.name}>
  75. <img className="add-panel__item-img" src={panel.info.logos.small} />
  76. <div className="add-panel__item-name">{panel.name}</div>
  77. </div>
  78. );
  79. }
  80. render() {
  81. return (
  82. <div className="panel-container">
  83. <div className="add-panel">
  84. <div className="add-panel__header">
  85. <i className="gicon gicon-add-panel" />
  86. <span className="add-panel__title">New Panel</span>
  87. <span className="add-panel__sub-title">Select a visualization</span>
  88. </div>
  89. <ScrollBar className="add-panel__items">
  90. {this.state.panelPlugins.map(this.renderPanelItem.bind(this))}
  91. </ScrollBar>
  92. </div>
  93. </div>
  94. );
  95. }
  96. }