-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
cockpit-components-dialog.jsx
331 lines (310 loc) · 12.3 KB
/
cockpit-components-dialog.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* This file is part of Cockpit.
*
* Copyright (C) 2016 Red Hat, Inc.
*
* Cockpit is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Cockpit is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
*/
var cockpit = require("cockpit");
var React = require("react");
var _ = cockpit.gettext;
require("page.css");
/*
* React template for a Cockpit dialog footer
* It can display an error, wait for an action to complete,
* has a 'Cancel' button and an action button (defaults to 'OK')
* Expected props:
* - cancel_clicked optional
* Callback called when the dialog is canceled
* - cancel_style
* css class used for the cancel button, defaults to 'cancel'
* - cancel_caption optional, defaults to 'Cancel'
* - list of actions, each an object with:
* - clicked
* Callback function that is expected to return a promise.
* parameter: callback to set the progress text (will be displayed next to spinner)
* - caption optional, defaults to 'Ok'
* - disabled optional, defaults to false
* - style defaults to 'default', other options: 'primary', 'danger'
* - static_error optional, always show this error
* - dialog_done optional, callback when dialog is finished (param true if success, false on cancel)
*/
var DialogFooter = React.createClass({
propTypes: {
cancel_clicked: React.PropTypes.func,
cancel_caption: React.PropTypes.string,
actions: React.PropTypes.array,
static_error: React.PropTypes.string,
dialog_done: React.PropTypes.func,
},
getInitialState: function() {
return {
action_in_progress: false,
action_in_progress_promise: null,
action_progress_message: '',
action_canceled: false,
error_message: null,
};
},
keyUpHandler: function(e) {
if (e.keyCode == 27) {
this.cancel_click();
e.stopPropagation();
}
},
componentDidMount: function() {
document.body.classList.add("modal-in");
document.addEventListener('keyup', this.keyUpHandler.bind(this));
},
componentWillUnmount: function() {
document.body.classList.remove("modal-in");
document.removeEventListener('keyup', this.keyUpHandler.bind(this));
},
update_progress: function(msg) {
this.setState({ action_progress_message: msg });
},
action_click: function(handler, e) {
// only consider clicks with the primary button
if (e && e.button !== 0)
return;
var self = this;
this.setState({
error_message: null,
action_progress_message: '',
action_in_progress: true,
action_canceled: false,
});
this.state.action_in_progress_promise = handler(this.update_progress.bind(this))
.done(function() {
self.setState({ action_in_progress: false, error_message: null });
if (self.props.dialog_done)
self.props.dialog_done(true);
})
.fail(function(error) {
if (self.state.action_canceled) {
if (self.props.dialog_done)
self.props.dialog_done(false);
}
/* Always log global dialog errors for easier debugging */
console.warn(error);
self.setState({ action_in_progress: false, error_message: error });
})
.progress(this.update_progress.bind(this));
if (e)
e.stopPropagation();
},
cancel_click: function(e) {
// only consider clicks with the primary button
if (e && e.button !== 0)
return;
this.setState({ action_canceled: true });
if (this.props.cancel_clicked)
this.props.cancel_clicked();
// an action might be in progress, let that handler decide what to do if they added a cancel function
if (this.state.action_in_progress && 'cancel' in this.state.action_in_progress_promise) {
this.state.action_in_progress_promise.cancel();
return;
}
if (this.props.dialog_done)
this.props.dialog_done(false);
if (e)
e.stopPropagation();
},
render: function() {
var cancel_caption, cancel_style;
if ('cancel_caption' in this.props)
cancel_caption = this.props.cancel_caption;
else
cancel_caption = _("Cancel");
if ('cancel_style' in this.props)
cancel_style = this.props.cancel_style;
else
cancel_style = "cancel";
cancel_style = "btn btn-default " + cancel_style;
// If an action is in progress, show the spinner with its message and disable all actions except cancel
var wait_element;
var actions_disabled;
if (this.state.action_in_progress) {
actions_disabled = 'disabled';
wait_element = <div className="dialog-wait-ct pull-left">
<div className="spinner spinner-sm"></div>
<span>{ this.state.action_progress_message }</span>
</div>;
}
var self = this;
var action_buttons = this.props.actions.map(function(action) {
var caption;
if ('caption' in action)
caption = action.caption;
else
caption = _("Ok");
var button_style = "btn-default";
var button_style_mapping = { 'primary': 'btn-primary', 'danger': 'btn-danger' };
if ('style' in action && action.style in button_style_mapping)
button_style = button_style_mapping[action.style];
button_style = "btn " + button_style + " apply";
var action_disabled = actions_disabled || ('disabled' in action && action.disabled);
return (<button
key={ caption }
className={ button_style }
onClick={ self.action_click.bind(self, action.clicked) }
disabled={ action_disabled }
>{ caption }</button>
);
});
// If we have an error message, display the error
var error_element;
var error_message;
if (this.props.static_error !== undefined && this.props.static_error !== null)
error_message = this.props.static_error;
else
error_message = this.state.error_message;
if (error_message) {
error_element = <div className="alert alert-danger dialog-error">
<span className="fa fa-exclamation-triangle"></span>
<span>{ error_message }</span>
</div>;
}
return (
<div className="modal-footer">
{ error_element }
{ wait_element }
<button
className={ cancel_style }
onClick={ this.cancel_click.bind(this) }
>{ cancel_caption }</button>
{ action_buttons }
</div>
);
}
});
/*
* React template for a Cockpit dialog
* The primary action button is disabled while its action is in progress (waiting for promise)
* Removes focus on other elements on showing
* Expected props:
* - title (string)
* - no_backdrop optional, skip backdrop if true
* - body (react element, top element should be of class modal-body)
* It is recommended for information gathering dialogs to pass references
* to the input components to the controller. That way, the controller can
* extract all necessary information (e.g. for input validation) when an
* action is triggered.
* - footer (react element, top element should be of class modal-footer)
* - id optional, id that is assigned to the top level dialog node, but not the backdrop
*/
var Dialog = React.createClass({
propTypes: {
title: React.PropTypes.string.isRequired,
no_backdrop: React.PropTypes.bool,
body: React.PropTypes.element.isRequired,
footer: React.PropTypes.element.isRequired,
id: React.PropTypes.string,
},
componentDidMount: function() {
// if we used a button to open this, make sure it's not focused anymore
if (document.activeElement)
document.activeElement.blur();
},
render: function() {
var backdrop;
if (!this.props.no_backdrop) {
backdrop = <div className="modal-backdrop fade in"></div>;
}
return (
<div>
{ backdrop }
<div className="modal fade in dialog-ct-visible" tabindex="-1">
<div id={this.props.id} className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<h4 className="modal-title">{ this.props.title }</h4>
</div>
{ this.props.body }
{ this.props.footer }
</div>
</div>
</div>
</div>
);
}
});
/* Create and show a dialog
* For this, create a containing DOM node at the body level
* The returned object has the following methods:
* - setFooterProps replace the current footerProps and render
* - setProps replace the current props and render
* - render render again using the stored props
* The DOM node and React metadata are freed once the dialog has closed
*/
var show_modal_dialog = function(props, footerProps) {
var dialogName = 'cockpit_modal_dialog';
// don't allow nested dialogs
if (document.getElementById(dialogName)) {
console.warn('Unable to create nested dialog');
return;
}
// create an element to render into
var rootElement = document.createElement("div");
rootElement.id = dialogName;
document.body.appendChild(rootElement);
// register our own on-close callback
var origCallback;
var closeCallback = function() {
if (origCallback)
origCallback.apply(this, arguments);
React.unmountComponentAtNode(rootElement);
rootElement.remove();
};
var dialogObj = { };
dialogObj.props = null;
dialogObj.footerProps = null;
dialogObj.render = function() {
dialogObj.props.footer = <DialogFooter {...dialogObj.footerProps} />;
React.render(<Dialog {...dialogObj.props} />, rootElement);
};
function updateFooterAndRender() {
if (dialogObj.props === null || dialogObj.props === undefined)
dialogObj.props = { };
dialogObj.props.footer = <DialogFooter {...dialogObj.footerProps} />;
dialogObj.render();
}
dialogObj.setFooterProps = function(footerProps) {
/* Always log error messages to console for easier debugging */
if (footerProps.static_error)
console.warn(footerProps.static_error);
dialogObj.footerProps = footerProps;
if (dialogObj.footerProps === null || dialogObj.footerProps === undefined)
dialogObj.footerProps = { };
if (dialogObj.footerProps.dialog_done != closeCallback) {
origCallback = dialogObj.footerProps.dialog_done;
dialogObj.footerProps.dialog_done = closeCallback;
}
updateFooterAndRender();
};
dialogObj.setProps = function(props) {
dialogObj.props = props;
updateFooterAndRender();
};
dialogObj.setFooterProps(footerProps);
dialogObj.setProps(props);
// now actually render
dialogObj.render();
return dialogObj;
};
module.exports = {
Dialog: Dialog,
DialogFooter: DialogFooter,
show_modal_dialog: show_modal_dialog,
};