| 1 |
/* |
| 2 |
* Copyright (C) 2018 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
/* STORAGE DIALOGS |
| 7 |
|
| 8 |
To show a modal dialog, make a call like this: |
| 9 |
|
| 10 |
dialog_show({ Title: _("What is your name?"), |
| 11 |
Fields: [ |
| 12 |
TextInput("name", _("Name"), |
| 13 |
{ validate: val => (val == ""? _("Name can't be empty") : null) }) |
| 14 |
] |
| 15 |
Action: { |
| 16 |
Title: _("Ok"), |
| 17 |
action: vals => { console.log("Hello, " + vals.name + "!"); } |
| 18 |
} |
| 19 |
}); |
| 20 |
|
| 21 |
The call to dialog_show will open the dialog and return |
| 22 |
immediately. Later, when the user clicks on "Ok", the "action" |
| 23 |
function will be called with the values of the dialog fields. The |
| 24 |
action function usually returns a promise, although it does not in |
| 25 |
the example above. When that promise resolves, the dialog is |
| 26 |
closed. When the promise is rejected, it's error is displayed in |
| 27 |
the dialog, and the dialog stays open. |
| 28 |
|
| 29 |
Fields are described by calling functions such as TextInput. A |
| 30 |
number of generic ones are defined here, and you can define more |
| 31 |
specialized ones yourself. |
| 32 |
|
| 33 |
They are all called like this: |
| 34 |
|
| 35 |
FieldFunction(tag, title, { option: value, ... }) |
| 36 |
|
| 37 |
The "tag" is used to uniquely identify this field in the dialog. |
| 38 |
The action function will receive the values of all fields in an |
| 39 |
object, and the tag of a field is the key in that object, for |
| 40 |
example. The tag is also used to interact with a field from tests. |
| 41 |
|
| 42 |
ACTION FUNCTIONS |
| 43 |
|
| 44 |
The action function is called like this: |
| 45 |
|
| 46 |
action(values, progress_callback) |
| 47 |
|
| 48 |
The "values" parameter contains the validated values of the dialog |
| 49 |
fields and the "progress_callback" can be called by the action function |
| 50 |
to update the progress information in the dialog while it runs. |
| 51 |
|
| 52 |
The progress callback should be called like this: |
| 53 |
|
| 54 |
progress_callback(message, cancel_callback) |
| 55 |
|
| 56 |
The "message" will be displayed in the dialog and if "cancel_callback" is |
| 57 |
not null, the Cancel button in the dialog will be enabled and |
| 58 |
"cancel_callback" will be called when the user clicks it. |
| 59 |
|
| 60 |
The return value of the action function is normally a promise. When |
| 61 |
it is resolved, the dialog is closed. When it is rejected the value |
| 62 |
given in the rejection is displayed as an error in the dialog. |
| 63 |
|
| 64 |
If the error value is a string, it is displayed as a global failure |
| 65 |
message. When it is an object, it contains errors for individual |
| 66 |
fields in this form: |
| 67 |
|
| 68 |
{ tag1: message, tag2: message } |
| 69 |
|
| 70 |
As a special case, when "message" is "true", the field is rendered |
| 71 |
as having an error (with a red outline, say), but without any |
| 72 |
directly associated text. The idea is that a group of fields is in |
| 73 |
error, and the error message for all of them is shown below the last |
| 74 |
one in the group. |
| 75 |
|
| 76 |
COMMON FIELD OPTIONS |
| 77 |
|
| 78 |
Each field function describes its options. However, there are some |
| 79 |
options that apply to all fields: |
| 80 |
|
| 81 |
- value |
| 82 |
|
| 83 |
The initial value of the field. |
| 84 |
|
| 85 |
- visible: vals -> boolean |
| 86 |
|
| 87 |
This function determines whether the field is shown or not. |
| 88 |
|
| 89 |
- validate: (val, vals) -> null-or-error-string (or promise) |
| 90 |
|
| 91 |
The validate function receives the current value of the field and |
| 92 |
should return "null" (or something falsey) when that value is |
| 93 |
acceptable. Otherwise, it should return a suitable error message. |
| 94 |
|
| 95 |
The second argument has all values of all fields, in case you need |
| 96 |
to look at more than one field. |
| 97 |
|
| 98 |
It is permissible to overwrite fields of "vals" to change the final |
| 99 |
value of a field. |
| 100 |
|
| 101 |
The validate function can also return a promise which resolves to |
| 102 |
null or an error message. If that promise is rejected, that error |
| 103 |
is shown globally in the dialog as if the action function had |
| 104 |
failed. |
| 105 |
|
| 106 |
The validate function will only be called for currently visible |
| 107 |
fields. |
| 108 |
|
| 109 |
- widest_title |
| 110 |
|
| 111 |
This is a hack to force the column of titles to be a certain |
| 112 |
minimum width, namely the width of the widest_title. This matters |
| 113 |
when there are rows that are only sometimes visible and the layout |
| 114 |
would jump around when they change visibility. |
| 115 |
|
| 116 |
Technically, the first column of a row shows the "title" but is as |
| 117 |
wide as its "widest_title". The idea is that you put the widest |
| 118 |
title of all fields in the widest_title option of one of the rows |
| 119 |
that are always visible. |
| 120 |
|
| 121 |
- explanation |
| 122 |
|
| 123 |
A test to show below the field, as an explanation. |
| 124 |
|
| 125 |
RUNNING TASKS AND DYNAMIC UPDATES |
| 126 |
|
| 127 |
The dialog_show function returns an object that can be used to interact |
| 128 |
with the dialog in various ways while it is open. |
| 129 |
|
| 130 |
dlg = dialog_show(...) |
| 131 |
|
| 132 |
One can run asynchronous tasks: |
| 133 |
|
| 134 |
dlg.run("title", promise) |
| 135 |
|
| 136 |
This will disable the footer buttons and wait for promise to be resolved |
| 137 |
or rejected while showing "title" and a spinner. |
| 138 |
|
| 139 |
One can set field values and options: |
| 140 |
|
| 141 |
dlg.set_values({ tag1: value1, tag2: value2, ... }) |
| 142 |
dlg.set_options(tag, { opt1: value1, opt2: value2, ... }) |
| 143 |
|
| 144 |
It is also possible to specify a "update" function when creating the dialog: |
| 145 |
|
| 146 |
dialog_show({ ... |
| 147 |
update: function (dlg, vals, trigger) { } |
| 148 |
... }) |
| 149 |
|
| 150 |
This function is called whenever the values of fields are changed. The |
| 151 |
"trigger" argument is the tag of the field that has just been changed. |
| 152 |
|
| 153 |
DEFINING NEW FIELD TYPES |
| 154 |
|
| 155 |
To define a new field type, just define a new function that follows |
| 156 |
a few rules. Here is TextInput: |
| 157 |
|
| 158 |
export const TextInput = (tag, title, options) => { |
| 159 |
return { |
| 160 |
tag: tag, |
| 161 |
title: title, |
| 162 |
options: options, |
| 163 |
initial_value: "", |
| 164 |
|
| 165 |
render: (val, change) => |
| 166 |
<input data-field={tag} |
| 167 |
className="form-control" type="text" value={val} |
| 168 |
onChange={event => change(event.target.value)}/> |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
As you can see, a field function should return an object with a |
| 173 |
couple of fields. The "tag", "title", and "options" field just |
| 174 |
store the parameters to the field function. The rest are these: |
| 175 |
|
| 176 |
- initial_value |
| 177 |
|
| 178 |
This is the initial value of the field. |
| 179 |
|
| 180 |
- render: (val, change) -> React components |
| 181 |
|
| 182 |
This should render the value part of the field, that is, the second |
| 183 |
column in the table layout. The title is in the first column and |
| 184 |
is rendered by the generic dialog machinery. |
| 185 |
|
| 186 |
The "val" parameter is the current value and you should make sure |
| 187 |
that the DOM element really shows that value, and not something |
| 188 |
that might have left behind by previous user interactions. |
| 189 |
|
| 190 |
The "change" parameter is a function that should be called with a |
| 191 |
new value for the field whenever the user has interacted with it. |
| 192 |
|
| 193 |
For the benefits of the integration tests, the DOM elements should |
| 194 |
also contain "data-field" and maybe a "data-field-type" attributes. The |
| 195 |
"data-field" value should be that tag of the field, and |
| 196 |
"data-field-type" type is used by the tests to know how to interact |
| 197 |
with the field. If you find to need it, just pick a reasonable value |
| 198 |
and extend the test suite to handle it. |
| 199 |
|
| 200 |
This function is not called at all for invisible fields. |
| 201 |
*/ |
| 202 |
|
| 203 |
import cockpit from "cockpit"; |
| 204 |
|
| 205 |
import React, { useState } from "react"; |
| 206 |
import { Alert } from "@patternfly/react-core/dist/esm/components/Alert/index.js"; |
| 207 |
import { FormSelect, FormSelectOption } from "@patternfly/react-core/dist/esm/components/FormSelect/index.js"; |
| 208 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 209 |
import { Checkbox } from "@patternfly/react-core/dist/esm/components/Checkbox/index.js"; |
| 210 |
import { DataList, DataListCell, DataListCheck, DataListItem, DataListItemCells, DataListItemRow } from "@patternfly/react-core/dist/esm/components/DataList/index.js"; |
| 211 |
import { Form, FormGroup } from "@patternfly/react-core/dist/esm/components/Form/index.js"; |
| 212 |
import { Grid, GridItem } from "@patternfly/react-core/dist/esm/layouts/Grid/index.js"; |
| 213 |
import { Radio } from "@patternfly/react-core/dist/esm/components/Radio/index.js"; |
| 214 |
import { Slider } from "@patternfly/react-core/dist/esm/components/Slider/index.js"; |
| 215 |
import { Spinner } from "@patternfly/react-core/dist/esm/components/Spinner/index.js"; |
| 216 |
import { Split } from "@patternfly/react-core/dist/esm/layouts/Split/index.js"; |
| 217 |
import { TextInput as TextInputPF4 } from "@patternfly/react-core/dist/esm/components/TextInput/index.js"; |
| 218 |
import { Popover } from "@patternfly/react-core/dist/esm/components/Popover/index.js"; |
| 219 |
import { HelperText, HelperTextItem } from "@patternfly/react-core/dist/esm/components/HelperText/index.js"; |
| 220 |
import { List, ListItem } from "@patternfly/react-core/dist/esm/components/List/index.js"; |
| 221 |
import { ExclamationTriangleIcon, InfoIcon, HelpIcon, EyeIcon, EyeSlashIcon } from "@patternfly/react-icons"; |
| 222 |
import { InputGroup } from "@patternfly/react-core/dist/esm/components/InputGroup/index.js"; |
| 223 |
import { Table, Tbody, Tr, Td } from '@patternfly/react-table'; |
| 224 |
|
| 225 |
import { TypeaheadSelect } from "cockpit-components-typeahead-select"; |
| 226 |
import { show_modal_dialog, apply_modal_dialog } from "cockpit-components-dialog.jsx"; |
| 227 |
import { ListingTable } from "cockpit-components-table.jsx"; |
| 228 |
import { FormHelper } from "cockpit-components-form-helper"; |
| 229 |
|
| 230 |
import { |
| 231 |
decode_filename, fmt_size, block_name, format_size_and_text, format_delay, for_each_async, get_byte_units, |
| 232 |
BTRFS_TOOL_MOUNT_PATH |
| 233 |
} from "./utils.js"; |
| 234 |
import { fmt_to_fragments } from "utils.jsx"; |
| 235 |
import client from "./client.js"; |
| 236 |
|
| 237 |
import fsys_is_empty_sh from "./fsys-is-empty.sh"; |
| 238 |
|
| 239 |
const _ = cockpit.gettext; |
| 240 |
|
| 241 |
function make_rows(fields, values, errors, onChange) { |
| 242 |
return fields.map((f, i) => <Row key={i} field={f} values={values} errors={errors} onChange={onChange} />) |
| 243 |
.filter(r => r); |
| 244 |
} |
| 245 |
|
| 246 |
function is_visible(field, values) { |
| 247 |
return !field.options || field.options.visible == undefined || field.options.visible(values); |
| 248 |
} |
| 249 |
|
| 250 |
const Field = ({ field, values, errors, onChange }) => { |
| 251 |
const { tag, options } = field; |
| 252 |
|
| 253 |
if (!is_visible(field, values)) |
| 254 |
return null; |
| 255 |
|
| 256 |
const error = errors && errors[tag]; |
| 257 |
const explanation = options && options.explanation; |
| 258 |
const validated = (tag && errors && errors[tag]) ? 'error' : 'default'; |
| 259 |
|
| 260 |
function change(val) { |
| 261 |
values[tag] = val; |
| 262 |
onChange(tag); |
| 263 |
} |
| 264 |
|
| 265 |
return ( |
| 266 |
<> |
| 267 |
{field.render(values[tag], change, validated, error)} |
| 268 |
<FormHelper helperText={explanation} helperTextInvalid={validated && error} /> |
| 269 |
</>); |
| 270 |
}; |
| 271 |
|
| 272 |
const Row = ({ field, values, errors, onChange }) => { |
| 273 |
const { title, options } = field; |
| 274 |
|
| 275 |
if (!is_visible(field, values)) |
| 276 |
return null; |
| 277 |
|
| 278 |
const field_elts = <Field field={field} values={values} errors={errors} onChange={onChange} />; |
| 279 |
let nested_elts = []; |
| 280 |
if (options && options.nested_fields) { |
| 281 |
if (field.is_group) |
| 282 |
nested_elts = options.nested_fields.map(f => <Field key={f.tag} |
| 283 |
field={f} |
| 284 |
values={values} |
| 285 |
errors={errors} |
| 286 |
onChange={onChange} />); |
| 287 |
else |
| 288 |
nested_elts = make_rows(options.nested_fields, values, errors, onChange); |
| 289 |
} |
| 290 |
|
| 291 |
if (title || title == "") { |
| 292 |
let titleLabel = title; |
| 293 |
|
| 294 |
if (options.widest_title) |
| 295 |
titleLabel = ( |
| 296 |
<> |
| 297 |
<div className="widest-title">{options.widest_title}</div> |
| 298 |
<div>{title}</div> |
| 299 |
</> |
| 300 |
); |
| 301 |
return ( |
| 302 |
<FormGroup label={titleLabel} hasNoPaddingTop={field.hasNoPaddingTop}> |
| 303 |
{ field_elts } |
| 304 |
{ nested_elts } |
| 305 |
</FormGroup> |
| 306 |
); |
| 307 |
} else if (!field.bare) { |
| 308 |
return ( |
| 309 |
<FormGroup hasNoPaddingTop={field.hasNoPaddingTop}> |
| 310 |
{ field_elts } |
| 311 |
{ nested_elts } |
| 312 |
</FormGroup> |
| 313 |
); |
| 314 |
} else |
| 315 |
return field_elts; |
| 316 |
}; |
| 317 |
|
| 318 |
const Body = ({ body, teardown, fields, values, errors, isFormHorizontal, onChange }) => { |
| 319 |
let error_alert = null; |
| 320 |
|
| 321 |
if (errors && errors.toString() != "[object Object]") { |
| 322 |
// This is a global error from a failed action |
| 323 |
error_alert = <Alert variant='danger' isInline title={errors.toString()} />; |
| 324 |
errors = null; |
| 325 |
} |
| 326 |
|
| 327 |
return ( |
| 328 |
<> |
| 329 |
{ error_alert } |
| 330 |
{ body || null } |
| 331 |
{ fields.length > 0 |
| 332 |
? <Form onSubmit={apply_modal_dialog} |
| 333 |
isHorizontal={isFormHorizontal !== false}> |
| 334 |
{ make_rows(fields, values, errors, onChange) } |
| 335 |
</Form> |
| 336 |
: null } |
| 337 |
{ teardown } |
| 338 |
</> |
| 339 |
); |
| 340 |
}; |
| 341 |
|
| 342 |
const ExtraConfirmation = ({ text, onChange }) => { |
| 343 |
const [confirmed, setConfirmed] = useState(false); |
| 344 |
|
| 345 |
return ( |
| 346 |
<Checkbox isChecked={confirmed} |
| 347 |
id="dialog-confirm" |
| 348 |
label={text} |
| 349 |
onChange={(_, val) => { |
| 350 |
setConfirmed(val); |
| 351 |
onChange(val); |
| 352 |
}} />); |
| 353 |
}; |
| 354 |
|
| 355 |
function flatten_fields(fields) { |
| 356 |
return fields.reduce( |
| 357 |
(acc, val) => acc.concat([val]).concat(val.options && val.options.nested_fields |
| 358 |
? flatten_fields(val.options.nested_fields) |
| 359 |
: []), |
| 360 |
[]); |
| 361 |
} |
| 362 |
|
| 363 |
export const dialog_open = (def) => { |
| 364 |
const nested_fields = def.Fields || []; |
| 365 |
const fields = flatten_fields(nested_fields); |
| 366 |
const values = { }; |
| 367 |
let confirmation = null; |
| 368 |
let confirmed = false; |
| 369 |
let errors = null; |
| 370 |
|
| 371 |
fields.forEach(f => { values[f.tag] = f.initial_value }); |
| 372 |
|
| 373 |
// We reconstruct the body every time the values change so that it |
| 374 |
// will be re-rendered. This could be done with some state in the |
| 375 |
// Body component maybe, but we also want the values up here so |
| 376 |
// that we can pass them to validate and the action function. |
| 377 |
|
| 378 |
const update = () => { |
| 379 |
dlg.setProps(props()); |
| 380 |
}; |
| 381 |
|
| 382 |
const props = () => { |
| 383 |
return { |
| 384 |
id: "dialog", |
| 385 |
title: def.Title, |
| 386 |
titleIconVariant: (def.Action && (def.Action.Danger || def.Action.DangerButton)) ? "warning" : null, |
| 387 |
body: <Body body={def.Body} |
| 388 |
teardown={def.Teardown} |
| 389 |
fields={nested_fields} |
| 390 |
values={values} |
| 391 |
errors={errors} |
| 392 |
isFormHorizontal={def.isFormHorizontal} |
| 393 |
onChange={trigger => { |
| 394 |
errors = null; |
| 395 |
if (def.update) |
| 396 |
def.update(self, values, trigger); |
| 397 |
update(); |
| 398 |
}} /> |
| 399 |
}; |
| 400 |
}; |
| 401 |
|
| 402 |
const update_footer = (running_title, running_promise) => { |
| 403 |
dlg.setFooterProps(footer_props(running_title, running_promise)); |
| 404 |
}; |
| 405 |
|
| 406 |
function run_action(progress_callback, variant) { |
| 407 |
const func = () => { |
| 408 |
return validate(variant) |
| 409 |
.then(validated_values => { |
| 410 |
const visible_values = { variant }; |
| 411 |
fields.forEach(f => { |
| 412 |
if (is_visible(f, values)) |
| 413 |
visible_values[f.tag] = validated_values[f.tag]; |
| 414 |
}); |
| 415 |
if (def.Action.wrapper) |
| 416 |
return def.Action.wrapper(visible_values, progress_callback, |
| 417 |
def.Action.action); |
| 418 |
else |
| 419 |
return def.Action.action(visible_values, progress_callback); |
| 420 |
}) |
| 421 |
.catch(errs => { |
| 422 |
if (errs && errs.toString() != "[object Object]") { |
| 423 |
// Log errors from failed actions, for debugging and |
| 424 |
// to allow the test suite to catch known issues. |
| 425 |
console.warn(errs.toString()); |
| 426 |
} |
| 427 |
errors = errs; |
| 428 |
update(); |
| 429 |
update_footer(); |
| 430 |
return Promise.reject(); |
| 431 |
}); |
| 432 |
}; |
| 433 |
return client.run(func); |
| 434 |
} |
| 435 |
|
| 436 |
const footer_props = (running_title, running_promise) => { |
| 437 |
const actions = []; |
| 438 |
|
| 439 |
function add_action(variant) { |
| 440 |
actions.push({ |
| 441 |
caption: variant.Title, |
| 442 |
style: actions.length == 0 ? "primary" : "secondary", |
| 443 |
danger: def.Action.Danger || def.Action.DangerButton, |
| 444 |
disabled: (running_promise != null || |
| 445 |
(def.Action.disable_on_error && |
| 446 |
errors && errors.toString() != "[object Object]") || |
| 447 |
(confirmation && !confirmed)), |
| 448 |
clicked: progress_callback => run_action(progress_callback, variant.tag), |
| 449 |
}); |
| 450 |
} |
| 451 |
|
| 452 |
if (def.Action) { |
| 453 |
if (def.Action.Title) { |
| 454 |
add_action({ |
| 455 |
Title: def.Action.Title, |
| 456 |
tag: null, |
| 457 |
}); |
| 458 |
} |
| 459 |
|
| 460 |
if (def.Action.Variants) { |
| 461 |
for (const v of def.Action.Variants) { |
| 462 |
add_action(v); |
| 463 |
} |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
let extra = null; |
| 468 |
if (confirmation) { |
| 469 |
extra = <ExtraConfirmation text={confirmation} |
| 470 |
onChange={val => { |
| 471 |
confirmed = val; |
| 472 |
update_footer(); |
| 473 |
}} />; |
| 474 |
} else if (def.Action && def.Action.Danger) { |
| 475 |
extra = ( |
| 476 |
<div> |
| 477 |
<HelperText><HelperTextItem variant="error">{def.Action.Danger} </HelperTextItem></HelperText> |
| 478 |
</div>); |
| 479 |
} |
| 480 |
|
| 481 |
return { |
| 482 |
idle_message: (running_promise |
| 483 |
? <> |
| 484 |
<span>{running_title}</span> |
| 485 |
<Spinner className="dialog-wait-ct-spinner" size="md" /> |
| 486 |
</> |
| 487 |
: null), |
| 488 |
extra_element: extra, |
| 489 |
actions, |
| 490 |
cancel_button: def.Action ? {} : { text: _("Close"), variant: "secondary" } |
| 491 |
}; |
| 492 |
}; |
| 493 |
|
| 494 |
const validate = (variant) => { |
| 495 |
// The validation functions sometimes change the dialog values |
| 496 |
// for the benefit of the action functions. For example, a |
| 497 |
// SizeSlider will convert from "text plus unit" to a numeric |
| 498 |
// value during validation. |
| 499 |
// |
| 500 |
// However, if the action fails, we don't want the values in |
| 501 |
// the dialog to change. The SizeSlider would convert back |
| 502 |
// from a numeric value to "text plus unit", for example, and |
| 503 |
// that conversion might change what the user had type. |
| 504 |
// |
| 505 |
// So we make a copy of the dialog state and let the validate |
| 506 |
// and action functions work with that. |
| 507 |
// |
| 508 |
const validated_values = { ...values }; |
| 509 |
|
| 510 |
return Promise.all(fields.map(f => { |
| 511 |
if (is_visible(f, values) && f.options && f.options.validate) |
| 512 |
return f.options.validate(validated_values[f.tag], validated_values, variant); |
| 513 |
else |
| 514 |
return null; |
| 515 |
})).then(results => { |
| 516 |
const errors = { }; |
| 517 |
let scrolled = false; |
| 518 |
fields.forEach((f, i) => { |
| 519 |
if (results[i]) { |
| 520 |
if (!scrolled) { |
| 521 |
show_field(f.tag); |
| 522 |
scrolled = true; |
| 523 |
} |
| 524 |
errors[f.tag] = results[i]; |
| 525 |
} |
| 526 |
}); |
| 527 |
if (Object.keys(errors).length > 0) |
| 528 |
return Promise.reject(errors); |
| 529 |
return validated_values; |
| 530 |
}); |
| 531 |
}; |
| 532 |
|
| 533 |
const dlg = show_modal_dialog(props(), footer_props(null, null)); |
| 534 |
|
| 535 |
function show_field(tag) { |
| 536 |
function scroll() { |
| 537 |
const field_element = document.querySelector('#dialog [data-field="' + tag + '"]'); |
| 538 |
if (field_element) |
| 539 |
field_element.scrollIntoView({ behavior: "smooth", block: "nearest" }); |
| 540 |
} |
| 541 |
// By the time show_field is called from the "update" |
| 542 |
// callback, newly visible fields don't exist yet in the |
| 543 |
// DOM, so delay the scrolling a bit. |
| 544 |
window.setTimeout(scroll, 10); |
| 545 |
} |
| 546 |
|
| 547 |
const self = { |
| 548 |
run: (title, promise) => { |
| 549 |
update_footer(title, promise); |
| 550 |
promise.then( |
| 551 |
() => { |
| 552 |
update_footer(null, null); |
| 553 |
}, |
| 554 |
(errs) => { |
| 555 |
if (errs) { |
| 556 |
errors = errs; |
| 557 |
update(); |
| 558 |
} |
| 559 |
update_footer(null, null); |
| 560 |
}); |
| 561 |
}, |
| 562 |
|
| 563 |
set_values: (new_vals) => { |
| 564 |
Object.assign(values, new_vals); |
| 565 |
update(); |
| 566 |
}, |
| 567 |
|
| 568 |
get_value: (tag) => { |
| 569 |
return values[tag]; |
| 570 |
}, |
| 571 |
|
| 572 |
update_actions: (new_actions) => { |
| 573 |
Object.assign(def.Action, new_actions); |
| 574 |
update_footer(null, null); |
| 575 |
}, |
| 576 |
|
| 577 |
set_nested_values: (key, new_vals) => { |
| 578 |
const updated = values[key]; |
| 579 |
Object.assign(updated, new_vals); |
| 580 |
values[key] = updated; |
| 581 |
update(); |
| 582 |
}, |
| 583 |
|
| 584 |
get_options: (tag) => { |
| 585 |
for (const f of fields) { |
| 586 |
if (f.tag == tag) { |
| 587 |
return f.options; |
| 588 |
} |
| 589 |
} |
| 590 |
}, |
| 591 |
|
| 592 |
set_options: (tag, new_options) => { |
| 593 |
fields.forEach(f => { |
| 594 |
if (f.tag == tag) { |
| 595 |
Object.assign(f.options, new_options); |
| 596 |
update(); |
| 597 |
} |
| 598 |
}); |
| 599 |
}, |
| 600 |
|
| 601 |
set_attribute: (name, value) => { |
| 602 |
def[name] = value; |
| 603 |
update(); |
| 604 |
}, |
| 605 |
|
| 606 |
add_danger: (danger) => { |
| 607 |
def.Action.Danger = <>{def.Action.Danger} {danger}</>; |
| 608 |
update(); |
| 609 |
}, |
| 610 |
|
| 611 |
need_confirmation: (conf) => { |
| 612 |
confirmation = conf; |
| 613 |
confirmed = false; |
| 614 |
def.Action.Danger = null; |
| 615 |
def.Action.DangerButton = true; |
| 616 |
update_footer(); |
| 617 |
}, |
| 618 |
|
| 619 |
show_field, |
| 620 |
|
| 621 |
close: () => { |
| 622 |
dlg.footerProps.dialog_done(); |
| 623 |
} |
| 624 |
}; |
| 625 |
|
| 626 |
for_each_async(def.Inits || [], |
| 627 |
init => { |
| 628 |
if (init) { |
| 629 |
const promise = init.func(self); |
| 630 |
self.run(init.title, promise); |
| 631 |
return promise; |
| 632 |
} else |
| 633 |
return Promise.resolve(); |
| 634 |
}); |
| 635 |
|
| 636 |
return self; |
| 637 |
}; |
| 638 |
|
| 639 |
/* GENERIC FIELD TYPES |
| 640 |
*/ |
| 641 |
|
| 642 |
export const TextInput = (tag, title, options) => { |
| 643 |
return { |
| 644 |
tag, |
| 645 |
title, |
| 646 |
options, |
| 647 |
initial_value: options.value || "", |
| 648 |
|
| 649 |
render: (val, change, validated) => |
| 650 |
<TextInputPF4 data-field={tag} data-field-type="text-input" |
| 651 |
validated={validated} |
| 652 |
aria-label={title} |
| 653 |
value={val} |
| 654 |
isDisabled={options.disabled} |
| 655 |
onChange={(_event, value) => change(value)} /> |
| 656 |
}; |
| 657 |
}; |
| 658 |
|
| 659 |
const PassInputElement = ({ tag, title, options, val, change, validated }) => { |
| 660 |
const [show, setShow] = useState(false); |
| 661 |
|
| 662 |
return ( |
| 663 |
<InputGroup> |
| 664 |
<TextInputPF4 data-field={tag} data-field-type="text-input" |
| 665 |
validated={validated} |
| 666 |
disabled={options.disabled} |
| 667 |
aria-label={title} |
| 668 |
autoComplete={options.new_password ? "new-password" : null} |
| 669 |
type={show ? "text" : "password"} |
| 670 |
value={val} |
| 671 |
onChange={(_event, value) => change(value)} /> |
| 672 |
<Button variant="control" |
| 673 |
onClick={() => setShow(!show)} |
| 674 |
isDisabled={options.disabled}> |
| 675 |
{ show ? <EyeSlashIcon /> : <EyeIcon /> } |
| 676 |
</Button> |
| 677 |
</InputGroup>); |
| 678 |
}; |
| 679 |
|
| 680 |
export const PassInput = (tag, title, options) => { |
| 681 |
return { |
| 682 |
tag, |
| 683 |
title, |
| 684 |
options, |
| 685 |
initial_value: options.value || "", |
| 686 |
|
| 687 |
render: (val, change, validated) => |
| 688 |
<PassInputElement tag={tag} |
| 689 |
title={title} |
| 690 |
options={options} |
| 691 |
val={val} |
| 692 |
change={change} |
| 693 |
validated={validated} /> |
| 694 |
}; |
| 695 |
}; |
| 696 |
|
| 697 |
const TypeAheadSelectElement = ({ value, options, change }) => { |
| 698 |
return ( |
| 699 |
<TypeaheadSelect toggleProps={ { id: "nfs-path-on-server" } } |
| 700 |
isScrollable |
| 701 |
isCreatable |
| 702 |
createOptionMessage={val => cockpit.format(_("Use $0"), val)} |
| 703 |
selected={value} |
| 704 |
onSelect={(_, value) => change(value)} |
| 705 |
onClearSelection={() => change("")} |
| 706 |
isDisabled={options.disabled} |
| 707 |
selectOptions={options.choices.map(entry => ({ value: entry, content: entry }))} /> |
| 708 |
); |
| 709 |
}; |
| 710 |
|
| 711 |
export const ComboBox = (tag, title, options) => { |
| 712 |
return { |
| 713 |
tag, |
| 714 |
title, |
| 715 |
options, |
| 716 |
initial_value: options.value || "", |
| 717 |
|
| 718 |
render: (val, change, validated) => { |
| 719 |
return ( |
| 720 |
<div data-field={tag} data-field-type="combobox"> |
| 721 |
<TypeAheadSelectElement value={val} options={options} change={change} /> |
| 722 |
</div> |
| 723 |
); |
| 724 |
} |
| 725 |
}; |
| 726 |
}; |
| 727 |
|
| 728 |
export const SelectOne = (tag, title, options) => { |
| 729 |
return { |
| 730 |
tag, |
| 731 |
title, |
| 732 |
options, |
| 733 |
initial_value: options.value || options.choices[0].value, |
| 734 |
|
| 735 |
render: (val, change, validated) => { |
| 736 |
return ( |
| 737 |
<div data-field={tag} data-field-type="select" data-value={val}> |
| 738 |
<FormSelect value={val} aria-label={tag} |
| 739 |
validated={validated} |
| 740 |
onChange={(_, value) => change(value)}> |
| 741 |
{ options.choices.map(c => <FormSelectOption value={c.value} isDisabled={c.disabled} |
| 742 |
key={c.title} label={c.title} />) } |
| 743 |
</FormSelect> |
| 744 |
</div> |
| 745 |
); |
| 746 |
} |
| 747 |
}; |
| 748 |
}; |
| 749 |
|
| 750 |
export const SelectOneRadio = (tag, title, options) => { |
| 751 |
return { |
| 752 |
tag, |
| 753 |
title, |
| 754 |
options, |
| 755 |
initial_value: options.value || options.choices[0].value, |
| 756 |
hasNoPaddingTop: true, |
| 757 |
|
| 758 |
render: (val, change) => { |
| 759 |
const vertical = options?.vertical || false; |
| 760 |
const fields = options.choices.map(c => ( |
| 761 |
<Radio key={c.value} isChecked={val == c.value} data-data={c.value} |
| 762 |
id={tag + '.' + c.value} |
| 763 |
onChange={() => change(c.value)} label={c.title} />)); |
| 764 |
|
| 765 |
if (vertical) { |
| 766 |
return ( |
| 767 |
<div data-field={tag} data-field-type="select-radio"> |
| 768 |
{fields} |
| 769 |
</div>); |
| 770 |
} else { |
| 771 |
return ( |
| 772 |
<Split hasGutter data-field={tag} data-field-type="select-radio"> |
| 773 |
{fields} |
| 774 |
</Split>); |
| 775 |
} |
| 776 |
} |
| 777 |
}; |
| 778 |
}; |
| 779 |
|
| 780 |
export const SelectRow = (tag, headers, options) => { |
| 781 |
return { |
| 782 |
tag, |
| 783 |
title: null, |
| 784 |
options, |
| 785 |
initial_value: options.value || options.choices[0].value, |
| 786 |
|
| 787 |
render: (val, change) => { |
| 788 |
return ( |
| 789 |
<table data-field={tag} data-field-type=" select-row" className="dialog-select-row-table"> |
| 790 |
<thead> |
| 791 |
<tr>{headers.map(h => <th key={h}>{h}</th>)}</tr> |
| 792 |
</thead> |
| 793 |
<tbody> |
| 794 |
{ options.choices.map(row => { |
| 795 |
return ( |
| 796 |
<tr key={row.value} |
| 797 |
onMouseDown={ev => { if (ev && ev.button === 0) change(row.value); }} |
| 798 |
className={row.value == val ? "highlight-ct" : ""}> |
| 799 |
{row.columns.map(c => <td key={c}>{c}</td>)} |
| 800 |
</tr> |
| 801 |
); |
| 802 |
}) |
| 803 |
} |
| 804 |
</tbody> |
| 805 |
</table> |
| 806 |
); |
| 807 |
} |
| 808 |
}; |
| 809 |
}; |
| 810 |
|
| 811 |
function nice_block_name(block) { |
| 812 |
return block_name(client.blocks[block.CryptoBackingDevice] || block); |
| 813 |
} |
| 814 |
|
| 815 |
export const SelectSpaces = (tag, title, options) => { |
| 816 |
return { |
| 817 |
tag, |
| 818 |
title, |
| 819 |
options, |
| 820 |
initial_value: options.value || [], |
| 821 |
hasNoPaddingTop: options.spaces.length == 0, |
| 822 |
|
| 823 |
render: (val, change) => { |
| 824 |
if (options.spaces.length === 0) |
| 825 |
return <span className="text-danger">{options.empty_warning}</span>; |
| 826 |
|
| 827 |
return ( |
| 828 |
<DataList isCompact |
| 829 |
data-field={tag} data-field-type="select-spaces"> |
| 830 |
{ options.spaces.map(spc => { |
| 831 |
const selected = (val.indexOf(spc) >= 0); |
| 832 |
const block = spc.block ? nice_block_name(spc.block) : ""; |
| 833 |
const desc = block === spc.desc ? "" : spc.desc; |
| 834 |
|
| 835 |
const on_change = (_event, checked) => { |
| 836 |
// Be careful to keep "val" in the same order as "options.spaces". |
| 837 |
if (checked && !selected) |
| 838 |
change(options.spaces.filter(v => val.indexOf(v) >= 0 || v == spc)); |
| 839 |
else if (!checked && selected) |
| 840 |
change(val.filter(v => (v != spc))); |
| 841 |
}; |
| 842 |
|
| 843 |
const datalistcells = ( |
| 844 |
<DataListItemCells |
| 845 |
dataListCells={[ |
| 846 |
<DataListCell key="select-space-name" className="select-space-name"> |
| 847 |
{format_size_and_text(spc.size, desc)} |
| 848 |
</DataListCell>, |
| 849 |
<DataListCell alignRight isFilled={false} key="select-space-details" className="select-space-details"> |
| 850 |
{block} |
| 851 |
</DataListCell>, |
| 852 |
]} |
| 853 |
/>); |
| 854 |
|
| 855 |
const key = block || desc; |
| 856 |
|
| 857 |
return ( |
| 858 |
<DataListItem data-space-name={key} key={key}> |
| 859 |
<DataListItemRow> |
| 860 |
<DataListCheck id={(spc.block ? spc.block.Device : spc.desc) + "-row-checkbox"} |
| 861 |
isDisabled={options.min_selected && |
| 862 |
selected && val.length <= options.min_selected} |
| 863 |
isChecked={selected} onChange={on_change} /> |
| 864 |
<label htmlFor={(spc.block ? spc.block.Device : spc.desc) + "-row-checkbox"} |
| 865 |
className='data-list-row-checkbox-label'> |
| 866 |
{datalistcells} |
| 867 |
</label> |
| 868 |
</DataListItemRow> |
| 869 |
</DataListItem> |
| 870 |
); |
| 871 |
}) |
| 872 |
} |
| 873 |
</DataList> |
| 874 |
); |
| 875 |
} |
| 876 |
}; |
| 877 |
}; |
| 878 |
|
| 879 |
export const SelectSpace = (tag, title, options) => { |
| 880 |
return { |
| 881 |
tag, |
| 882 |
title, |
| 883 |
options, |
| 884 |
initial_value: null, |
| 885 |
|
| 886 |
render: (val, change) => { |
| 887 |
if (options.spaces.length === 0) |
| 888 |
return <span className="text-danger">{options.empty_warning}</span>; |
| 889 |
|
| 890 |
return ( |
| 891 |
<DataList isCompact |
| 892 |
data-field={tag} data-field-type="select-spaces"> |
| 893 |
{ options.spaces.map(spc => { |
| 894 |
const block = spc.block ? nice_block_name(spc.block) : ""; |
| 895 |
const desc = block === spc.desc ? "" : spc.desc; |
| 896 |
const on_change = (event) => { |
| 897 |
if (event.target.checked) |
| 898 |
change(spc); |
| 899 |
}; |
| 900 |
|
| 901 |
const key = block || desc; |
| 902 |
|
| 903 |
return ( |
| 904 |
<DataListItem data-space-name={key} key={key}> |
| 905 |
<DataListItemRow> |
| 906 |
<div className="pf-v6-c-data-list__item-control"> |
| 907 |
<div className="pf-v6-c-data-list__check"> |
| 908 |
<input type='radio' value={desc} name='space' checked={val == spc} onChange={on_change} /> |
| 909 |
</div> |
| 910 |
</div> |
| 911 |
<DataListItemCells |
| 912 |
dataListCells={[ |
| 913 |
<DataListCell key="select-space-name" className="select-space-name"> |
| 914 |
{format_size_and_text(spc.size, desc)} |
| 915 |
</DataListCell>, |
| 916 |
<DataListCell alignRight isFilled={false} key="select-space-details" className="select-space-details"> |
| 917 |
{block} |
| 918 |
</DataListCell>, |
| 919 |
]} |
| 920 |
/> |
| 921 |
</DataListItemRow> |
| 922 |
</DataListItem> |
| 923 |
); |
| 924 |
}) |
| 925 |
} |
| 926 |
</DataList> |
| 927 |
); |
| 928 |
} |
| 929 |
}; |
| 930 |
}; |
| 931 |
|
| 932 |
const CheckBoxComponent = ({ tag, val, title, tooltip, disabled, update_function }) => { |
| 933 |
return ( |
| 934 |
<Checkbox data-field={tag} data-field-type="checkbox" |
| 935 |
id={tag} |
| 936 |
isChecked={val} |
| 937 |
isDisabled={disabled} |
| 938 |
label={ |
| 939 |
<> |
| 940 |
{title} |
| 941 |
{ tooltip && <Popover bodyContent={tooltip}> |
| 942 |
<Button icon={<HelpIcon />} className="dialog-item-tooltip" variant="link" /> |
| 943 |
</Popover> |
| 944 |
} |
| 945 |
</> |
| 946 |
} |
| 947 |
onChange={(_, v) => update_function(v)} /> |
| 948 |
); |
| 949 |
}; |
| 950 |
|
| 951 |
export const CheckBoxes = (tag, title, options) => { |
| 952 |
return { |
| 953 |
tag, |
| 954 |
title, |
| 955 |
options, |
| 956 |
initial_value: options.value || { }, |
| 957 |
hasNoPaddingTop: true, |
| 958 |
|
| 959 |
render: (val, change) => { |
| 960 |
const fieldset = options.fields.map(field => { |
| 961 |
const ftag = tag + "." + field.tag; |
| 962 |
const fval = (val[field.tag] !== undefined) ? val[field.tag] : false; |
| 963 |
function fchange(newval) { |
| 964 |
val[field.tag] = newval; |
| 965 |
change(val); |
| 966 |
} |
| 967 |
|
| 968 |
if (field.type === undefined || field.type == "checkbox") |
| 969 |
return <CheckBoxComponent key={`checkbox-${ftag}`} |
| 970 |
tag={ftag} |
| 971 |
val={fval} |
| 972 |
title={field.title} |
| 973 |
disabled={field.disabled} |
| 974 |
tooltip={field.tooltip} |
| 975 |
options={options} |
| 976 |
update_function={fchange} />; |
| 977 |
else if (field.type == "checkboxWithInput") |
| 978 |
return <TextInputCheckedComponent key={`checkbox-with-text-${ftag}`} |
| 979 |
tag={ftag} |
| 980 |
val={fval} |
| 981 |
title={field.title} |
| 982 |
update_function={fchange} />; |
| 983 |
else |
| 984 |
return null; |
| 985 |
}); |
| 986 |
|
| 987 |
if (options.fields.length == 1) |
| 988 |
return fieldset; |
| 989 |
|
| 990 |
// eslint-disable-next-line react/jsx-no-useless-fragment |
| 991 |
return <>{ fieldset }</>; |
| 992 |
} |
| 993 |
}; |
| 994 |
}; |
| 995 |
|
| 996 |
const TextInputCheckedComponent = ({ tag, val, title, update_function }) => { |
| 997 |
return ( |
| 998 |
<div data-field={tag} data-field-type="text-input-checked" key={tag}> |
| 999 |
<Checkbox isChecked={val !== false} |
| 1000 |
id={tag} |
| 1001 |
label={title} |
| 1002 |
onChange={(_event, checked) => update_function(checked ? "" : false)} /> |
| 1003 |
{val !== false && <TextInputPF4 id={tag + "-input"} value={val} onChange={(_event, value) => update_function(value)} />} |
| 1004 |
</div> |
| 1005 |
); |
| 1006 |
}; |
| 1007 |
|
| 1008 |
export const Skip = (className, options) => { |
| 1009 |
return { |
| 1010 |
tag: false, |
| 1011 |
title: null, |
| 1012 |
options, |
| 1013 |
initial_value: false, |
| 1014 |
|
| 1015 |
render: () => { |
| 1016 |
return <div className={className} />; |
| 1017 |
} |
| 1018 |
}; |
| 1019 |
}; |
| 1020 |
|
| 1021 |
export const Message = (text, options) => { |
| 1022 |
return { |
| 1023 |
options, |
| 1024 |
|
| 1025 |
render: () => <HelperText><HelperTextItem icon={<InfoIcon />}>{text}</HelperTextItem></HelperText>, |
| 1026 |
}; |
| 1027 |
}; |
| 1028 |
|
| 1029 |
function size_slider_round(value, round) { |
| 1030 |
if (round) { |
| 1031 |
if (typeof round == "function") |
| 1032 |
value = round(value); |
| 1033 |
else |
| 1034 |
value = Math.round(value / round) * round; |
| 1035 |
} else { |
| 1036 |
// Only produce integers by default |
| 1037 |
value = Math.round(value); |
| 1038 |
} |
| 1039 |
return value; |
| 1040 |
} |
| 1041 |
|
| 1042 |
class SizeSliderElement extends React.Component { |
| 1043 |
constructor(props) { |
| 1044 |
super(); |
| 1045 |
this.units = get_byte_units(props.value || props.max); |
| 1046 |
this.state = { unit: this.units.find(u => u.selected).factor }; |
| 1047 |
} |
| 1048 |
|
| 1049 |
render() { |
| 1050 |
const { val, max, round, onChange, tag } = this.props; |
| 1051 |
const min = this.props.min || 0; |
| 1052 |
const { unit } = this.state; |
| 1053 |
|
| 1054 |
const change_slider = (_event, f) => { |
| 1055 |
onChange(Math.max(min, size_slider_round(f, round))); |
| 1056 |
}; |
| 1057 |
|
| 1058 |
const change_text = (value) => { |
| 1059 |
/* We keep the literal string as the value and only |
| 1060 |
* interpret it below in the validate function inside |
| 1061 |
* SizeSlider. This allows people to freely interact with |
| 1062 |
* the text input without getting the text changed all the |
| 1063 |
* time by rounding, etc. |
| 1064 |
*/ |
| 1065 |
onChange({ text: value, unit }); |
| 1066 |
}; |
| 1067 |
|
| 1068 |
let slider_val; |
| 1069 |
let text_val; |
| 1070 |
if (val.text && val.unit) { |
| 1071 |
slider_val = Number(val.text) * val.unit; |
| 1072 |
text_val = val.text; |
| 1073 |
} else { |
| 1074 |
slider_val = val; |
| 1075 |
text_val = cockpit.format_number(val / unit); |
| 1076 |
} |
| 1077 |
|
| 1078 |
const change_unit = (_, u) => { |
| 1079 |
if (val.unit) |
| 1080 |
onChange({ text: val.text, unit: Number(u) }); |
| 1081 |
else |
| 1082 |
onChange(size_slider_round(val / unit * Number(u), round)); |
| 1083 |
this.setState({ unit: Number(u) }); |
| 1084 |
}; |
| 1085 |
|
| 1086 |
return ( |
| 1087 |
<Grid hasGutter className="size-slider"> |
| 1088 |
<GridItem span={12} sm={8}> |
| 1089 |
<Slider showBoundaries={false} min={min} max={max} step={(max - min) / 500} |
| 1090 |
value={slider_val} onChange={change_slider} /> |
| 1091 |
</GridItem> |
| 1092 |
<GridItem span={6} sm={2}> |
| 1093 |
<TextInputPF4 className="size-text" value={text_val} onChange={(_event, value) => change_text(value)} /> |
| 1094 |
</GridItem> |
| 1095 |
<GridItem span={6} sm={2}> |
| 1096 |
<FormSelect className="size-unit" value={unit} aria-label={tag} onChange={change_unit}> |
| 1097 |
{ this.units.map(u => <FormSelectOption value={u.factor} key={u.name} label={u.name} />) } |
| 1098 |
</FormSelect> |
| 1099 |
</GridItem> |
| 1100 |
</Grid> |
| 1101 |
); |
| 1102 |
} |
| 1103 |
} |
| 1104 |
|
| 1105 |
export const SizeSlider = (tag, title, options) => { |
| 1106 |
const validate = (val, vals) => { |
| 1107 |
let msg = null; |
| 1108 |
|
| 1109 |
if (val.text && val.unit) { |
| 1110 |
// Convert to number. |
| 1111 |
const unit = val.unit; |
| 1112 |
|
| 1113 |
val = Number(val.text) * unit; |
| 1114 |
|
| 1115 |
// As a special case, if the user types something that |
| 1116 |
// looks like the maximum (or minimum) when formatted, |
| 1117 |
// always use exactly the maximum (or minimum). Otherwise |
| 1118 |
// we have the confusing possibility that with the exact |
| 1119 |
// same string in the text input, the size is sometimes |
| 1120 |
// too large (or too small) and sometimes not. |
| 1121 |
|
| 1122 |
const sanitize = (limit) => { |
| 1123 |
const fmt = cockpit.format_number(limit / unit); |
| 1124 |
const parse = +fmt * unit; |
| 1125 |
|
| 1126 |
if (val == parse) |
| 1127 |
val = limit; |
| 1128 |
}; |
| 1129 |
|
| 1130 |
sanitize(all_options.min || 0); |
| 1131 |
sanitize(all_options.max); |
| 1132 |
|
| 1133 |
val = size_slider_round(val, all_options.round); |
| 1134 |
vals[tag] = val; |
| 1135 |
} |
| 1136 |
|
| 1137 |
if (isNaN(val)) |
| 1138 |
msg = _("Size must be a number"); |
| 1139 |
else if (val === 0) |
| 1140 |
msg = _("Size cannot be zero"); |
| 1141 |
else if (val < 0) |
| 1142 |
msg = _("Size cannot be negative"); |
| 1143 |
else if (!options.allow_infinite && val > options.max) |
| 1144 |
msg = _("Size is too large"); |
| 1145 |
else if (options.min !== undefined && val < options.min) |
| 1146 |
msg = cockpit.format(_("Size must be at least $0"), fmt_size(options.min)); |
| 1147 |
else if (options.validate) |
| 1148 |
msg = options.validate(val, vals); |
| 1149 |
|
| 1150 |
return msg; |
| 1151 |
}; |
| 1152 |
|
| 1153 |
/* This object might be mutated by dialog.set_options(), so we |
| 1154 |
have to use it below for the 'max' option in order to pick up |
| 1155 |
changes to it. |
| 1156 |
*/ |
| 1157 |
const all_options = Object.assign({ }, options, { validate }); |
| 1158 |
|
| 1159 |
return { |
| 1160 |
tag, |
| 1161 |
title, |
| 1162 |
options: all_options, |
| 1163 |
initial_value: options.value || options.max || 0, |
| 1164 |
|
| 1165 |
render: (val, change) => { |
| 1166 |
return ( |
| 1167 |
<div data-field={tag} data-field-type="size-slider"> |
| 1168 |
<SizeSliderElement val={val} |
| 1169 |
max={all_options.max} |
| 1170 |
min={all_options.min} |
| 1171 |
round={all_options.round} |
| 1172 |
tag={tag} |
| 1173 |
onChange={change} /> |
| 1174 |
</div> |
| 1175 |
); |
| 1176 |
} |
| 1177 |
}; |
| 1178 |
}; |
| 1179 |
|
| 1180 |
export const Group = (title, fields) => { |
| 1181 |
return { |
| 1182 |
tag: null, |
| 1183 |
title, |
| 1184 |
is_group: true, |
| 1185 |
hasNoPaddingTop: true, |
| 1186 |
options: { nested_fields: fields }, |
| 1187 |
|
| 1188 |
render: (val, change) => null, |
| 1189 |
}; |
| 1190 |
}; |
| 1191 |
|
| 1192 |
export const BlockingMessage = (usage) => { |
| 1193 |
const usage_desc = { |
| 1194 |
pvol: _("physical volume of LVM2 volume group"), |
| 1195 |
"mdraid-member": _("member of MDRAID device"), |
| 1196 |
vdo: _("backing device for VDO device"), |
| 1197 |
"stratis-pool-member": _("member of Stratis pool"), |
| 1198 |
mounted: _("Filesystem outside the target"), |
| 1199 |
"btrfs-device": _("device of btrfs volume"), |
| 1200 |
}; |
| 1201 |
|
| 1202 |
const rows = []; |
| 1203 |
usage.forEach(use => { |
| 1204 |
if (use.blocking && use.block) { |
| 1205 |
const name = teardown_block_name(use); |
| 1206 |
rows.push({ |
| 1207 |
columns: [name, use.location || "-", usage_desc[use.usage] || "-"] |
| 1208 |
}); |
| 1209 |
} |
| 1210 |
}); |
| 1211 |
|
| 1212 |
return ( |
| 1213 |
<div> |
| 1214 |
<HelperText><HelperTextItem variant="warning">{_("This device is currently in use.")}</HelperTextItem></HelperText> |
| 1215 |
<ListingTable variant='compact' |
| 1216 |
columns={[ |
| 1217 |
{ title: _("Device") }, |
| 1218 |
{ title: _("Location") }, |
| 1219 |
{ title: _("Use") } |
| 1220 |
]} |
| 1221 |
rows={rows} /> |
| 1222 |
</div>); |
| 1223 |
}; |
| 1224 |
|
| 1225 |
const UsersPopover = ({ users }) => { |
| 1226 |
const max = 10; |
| 1227 |
const services = users.filter(u => u.unit); |
| 1228 |
const processes = users.filter(u => u.pid); |
| 1229 |
|
| 1230 |
return ( |
| 1231 |
<Popover |
| 1232 |
appendTo={document.body} |
| 1233 |
bodyContent={ |
| 1234 |
<> |
| 1235 |
{ services.length > 0 |
| 1236 |
? <> |
| 1237 |
<p><b>{_("Services using the location")}</b></p> |
| 1238 |
<List> |
| 1239 |
{ services.slice(0, max).map((u, i) => <ListItem key={i}>{u.unit.replace(/\.service$/, "")}</ListItem>) } |
| 1240 |
{ services.length > max ? <ListItem key={max}>...</ListItem> : null } |
| 1241 |
</List> |
| 1242 |
</> |
| 1243 |
: null |
| 1244 |
} |
| 1245 |
{ services.length > 0 && processes.length > 0 |
| 1246 |
? <br /> |
| 1247 |
: null |
| 1248 |
} |
| 1249 |
{ processes.length > 0 |
| 1250 |
? <> |
| 1251 |
<p><b>{_("Processes using the location")}</b></p> |
| 1252 |
<List> |
| 1253 |
{ processes.slice(0, max).map((u, i) => <ListItem key={i}>{u.comm} (user: {u.user}, pid: {u.pid})</ListItem>) } |
| 1254 |
{ processes.length > max ? <ListItem key={max}>...</ListItem> : null } |
| 1255 |
</List> |
| 1256 |
</> |
| 1257 |
: null |
| 1258 |
} |
| 1259 |
</>}> |
| 1260 |
<Button icon={<ExclamationTriangleIcon className="ct-icon-exclamation-triangle" />} variant="link" style={{ visibility: users.length == 0 ? "hidden" : null }}> |
| 1261 |
{ "\n" } |
| 1262 |
{_("Currently in use")} |
| 1263 |
</Button> |
| 1264 |
</Popover>); |
| 1265 |
}; |
| 1266 |
|
| 1267 |
function is_expected_unmount(usage, expect_single_unmount) { |
| 1268 |
return (expect_single_unmount && usage.length == 1 && |
| 1269 |
usage[0].usage == "mounted" && usage[0].location == expect_single_unmount); |
| 1270 |
} |
| 1271 |
|
| 1272 |
const teardown_block_name = use => { |
| 1273 |
const block_stratis = client.blocks_stratis_fsys[use.block.path]; |
| 1274 |
const block_btrfs = client.blocks_fsys_btrfs[use.block.path]; |
| 1275 |
let name; |
| 1276 |
if (block_stratis) { |
| 1277 |
name = block_stratis.Devnode; |
| 1278 |
} else if (block_btrfs && use.name) { |
| 1279 |
name = use.name; |
| 1280 |
} else { |
| 1281 |
name = block_name(client.blocks[use.block.CryptoBackingDevice] || use.block); |
| 1282 |
} |
| 1283 |
|
| 1284 |
return name.replace(/^\/dev\//, ""); |
| 1285 |
}; |
| 1286 |
|
| 1287 |
export const TeardownMessage = (usage, expect_single_unmount) => { |
| 1288 |
if (!usage.Teardown) |
| 1289 |
return null; |
| 1290 |
|
| 1291 |
if (client.in_anaconda_mode() && !expect_single_unmount) |
| 1292 |
return <AnacondaTeardownMessage usage={usage} />; |
| 1293 |
|
| 1294 |
if (is_expected_unmount(usage, expect_single_unmount)) |
| 1295 |
return <StopProcessesMessage mount_point={expect_single_unmount} users={usage[0].users} />; |
| 1296 |
|
| 1297 |
const rows = []; |
| 1298 |
usage.forEach((use, index) => { |
| 1299 |
if (use.block) { |
| 1300 |
const name = teardown_block_name(use); |
| 1301 |
let location = use.location; |
| 1302 |
|
| 1303 |
/* Don't show mount points used internally by Cockpit. |
| 1304 |
* It's fine to tear them down, but we don't want people |
| 1305 |
* to start worrying about them. |
| 1306 |
*/ |
| 1307 |
if (location && location.startsWith(BTRFS_TOOL_MOUNT_PATH)) |
| 1308 |
return; |
| 1309 |
|
| 1310 |
if (use.usage == "mounted") { |
| 1311 |
location = client.strip_mount_point_prefix(location); |
| 1312 |
if (location === false) |
| 1313 |
location = _("(Not part of target)"); |
| 1314 |
} |
| 1315 |
rows.push({ |
| 1316 |
columns: [name, |
| 1317 |
location || "-", |
| 1318 |
use.actions.length ? use.actions.join(", ") : "-", |
| 1319 |
{ |
| 1320 |
title: <UsersPopover users={use.users || []} />, |
| 1321 |
props: { className: "pf-v6-u-text-align-right" } |
| 1322 |
} |
| 1323 |
] |
| 1324 |
}); |
| 1325 |
} |
| 1326 |
}); |
| 1327 |
|
| 1328 |
if (rows.length == 0) |
| 1329 |
return null; |
| 1330 |
|
| 1331 |
return ( |
| 1332 |
<div className="modal-footer-teardown"> |
| 1333 |
<p>{_("These changes will be made:")}</p> |
| 1334 |
<ListingTable variant='compact' |
| 1335 |
columns={[ |
| 1336 |
{ title: _("Device") }, |
| 1337 |
{ title: _("Location") }, |
| 1338 |
{ title: _("Action") }, |
| 1339 |
{ title: "" } |
| 1340 |
]} |
| 1341 |
rows={rows} /> |
| 1342 |
</div>); |
| 1343 |
}; |
| 1344 |
|
| 1345 |
const AnacondaTeardownMessage = ({ usage }) => { |
| 1346 |
const rows = []; |
| 1347 |
|
| 1348 |
usage.forEach((use, index) => { |
| 1349 |
if (use.data_warning) { |
| 1350 |
const name = teardown_block_name(use); |
| 1351 |
const location = client.strip_mount_point_prefix(use.location) || use.block.IdLabel || "-"; |
| 1352 |
|
| 1353 |
rows.push( |
| 1354 |
<Tr key={index}> |
| 1355 |
<Td className="pf-v6-u-font-weight-bold">{name}</Td> |
| 1356 |
<Td>{location}</Td> |
| 1357 |
<Td>{use.data_warning}</Td> |
| 1358 |
</Tr>); |
| 1359 |
} |
| 1360 |
}); |
| 1361 |
|
| 1362 |
if (rows.length > 0) { |
| 1363 |
return ( |
| 1364 |
<div className="modal-footer-teardown"> |
| 1365 |
<HelperText> |
| 1366 |
<HelperTextItem variant="error"> |
| 1367 |
{_("Important data might be deleted:")} |
| 1368 |
</HelperTextItem> |
| 1369 |
</HelperText> |
| 1370 |
<Table variant="compact" borders={false}><Tbody>{rows}</Tbody></Table> |
| 1371 |
</div>); |
| 1372 |
} |
| 1373 |
}; |
| 1374 |
|
| 1375 |
export function teardown_danger_message(usage, expect_single_unmount) { |
| 1376 |
if (is_expected_unmount(usage, expect_single_unmount)) |
| 1377 |
return stop_processes_danger_message(usage[0].users); |
| 1378 |
|
| 1379 |
const usage_with_users = usage.filter(u => u.users); |
| 1380 |
const n_processes = usage_with_users.reduce((sum, u) => sum + u.users.filter(u => u.pid).length, 0); |
| 1381 |
const n_services = usage_with_users.reduce((sum, u) => sum + u.users.filter(u => u.unit).length, 0); |
| 1382 |
if (n_processes > 0 && n_services > 0) { |
| 1383 |
return _("Related processes and services will be forcefully stopped."); |
| 1384 |
} else if (n_processes > 0) { |
| 1385 |
return _("Related processes will be forcefully stopped."); |
| 1386 |
} else if (n_services > 0) { |
| 1387 |
return _("Related services will be forcefully stopped."); |
| 1388 |
} else { |
| 1389 |
return null; |
| 1390 |
} |
| 1391 |
} |
| 1392 |
|
| 1393 |
export function init_teardown_usage(client, usage, expect_single_unmount) { |
| 1394 |
return { |
| 1395 |
title: _("Checking filesystem usage"), |
| 1396 |
func: async function (dlg) { |
| 1397 |
let have_data = false; |
| 1398 |
for (const u of usage) { |
| 1399 |
if (u.usage == "mounted") { |
| 1400 |
u.users = await client.find_mount_users(u.location); |
| 1401 |
} |
| 1402 |
if (client.in_anaconda_mode() && !expect_single_unmount && u.block) { |
| 1403 |
if (u.block.IdUsage == "filesystem" && |
| 1404 |
["xfs", "ext2", "ext3", "ext4", "btrfs", "vfat", "ntfs"].indexOf(u.block.IdType) >= 0) { |
| 1405 |
const empty = await cockpit.script(fsys_is_empty_sh, |
| 1406 |
[decode_filename(u.block.PreferredDevice)], |
| 1407 |
{ superuser: "require", err: "message" }); |
| 1408 |
if (empty.trim() != "yes") { |
| 1409 |
try { |
| 1410 |
const info = JSON.parse(empty); |
| 1411 |
u.data_warning = cockpit.format(_("$0 used, $1 total"), |
| 1412 |
fmt_size((info.total - info.free) * info.unit), |
| 1413 |
fmt_size(info.total * info.unit)); |
| 1414 |
} catch { |
| 1415 |
u.data_warning = _("Device contains unrecognized data"); |
| 1416 |
} |
| 1417 |
} |
| 1418 |
} else if (u.block.IdUsage == "crypto" && !client.blocks_cleartext[u.block.path]) { |
| 1419 |
u.data_warning = _("Locked encrypted device might contain data"); |
| 1420 |
} else if (!client.blocks_ptable[u.block.path] && |
| 1421 |
u.block.IdUsage && u.block.IdUsage != "raid") { |
| 1422 |
u.data_warning = _("Device contains unrecognized data"); |
| 1423 |
} |
| 1424 |
if (u.data_warning) |
| 1425 |
have_data = true; |
| 1426 |
} |
| 1427 |
} |
| 1428 |
|
| 1429 |
if (have_data) { |
| 1430 |
usage.Teardown = true; |
| 1431 |
dlg.need_confirmation(_("I confirm I want to lose this data forever")); |
| 1432 |
} else if (client.in_anaconda_mode() && !expect_single_unmount) { |
| 1433 |
dlg.need_confirmation(null); |
| 1434 |
} else { |
| 1435 |
const msg = teardown_danger_message(usage, expect_single_unmount); |
| 1436 |
if (msg) |
| 1437 |
dlg.add_danger(msg); |
| 1438 |
} |
| 1439 |
dlg.set_attribute("Teardown", TeardownMessage(usage, expect_single_unmount)); |
| 1440 |
} |
| 1441 |
}; |
| 1442 |
} |
| 1443 |
|
| 1444 |
export const StopProcessesMessage = ({ mount_point, users }) => { |
| 1445 |
if (!users || users.length == 0) |
| 1446 |
return null; |
| 1447 |
|
| 1448 |
const process_rows = users.filter(u => u.pid).map(u => { |
| 1449 |
return { |
| 1450 |
columns: [ |
| 1451 |
u.pid, |
| 1452 |
{ title: u.cmd.substring(0, 100), props: { modifier: "breakWord" } }, |
| 1453 |
u.user || "-", |
| 1454 |
{ title: format_delay(-u.since * 1000), props: { modifier: "nowrap" } } |
| 1455 |
] |
| 1456 |
}; |
| 1457 |
}); |
| 1458 |
|
| 1459 |
const service_rows = users.filter(u => u.unit).map(u => { |
| 1460 |
return { |
| 1461 |
columns: [ |
| 1462 |
{ title: u.unit.replace(/\.service$/, ""), props: { modifier: "breakWord" } }, |
| 1463 |
{ title: u.cmd.substring(0, 100), props: { modifier: "breakWord" } }, |
| 1464 |
{ title: u.desc || "", props: { modifier: "breakWord" } }, |
| 1465 |
{ title: format_delay(-u.since * 1000), props: { modifier: "nowrap" } } |
| 1466 |
] |
| 1467 |
}; |
| 1468 |
}); |
| 1469 |
|
| 1470 |
// If both tables are shown, we press the columns into a uniform |
| 1471 |
// width to reduce the visual mess. |
| 1472 |
const colprops = (process_rows.length > 0 && service_rows.length > 0) ? { width: 25 } : { }; |
| 1473 |
|
| 1474 |
return ( |
| 1475 |
<div className="modal-footer-teardown"> |
| 1476 |
{ process_rows.length > 0 |
| 1477 |
? <> |
| 1478 |
<p>{fmt_to_fragments(_("The mount point $0 is in use by these processes:"), <b>{mount_point}</b>)}</p> |
| 1479 |
<ListingTable variant='compact' |
| 1480 |
columns={ |
| 1481 |
[ |
| 1482 |
{ title: _("PID"), props: colprops }, |
| 1483 |
{ title: _("Command"), props: colprops }, |
| 1484 |
{ title: _("User"), props: colprops }, |
| 1485 |
{ title: _("Started"), props: colprops } |
| 1486 |
] |
| 1487 |
} |
| 1488 |
rows={process_rows} /> |
| 1489 |
</> |
| 1490 |
: null |
| 1491 |
} |
| 1492 |
{ process_rows.length > 0 && service_rows.length > 0 |
| 1493 |
? <br /> |
| 1494 |
: null |
| 1495 |
} |
| 1496 |
{ service_rows.length > 0 |
| 1497 |
? <> |
| 1498 |
<p>{fmt_to_fragments(_("The mount point $0 is in use by these services:"), <b>{mount_point}</b>)}</p> |
| 1499 |
<ListingTable variant='compact' |
| 1500 |
columns={ |
| 1501 |
[ |
| 1502 |
{ title: _("Service"), props: colprops }, |
| 1503 |
{ title: _("Command"), props: colprops }, |
| 1504 |
{ title: _("Description"), props: colprops }, |
| 1505 |
{ title: _("Started"), props: colprops } |
| 1506 |
] |
| 1507 |
} |
| 1508 |
rows={service_rows} /> |
| 1509 |
</> |
| 1510 |
: null |
| 1511 |
} |
| 1512 |
</div>); |
| 1513 |
}; |
| 1514 |
|
| 1515 |
export const stop_processes_danger_message = (users) => { |
| 1516 |
const n_processes = users.filter(u => u.pid).length; |
| 1517 |
const n_services = users.filter(u => u.unit).length; |
| 1518 |
|
| 1519 |
if (n_processes > 0 && n_services > 0) |
| 1520 |
return _("The listed processes and services will be forcefully stopped."); |
| 1521 |
else if (n_processes > 0) |
| 1522 |
return _("The listed processes will be forcefully stopped."); |
| 1523 |
else if (n_services > 0) |
| 1524 |
return _("The listed services will be forcefully stopped."); |
| 1525 |
else |
| 1526 |
return null; |
| 1527 |
}; |