| 1 |
/* |
| 2 |
* Copyright (C) 2016 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import cockpit from "cockpit"; |
| 7 |
import React from "react"; |
| 8 |
import { createRoot } from "react-dom/client"; |
| 9 |
import PropTypes from "prop-types"; |
| 10 |
import { Alert } from "@patternfly/react-core/dist/esm/components/Alert/index.js"; |
| 11 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 12 |
import { |
| 13 |
Modal, ModalBody, ModalFooter, ModalHeader |
| 14 |
} from '@patternfly/react-core/dist/esm/components/Modal/index.js'; |
| 15 |
import { Popover } from "@patternfly/react-core/dist/esm/components/Popover/index.js"; |
| 16 |
import { Stack, StackItem } from "@patternfly/react-core/dist/esm/layouts/Stack/index.js"; |
| 17 |
import { HelpIcon, ExternalLinkAltIcon } from '@patternfly/react-icons'; |
| 18 |
|
| 19 |
import "cockpit-components-dialog.scss"; |
| 20 |
|
| 21 |
const _ = cockpit.gettext; |
| 22 |
|
| 23 |
/* |
| 24 |
* React template for a Cockpit dialog footer |
| 25 |
* It can wait for an action to complete, |
| 26 |
* has a 'Cancel' button and an action button (defaults to 'OK') |
| 27 |
* Expected props: |
| 28 |
* - cancel_clicked optional |
| 29 |
* Callback called when the dialog is canceled |
| 30 |
* - cancel_button optional, defaults to 'Cancel' text styled as a link |
| 31 |
* - list of actions, each an object with: |
| 32 |
* - clicked |
| 33 |
* Callback function that is expected to return a promise. |
| 34 |
* parameter: callback to set the progress text |
| 35 |
* - caption optional, defaults to 'Ok' |
| 36 |
* - disabled optional, defaults to false |
| 37 |
* - style defaults to 'secondary', other options: 'primary', 'danger' |
| 38 |
* - idle_message optional, always show this message on the last row when idle |
| 39 |
* - dialog_done optional, callback when dialog is finished (param true if success, false on cancel) |
| 40 |
* - set_error: required, callback to set/clear error message from actions |
| 41 |
*/ |
| 42 |
class DialogFooter extends React.Component { |
| 43 |
constructor(props) { |
| 44 |
super(props); |
| 45 |
this.state = { |
| 46 |
action_in_progress: false, |
| 47 |
action_progress_message: '', |
| 48 |
action_progress_cancel: null, |
| 49 |
action_canceled: false, |
| 50 |
action_caption_in_progress: '', |
| 51 |
}; |
| 52 |
this.update_progress = this.update_progress.bind(this); |
| 53 |
this.cancel_click = this.cancel_click.bind(this); |
| 54 |
} |
| 55 |
|
| 56 |
update_progress(msg, cancel) { |
| 57 |
this.setState({ action_progress_message: msg, action_progress_cancel: cancel }); |
| 58 |
} |
| 59 |
|
| 60 |
action_click(handler, caption, e) { |
| 61 |
this.setState({ |
| 62 |
action_progress_message: '', |
| 63 |
action_in_progress: true, |
| 64 |
action_caption_in_progress: caption, |
| 65 |
action_canceled: false, |
| 66 |
}); |
| 67 |
|
| 68 |
const p = handler(this.update_progress) |
| 69 |
.then(() => { |
| 70 |
this.props.set_error(null); |
| 71 |
this.setState({ action_in_progress: false }); |
| 72 |
if (this.props.dialog_done) |
| 73 |
this.props.dialog_done(true); |
| 74 |
}) |
| 75 |
.catch(error => { |
| 76 |
if (this.state.action_canceled) { |
| 77 |
if (this.props.dialog_done) |
| 78 |
this.props.dialog_done(false); |
| 79 |
} else { |
| 80 |
this.props.set_error(error); |
| 81 |
this.setState({ action_in_progress: false }); |
| 82 |
} |
| 83 |
/* Always log global dialog errors for easier debugging */ |
| 84 |
if (error) |
| 85 |
console.warn(error.message || error.toString()); |
| 86 |
}); |
| 87 |
|
| 88 |
if (p.progress) |
| 89 |
p.progress(this.update_progress); |
| 90 |
|
| 91 |
if (e) |
| 92 |
e.stopPropagation(); |
| 93 |
} |
| 94 |
|
| 95 |
cancel_click(e) { |
| 96 |
this.setState({ action_canceled: true }); |
| 97 |
|
| 98 |
if (this.props.cancel_clicked) |
| 99 |
this.props.cancel_clicked(); |
| 100 |
|
| 101 |
// an action might be in progress, let that handler decide what to do if they added a cancel function |
| 102 |
if (this.state.action_in_progress && this.state.action_progress_cancel) { |
| 103 |
this.state.action_progress_cancel(); |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
if (this.props.dialog_done) |
| 108 |
this.props.dialog_done(false); |
| 109 |
if (e) |
| 110 |
e.stopPropagation(); |
| 111 |
} |
| 112 |
|
| 113 |
render() { |
| 114 |
const cancel_text = this.props?.cancel_button?.text ?? _("Cancel"); |
| 115 |
const cancel_variant = this.props?.cancel_button?.variant ?? "link"; |
| 116 |
|
| 117 |
// If an action is in progress, show the spinner with its message and disable all actions. |
| 118 |
// Cancel is only enabled when the action promise has a cancel method, or we get one |
| 119 |
// via the progress reporting. |
| 120 |
|
| 121 |
let wait_element; |
| 122 |
let actions_disabled = false; |
| 123 |
const cancel_disabled = this.state.action_in_progress && !this.state.action_progress_cancel; |
| 124 |
if (this.state.action_in_progress) { |
| 125 |
actions_disabled = true; |
| 126 |
wait_element = <div className="dialog-wait-ct"> |
| 127 |
<span>{ this.state.action_progress_message }</span> |
| 128 |
</div>; |
| 129 |
} else if (this.props.idle_message) { |
| 130 |
wait_element = <div className="dialog-wait-ct"> |
| 131 |
{ this.props.idle_message } |
| 132 |
</div>; |
| 133 |
} |
| 134 |
|
| 135 |
const action_buttons = this.props.actions.map(action => { |
| 136 |
let caption; |
| 137 |
if ('caption' in action) |
| 138 |
caption = action.caption; |
| 139 |
else |
| 140 |
caption = _("Ok"); |
| 141 |
|
| 142 |
let variant = action.style || "secondary"; |
| 143 |
if (variant == "primary" && action.danger) |
| 144 |
variant = "danger"; |
| 145 |
|
| 146 |
return (<Button |
| 147 |
key={ caption } |
| 148 |
className="apply" |
| 149 |
variant={ variant } |
| 150 |
isLoading={ this.state.action_in_progress && this.state.action_caption_in_progress == caption } |
| 151 |
isDanger={ action.danger } |
| 152 |
onClick={ this.action_click.bind(this, action.clicked, caption) } |
| 153 |
isDisabled={ actions_disabled || action.disabled } |
| 154 |
>{ caption }</Button> |
| 155 |
); |
| 156 |
}); |
| 157 |
|
| 158 |
return ( |
| 159 |
<> |
| 160 |
{ this.props.extra_element } |
| 161 |
{ action_buttons } |
| 162 |
<Button variant={cancel_variant} className="cancel" onClick={this.cancel_click} isDisabled={cancel_disabled}>{ cancel_text }</Button> |
| 163 |
{ wait_element } |
| 164 |
</> |
| 165 |
); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
DialogFooter.propTypes = { |
| 170 |
cancel_clicked: PropTypes.func, |
| 171 |
cancel_button: PropTypes.object, |
| 172 |
actions: PropTypes.array.isRequired, |
| 173 |
dialog_done: PropTypes.func, |
| 174 |
set_error: PropTypes.func.isRequired, |
| 175 |
}; |
| 176 |
|
| 177 |
/* |
| 178 |
* React template for a Cockpit dialog |
| 179 |
* The primary action button is disabled while its action is in progress (waiting for promise) |
| 180 |
* Removes focus on other elements on showing |
| 181 |
* Expected props: |
| 182 |
* - title (string) |
| 183 |
* - body (react element, top element should be of class modal-body) |
| 184 |
* It is recommended for information gathering dialogs to pass references |
| 185 |
* to the input components to the controller. That way, the controller can |
| 186 |
* extract all necessary information (e.g. for input validation) when an |
| 187 |
* action is triggered. |
| 188 |
* - static_error optional, always show this error after the body element |
| 189 |
* - footer (react element, top element should be of class modal-footer) |
| 190 |
* - id optional, id that is assigned to the top level dialog node, but not the backdrop |
| 191 |
* - variant: See PF6 Modal component's 'variant' property |
| 192 |
* - titleIconVariant: See PF6 ModalHeader component's 'titleIconVariant' property |
| 193 |
*/ |
| 194 |
class Dialog extends React.Component { |
| 195 |
componentDidMount() { |
| 196 |
// For the scenario that cockpit-storage is used inside anaconda Web UI |
| 197 |
// We need to know if there is an open dialog in order to create the backdrop effect |
| 198 |
// on the parent window |
| 199 |
window.sessionStorage.setItem("cockpit_has_modal", true); |
| 200 |
|
| 201 |
// if we used a button to open this, make sure it's not focused anymore |
| 202 |
if (document.activeElement) |
| 203 |
document.activeElement.blur(); |
| 204 |
} |
| 205 |
|
| 206 |
componentWillUnmount() { |
| 207 |
window.sessionStorage.setItem("cockpit_has_modal", false); |
| 208 |
} |
| 209 |
|
| 210 |
render() { |
| 211 |
let help = null; |
| 212 |
let footer = null; |
| 213 |
if (this.props.helpLink) |
| 214 |
footer = <a href={this.props.helpLink} target="_blank" rel="noopener noreferrer">{_("Learn more")} <ExternalLinkAltIcon /></a>; |
| 215 |
|
| 216 |
if (this.props.helpMessage) |
| 217 |
help = <Popover |
| 218 |
bodyContent={this.props.helpMessage} |
| 219 |
footerContent={footer} |
| 220 |
> |
| 221 |
<Button icon={<HelpIcon />} variant="plain" aria-label={_("Learn more")} /> |
| 222 |
</Popover>; |
| 223 |
|
| 224 |
const error = this.props.error || this.props.static_error; |
| 225 |
const error_alert = error && <Alert variant='danger' isInline title={error} />; |
| 226 |
|
| 227 |
return ( |
| 228 |
<Modal position="top" variant={this.props.variant || "medium"} |
| 229 |
onEscapePress={() => undefined} |
| 230 |
id={this.props.id} |
| 231 |
isOpen> |
| 232 |
<ModalHeader title={this.props.title} |
| 233 |
titleIconVariant={this.props.titleIconVariant} |
| 234 |
help={help} |
| 235 |
/> |
| 236 |
<ModalBody> |
| 237 |
<Stack hasGutter> |
| 238 |
{ error_alert } |
| 239 |
<StackItem> |
| 240 |
{ this.props.body } |
| 241 |
</StackItem> |
| 242 |
</Stack> |
| 243 |
</ModalBody> |
| 244 |
<ModalFooter> |
| 245 |
{this.props.footer} |
| 246 |
</ModalFooter> |
| 247 |
</Modal> |
| 248 |
); |
| 249 |
} |
| 250 |
} |
| 251 |
Dialog.propTypes = { |
| 252 |
// TODO: fix following by refactoring the logic showing modal dialog (recently show_modal_dialog()) |
| 253 |
title: PropTypes.string, // is effectively required, but show_modal_dialog() provides initially no props and resets them later. |
| 254 |
body: PropTypes.element, // is effectively required, see above |
| 255 |
static_error: PropTypes.string, |
| 256 |
error: PropTypes.string, |
| 257 |
footer: PropTypes.element, // is effectively required, see above |
| 258 |
id: PropTypes.string, |
| 259 |
}; |
| 260 |
|
| 261 |
/* Create and show a dialog |
| 262 |
* For this, create a containing DOM node at the body level |
| 263 |
* The returned object has the following methods: |
| 264 |
* - setFooterProps replace the current footerProps and render |
| 265 |
* - setProps replace the current props and render |
| 266 |
* - render render again using the stored props |
| 267 |
* The DOM node and React metadata are freed once the dialog has closed |
| 268 |
*/ |
| 269 |
export function show_modal_dialog(props, footerProps) { |
| 270 |
const dialogName = 'cockpit_modal_dialog'; |
| 271 |
// don't allow nested dialogs, just close whatever is open |
| 272 |
const curElement = document.getElementById(dialogName); |
| 273 |
let root; |
| 274 |
if (curElement) { |
| 275 |
root = createRoot(curElement); |
| 276 |
root.unmount(); |
| 277 |
curElement.remove(); |
| 278 |
} |
| 279 |
// create an element to render into |
| 280 |
const rootElement = document.createElement("div"); |
| 281 |
root = createRoot(rootElement); |
| 282 |
rootElement.id = dialogName; |
| 283 |
document.body.appendChild(rootElement); |
| 284 |
|
| 285 |
// register our own on-close callback |
| 286 |
let origCallback; |
| 287 |
const closeCallback = function() { |
| 288 |
if (origCallback) |
| 289 |
origCallback.apply(this, arguments); |
| 290 |
root.unmount(); |
| 291 |
rootElement.remove(); |
| 292 |
}; |
| 293 |
|
| 294 |
const dialogObj = { }; |
| 295 |
let error = null; |
| 296 |
dialogObj.props = props; |
| 297 |
dialogObj.footerProps = null; |
| 298 |
dialogObj.render = function() { |
| 299 |
dialogObj.props.footer = <DialogFooter {...dialogObj.footerProps} />; |
| 300 |
// Don't render if we are no longer part of the document. |
| 301 |
// This would be mostly harmless except that it will remove |
| 302 |
// the input focus from whatever element has it, which is |
| 303 |
// unpleasant and also disrupts the tests. |
| 304 |
if (rootElement.offsetParent) |
| 305 |
root.render(<Dialog {...dialogObj.props} error={error} />); |
| 306 |
}; |
| 307 |
function updateFooterAndRender() { |
| 308 |
if (dialogObj.props === null || dialogObj.props === undefined) |
| 309 |
dialogObj.props = { }; |
| 310 |
dialogObj.props.footer = <DialogFooter {...dialogObj.footerProps} />; |
| 311 |
dialogObj.render(); |
| 312 |
} |
| 313 |
dialogObj.setFooterProps = function(footerProps) { |
| 314 |
dialogObj.footerProps = footerProps; |
| 315 |
if (dialogObj.footerProps.dialog_done != closeCallback) { |
| 316 |
origCallback = dialogObj.footerProps.dialog_done; |
| 317 |
dialogObj.footerProps.dialog_done = closeCallback; |
| 318 |
} |
| 319 |
dialogObj.footerProps.set_error = e => { |
| 320 |
error = typeof e === 'object' && e !== null ? (e.message || e.toString()) : e; |
| 321 |
dialogObj.render(); |
| 322 |
}; |
| 323 |
updateFooterAndRender(); |
| 324 |
}; |
| 325 |
dialogObj.setProps = function(props) { |
| 326 |
dialogObj.props = props; |
| 327 |
updateFooterAndRender(); |
| 328 |
}; |
| 329 |
dialogObj.setFooterProps(footerProps); |
| 330 |
dialogObj.setProps(props); |
| 331 |
|
| 332 |
// now actually render |
| 333 |
dialogObj.render(); |
| 334 |
|
| 335 |
return dialogObj; |
| 336 |
} |
| 337 |
|
| 338 |
export function apply_modal_dialog(event) { |
| 339 |
const dialog = event.target?.closest("[role=dialog]"); |
| 340 |
const button = dialog?.querySelector("button.apply"); |
| 341 |
|
| 342 |
if (button) { |
| 343 |
const event = new MouseEvent('click', { |
| 344 |
view: window, |
| 345 |
bubbles: true, |
| 346 |
cancelable: true, |
| 347 |
button: 0 |
| 348 |
}); |
| 349 |
button.dispatchEvent(event); |
| 350 |
} |
| 351 |
|
| 352 |
event.preventDefault(); |
| 353 |
return false; |
| 354 |
} |