forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDashboardRow.tsx
128 lines (109 loc) · 3.82 KB
/
DashboardRow.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import classNames from 'classnames';
import React from 'react';
import { Unsubscribable } from 'rxjs';
import {connect} from "react-redux"
import { selectors } from '@grafana/e2e-selectors';
import { getTemplateSrv, RefreshEvent } from '@grafana/runtime';
import { Icon } from '@grafana/ui';
import appEvents from 'app/core/app_events';
import { ShowConfirmModalEvent } from '../../../../types/events';
import { DashboardModel } from '../../state/DashboardModel';
import { PanelModel } from '../../state/PanelModel';
import { RowOptionsButton } from '../RowOptions/RowOptionsButton';
import { StoreState } from 'app/types';
export interface DashboardRowProps {
panel: PanelModel;
dashboard: DashboardModel;
isFnDashboard: boolean
}
class Component extends React.Component<DashboardRowProps> {
sub?: Unsubscribable;
componentDidMount() {
this.sub = this.props.dashboard.events.subscribe(RefreshEvent, this.onVariableUpdated);
}
componentWillUnmount() {
if (this.sub) {
this.sub.unsubscribe();
}
}
onVariableUpdated = () => {
this.forceUpdate();
};
onToggle = () => {
this.props.dashboard.toggleRow(this.props.panel);
};
onUpdate = (title: string, repeat?: string | null) => {
this.props.panel.setProperty('title', title);
this.props.panel.setProperty('repeat', repeat ?? undefined);
this.props.panel.render();
this.props.dashboard.processRepeats();
this.forceUpdate();
};
onDelete = () => {
appEvents.publish(
new ShowConfirmModalEvent({
title: 'Delete row',
text: 'Are you sure you want to remove this row and all its panels?',
altActionText: 'Delete row only',
icon: 'trash-alt',
onConfirm: () => {
this.props.dashboard.removeRow(this.props.panel, true);
},
onAltAction: () => {
this.props.dashboard.removeRow(this.props.panel, false);
},
})
);
};
render() {
const classes = classNames({
'dashboard-row': true,
'dashboard-row--collapsed': this.props.panel.collapsed,
});
const {isFnDashboard} = this.props
const title = getTemplateSrv().replace(this.props.panel.title, this.props.panel.scopedVars, 'text');
const count = this.props.panel.panels ? this.props.panel.panels.length : 0;
const panels = count === 1 ? 'panel' : 'panels';
const canEdit = this.props.dashboard.meta.canEdit === true && !isFnDashboard;
return (
<div className={classes} data-testid="dashboard-row-container">
<button
className="dashboard-row__title pointer"
type="button"
data-testid={selectors.components.DashboardRow.title(title)}
onClick={this.onToggle}
>
<Icon name={this.props.panel.collapsed ? 'angle-right' : 'angle-down'} />
{title}
<span className="dashboard-row__panel_count">
({count} {panels})
</span>
</button>
{canEdit && (
<div className="dashboard-row__actions">
<RowOptionsButton
title={this.props.panel.title}
repeat={this.props.panel.repeat}
onUpdate={this.onUpdate}
/>
<button type="button" className="pointer" onClick={this.onDelete} aria-label="Delete row">
<Icon name="trash-alt" />
</button>
</div>
)}
{this.props.panel.collapsed === true && (
<div className="dashboard-row__toggle-target" onClick={this.onToggle}>
</div>
)}
{canEdit && <div data-testid="dashboard-row-drag" className="dashboard-row__drag grid-drag-handle" />}
</div>
);
}
}
function mapStateToProps () {
return (state: StoreState) => ({
isFnDashboard: state.fnGlobalState.FNDashboard
});
}
export const DashboardRow = connect(mapStateToProps)(Component);