Commit 3215f8ab by ijin08

replaced content in addpanelpanel with three buttons that can create new panel,…

replaced content in addpanelpanel with three buttons that can create new panel, paste copied panel, and add a new row, to paste panel one must copy one first, code is still quite rough
parent 5d4034be
import React from 'react'; import React from 'react';
import _ from 'lodash'; import _ from 'lodash';
import classNames from 'classnames';
import config from 'app/core/config'; import config from 'app/core/config';
import { PanelModel } from '../panel_model'; import { PanelModel } from '../panel_model';
import { DashboardModel } from '../dashboard_model'; import { DashboardModel } from '../dashboard_model';
...@@ -8,6 +7,8 @@ import ScrollBar from 'app/core/components/ScrollBar/ScrollBar'; ...@@ -8,6 +7,8 @@ import ScrollBar from 'app/core/components/ScrollBar/ScrollBar';
import store from 'app/core/store'; import store from 'app/core/store';
import { LS_PANEL_COPY_KEY } from 'app/core/constants'; import { LS_PANEL_COPY_KEY } from 'app/core/constants';
import Highlighter from 'react-highlight-words'; import Highlighter from 'react-highlight-words';
import { updateLocation } from 'app/core/actions';
import { store as reduxStore } from 'app/store/store';
export interface AddPanelPanelProps { export interface AddPanelPanelProps {
panel: PanelModel; panel: PanelModel;
...@@ -27,12 +28,11 @@ export class AddPanelPanel extends React.Component<AddPanelPanelProps, AddPanelP ...@@ -27,12 +28,11 @@ export class AddPanelPanel extends React.Component<AddPanelPanelProps, AddPanelP
constructor(props) { constructor(props) {
super(props); super(props);
this.handleCloseAddPanel = this.handleCloseAddPanel.bind(this); this.handleCloseAddPanel = this.handleCloseAddPanel.bind(this);
this.renderPanelItem = this.renderPanelItem.bind(this);
this.panelSizeChanged = this.panelSizeChanged.bind(this); this.panelSizeChanged = this.panelSizeChanged.bind(this);
this.state = { this.state = {
panelPlugins: this.getPanelPlugins(''), panelPlugins: this.getPanelPlugins(),
copiedPanelPlugins: this.getCopiedPanelPlugins(''), copiedPanelPlugins: this.getCopiedPanelPlugins(),
filter: '', filter: '',
tab: 'Add', tab: 'Add',
}; };
...@@ -52,27 +52,31 @@ export class AddPanelPanel extends React.Component<AddPanelPanelProps, AddPanelP ...@@ -52,27 +52,31 @@ export class AddPanelPanel extends React.Component<AddPanelPanelProps, AddPanelP
}); });
} }
getPanelPlugins(filter) { getPanelPlugins() {
let panels = _.chain(config.panels) const panelsList = _.chain(config.panels)
.filter({ hideFromList: false }) .filter({ hideFromList: false })
.map(item => item) .map(item => item)
.value(); .value();
const panels = [];
for (let i = 0; i < panelsList.length; i++) {
if (panelsList[i].id === 'graph') {
panels.push(panelsList[i]);
}
}
// add special row type // add special row type
panels.push({ id: 'row', name: 'Row', sort: 8, info: { logos: { small: 'public/img/icn-row.svg' } } }); panels.push({ id: 'row', name: 'Row', sort: 8, info: { logos: { small: 'public/img/icn-row.svg' } } });
panels = this.filterPanels(panels, filter);
// add sort by sort property // add sort by sort property
return _.sortBy(panels, 'sort'); return panels;
} }
getCopiedPanelPlugins(filter) { getCopiedPanelPlugins() {
const panels = _.chain(config.panels) const panels = _.chain(config.panels)
.filter({ hideFromList: false }) .filter({ hideFromList: false })
.map(item => item) .map(item => item)
.value(); .value();
let copiedPanels = []; const copiedPanels = [];
const copiedPanelJson = store.get(LS_PANEL_COPY_KEY); const copiedPanelJson = store.get(LS_PANEL_COPY_KEY);
if (copiedPanelJson) { if (copiedPanelJson) {
...@@ -86,13 +90,45 @@ export class AddPanelPanel extends React.Component<AddPanelPanelProps, AddPanelP ...@@ -86,13 +90,45 @@ export class AddPanelPanel extends React.Component<AddPanelPanelProps, AddPanelP
copiedPanels.push(pluginCopy); copiedPanels.push(pluginCopy);
} }
} }
return _.sortBy(copiedPanels, 'sort');
}
handleCloseAddPanel(evt) {
evt.preventDefault();
this.props.dashboard.removePanel(this.props.dashboard.panels[0]);
}
copiedPanels = this.filterPanels(copiedPanels, filter); renderText(text: string) {
const searchWords = this.state.filter.split('');
return <Highlighter highlightClassName="highlight-search-match" textToHighlight={text} searchWords={searchWords} />;
}
return _.sortBy(copiedPanels, 'sort'); noCopiedPanelPlugins() {
return <div className="add-panel__no-panels">No copied panels yet.</div>;
}
copyButton(panel) {
return (
<button className="btn-inverse btn" onClick={() => this.onPasteCopiedPanel(panel)} title={panel.name}>
Paste copied Panel
</button>
);
}
moveToEdit(panel) {
reduxStore.dispatch(
updateLocation({
query: {
panelId: panel.id,
edit: true,
fullscreen: true,
},
partial: true,
})
);
} }
onAddPanel = panelPluginInfo => { onCreateNewPanel = panelPluginInfo => {
const dashboard = this.props.dashboard; const dashboard = this.props.dashboard;
const { gridPos } = this.props.panel; const { gridPos } = this.props.panel;
...@@ -102,11 +138,6 @@ export class AddPanelPanel extends React.Component<AddPanelPanelProps, AddPanelP ...@@ -102,11 +138,6 @@ export class AddPanelPanel extends React.Component<AddPanelPanelProps, AddPanelP
gridPos: { x: gridPos.x, y: gridPos.y, w: gridPos.w, h: gridPos.h }, gridPos: { x: gridPos.x, y: gridPos.y, w: gridPos.w, h: gridPos.h },
}; };
if (panelPluginInfo.id === 'row') {
newPanel.title = 'Row title';
newPanel.gridPos = { x: 0, y: 0 };
}
// apply panel template / defaults // apply panel template / defaults
if (panelPluginInfo.defaults) { if (panelPluginInfo.defaults) {
_.defaults(newPanel, panelPluginInfo.defaults); _.defaults(newPanel, panelPluginInfo.defaults);
...@@ -117,95 +148,56 @@ export class AddPanelPanel extends React.Component<AddPanelPanelProps, AddPanelP ...@@ -117,95 +148,56 @@ export class AddPanelPanel extends React.Component<AddPanelPanelProps, AddPanelP
} }
dashboard.addPanel(newPanel); dashboard.addPanel(newPanel);
this.moveToEdit(newPanel);
dashboard.removePanel(this.props.panel); dashboard.removePanel(this.props.panel);
}; };
handleCloseAddPanel(evt) { onPasteCopiedPanel = panelPluginInfo => {
evt.preventDefault(); const dashboard = this.props.dashboard;
this.props.dashboard.removePanel(this.props.dashboard.panels[0]); const { gridPos } = this.props.panel;
}
renderText(text: string) {
const searchWords = this.state.filter.split('');
return <Highlighter highlightClassName="highlight-search-match" textToHighlight={text} searchWords={searchWords} />;
}
renderPanelItem(panel, index) { const newPanel: any = {
return ( type: panelPluginInfo.id,
<div key={index} className="add-panel__item" onClick={() => this.onAddPanel(panel)} title={panel.name}> title: 'Panel Title',
<img className="add-panel__item-img" src={panel.info.logos.small} /> gridPos: { x: gridPos.x, y: gridPos.y, w: gridPos.w, h: gridPos.h },
<div className="add-panel__item-name">{this.renderText(panel.name)}</div> };
</div>
);
}
noCopiedPanelPlugins() { // apply panel template / defaults
return <div className="add-panel__no-panels">No copied panels yet.</div>; if (panelPluginInfo.defaults) {
} _.defaults(newPanel, panelPluginInfo.defaults);
newPanel.gridPos.w = panelPluginInfo.defaults.gridPos.w;
newPanel.gridPos.h = panelPluginInfo.defaults.gridPos.h;
newPanel.title = panelPluginInfo.defaults.title;
store.delete(LS_PANEL_COPY_KEY);
}
filterChange(evt) { dashboard.addPanel(newPanel);
this.setState({
filter: evt.target.value,
panelPlugins: this.getPanelPlugins(evt.target.value),
copiedPanelPlugins: this.getCopiedPanelPlugins(evt.target.value),
});
}
filterKeyPress(evt) { dashboard.removePanel(this.props.panel);
if (evt.key === 'Enter') { };
const panel = _.head(this.state.panelPlugins);
if (panel) {
this.onAddPanel(panel);
}
}
}
filterPanels(panels, filter) { onCreateNewRow = panelPluginInfo => {
const regex = new RegExp(filter, 'i'); const dashboard = this.props.dashboard;
return panels.filter(panel => {
return regex.test(panel.name);
});
}
openCopy() { const newRow: any = {
this.setState({ type: panelPluginInfo.id,
tab: 'Copy', title: 'Row title',
filter: '', gridPos: { x: 0, y: 0 },
panelPlugins: this.getPanelPlugins(''), };
copiedPanelPlugins: this.getCopiedPanelPlugins(''),
});
}
openAdd() { dashboard.addPanel(newRow);
this.setState({ dashboard.removePanel(this.props.panel);
tab: 'Add', };
filter: '',
panelPlugins: this.getPanelPlugins(''),
copiedPanelPlugins: this.getCopiedPanelPlugins(''),
});
}
render() { render() {
const addClass = classNames({ const panel = this.state.panelPlugins[0];
'active active--panel': this.state.tab === 'Add', const row = this.state.panelPlugins[1];
'': this.state.tab === 'Copy',
});
const copyClass = classNames({
'': this.state.tab === 'Add',
'active active--panel': this.state.tab === 'Copy',
});
let panelTab; let addCopyButton;
if (this.state.tab === 'Add') { if (this.state.copiedPanelPlugins.length === 1) {
panelTab = this.state.panelPlugins.map(this.renderPanelItem); addCopyButton = this.copyButton(this.state.copiedPanelPlugins[0]);
} else if (this.state.tab === 'Copy') {
if (this.state.copiedPanelPlugins.length > 0) {
panelTab = this.state.copiedPanelPlugins.map(this.renderPanelItem);
} else {
panelTab = this.noCopiedPanelPlugins();
}
} }
return ( return (
...@@ -213,40 +205,21 @@ export class AddPanelPanel extends React.Component<AddPanelPanelProps, AddPanelP ...@@ -213,40 +205,21 @@ export class AddPanelPanel extends React.Component<AddPanelPanelProps, AddPanelP
<div className="add-panel"> <div className="add-panel">
<div className="add-panel__header"> <div className="add-panel__header">
<i className="gicon gicon-add-panel" /> <i className="gicon gicon-add-panel" />
<span className="add-panel__title">New Panel</span>
<ul className="gf-tabs">
<li className="gf-tabs-item">
<div className={'gf-tabs-link pointer ' + addClass} onClick={this.openAdd.bind(this)}>
Add
</div>
</li>
<li className="gf-tabs-item">
<div className={'gf-tabs-link pointer ' + copyClass} onClick={this.openCopy.bind(this)}>
Paste
</div>
</li>
</ul>
<button className="add-panel__close" onClick={this.handleCloseAddPanel}> <button className="add-panel__close" onClick={this.handleCloseAddPanel}>
<i className="fa fa-close" /> <i className="fa fa-close" />
</button> </button>
</div> </div>
<ScrollBar ref={element => (this.scrollbar = element)} className="add-panel__items"> <div className="add-panel-btn-container">
<div className="add-panel__searchbar"> <div className="gf-form-button-row">
<label className="gf-form gf-form--grow gf-form--has-input-icon"> <button className="btn-success btn" onClick={() => this.onCreateNewPanel(panel)} title={panel.name}>
<input Create new Panel
type="text" </button>
autoFocus {addCopyButton}
className="gf-form-input gf-form--grow" <button className="btn-inverse btn" onClick={() => this.onCreateNewRow(row)} title={row.name}>
placeholder="Panel Search Filter" Add new Row
value={this.state.filter} </button>
onChange={this.filterChange.bind(this)}
onKeyPress={this.filterKeyPress.bind(this)}
/>
<i className="gf-form-input-icon fa fa-search" />
</label>
</div> </div>
{panelTab} </div>
</ScrollBar>
</div> </div>
</div> </div>
); );
......
...@@ -4,20 +4,17 @@ ...@@ -4,20 +4,17 @@
.add-panel { .add-panel {
height: 100%; height: 100%;
//display: flex;
.baron__root { //flex-direction: column;
height: calc(100% - 43px);
}
} }
.add-panel__header { .add-panel__header {
top: 0;
position: absolute;
padding: 0 15px; padding: 0 15px;
display: flex; display: flex;
align-items: center; align-items: center;
background: $page-header-bg; width: 100%;
box-shadow: $page-header-shadow;
border-bottom: 1px solid $page-header-border-color;
.gicon { .gicon {
font-size: 30px; font-size: 30px;
margin-right: $spacer; margin-right: $spacer;
...@@ -32,18 +29,6 @@ ...@@ -32,18 +29,6 @@
margin-right: -10px; margin-right: -10px;
} }
.add-panel__title {
font-size: $font-size-md;
margin-right: $spacer*2;
}
.add-panel__sub-title {
font-style: italic;
color: $text-muted;
position: relative;
top: 1px;
}
.add-panel__items { .add-panel__items {
padding: 3px 8px; padding: 3px 8px;
display: flex; display: flex;
...@@ -97,3 +82,10 @@ ...@@ -97,3 +82,10 @@
width: 100%; width: 100%;
padding: 3px 8px; padding: 3px 8px;
} }
.add-panel-btn-container {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment