| 1 |
/* |
| 2 |
* Copyright (C) 2024 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import cockpit from "cockpit"; |
| 7 |
|
| 8 |
import { ManifestKeyword, ManifestDocs, ManifestSection, Manifest, Manifests } from "./manifests"; |
| 9 |
import { Machine } from "./machines/machines"; |
| 10 |
|
| 11 |
export interface Location { |
| 12 |
host: string; |
| 13 |
path: string; |
| 14 |
hash: string; |
| 15 |
} |
| 16 |
|
| 17 |
export function encode_location(location: Partial<Location>): string { |
| 18 |
const shell_embedded = window.location.pathname.indexOf(".html") !== -1; |
| 19 |
if (shell_embedded) |
| 20 |
return window.location.toString(); |
| 21 |
|
| 22 |
const path = []; |
| 23 |
if (location.host && location.host !== "localhost") |
| 24 |
path.push("@" + location.host); |
| 25 |
if (location.path) |
| 26 |
path.push(...location.path.split("/")); |
| 27 |
let string = cockpit.location.encode(path, undefined, true); |
| 28 |
if (location.hash && location.hash !== "/") |
| 29 |
string += "#" + location.hash; |
| 30 |
return string; |
| 31 |
} |
| 32 |
|
| 33 |
/* Decodes navigate state from a string */ |
| 34 |
export function decode_location(string: string): Location { |
| 35 |
let hash = ""; |
| 36 |
const pos = string.indexOf("#"); |
| 37 |
if (pos !== -1) { |
| 38 |
hash = string.substring(pos + 1); |
| 39 |
string = string.substring(0, pos); |
| 40 |
} |
| 41 |
if (string[0] != '/') |
| 42 |
string = "/" + string; |
| 43 |
const path = cockpit.location.decode(string); |
| 44 |
let host; |
| 45 |
if (path[0] && path[0][0] == "@") |
| 46 |
host = (path.shift() as string).substring(1); |
| 47 |
else |
| 48 |
host = "localhost"; |
| 49 |
if (path.length && path[path.length - 1] == "index") |
| 50 |
path.pop(); |
| 51 |
return { host, path: path.join("/"), hash }; |
| 52 |
} |
| 53 |
|
| 54 |
export function decode_window_location(): Location { |
| 55 |
const shell_embedded = window.location.pathname.indexOf(".html") !== -1; |
| 56 |
|
| 57 |
if (shell_embedded) |
| 58 |
return decode_location("/" + window.location.hash); |
| 59 |
else |
| 60 |
return decode_location(window.location.pathname + window.location.hash); |
| 61 |
} |
| 62 |
|
| 63 |
export function replace_window_location(location: Partial<Location>): void { |
| 64 |
window.history.replaceState(null, "", encode_location(location)); |
| 65 |
} |
| 66 |
|
| 67 |
export function push_window_location(location: Partial<Location>): void { |
| 68 |
window.history.pushState(null, "", encode_location(location)); |
| 69 |
} |
| 70 |
|
| 71 |
export interface ManifestItem { |
| 72 |
path: string; |
| 73 |
hash: string; |
| 74 |
section: string; |
| 75 |
label: string; |
| 76 |
order: number; |
| 77 |
docs: ManifestDocs[] | undefined; |
| 78 |
keywords: ManifestKeyword[]; |
| 79 |
} |
| 80 |
|
| 81 |
export class CompiledComponents { |
| 82 |
manifests: Manifests; |
| 83 |
items: { [path: string] : ManifestItem; } = { }; |
| 84 |
|
| 85 |
constructor(manifests?: Manifests) { |
| 86 |
this.manifests = manifests || { }; |
| 87 |
} |
| 88 |
|
| 89 |
load(section: string, getter: (man: Manifest) => ManifestSection | undefined): void { |
| 90 |
Object.entries(this.manifests).forEach(([name, manifest]) => { |
| 91 |
const manifest_section = getter(manifest) || {}; |
| 92 |
Object.entries(manifest_section).forEach(([prop, info]) => { |
| 93 |
const item: ManifestItem = { |
| 94 |
path: "", // set below |
| 95 |
hash: "", // set below |
| 96 |
section, |
| 97 |
label: info.label ? cockpit.gettext(info.label) : prop, |
| 98 |
order: info.order === undefined ? 1000 : info.order, |
| 99 |
docs: info.docs, |
| 100 |
keywords: info.keywords || [{ matches: [], weight: 3, translate: true, goto: undefined }], |
| 101 |
}; |
| 102 |
|
| 103 |
// Always first keyword should be page name |
| 104 |
const page_name = item.label.toLowerCase(); |
| 105 |
if (item.keywords[0].matches.indexOf(page_name) < 0) |
| 106 |
item.keywords[0].matches.unshift(page_name); |
| 107 |
|
| 108 |
// Keywords from manifest have different defaults than are usual |
| 109 |
item.keywords.forEach(i => { |
| 110 |
i.weight = i.weight || 3; |
| 111 |
i.translate = i.translate === undefined ? true : i.translate; |
| 112 |
}); |
| 113 |
|
| 114 |
if (info.path) |
| 115 |
item.path = info.path.replace(/\.html$/, ""); |
| 116 |
else |
| 117 |
item.path = name + "/" + prop; |
| 118 |
|
| 119 |
/* Split out any hash in the path */ |
| 120 |
const pos = item.path.indexOf("#"); |
| 121 |
if (pos !== -1) { |
| 122 |
item.hash = item.path.substring(pos + 1); |
| 123 |
item.path = item.path.substring(0, pos); |
| 124 |
} |
| 125 |
|
| 126 |
/* Fix component for compatibility and normalize it */ |
| 127 |
if (item.path.indexOf("/") === -1) |
| 128 |
item.path = name + "/" + item.path; |
| 129 |
if (item.path.slice(-6) == "/index") |
| 130 |
item.path = item.path.slice(0, -6); |
| 131 |
this.items[item.path] = item; |
| 132 |
}); |
| 133 |
}); |
| 134 |
} |
| 135 |
|
| 136 |
ordered(section: string): ManifestItem[] { |
| 137 |
const list: ManifestItem[] = []; |
| 138 |
for (const x in this.items) { |
| 139 |
if (!section || this.items[x].section === section) |
| 140 |
list.push(this.items[x]); |
| 141 |
} |
| 142 |
list.sort(function(a, b) { |
| 143 |
let ret = a.order - b.order; |
| 144 |
if (ret === 0) |
| 145 |
ret = a.label.localeCompare(b.label); |
| 146 |
return ret; |
| 147 |
}); |
| 148 |
return list; |
| 149 |
} |
| 150 |
|
| 151 |
find_path_item(path: string): ManifestItem { |
| 152 |
let component = path; |
| 153 |
if (this.items[path] === undefined) { |
| 154 |
let s = path; |
| 155 |
while (s && this.items[s] === undefined) |
| 156 |
s = s.substring(0, s.lastIndexOf("/")); |
| 157 |
component = s; |
| 158 |
} |
| 159 |
|
| 160 |
// Still don't know where it comes from, check for parent |
| 161 |
if (!component) { |
| 162 |
const comp = this.manifests[path]; |
| 163 |
if (comp && comp.parent && comp.parent.component) |
| 164 |
component = comp.parent.component; |
| 165 |
} |
| 166 |
|
| 167 |
const item = this.items[component]; |
| 168 |
if (item) |
| 169 |
return item; |
| 170 |
|
| 171 |
// Return something that can be used when the user navigates |
| 172 |
// to a URL for a non-existing component. |
| 173 |
|
| 174 |
return { |
| 175 |
path, |
| 176 |
label: path, |
| 177 |
section: "menu", |
| 178 |
hash: "", |
| 179 |
order: 3, |
| 180 |
keywords: [], |
| 181 |
docs: undefined |
| 182 |
}; |
| 183 |
} |
| 184 |
|
| 185 |
find_path_manifest(path: string): Manifest { |
| 186 |
const parts = path.split("/"); |
| 187 |
const pkg = parts[0]; |
| 188 |
|
| 189 |
return (this.manifests[pkg]) || { }; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
export function compile_manifests(manifests?: Manifests): CompiledComponents { |
| 194 |
const compiled = new CompiledComponents(manifests); |
| 195 |
compiled.load("tools", m => m.tools); |
| 196 |
compiled.load("dashboard", m => m.dashboard); |
| 197 |
compiled.load("menu", m => m.menu); |
| 198 |
return compiled; |
| 199 |
} |
| 200 |
|
| 201 |
function component_checksum(machine: Machine, path: string): string | undefined { |
| 202 |
const parts = path.split("/"); |
| 203 |
const pkg = parts[0]; |
| 204 |
if (machine.manifests && machine.manifests[pkg] && machine.manifests[pkg][".checksum"]) |
| 205 |
return "$" + machine.manifests[pkg][".checksum"]; |
| 206 |
} |
| 207 |
|
| 208 |
export function compute_frame_url(machine: Machine, path: string): string { |
| 209 |
let base; |
| 210 |
let checksum; |
| 211 |
if (machine.manifests && machine.manifests[".checksum"]) |
| 212 |
checksum = "$" + machine.manifests[".checksum"]; |
| 213 |
else |
| 214 |
checksum = machine.checksum; |
| 215 |
|
| 216 |
if (checksum && checksum == component_checksum(machine, path)) { |
| 217 |
if (machine.connection_string === "localhost") |
| 218 |
base = ".."; |
| 219 |
else |
| 220 |
base = "../../" + checksum; |
| 221 |
} else { |
| 222 |
/* If we don't have any checksums, or if the component specifies a different |
| 223 |
checksum than the machine, load it via a non-caching @<host> path. This |
| 224 |
makes sure that we get the right files, and also that we don't poisen the |
| 225 |
cache with wrong files. |
| 226 |
|
| 227 |
We can't use a $<component-checksum> path since cockpit-ws only knows how to |
| 228 |
route the machine checksum. |
| 229 |
|
| 230 |
TODO - make it possible to use $<component-checksum>. |
| 231 |
*/ |
| 232 |
base = "../../@" + machine.connection_string; |
| 233 |
} |
| 234 |
|
| 235 |
let url = base + "/" + path; |
| 236 |
if (path.indexOf("/") === -1) |
| 237 |
url += "/index"; |
| 238 |
url += ".html"; |
| 239 |
|
| 240 |
return url; |
| 241 |
} |