| 1 |
= cockpit.js: Utilities |
| 2 |
|
| 3 |
Various utility functions |
| 4 |
|
| 5 |
[[cockpit-format]] |
| 6 |
== cockpit.format() |
| 7 |
|
| 8 |
.... |
| 9 |
string = cockpit.format(template, args) |
| 10 |
string = cockpit.format(template, [arg, ...]) |
| 11 |
.... |
| 12 |
|
| 13 |
Format a string interpolating `+args+` into `+template+` using shell |
| 14 |
like syntax. The `+args+` may be either an array or javascript object. |
| 15 |
The `+template+` can contain fields that look like `+$name+` or |
| 16 |
`+${name}+` or `+$0+`. Numeric fields are used with array `+args+` and |
| 17 |
start at zero. |
| 18 |
|
| 19 |
In the second form, multiple `+arg+` arguments may be passed directly, |
| 20 |
and interpolated as as numeric fields in the `+template+`. |
| 21 |
|
| 22 |
All falsy arguments except the numbers `+0+` and `+0.0+`are replaced by |
| 23 |
an empty string. |
| 24 |
|
| 25 |
[[cockpit-format-number]] |
| 26 |
== cockpit.format_number() |
| 27 |
|
| 28 |
.... |
| 29 |
string = cockpit.format_number(number, [precision]) |
| 30 |
.... |
| 31 |
|
| 32 |
Formats `+number+` into a displayable `+string+`. If the number is not |
| 33 |
an integer, it is rounded to the given number of decimal places, |
| 34 |
defaulting to 3. If the number is near zero, but not quite zero it is |
| 35 |
rounded to the smallest non-zero value of the given precision; i.e. |
| 36 |
±0.001 for default precision 3. |
| 37 |
|
| 38 |
If `+number+` is `+null+` or `+undefined+` an empty string will be |
| 39 |
returned. |
| 40 |
|
| 41 |
[[cockpit-format-bytes]] |
| 42 |
== cockpit.format_bytes() |
| 43 |
|
| 44 |
.... |
| 45 |
string = cockpit.format_bytes(number, [options]) |
| 46 |
.... |
| 47 |
|
| 48 |
Formats `+number+` into a displayable `+string+` with a suffix, such as |
| 49 |
_kB_ or _MB_. |
| 50 |
|
| 51 |
By default, SI units are used. IEC units (1024-based) can be requested |
| 52 |
by including `+base2: true+` in `+options+`. |
| 53 |
|
| 54 |
By default, non-integer numbers will be formatted with 3 digits of |
| 55 |
precision. This can be changed with `+options.precision+`. |
| 56 |
|
| 57 |
If `+number+` is `+null+` or `+undefined+` an empty string will be |
| 58 |
returned. |
| 59 |
|
| 60 |
[[cockpit-format-bytes-per-sec]] |
| 61 |
== cockpit.format_bytes_per_sec() |
| 62 |
|
| 63 |
.... |
| 64 |
string = cockpit.format_bytes_per_sec(number, [options]) |
| 65 |
.... |
| 66 |
|
| 67 |
Format `+number+` of bytes into a displayable speed `+string+`. |
| 68 |
|
| 69 |
This function is mostly equivalent to `+cockpit.format_bytes()+` but the |
| 70 |
returned value contains a unit like _kB/s_ or _MB/s_. |
| 71 |
|
| 72 |
[[cockpit-format-bits-per-sec]] |
| 73 |
== cockpit.format_bits_per_sec() |
| 74 |
|
| 75 |
.... |
| 76 |
string = cockpit.format_bits_per_sec(number, [options]) |
| 77 |
.... |
| 78 |
|
| 79 |
Format `+number+` of bits into a displayable speed `+string+`. |
| 80 |
|
| 81 |
This function is mostly equivalent to `+cockpit.format_bytes()+` but the |
| 82 |
returned value contains a unit like _kbps_ or _Mbps_. |
| 83 |
|
| 84 |
This function does not support IEC units. `+base2+` may not be passed as |
| 85 |
part of `+options+`. |
| 86 |
|
| 87 |
[[cockpit-init]] |
| 88 |
== cockpit.init() |
| 89 |
|
| 90 |
.... |
| 91 |
await cockpit.init(); |
| 92 |
|
| 93 |
cockpit.init().then(() => { ... }); |
| 94 |
.... |
| 95 |
|
| 96 |
Requests initialization of the Cockpit client library. This will ensure |
| 97 |
that the transport is connected and we are ready to create channels. It |
| 98 |
also populates the `+cockpit.info+` field. |
| 99 |
|
| 100 |
This function returns a promise. Initialization isn't complete until the |
| 101 |
promise has resolved. You can either `+await+` it or call `+.then()+` on |
| 102 |
it. |
| 103 |
|
| 104 |
[[cockpit-info]] |
| 105 |
== cockpit.info |
| 106 |
|
| 107 |
.... |
| 108 |
cockpit.info.channels[payload] |
| 109 |
|
| 110 |
cockpit.info.os_release[field] |
| 111 |
|
| 112 |
cockpit.info.user |
| 113 |
|
| 114 |
cockpit.info.ws.version |
| 115 |
.... |
| 116 |
|
| 117 |
This object contains information about Cockpit itself. It is only |
| 118 |
available after cockpit.init() has been called and awaited. |
| 119 |
|
| 120 |
`+channels+`:: |
| 121 |
This is a mapping of channel payload types (keys, strings) supported |
| 122 |
by the bridge to capabilities advertised by those channels (values, |
| 123 |
lists of strings). Channels are listed even if they don't advertise |
| 124 |
any capabilities, making this useful as a way to determine which |
| 125 |
channel types are supported by the bridge. |
| 126 |
`+os_release+`:: |
| 127 |
This is the data from the `+/etc/os-release+` or |
| 128 |
`+/usr/lib/os-release+` on the system that the bridge is running on. |
| 129 |
It is a mapping from the key names to their values. See the |
| 130 |
https://www.freedesktop.org/software/systemd/man/latest/os-release.html[os-release |
| 131 |
Specification] for information about the available keys. |
| 132 |
`+user+`:: |
| 133 |
Contains information about the user we're logged in as. |
| 134 |
`+uid+`;; |
| 135 |
This is unix user id as an integer. |
| 136 |
`+gid+`;; |
| 137 |
This is unix user group id as an integer. |
| 138 |
`+name+`;; |
| 139 |
This is the unix user name like `+"root"+`. |
| 140 |
`+fullname+`;; |
| 141 |
This is a readable name for the user, from the GECOS field. |
| 142 |
`+group+`;; |
| 143 |
This is the primary group name of the user. |
| 144 |
`+groups+`;; |
| 145 |
This is an array of group names to which the user belongs. The first |
| 146 |
item in this list is the primary group. |
| 147 |
`+home+`;; |
| 148 |
This is user's home directory. |
| 149 |
`+shell+`;; |
| 150 |
This is unix user shell. |
| 151 |
`+ws+`:: |
| 152 |
Contains information about the webserver Cockpit is being served with. |
| 153 |
`+version+`;; |
| 154 |
The version of the webserver. |
| 155 |
|
| 156 |
[[cockpit-event-target]] |
| 157 |
== cockpit.event_target |
| 158 |
|
| 159 |
.... |
| 160 |
cockpit.event_target(object, [handlers]) |
| 161 |
.... |
| 162 |
|
| 163 |
Adds an |
| 164 |
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget[EventTarget] |
| 165 |
implementation to the `+object+`. Optionally store the handlers in |
| 166 |
`+handlers+` if its specified. |