| 1 |
= cockpit.js: Page Location and Jumping |
| 2 |
|
| 3 |
Page location and navigation between components |
| 4 |
|
| 5 |
[[cockpit-location-general]] |
| 6 |
== Page location |
| 7 |
|
| 8 |
.... |
| 9 |
location = cockpit.location |
| 10 |
cockpit.location = "/path" |
| 11 |
.... |
| 12 |
|
| 13 |
Cockpit components often have different views, without changing the HTML |
| 14 |
file that is being viewed. These are known as pages. |
| 15 |
`+cockpit.location+` is an object that can be used to read the current |
| 16 |
page and to navigate to a different page location. It works by updating |
| 17 |
`+window.location.hash+`. |
| 18 |
|
| 19 |
The `+cockpit.location+` looks like a HTTP path with a possible query |
| 20 |
string: |
| 21 |
|
| 22 |
.... |
| 23 |
/path/sub/page?option=value,option2 |
| 24 |
.... |
| 25 |
|
| 26 |
The link:#cockpit-location-path[`+location.path+`] and |
| 27 |
link:#cockpit-location-options[`+location.options+`] contain a parsed |
| 28 |
form of the location. While the location cannot be modified in place, a |
| 29 |
new one can be created by assigning a string to `+cockpit.location+` or |
| 30 |
by calling the link:#cockpit-location-go[`+location.go()+`] function. |
| 31 |
|
| 32 |
`+cockpit.location+` is designed similarly to `+window.location+` in |
| 33 |
that the location object is preplaced whenever the current page location |
| 34 |
changes. To be aware of when the page location changes listen for the |
| 35 |
link:#cockpit-location-changed[`+cockpit.onlocationchanged+`] event. |
| 36 |
|
| 37 |
Using the location object as a string will result in the |
| 38 |
link:#cockpit-location-href[`+location.href+`]. |
| 39 |
|
| 40 |
[[cockpit-location-href]] |
| 41 |
=== location.href |
| 42 |
|
| 43 |
The string representation of this page location, including any options. |
| 44 |
|
| 45 |
[[cockpit-location-path]] |
| 46 |
=== location.path |
| 47 |
|
| 48 |
An array of path segments, parsed and decoded appropriately. An empty |
| 49 |
array denotes the root path. |
| 50 |
|
| 51 |
[[cockpit-location-options]] |
| 52 |
=== location.options |
| 53 |
|
| 54 |
A javascript object containing the various options present in the |
| 55 |
location. |
| 56 |
|
| 57 |
If an option appears more than once, its value will be an array. |
| 58 |
|
| 59 |
[[cockpit-location-go]] |
| 60 |
=== location.go() |
| 61 |
|
| 62 |
.... |
| 63 |
location.go(path, [options]) |
| 64 |
.... |
| 65 |
|
| 66 |
Changes the current location to the given `+path+` and `+options+`. If |
| 67 |
the `+path+` argument is a string, it will be parsed into a path. If it |
| 68 |
is a relative path, then the result will be relative to the current |
| 69 |
`+location.path+`. If the `+path+` argument is an array of path |
| 70 |
segments, it will be treated as a full parsed absolute path. |
| 71 |
|
| 72 |
Any options found in a `+path+` will be added to those in the optional |
| 73 |
`+options+` argument, and used in the result. |
| 74 |
|
| 75 |
The location change will only take effect if the location has not |
| 76 |
changed in the meantime. This can be to good effect by saving a |
| 77 |
`+cockpit.location+` object and doing a conditional navigation, by |
| 78 |
calling the saved `+location.go()+` method later. This will only |
| 79 |
navigate if the user or other code has not navigated in the meantime. |
| 80 |
|
| 81 |
[[cockpit-location-replace]] |
| 82 |
=== location.replace() |
| 83 |
|
| 84 |
.... |
| 85 |
location.replace(path, [options]) |
| 86 |
.... |
| 87 |
|
| 88 |
Similar to link:#cockpit-location-go[`+location.go()+`] except the |
| 89 |
location change will not result in a navigation change in the browser's |
| 90 |
history. |
| 91 |
|
| 92 |
[[cockpit-location-decode]] |
| 93 |
=== location.decode() |
| 94 |
|
| 95 |
.... |
| 96 |
path = location.decode(href, [options]) |
| 97 |
.... |
| 98 |
|
| 99 |
Decode a cockpit href into its `+path+` array. If the `+options+` |
| 100 |
argument is specified, then it will be populated with options found in |
| 101 |
the href. |
| 102 |
|
| 103 |
If href is a relative path it will be resolved relative to |
| 104 |
`+location.href+`. |
| 105 |
|
| 106 |
[[cockpit-location-encode]] |
| 107 |
=== location.encode() |
| 108 |
|
| 109 |
.... |
| 110 |
href = location.encode(path, [options]) |
| 111 |
.... |
| 112 |
|
| 113 |
Encode the given `+path+` and `+options+` into a cockpit href. The |
| 114 |
`+path+` argument may be an array of path segments, or a string path. If |
| 115 |
a relative path is passed, it will be resolved relative to |
| 116 |
`+location.href+`. |
| 117 |
|
| 118 |
[[cockpit-location-changed]] |
| 119 |
=== cockpit.onlocationchanged |
| 120 |
|
| 121 |
.... |
| 122 |
cockpit.addEventListener("locationchanged", function() { ... }) |
| 123 |
.... |
| 124 |
|
| 125 |
An event emitted when over the `+cockpit.location+` changes. Typically a |
| 126 |
component reacts to this event by updating its interface to reflect the |
| 127 |
new link:#cockpit-location-path[`+cockpit.location.path+`] and |
| 128 |
link:#cockpit-location-options[`+cockpit.location.options+`]. |
| 129 |
|
| 130 |
This event is not triggered immediately during a `+location.go()+` or |
| 131 |
similar call. It will be triggered asynchronously at a later time. |
| 132 |
|
| 133 |
[[cockpit-jump]] |
| 134 |
== Jumping between components |
| 135 |
|
| 136 |
.... |
| 137 |
cockpit.jump("/system/log") |
| 138 |
.... |
| 139 |
|
| 140 |
In Cockpit in there multiple components shown. In order to tell Cockpit |
| 141 |
to jump to and show another component and a certain location within that |
| 142 |
component, use the `+cockpit.jump()+` function. Stable component paths |
| 143 |
are documented. Don't assume you can navigate into paths that are not |
| 144 |
stable API. |
| 145 |
|
| 146 |
[[cockpit-jump-jump]] |
| 147 |
=== cockpit.jump() |
| 148 |
|
| 149 |
.... |
| 150 |
cockpit.jump(path, [ host ]) |
| 151 |
.... |
| 152 |
|
| 153 |
Ask Cockpit to jump to another component. The location of the current |
| 154 |
component will not be affected. The `+path+` argument can be a string |
| 155 |
path, starting with `+/+` or an array containing the parts of a path |
| 156 |
that will be joined to create a path. If `+host+` is not specified, then |
| 157 |
the component on the same host as the caller will be displayed. If host |
| 158 |
is null, then the host portion of the path will be removed, displaying |
| 159 |
the component on the host that cockpit is connected directly to. |
| 160 |
|
| 161 |
If the calling component is not running within Cockpit, or the calling |
| 162 |
component is not currently displayed, then the jump will not happen, and |
| 163 |
this function has no effect. |
| 164 |
|
| 165 |
[[cockpit-jump-hidden]] |
| 166 |
=== cockpit.hidden |
| 167 |
|
| 168 |
A boolean property that indicates if the current component page is |
| 169 |
visible or hidden. When the code or user jumps to another component, the |
| 170 |
prior one remains loaded and initialized but is hidden. Use this |
| 171 |
property together with the |
| 172 |
link:#cockpit-jump-visibilitychange[`+cockpit.onvisibilitychange+`] |
| 173 |
event to decide whether or not to perform expensive tasks to update the |
| 174 |
interface. |
| 175 |
|
| 176 |
This property is analogous to the `+document.hidden+` page visibility |
| 177 |
API, but works with the document and frame implementation of Cockpit. |
| 178 |
|
| 179 |
[[cockpit-jump-visibilitychange]] |
| 180 |
=== cockpit.onvisibilitychange |
| 181 |
|
| 182 |
.... |
| 183 |
cockpit.onvisibilitychange = function() { ... } |
| 184 |
.... |
| 185 |
|
| 186 |
This event is emitted when the |
| 187 |
link:#cockpit-jump-hidden[`+cockpit.hidden+`] property changes. This |
| 188 |
event is similar to the `+document.onvisibilitychange+` API, but works |
| 189 |
with the document and frame implementation of Cockpit. |