-
Notifications
You must be signed in to change notification settings - Fork 356
/
Copy pathdashboard_toolbar.jsx
131 lines (115 loc) · 3.32 KB
/
dashboard_toolbar.jsx
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
129
130
131
/* eslint-disable react/destructuring-assignment */
import React from 'react';
import PropTypes from 'prop-types';
import { OverflowMenu, OverflowMenuItem } from 'carbon-components-react';
import { ChevronDown20 } from '@carbon/icons-react';
const addClick = (item) =>
window.miqJqueryRequest(`/dashboard/widget_add?widget=${item.id}`, { beforeSend: true, complete: true });
const resetClick = () => {
/* eslint no-alert: off */
if (window.confirm(__("Are you sure you want to reset this Dashboard's Widgets to the defaults?"))) {
window.miqJqueryRequest('/dashboard/reset_widgets', { beforeSend: true });
}
};
const resetButton = () => (
<button
type="button"
className="btn btn-default refresh_button"
title={__('Reset Dashboard Widgets to the defaults')}
onClick={resetClick}
>
<i className="fa fa-reply fa-lg" />
</button>
);
const closeFunc = () => {
document.getElementById('dropdown-custom-2').focus();
};
const MenuIcon = (props) => (
<div>
{ props.image && <i className={props.image} /> }
<span>{ props.text }</span>
</div>
);
MenuIcon.propTypes = {
image: PropTypes.string,
text: PropTypes.string,
};
MenuIcon.defaultProps = {
image: undefined,
text: undefined,
};
const addMenu = (items, locked) => {
const title = locked
? __('Cannot add a Widget, this Dashboard has been locked by the Administrator')
: __('Add widget');
const buttonName = __('Add widget');
return (
<OverflowMenu
ariaLabel={title}
id="dropdown-custom-2"
floatingmenu="true"
disabled={locked}
title={title}
onClose={closeFunc}
menuOptionsClass="scrollable-options"
renderIcon={() => (
<div className="toolbar-dashboard">
<span>{buttonName}</span>
<ChevronDown20 />
</div>
)}
>
{ items.map((item) => (
<OverflowMenuItem
className="scrollable-menu"
key={item.id}
aria-label={item.title}
hasDivider={(item.type === 'separator')}
disabled={(item.type === 'separator')}
itemText={<MenuIcon image={item.image} text={item.text} />}
title={item.title ? item.title : null}
requireTitle
onClick={(item.type !== 'separator') ? (() => addClick(item)) : null}
/>
)) }
</OverflowMenu>
);
};
const renderDisabled = () => (
<div className="btn-group.dropdown">
<button
type="button"
className="disabled btn btn-default dropdown-toggle"
title={__('No Widgets available to add')}
>
<i className="fa fa-plus fa-lg" />
<span className="caret" />
</button>
</div>
);
const DashboardToolbar = (props) => {
const {
items, allowAdd, allowReset, locked,
} = props;
const renderContent = () => (
<>
{ allowAdd && addMenu(items, locked) }
{ allowReset && resetButton() }
</>
);
return (
<div className="toolbar-pf-actions miq-toolbar-actions">
<div className="miq-toolbar-group form-group">
{ items.length === 0 ? renderDisabled() : renderContent() }
</div>
</div>
);
};
DashboardToolbar.propTypes = {
allowAdd: PropTypes.bool.isRequired,
allowReset: PropTypes.bool.isRequired,
items: PropTypes.arrayOf(PropTypes.any).isRequired,
locked: PropTypes.bool.isRequired,
};
export default DashboardToolbar;